def test_bisection(self): """Test values of implied volatility (bisection method).""" premium = .024 price = 1 strike = 1 riskfree = .02 maturity = 30/365 call = True moneyness = lfmoneyness(price, strike, riskfree, maturity) vol = impvol_bisection(moneyness, maturity, premium, call) self.assertAlmostEqual(float(vol), .2, 2) strike = [1, .95] premium = [.024, .057] moneyness = lfmoneyness(price, strike, riskfree, maturity) vol = impvol_bisection(moneyness, maturity, premium, call) np.testing.assert_array_almost_equal(vol, [.2, .2], 2)
def test_bs_prices(self): """Test accuracy of back and forth BS conversion.""" count = int(1e3) maturity = 30/365 call = True sigma = np.random.uniform(.05, .8, count) moneyness = np.random.uniform(-.1, .1, count) premium = blackscholes_norm(moneyness, maturity, sigma, call) vol = impvol_bisection(moneyness, maturity, premium, call) np.testing.assert_array_almost_equal(vol, sigma, 3)