python计算投资回收期

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

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值