首页 > 解决方案 > 求解利率以使 NPV 为零 Python

问题描述

到目前为止,我的解决方案(不起作用并卡住)使用 NPV 公式计算每月现金流量并尝试找到贴现率使其为零:

def goal_seek(target,cashflows,_threshold):
    threshold = _threshold
    lower = -10000
    upper = 10000
    solve = (lower + upper)/2
    while abs(threshold) >= _threshold:
        print(f'Threshold is: {threshold}')
        print(f'range is: {lower}  ----  {solve}   ----   {upper}')
        if threshold < 0:
            upper = solve
            solve = (lower + upper)/2
        elif threshold > 0:
            lower = solve
            solve = (lower + upper)/2        
        threshold = target - numpy.npv(((numpy.sign(solve) * (numpy.abs(solve)+1) ** (1 / 12))-1),cashflows)

    print(f'Final result: Threshold: {threshold}....Solved input: {solve}')

            
        return solve

goal_seek(0,[-1,0,0,0,.5],.0001)

实现上面的例子会导致这个二分搜索算法卡在下面

阈值是:0.9767928293785059 范围是:10000.0 ---- 10000.0 ---- 10000

是否有一个简单的 scipy 模块来求解单个变量非线性方程,例如 NPV?

标签: pythonnumpy

解决方案



推荐阅读