Python for Finance: Analyze Big Financial Data

(Elle) #1
                                                                                                    ((0.5,  3.01,   0.5),       #   range   for kappa
(15., 30.1, 5.), # range for theta
(0.5, 5.51, 1 )), # range for volatility
finish=None)
Out[46]: i kappa theta vola —> MSE
0 0.500 15.000 0.500 —> 10.393
20 0.500 30.000 1.500 —> 2.071
40 1.000 25.000 3.500 —> 0.180
60 1.500 20.000 5.500 —> 0.718
80 2.000 20.000 1.500 —> 5.501
100 2.500 15.000 3.500 —> 5.571
120 2.500 30.000 5.500 —> 22.992
140 3.000 30.000 1.500 —> 14.493
CPU times: user 18.6 s, sys: 1.68 s, total: 20.3 s
Wall time: 20.3 s

The intermediate optimal results are as follows. The MSE is already quite low:


In  [ 47 ]: i   =    0
mean_squared_error(opt_global)
Out[47]: i kappa theta vola —> MSE
0 1.500 20.000 4.500 —> 0.008

                                    0.0076468730485555626

Next, we use the intermediate optimal parameters as input for the local optimization:


In  [ 48 ]: %%time
i = 0
opt_local = spo.fmin(mean_squared_error, opt_global,
xtol=0.00001, ftol=0.00001,
maxiter= 100 , maxfun= 350 )
Out[48]: i kappa theta vola —> MSE
0 1.500 20.000 4.500 —> 0.008
20 1.510 19.235 4.776 —> 0.008
40 1.563 18.926 4.844 —> 0.005
60 1.555 18.957 4.828 —> 0.005
80 1.556 18.947 4.832 —> 0.005
100 1.556 18.948 4.831 —> 0.005
120 1.556 18.948 4.831 —> 0.005
Optimization terminated successfully.
Current function value: 0.004654
Iterations: 64
Function evaluations: 138
CPU times: user 17.7 s, sys: 1.67 s, total: 19.3 s
Wall time: 19.4 s

This time the results are:


In  [ 49 ]: i   =    0
mean_squared_error(opt_local)
Out[49]: i kappa theta vola —> MSE
0 1.556 18.948 4.831 —> 0.005

                                    0.0046542736439999875

The resulting model values are:


In  [ 50 ]: calculate_model_values(opt_local)
Out[50]: {46482: 4.746597,
46483: 4.286923,
46484: 3.863346,
46485: 3.474144,
46486: 3.119211,
46487: 2.793906,
46488: 2.494882,
46489: 2.224775,
46490: 1.98111}

Let us store these in the option_selection DataFrame and calculate the differences from


the market prices:


In  [ 51 ]: option_selection[‘MODEL’]   =   \
Free download pdf