Python for Finance: Analyze Big Financial Data

(Elle) #1
dt  =   T   /   M
rand = np.random.standard_normal((M + 1 , I))
S = np.zeros((M + 1 , I)); S[ 0 ] = S0
for t in range( 1 , M + 1 ):
S[t] = S[t- 1 ] * np.exp((r - 0.5 * vola ** 2 ) * dt
+ vola * np.sqrt(dt) * rand[t])
value = (np.exp(-r * T)
* np.sum(np.maximum(S[- 1 ] - strike, 0 )) / I)
return value

The Sequential Calculation


As the benchmark case we take the valuation of 100 options with different strike prices.


The function seq_value calculates the Monte Carlo estimators and returns list objects


containing strikes and valuation results:


In  [ 36 ]: def seq_value(n):
”’ Sequential option valuation.

                                                    Parameters
==========
n : int
number of option valuations/strikes
”’
strikes = np.linspace( 80 , 120 , n)
option_values = []
for strike in strikes:
option_values.append(bsm_mcs_valuation(strike))
return strikes, option_values
In [ 37 ]: n = 100 # number of options to be valued
%time strikes, option_values_seq = seq_value(n)
Out[37]: CPU times: user 11.7 s, sys: 1e+03 μs, total: 11.7 s
Wall time: 11.7 s

The productivity is roughly 8.5 options per second. Figure 8-1 shows the valuation results:


In  [ 38 ]: import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=( 8 , 4 ))
plt.plot(strikes, option_values_seq, ‘b’)
plt.plot(strikes, option_values_seq, ‘r.’)
plt.grid(True)
plt.xlabel(‘strikes’)
plt.ylabel(‘European call option values’)

Figure 8-1. European call option values by Monte Carlo simulation

The Parallel Calculation


For the parallel calculation of the 100 option values, we use IPython.parallel and a


local “cluster.” A local cluster is most easily started via the Clusters tab in the IPython

Free download pdf