Python for Finance: Analyze Big Financial Data

(Elle) #1

There might also be a trade-off between compactness and readability in that this


implementation approach makes it quite difficult to grasp what exactly is going on on the


NumPy level. However, it shows how far one can go sometimes with NumPy vectorization.


Graphical Analysis


Finally, let us have a graphical look at the underlying mechanics (refer to Chapter 5 for an


explanation of the matplotlib plotting library). First, we plot the first 10 simulated paths


over all time steps. Figure 3-2 shows the output:


In  [ 29 ]: import matplotlib.pyplot as plt
plt.plot(S[:, : 10 ])
plt.grid(True)
plt.xlabel(‘time step’)
plt.ylabel(‘index level’)

Figure 3-2. The first 10 simulated index level paths

Second, we want to see the frequency of the simulated index levels at the end of the


simulation period. Figure 3-3 shows the output, this time illustrating the (approximately)


log-normal distribution of the end-of-period index level values:


In  [ 30 ]: plt.hist(S[- 1 ],   bins= 50 )
plt.grid(True)
plt.xlabel(‘index level’)
plt.ylabel(‘frequency’)

The same type of figure looks completely different for the option’s end-of-period


(maturity) inner values, as Figure 3-4 illustrates:


In  [ 31 ]: plt.hist(np.maximum(S[- 1 ] -   K,   0 ),   bins= 50 )
plt.grid(True)
plt.xlabel(‘option inner value’)
plt.ylabel(‘frequency’)
plt.ylim( 0 , 50000 )
Free download pdf