def BCC_iv_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 lamb: float jump intensity mu: float expected jump size delta: float standard deviation of jump Returns ======= MSE: float mean squared error """ global i, min_MSE kappa_v, theta_v, sigma_v, rho, v0, lamb, mu, delta = p0 if ( kappa_v < 0.0 or theta_v < 0.005 or sigma_v < 0.0 or rho < -1.0 or rho > 1.0 or v0 < 0.0 or lamb < 0.0 or mu < -0.6 or mu > 0.0 or delta < 0.0 ): return 5000.0 if 2 * kappa_v * theta_v < sigma_v ** 2: return 5000.0 se = [] for row, option in options.iterrows(): call = call_option(S0, option["Strike"], option["Date"], option["Maturity"], option["r"], option["Market_IV"]) model_value = BCC_call_value( S0, option["Strike"], option["T"], option["r"], kappa_v, theta_v, sigma_v, rho, v0, lamb, mu, delta ) model_iv = call.imp_vol(model_value, 0.15) se.append(((model_iv - option["Market_IV"]) * call.vega()) ** 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
def BCC_iv_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 lamb: float jump intensity mu: float expected jump size delta: float standard deviation of jump Returns ======= MSE: float mean squared error ''' global i, min_MSE kappa_v, theta_v, sigma_v, rho, v0, lamb, mu, delta = p0 if kappa_v < 0.0 or theta_v < 0.005 or sigma_v < 0.0 or \ rho < -1.0 or rho > 1.0 or v0 < 0.0 or lamb < 0.0 or \ mu < -.6 or mu > 0.0 or delta < 0.0: return 5000.0 if 2 * kappa_v * theta_v < sigma_v**2: return 5000.0 se = [] for row, option in options.iterrows(): call = call_option(S0, option['Strike'], option['Date'], option['Maturity'], option['r'], option['Market_IV']) model_value = BCC_call_value(S0, option['Strike'], option['T'], option['r'], kappa_v, theta_v, sigma_v, rho, v0, lamb, mu, delta) model_iv = call.imp_vol(model_value, 0.15) se.append(((model_iv - option['Market_IV']) * call.vega())**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
def calculate_imp_vols(data): ''' Calculate all implied volatilities for the European call options given the tolerance level for moneyness of the option.''' data['Imp_Vol'] = 0.0 tol = 0.30 # tolerance for moneyness for row in data.index: t = data['Date'][row] T = data['Maturity'][row] ttm = (T - t).days / 365. forward = np.exp(r * ttm) * S0 if (abs(data['Strike'][row] - forward) / forward) < tol: call = call_option(S0, data['Strike'][row], t, T, r, 0.2) data['Imp_Vol'][row] = call.imp_vol(data['Call'][row]) return data
def calculate_imp_vols(data): ''' Calculate all implied volatilities for the European call options given the tolerance level for moneyness of the option.''' data['Imp_Vol'] = 0.0 tol = 0.30 # tolerance for moneyness for row in data.index: t = data['Date'][row] T = data['Maturity'][row] ttm = (T - t)/864e10/365. # it is 18 digits unix type timestamp, pls refer to https://www.epochconverter.com/ldap forward = np.exp(r * ttm) * S0 if (abs(data['Strike'][row] - forward) / forward) < tol: call = call_option(S0, data['Strike'][row], t, T, r, 0.2) data.loc[row, 'Imp_Vol'] = call.imp_vol(data.loc[row, 'Call']) return data
def calculate_implied_volatilities(filename): ''' Calculates market and model implied volatilities. ''' h5 = pd.HDFStore(filename, 'r') options = h5['options'] h5.close() for row, option in options.iterrows(): # T = (option['Maturity'] - option['Date']).days / 365. # B0T = B([kappa_r, theta_r, sigma_r, r0, T]) # r = -math.log(B0T) / T call = call_option(S0, option['Strike'], option['Date'], option['Maturity'], option['r'], 0.1) options.loc[row, 'market_iv'] = call.imp_vol(option['Call'], 0.15) options.loc[row, 'model_iv'] = call.imp_vol(option['Model'], 0.15) return options
def calculate_imp_vols(data): ''' Calculate all implied volatilities for the European call options given the tolerance level for moneyness of the option.''' data['Imp_Vol'] = 0.0 tol = 0.30 # tolerance for moneyness for row in data.index: t = data['Date'][row] T = data['Maturity'][row] ttm = (T - t).days / 365. forward = np.exp(r * ttm) * S0 if (abs(data['Strike'][row] - forward) / forward) < tol: call = call_option(S0, data['Strike'][row], t, T, r, 0.2) data.loc[row, 'Imp_Vol'] = call.imp_vol(data.loc[row, 'Call']) return data
def calculate_implied_volatilities(filename): ''' Calculates market and model implied volatilities. ''' h5 = pd.HDFStore(filename, 'r') options = h5['options'] h5.close() for row, option in options.iterrows(): T = (option['Maturity'] - option['Date']).days / 365. B0T = B([kappa_r, theta_r, sigma_r, r0, T]) r = -math.log(B0T) / T call = call_option(S0, option['Strike'], option['Date'], option['Maturity'], option['r'], 0.1) options.ix[row, 'market_iv'] = call.imp_vol(option['Call'], 0.15) options.ix[row, 'model_iv'] = call.imp_vol(option['Model'], 0.15) return options
# Calibrate Short Rate Model # kappa_r, theta_r, sigma_r = CIR_calibration() # # Market Data from www.eurexchange.com # as of 30. September 2014 # S0 = 3225.93 # EURO STOXX 50 level 30.09.2014 r0 = r_list[0] # initial short rate (Eonia 30.09.2014) # # Market Implied Volatilities # for row, option in options.iterrows(): call = call_option(S0, option['Strike'], option['Date'], option['Maturity'], option['r'], 0.15) options.loc[row, 'Market_IV'] = call.imp_vol(option['Call'], 0.15) # # Calibration Functions # i = 0 min_MSE = 5000.0 def BCC_iv_error_function(p0): ''' Error function for parameter calibration in BCC97 model via Lewis (2001) Fourier approach. Parameters ==========
# Calibrate Short Rate Model # kappa_r, theta_r, sigma_r = CIR_calibration() # # Market Data from www.eurexchange.com # as of 30. September 2014 # S0 = 3225.93 # EURO STOXX 50 level 30.09.2014 r0 = r_list[0] # initial short rate (Eonia 30.09.2014) # # Market Implied Volatilities # for row, option in options.iterrows(): call = call_option(S0, option["Strike"], option["Date"], option["Maturity"], option["r"], 0.15) options.ix[row, "Market_IV"] = call.imp_vol(option["Call"], 0.15) # # Calibration Functions # i = 0 min_MSE = 5000.0 def BCC_iv_error_function(p0): """ Error function for parameter calibration in BCC97 model via Lewis (2001) Fourier approach. Parameters ==========