Python for Finance: Analyze Big Financial Data

(Elle) #1
Figure 11-1. Ten simulated paths of geometric Brownian motion

Consider the very first simulated path over the 50 time steps:


In  [ 7 ]:  paths[:,     0 ].round( 4 )
Out[7]: array([ 100. , 97.821 , 98.5573, 106.1546, 105.899 , 99.8363,
100.0145, 102.6589, 105.6643, 107.1107, 108.7943, 108.2449,
106.4105, 101.0575, 102.0197, 102.6052, 109.6419, 109.5725,
112.9766, 113.0225, 112.5476, 114.5585, 109.942 , 112.6271,
112.7502, 116.3453, 115.0443, 113.9586, 115.8831, 117.3705,
117.9185, 110.5539, 109.9687, 104.9957, 108.0679, 105.7822,
105.1585, 104.3304, 108.4387, 105.5963, 108.866 , 108.3284,
107.0077, 106.0034, 104.3964, 101.0637, 98.3776, 97.135 ,
95.4254, 96.4271, 96.3386])

A log-return series for a simulated path might then take on the form:


In  [ 8 ]:  log_returns[:,   0 ].round( 4 )
Out[8]: array([-0.022 , 0.0075, 0.0743, -0.0024, -0.059 , 0.0018, 0.0261,
0.0289, 0.0136, 0.0156, -0.0051, -0.0171, -0.0516, 0.0095,
0.0057, 0.0663, -0.0006, 0.0306, 0.0004, -0.0042, 0.0177,
-0.0411, 0.0241, 0.0011, 0.0314, -0.0112, -0.0095, 0.0167,
0.0128, 0.0047, -0.0645, -0.0053, -0.0463, 0.0288, -0.0214,
-0.0059, -0.0079, 0.0386, -0.0266, 0.0305, -0.0049, -0.0123,
-0.0094, -0.0153, -0.0324, -0.0269, -0.0127, -0.0178, 0.0104,
-0.0009])

This is something one might experience in financial markets as well: days when you make


a positive return on your investment and other days when you are losing money relative to


your most recent wealth position.


The function print_statistics is a wrapper function for the describe function from the


scipy.stats sublibrary. It mainly generates a more (human-)readable output for such


statistics as the mean, the skewness, or the kurtosis of a given (historical or simulated)


data set:


In  [ 9 ]:  def print_statistics(array):
”’ Prints selected statistics.

                                                Parameters
==========
array: ndarray
object to generate statistics on
”’
sta = scs.describe(array)
print “%14s %15s” % (‘statistic’, ‘value’)
print 30 * “-”
print “%14s %15.5f” % (‘size’, sta[ 0 ])
print “%14s %15.5f” % (‘min’, sta[ 1 ][ 0 ])
print “%14s %15.5f” % (‘max’, sta[ 1 ][ 1 ])
print “%14s %15.5f” % (‘mean’, sta[ 2 ])
print “%14s %15.5f” % (‘std’, np.sqrt(sta[ 3 ]))
Free download pdf