Python for Finance: Analyze Big Financial Data

(Elle) #1
Out[66]:    15.424277577732667

There is further potential to generalize the steps of the previous example. One option is to


define a new class that provides a method for calculating the NPV for different short rates


— i.e., a sensitivity analysis. We use, of course, the cash_flow_series class to inherit


from:


In  [ 67 ]: class cfs_sensitivity(cash_flow_series):
def npv_sensitivity(self, short_rates):
npvs = []
for rate in short_rates:
sr.rate = rate
npvs.append(self.net_present_value())
return np.array(npvs)
In [ 68 ]: cfs_sens = cfs_sensitivity(‘cfs’, time_list, cash_flows, sr)

For example, defining a list containing different short rates, we can easily compare the


resulting NPVs:


In  [ 69 ]: short_rates =   [0.01,  0.025,  0.05,   0.075,  0.1,    0.125,  0.15,   0.2]
In [ 70 ]: npvs = cfs_sens.npv_sensitivity(short_rates)
npvs
Out[70]: array([ 23.01739219, 20.10770244, 15.42427758, 10.94027255,
6.64667738, 2.53490386, -1.40323463, -8.78945889])

Figure 13-3 shows the result graphically. The thicker horizontal line (at 0) shows the


cutoff point between a profitable investment and one that should be dismissed given the


respective (short) rate:


In  [ 71 ]: plt.plot(short_rates,   npvs,   ‘b’)
plt.plot(short_rates, npvs, ‘ro’)
plt.plot(( 0 , max(short_rates)), ( 0 , 0 ), ‘r’, lw= 2 )
plt.grid(True)
plt.xlabel(‘short rate’)
plt.ylabel(‘net present value’)

Figure 13-3. Net present values of cash flow list for different short rates
Free download pdf