def test_heston(self): """Test Heston model.""" price, strike = 100, 90 riskfree, maturity = 0, 30 / 365 moneyness = np.log(strike / price) - riskfree * maturity lm = 1.5768 mu = .12**2 eta = .5751 rho = -.0 sigma = .12**2 param = HestonParam(lm=lm, mu=mu, eta=eta, rho=rho, sigma=sigma) model = Heston(param, riskfree, maturity) premium = cosmethod(model, moneyness=moneyness, call=True) self.assertEqual(premium.shape, (1, )) moneyness = np.linspace(-.1, .1, 10) premium = cosmethod(model, moneyness=moneyness, call=True) self.assertEqual(premium.shape, moneyness.shape) riskfree = np.zeros_like(moneyness) premium = cosmethod(model, moneyness=moneyness, call=True) self.assertEqual(premium.shape, moneyness.shape)
def single_premium(): """Test COS method for floats. """ price, strike = 100, 90 riskfree, maturity = 0, 30 / 365 sigma = .15 moneyness = np.log(strike / price) - riskfree * maturity model = GBM(GBMParam(sigma=sigma), riskfree, maturity) premium = cosmethod(model, moneyness=moneyness, call=True) print(premium) nu = .2 theta = -.14 sigma = .25 param = VarGammaParam(theta=theta, nu=nu, sigma=sigma) model = VarGamma(param, riskfree, maturity) premium = cosmethod(model, moneyness=moneyness, call=True) print(premium) lm = 1.5768 mu = .12**2 eta = .5751 rho = -.0 sigma = .12**2 param = HestonParam(lm=lm, mu=mu, eta=eta, rho=rho, sigma=sigma) model = Heston(param, riskfree, maturity) premium = cosmethod(model, moneyness=moneyness, call=True) print(premium) rho = .55 delta = .75 mu = .2**2 / 365 sigma = .2**2 / 365 phi = -.0 theta1 = -16.0 theta2 = 20.95 param = ARGParam(rho=rho, delta=delta, mu=mu, sigma=sigma, phi=phi, theta1=theta1, theta2=theta2) model = ARG(param, riskfree, maturity) premium = cosmethod(model, moneyness=moneyness, call=True) print(premium)
def cos_heston_price(S_0, K, r, T, v, v_0, kappa, gamma, rho, call=True): """ Pricer for European option (single asset) under Heston dynamic :param S_0: init stock price :param K: strike :param r: risk-free rate :param T: maturity :param v: long term mean variance :param v_0: initial variance :param kappa: reversion speed :param gamma: vol coefficient of vol :param rho: correlation between stock and volatility dynamic :param call: call if True, put otherwise :return: Option price """ moneyness = np.log(K / S_0) - r * T param = HestonParam(lm=kappa, mu=v, eta=gamma, rho=rho, sigma=v_0) model = Heston(param, r, T) P = cosmethod(model, moneyness=moneyness, call=call) return P[0]
def multiple_premia_heston(nobs=2000): """Test COS method on the grid. """ lm = 1.5768 mu = .12**2 eta = .5751 rho = -.0 sigma = .12**2 price = 1 strike = np.exp(np.linspace(-.1, .1, nobs)) maturity = 30 / 365 riskfree = .01 * np.ones(nobs) moneyness = np.log(strike / price) - riskfree * maturity call = np.ones_like(moneyness).astype(bool) call[moneyness < 0] = False param = HestonParam(lm=lm, mu=mu, eta=eta, rho=rho, sigma=sigma) model = Heston(param, riskfree, maturity) premium = cosmethod(model, moneyness=moneyness, call=call) plt.plot(strike, premium) plt.show()