Frank
Frank

Reputation: 4459

Numpy's npv calculation

I am computing a NPV with numpy and with my own code, and the results differ. I must be making a mistake somewhere. Any pointer?

// 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

Upvotes: 4

Views: 4375

Answers (2)

Akavall
Akavall

Reputation: 86266

This was fixed in numpy 1.8

Fix to financial.npv

The npv function had a bug. Contrary to what the documentation stated, it summed from indexes 1 to M instead of from 0 to M - 1. The fix changes the returned value. The mirr function called the npv function, but worked around the problem, so that was also fixed and the return value of the mirr function remains unchanged.

NumPy 1.8.0 Release Notes

If I use np.npv in numpy 1.8 I get:

import numpy as np

r = .06

flows = np.zeros(16)
flows[0] = 1200
flows[3] = -450
flows[6] = -450
flows[15]= -450

result = np.npv(r, flows)

Result:

>>> result
317.16980210661666

Upvotes: 1

unutbu
unutbu

Reputation: 880279

It looks like (despite what it says in the docs) np.npv starts summing with t = 1, not 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

Indeed, np.npv is defined this way:

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

Upvotes: 6

Related Questions