def H93_calculate_model_values(p0): ''' Calculates all model values given parameter vector p0. ''' kappa_v, theta_v, sigma_v, rho, v0 = p0 values = [] for row, option in options.iterrows(): model_value = H93_call_value(S0, option['Strike'], option['T'], option['r'], kappa_v, theta_v, sigma_v, rho, v0) values.append(model_value) return np.array(values)
def H93_error_function(p0): ''' Error function for parameter calibration in BCC97 model via Lewis (2001) Fourier approach. Parameters ========== kappa_v: float mean-reversion factor theta_v: float long-run mean of variance sigma_v: float volatility of variance rho: float correlation between variance and stock/index level v0: float initial, instantaneous variance Returns ======= MSE: float mean squared error ''' global i, min_MSE kappa_v, theta_v, sigma_v, rho, v0 = p0 if kappa_v < 0.0 or theta_v < 0.005 or sigma_v < 0.0 or \ rho < -1.0 or rho > 1.0: return 500.0 if 2 * kappa_v * theta_v < sigma_v ** 2: return 500.0 se = [] for row, option in options.iterrows(): model_value = H93_call_value(S0, option['Strike'], option['T'], option['r'], kappa_v, theta_v, sigma_v, rho, v0) se.append((model_value - option['Call']) ** 2) MSE = sum(se) / len(se) min_MSE = min(min_MSE, MSE) if i % 25 == 0: print('%4d |' % i, np.array(p0), '| %7.3f | %7.3f' % (MSE, min_MSE)) i += 1 return MSE
matrix[4] = rel_r**2 matrix[3] = rel_S matrix[2] = rel_v matrix[1] = rel_r matrix[0] = 1 reg = np.linalg.lstsq(matrix.transpose(), rel_V) cv = np.dot(reg[0], matrix) erg = np.zeros((I), dtype=np.float) np.put(erg, relevant, cv) V[t] = np.where(h[t] > erg, h[t], V[t + 1] * df) # final discounting step df = np.exp(-(r[0] + r[1]) / 2 * dt) # European Option Values C0 = H93_call_value(S0, K, T, ra, kappa_v, theta_v, sigma_v, rho, v0) P0 = C0 + K * B0T - S0 P0_MCS = B0T * np.sum(h[-1]) / I x = B0T * h[-1] y = V[1] * df # Control Variate Correction if convar is True: # statistical correlation b = (np.sum( (x - np.mean(x)) * (y - np.mean(y))) / np.sum( (x - np.mean(x))**2)) # correction # set b instead of 1.0