Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    def test_pandas(self):
        """Test with pandas input."""
        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)
        table = pd.DataFrame({'premium': premium, 'moneyness': moneyness})
        table['maturity'] = maturity
        table['call'] = call
        table['imp_vol'] = impvol_table(table)

        self.assertIsInstance(table, pd.DataFrame)
        np.testing.assert_array_almost_equal(table['imp_vol'].values, sigma, 3)

        dct = {'premium': premium, 'moneyness': moneyness,
               'maturity': maturity, 'call': call}
        dct['imp_vol'] = impvol_table(dct)
        self.assertIsInstance(dct, dict)
        np.testing.assert_array_almost_equal(dct['imp_vol'], sigma, 3)