def test_vol_types(self): """Test correctness of types.""" moneyness, maturity, premium, call = 0, .3, .05, True vol = imp_vol(moneyness, maturity, premium, call) self.assertIsInstance(vol, np.ndarray) self.assertEqual(vol.shape, (1,)) moneyness, maturity, premium, call = [-.1, .1], .3, .05, True vol = imp_vol(moneyness, maturity, premium, call) self.assertIsInstance(vol, np.ndarray) self.assertEqual(vol.shape, (2,))
def test_vol_values(self): """Test values of implied volatility.""" premium = .024 price = 1 strike = 1 riskfree = .02 maturity = 30/365 call = True moneyness = lfmoneyness(price, strike, riskfree, maturity) vol = imp_vol(moneyness, maturity, premium, call) self.assertAlmostEqual(float(vol), .2, 2) strike = [1, .95] premium = [.024, .057] moneyness = lfmoneyness(price, strike, riskfree, maturity) vol = imp_vol(moneyness, maturity, premium, call) np.testing.assert_array_almost_equal(vol, [.2, .2], 2)