nth option, respectively. p is the parameter set provided as input to the option pricing
model.
Equation 19-1. Model calibration based on mean-squared error
The Python function mean_squared_error implements this approach to model calibration
technically. A global variable is used to control the output of intermediate parameter tuple
objects and the resulting MSE:
In [ 43 ]: i = 0
def mean_squared_error(p0):
”’ Returns the mean-squared error given
the model and market values.
Parameters
===========
p0 : tuple/list
tuple of kappa, theta, volatility
Returns
=======
MSE : float
mean-squared error
”’
global i
model_values = np.array(calculate_model_values(p0).values())
market_values = option_selection[‘PRICE’].values
option_diffs = model_values - market_values
MSE = np.sum(option_diffs ** 2 ) / len(option_diffs)
# vectorized MSE calculation
if i % 20 == 0 :
if i == 0 :
print ‘%4s %6s %6s %6s —> %6s’ % \
(‘i’, ‘kappa’, ‘theta’, ‘vola’, ‘MSE’)
print ‘%4d %6.3f %6.3f %6.3f —> %6.3f’ % \
(i, p0[ 0 ], p0[ 1 ], p0[ 2 ], MSE)
i += 1
return MSE
Again, a brief check to see if the function works in principle:
In [ 44 ]: mean_squared_error((0.5, 27.5, vol_est))
Out[44]: i kappa theta vola —> MSE
0 0.500 27.500 1.038 —> 4.390
4.3899900376937779
Chapter 9 introduces the Python and SciPy functions for convex optimization problems.
We will apply these here as well, so we begin with an import:
In [ 45 ]: import scipy.optimize as spo
The following calibration uses both global optimization via the brute function and local
optimization via the fmin function. First, the global optimization:
In [ 46 ]: %%time
i = 0
opt_global = spo.brute(mean_squared_error,