Python for Finance: Analyze Big Financial Data

(Elle) #1
Figure 11-7. Evolution of stock and index levels over time

Calculating the log returns with pandas is a bit more convenient than with NumPy, since we


can use the shift method:


In  [ 27 ]: log_returns =   np.log(data /   data.shift( 1 ))
log_returns.head()
Out[27]: ^GDAXI ^GSPC YHOO MSFT
Date
2006-01-03 NaN NaN NaN NaN
2006-01-04 0.011460 0.003666 0.001466 0.004967
2006-01-05 -0.001284 0.000016 0.013576 0.000900
2006-01-06 0.003581 0.009356 0.039656 -0.003155
2006-01-09 0.000143 0.003650 0.004848 -0.001808

Figure 11-8 provides all log returns in the form of histograms. Although not easy to judge,


one can guess that these frequency distributions might not be normal:


In  [ 28 ]: log_returns.hist(bins= 50 , figsize=( 9 ,    6 ))

Figure 11-8. Histogram of respective log returns
Free download pdf