gpt4 book ai didi

python - Numpy的npv计算

转载 作者:太空狗 更新时间:2023-10-30 02:22:12 27 4
gpt4 key购买 nike

我正在使用 numpy 和我自己的代码计算 NPV,结果不同。我一定是在某个地方犯了错误。有什么指点吗?

// Solution 1
r = .06
flows = {0:1200, 3:-450, 6:-450, 15:-450}
print sum([C/(1+r)**i for i,C in flows.iteritems()])
// => 317

// Solution using numpy's npv function
flows = zeros(16)
flows[0] = 1200
flows[3] = -450
flows[6] = -450
flows[15]= -450
print np.npv(r, flows)
// => 299

最佳答案

它看起来像(尽管它说的是 in the docs )np.npv 以 t = 1 而非 t = 0 开始求和:

In [56]: r = 0.06

In [57]: R = r+1

In [58]: (1200/R**0 - 450/R**3 - 450/R**6 - 450/R**15)
Out[58]: 317.16980210661666

In [59]: (1200/R**0 - 450/R**3 - 450/R**6 - 450/R**15)/R
Out[59]: 299.21679444020435

In [64]: np.npv(r, flows)*(1+r)
Out[64]: 317.16980210661683

确实,np.npvdefined this way :

def npv(rate, values):
values = np.asarray(values)
return (values / (1+rate)**np.arange(1,len(values)+1)).sum(axis=0)

关于python - Numpy的npv计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11923953/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com