Python for Finance: Analyze Big Financial Data

(Elle) #1

opportunity costs of the investment, is positive. In our case, the NPV is simply the sum of


the single present values:


In  [ 59 ]: #   net present value
np.sum(disc_facts * cash_flows)
Out[59]: 15.424277577732667

Obviously, for a short rate of 5% the investment should be made. What about a rate of


15%? Then the NPV becomes negative, and the investment should not be made:


In  [ 60 ]: sr.rate =   0.15
np.sum(sr.get_discount_factors(time_list) * cash_flows)
Out[60]: -1.4032346276182679

Cash Flow Series Class


With the experience gained through the previous example, the definition of another class


to model a cash flow series should be straightforward. This class should provide methods


to give back a list/array of present values and also the net present value for a given cash


flow series — i.e., cash flow values and dates/times:


In  [ 61 ]: class cash_flow_series(object):
”’ Class to model a cash flow series.

                                                    Attributes
==========
name : string
name of the object
time_list : list/array-like
list of (positive) year fractions
cash_flows : list/array-like
corresponding list of cash flow values
short_rate : instance of short_rate class
short rate object used for discounting

                                                    Methods
=======
present_value_list :
returns an array with present values
net_present_value :
returns NPV for cash flow series
”’
def __init__(self, name, time_list, cash_flows, short_rate):
self.name = name
self.time_list = time_list
self.cash_flows = cash_flows
self.short_rate = short_rate
def present_value_list(self):
df = self.short_rate.get_discount_factors(self.time_list)
return np.array(self.cash_flows) * df
def net_present_value(self):
return np.sum(self.present_value_list())

We use all objects from the previous example to instantiate the class:


In  [ 62 ]: sr.rate =   0.05
cfs = cash_flow_series(‘cfs’, time_list, cash_flows, sr)
In [ 63 ]: cfs.cash_flows
Out[63]: array([-100, 50, 75])
In [ 64 ]: cfs.time_list
Out[64]: [0.0, 1.0, 2.0]

We can now compare the present values and the NPV with the results from before.


Fortunately, we get the same results:


In  [ 65 ]: cfs.present_value_list()
Out[65]: array([-100. , 47.56147123, 67.86280635])
In [ 66 ]: cfs.net_present_value()
Free download pdf