import numpy_financial as npf
# 通过净现金流和利率,求净现值
def cash_flow_npv(i, cash_list):
npv = npf.npv(i, cash_list)
npv = round(npv, 2)
print(npv)
return npv
# 通过净现金流/时间和利率,求终值
def cash_flow_ffv(i, y, cash_list):
npv = cash_flow_npv(i, cash_list)
ffv = npf.fv(i, y, 0, npv)
# print("终值:")
# print(round(ffv, 2))
ffv = round(ffv, 2)
return ffv
# 通过现金流计算静态投资回收期
def static_cycle(out_list, in_list):
# out_list = [1000, 800, 0, 0, 0, 0] # 支出
# in_list = [0, 0, 500, 500, 500, 1200] # 收入
out_len = len(out_list)
in_len = len(in_list)
net_cash_flow = []
cumulative_cash_flow = []
positive_time = 0
if out_len == in_len and in_len != 0:
for n in range(0, out_len):
net_cash_flow.append(in_list[n]-out_list[n]) # 净现金流量
for j in range(0, out_len):
if j == 0:
cumulative_cash_flow.append(net_cash_flow[0])
else:
cumulative_cash_flow.append(net_cash_flow[j]+cumulative_cash_flow[j-1])
if cumulative_cash_flow[j] > 0:
positive_time = int(j)
break
# print(cumulative_cash_flow)
# print(positive_time)
count_out = 0
count_in = 0
for k in range(0, positive_time):
count_out = count_out+out_list[k]
count_in = count_in+in_list[k]
cycle_time = positive_time - 1 + abs(
(count_out - count_in) / (in_list[positive_time] - out_list[positive_time]))
print("静态投资回收期:{} 年".format(cycle_time))
return cycle_time
else:
print("收入和支出次数不等,请检查")
# 通过现金流计算动态投资回收期
def dynamic_cycle(i, out_list, in_list):
# out_list = [12000, 0, 0, 0, 0, 0] # 支出列表
# in_list = [0, 3000, 3000, 3000, 3000, 3000] # 收入列表
out_len = len(out_list)
in_len = len(in_list)
net_cash_flow = []
cumulative_cash_flow = []
positive_time = 0
if out_len == in_len and in_len != 0:
for n in range(0, out_len):
net_cash_flow.append(in_list[n]-out_list[n]) # 净现金流量
for l in range(0, out_len):
if l != 0:
net_cash_flow[l] = npf.pv(i, l, 0, -net_cash_flow[l]) # 净现值
net_cash_flow[l] = round(net_cash_flow[l], 2)
else:
net_cash_flow[0] = 1 * net_cash_flow[l]
for j in range(0, out_len):
if j == 0:
cumulative_cash_flow.append(net_cash_flow[0])
else:
cumulative_cash_flow.append(round(net_cash_flow[j]+cumulative_cash_flow[j-1], 2))
if cumulative_cash_flow[j] >= 0:
positive_time = int(j)
break
if j == out_len-1 and cumulative_cash_flow[j] < 0:
raise Exception("在现金流的年份内,累计金额没有达到正值,即回收期超过设置的现金流年限!")
print(cumulative_cash_flow)
print(positive_time)
count_i = cumulative_cash_flow[positive_time-1]
count_i = round(count_i, 2)
# print(count_i)
positive_va = net_cash_flow[positive_time]
cycle_time = positive_time - 1 + abs(count_i/positive_va)
cycle_time = round(cycle_time, 2)
print("动态投资回收期:{} 年".format(cycle_time))
return cycle_time
else:
print("收入和支出次数不等,请检查")
# 通过年值A计算回收期
def a_cycle(i, a, p):
nper = npf.nper(i, a, -p)
print("通过年值计算投资回收期:", round(nper.sum(), 2), '年')
return round(nper.sum(), 2)
if __name__ == '__main__':
# cycle(1)
# cash_float(0.02, 5, [-1000, -100, -100, -100, -100, -100]) # -1471.35
# cash_flow_npv(0.15, [-8000, 1000, 1000, 1000, 1000, 1000, 3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000])
# i = 0.15
# out_list = [8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# in_list = [0, 1000, 1000, 1000, 1000, 1000, 3000, 3000, 3000, 3000, 3000, 3000, 3000, 3000]
# # static_cycle()
# dynamic_cycle(i, out_list, in_list)
# a_cycle(0.15, 3000, 12000)
pass
python计算投资回收期
于 2024-01-14 20:22:27 首次发布