Beispiel #1
0
    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,))
Beispiel #2
0
    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, ))
Beispiel #3
0
    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)
Beispiel #4
0
    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)
    maturity = 30/365
    premium = .057
    call = True
    moneyness = lfmoneyness(price, strike, riskfree, maturity)
    vol = impvol_bisection(moneyness, maturity, premium, call)

    count = int(1e2)
    sigma = np.random.uniform(.05, .8, count)
    moneyness = np.random.uniform(-.1, .1, count)
    premium = blackscholes_norm(moneyness, maturity, sigma, call)

    text = 'Time elapsed: %.2f seconds'

    time_start = time.time()
    # Based on SciPy root method
    vol = imp_vol(moneyness, maturity, premium, call)
    print(np.allclose(sigma, vol))
    print(text % (time.time() - time_start))

    time_start = time.time()
    # Based on bisection method
    vol = impvol_bisection(moneyness, maturity, premium, call)
    print('Relative difference (percent) = ', ((sigma/vol-1).max()*100))
    print(np.allclose(vol, sigma, rtol=1e-3))
    print(text % (time.time() - time_start))

    table = pd.DataFrame({'premium': premium, 'moneyness': moneyness})
    table['maturity'] = maturity
    table['call'] = call

    table['imp_vol'] = impvol_table(table)
Beispiel #6
0
    maturity = 30 / 365
    premium = .057
    call = True
    moneyness = lfmoneyness(price, strike, riskfree, maturity)
    vol = impvol_bisection(moneyness, maturity, premium, call)

    count = int(1e2)
    sigma = np.random.uniform(.05, .8, count)
    moneyness = np.random.uniform(-.1, .1, count)
    premium = blackscholes_norm(moneyness, maturity, sigma, call)

    text = 'Time elapsed: %.2f seconds'

    time_start = time.time()
    # Based on SciPy root method
    vol = imp_vol(moneyness, maturity, premium, call)
    print(np.allclose(sigma, vol))
    print(text % (time.time() - time_start))

    time_start = time.time()
    # Based on bisection method
    vol = impvol_bisection(moneyness, maturity, premium, call)
    print('Relative difference (percent) = ', ((sigma / vol - 1).max() * 100))
    print(np.allclose(vol, sigma, rtol=1e-3))
    print(text % (time.time() - time_start))

    table = pd.DataFrame({'premium': premium, 'moneyness': moneyness})
    table['maturity'] = maturity
    table['call'] = call

    table['imp_vol'] = impvol_table(table)