Time for action – calculating the net present value
We will calculate the net present value for a random generated cash flow series:
Generate five random values for the cash flow series. Insert
-100
as the start value:cashflows = np.random.randint(100, size=5) cashflows = np.insert(cashflows, 0, -100) print("Cashflows", cashflows)
The cash flows would be as follows:
Cashflows [-100 38 48 90 17 36]
Call the
npv()
function to calculate the net present value from the cash flow series we generated in the previous step. Use a rate of3
percent:print("Net present value", np.npv(0.03, cashflows))
The net present value:
Net present value 107.435682443
What just happened?
We computed the net present value from a random generated cash flow series with the NumPy npv()
function (see netpresentvalue.py
):
from __future__ import print_function import numpy as np cashflows = np.random.randint(100, size=5) cashflows = np.insert(cashflows, 0, -100) print("Cashflows", cashflows) print("Net present...