Exemplo n.º 1
0
    def test_dtype(self, dtype):
        liability = LookbackOption(BrownianStock(dtype=dtype))
        liability.simulate()
        assert liability.payoff().dtype == dtype

        liability = LookbackOption(BrownianStock()).to(dtype=dtype)
        liability.simulate()
        assert liability.payoff().dtype == dtype
Exemplo n.º 2
0
    def test_dtype(self, dtype):
        liability = EuropeanBinaryOption(BrownianStock(dtype=dtype))
        assert liability.dtype == dtype
        liability.simulate()
        assert liability.payoff().dtype == dtype

        liability = EuropeanBinaryOption(BrownianStock()).to(dtype=dtype)
        liability.simulate()
        assert liability.payoff().dtype == dtype
Exemplo n.º 3
0
 def test_repr(self):
     liability = EuropeanOption(BrownianStock(), maturity=1.0)
     expect = "EuropeanOption(BrownianStock(...), maturity=1.00e+00)"
     assert repr(liability) == expect
     liability = EuropeanOption(BrownianStock(), maturity=1.0, call=False)
     expect = "EuropeanOption(BrownianStock(...), call=False, maturity=1.00e+00)"
     assert repr(liability) == expect
     liability = EuropeanOption(BrownianStock(), maturity=1.0, strike=2.0)
     expect = "EuropeanOption(BrownianStock(...), strike=2.0, maturity=1.00e+00)"
     assert repr(liability) == expect
Exemplo n.º 4
0
    def test_dtype(self, dtype):
        s = BrownianStock(dtype=dtype)
        s.simulate(1.0)
        assert s.prices.dtype == dtype

        s = BrownianStock().to(dtype=dtype)
        s.simulate(1.0)
        assert s.prices.dtype == dtype
Exemplo n.º 5
0
    def test_repr(self):
        m = BSAmericanBinaryOption()
        assert repr(m) == "BSAmericanBinaryOption()"

        liability = AmericanBinaryOption(BrownianStock(), strike=1.1)
        m = BSAmericanBinaryOption(liability)
        assert repr(m) == "BSAmericanBinaryOption(strike=1.1)"

        with pytest.raises(ValueError):
            # not yet supported
            liability = AmericanBinaryOption(BrownianStock(),
                                             strike=1.1,
                                             call=False)
            m = BSAmericanBinaryOption(liability)
            assert repr(m) == "BSAmericanBinaryOption(call=False, strike=1.1)"
Exemplo n.º 6
0
 def test_repr(self):
     s = BrownianStock(dt=1 / 100)
     assert repr(s) == "BrownianStock(volatility=2.00e-01, dt=1.00e-02)"
     s = BrownianStock(dt=1 / 100, cost=0.001)
     assert (
         repr(s) ==
         "BrownianStock(volatility=2.00e-01, cost=1.00e-03, dt=1.00e-02)")
     s = BrownianStock(dt=1 / 100, dtype=torch.float64)
     assert (
         repr(s) ==
         "BrownianStock(volatility=2.00e-01, dt=1.00e-02, dtype=torch.float64)"
     )
     s = BrownianStock(dt=1 / 100, device="cuda:0")
     assert (
         repr(s) ==
         "BrownianStock(volatility=2.00e-01, dt=1.00e-02, device=cuda:0)")
Exemplo n.º 7
0
    def test_shape(self):
        torch.distributions.Distribution.set_default_validate_args(False)

        deriv = EuropeanOption(BrownianStock())

        N = 2
        M_1 = 5
        M_2 = 6
        H_in = 3

        x = torch.empty((N, M_1, M_2, H_in))
        m = Hedger(MultiLayerPerceptron(), ["zero"])
        assert m(x).size() == torch.Size((N, M_1, M_2, 1))

        model = BlackScholes(deriv)
        m = Hedger(model, model.features())
        x = torch.empty((N, M_1, M_2, len(model.features())))
        assert m(x).size() == torch.Size((N, M_1, M_2, 1))

        model = WhalleyWilmott(deriv)
        m = Hedger(model, model.features())
        x = torch.empty((N, M_1, M_2, len(model.features())))
        assert m(x).size() == torch.Size((N, M_1, M_2, 1))

        model = Naked()
        m = Hedger(model, ["zero"])
        x = torch.empty((N, M_1, M_2, 10))
        assert m(x).size() == torch.Size((N, M_1, M_2, 1))
Exemplo n.º 8
0
    def test_repr(self):
        m = BSLookbackOption()
        assert repr(m) == "BSLookbackOption()"

        liability = LookbackOption(BrownianStock(), strike=1.1)
        m = BSLookbackOption(liability)
        assert repr(m) == "BSLookbackOption(strike=1.1)"
Exemplo n.º 9
0
    def test_repr(self):
        hedger = Hedger(Linear(2, 1), ["moneyness", "expiry_time"])
        assert repr(hedger) == (
            "Hedger(\n"
            "  features=['moneyness', 'expiry_time'],\n"
            "  (model): Linear(in_features=2, out_features=1, bias=True)\n"
            "  (criterion): EntropicRiskMeasure()\n"
            ")")

        liability = EuropeanOption(BrownianStock())
        model = BlackScholes(liability)
        hedger = Hedger(model, model.features())
        assert repr(hedger) == (
            "Hedger(\n"
            "  features=['log_moneyness', 'expiry_time', 'volatility'],\n"
            "  (model): BSEuropeanOption()\n"
            "  (criterion): EntropicRiskMeasure()\n"
            ")")

        hedger = Hedger(naked, ["moneyness", "expiry_time"])
        assert repr(hedger) == ("Hedger(\n"
                                "  model=naked,\n"
                                "  features=['moneyness', 'expiry_time'],\n"
                                "  (criterion): EntropicRiskMeasure()\n"
                                ")")
Exemplo n.º 10
0
    def test_repr(self):
        m = BSEuropeanOption()
        assert repr(m) == "BSEuropeanOption()"

        liability = EuropeanOption(BrownianStock(), strike=1.1, call=False)
        m = BSEuropeanOption(liability)
        assert repr(m) == "BSEuropeanOption(call=False, strike=1.1)"
Exemplo n.º 11
0
    def test_init(self):
        liability = EuropeanOption(BrownianStock())
        m = BlackScholes(liability)
        assert m.__class__ == BSEuropeanOption
        assert m.strike == 1.0
        assert m.call

        liability = EuropeanOption(BrownianStock(), strike=2.0, call=False)
        m = BlackScholes(liability)
        assert m.__class__ == BSEuropeanOption
        assert m.strike == 2.0
        assert not m.call

        liability = LookbackOption(BrownianStock())
        m = BlackScholes(liability)
        assert m.__class__ == BSLookbackOption
        assert m.strike == 1.0
        assert m.call
Exemplo n.º 12
0
    def test_compute_loss(self):
        torch.manual_seed(42)
        deriv = EuropeanOption(BrownianStock())
        hedger = Hedger(Naked(),
                        ["log_moneyness", "expiry_time", "volatility"])

        result = hedger.compute_loss(deriv)
        expect = EntropicRiskMeasure()(-deriv.payoff())
        assert torch.allclose(result, expect)
Exemplo n.º 13
0
 def test_repr(self):
     liability = AmericanBinaryOption(BrownianStock(), maturity=1.0)
     expect = "AmericanBinaryOption(BrownianStock(...), maturity=1.00e+00)"
     assert repr(liability) == expect
     liability = AmericanBinaryOption(BrownianStock(),
                                      maturity=1.0,
                                      call=False)
     expect = (
         "AmericanBinaryOption(BrownianStock(...), call=False, maturity=1.00e+00)"
     )
     assert repr(liability) == expect
     liability = AmericanBinaryOption(BrownianStock(),
                                      maturity=1.0,
                                      strike=2.0)
     expect = (
         "AmericanBinaryOption(BrownianStock(...), strike=2.0, maturity=1.00e+00)"
     )
     assert repr(liability) == expect
Exemplo n.º 14
0
    def test(self):
        liability = EuropeanOption(BrownianStock())
        liability.underlier.prices = torch.tensor([
            [1.0, 2.0, 3.0, 1.0],
            [1.5, 1.0, 4.0, 1.1],
            [2.0, 1.0, 5.0, 1.2],
            [3.0, 1.0, 6.0, 1.3],
        ])
        f = Barrier(2.0, up=True).of(liability)

        result = f[0]
        expect = torch.tensor([0.0, 1.0, 1.0, 0.0]).reshape(-1, 1)
        assert torch.allclose(result, expect)
        result = f[1]
        expect = torch.tensor([0.0, 1.0, 1.0, 0.0]).reshape(-1, 1)
        assert torch.allclose(result, expect)
        result = f[2]
        expect = torch.tensor([1.0, 1.0, 1.0, 0.0]).reshape(-1, 1)
        assert torch.allclose(result, expect)
        result = f[3]
        expect = torch.tensor([1.0, 1.0, 1.0, 0.0]).reshape(-1, 1)
        assert torch.allclose(result, expect)

        liability = EuropeanOption(BrownianStock())
        liability.underlier.prices = torch.tensor([
            [3.0, 1.0, 6.0, 1.3],
            [2.0, 1.0, 5.0, 1.2],
            [1.5, 1.0, 4.0, 1.1],
            [1.0, 2.0, 3.0, 1.0],
        ])
        f = Barrier(2.0, up=False).of(liability)

        result = f[0]
        expect = torch.tensor([0.0, 1.0, 0.0, 1.0]).reshape(-1, 1)
        assert torch.allclose(result, expect)
        result = f[1]
        expect = torch.tensor([1.0, 1.0, 0.0, 1.0]).reshape(-1, 1)
        assert torch.allclose(result, expect)
        result = f[2]
        expect = torch.tensor([1.0, 1.0, 0.0, 1.0]).reshape(-1, 1)
        assert torch.allclose(result, expect)
        result = f[3]
        expect = torch.tensor([1.0, 1.0, 0.0, 1.0]).reshape(-1, 1)
        assert torch.allclose(result, expect)
Exemplo n.º 15
0
 def test_payoff(self):
     liability = EuropeanOption(BrownianStock(), strike=2.0)
     liability.underlier.prices = torch.tensor([
         [1.0, 1.0, 1.0, 1.0],
         [1.0, 1.0, 1.0, 1.0],
         [1.9, 2.0, 2.1, 3.0],
     ])
     result = liability.payoff()
     expect = torch.tensor([0.0, 0.0, 0.1, 1.0])
     assert torch.allclose(result, expect)
Exemplo n.º 16
0
    def test_example(self):
        from pfhedge import Hedger
        from pfhedge.instruments import BrownianStock
        from pfhedge.instruments import EuropeanOption

        liability = EuropeanOption(BrownianStock())
        model = BSEuropeanOption()
        hedger = Hedger(model, model.features())
        price = hedger.price(liability)
        assert torch.allclose(price, torch.tensor(0.0221), atol=1e-4)
Exemplo n.º 17
0
 def test_payoff_put(self):
     liability = LookbackOption(BrownianStock(), strike=3.0, call=False)
     liability.underlier.prices = torch.tensor([
         [3.0, 6.0, 3.0],
         [2.0, 5.0, 4.0],
         [2.5, 4.0, 5.0],
     ])
     # min [2.0, 4.0, 3.0]
     result = liability.payoff()
     expect = torch.tensor([1.0, 0.0, 0.0])
     assert torch.allclose(result, expect)
Exemplo n.º 18
0
 def test_payoff(self):
     liability = LookbackOption(BrownianStock(), strike=3.0)
     liability.underlier.prices = torch.tensor([
         [1.0, 2.0, 3.0],
         [2.0, 3.0, 2.0],
         [1.5, 4.0, 1.0],
     ])
     # max [2.0, 4.0, 3.0]
     result = liability.payoff()
     expect = torch.tensor([0.0, 1.0, 0.0])
     assert torch.allclose(result, expect)
Exemplo n.º 19
0
    def test_example(self):
        from pfhedge import Hedger
        from pfhedge.instruments import BrownianStock
        from pfhedge.instruments import LookbackOption

        deriv = LookbackOption(BrownianStock(), strike=1.03)
        model = BSLookbackOption(deriv)
        hedger = Hedger(model, model.features())
        price = hedger.price(deriv)

        assert torch.allclose(price, torch.tensor(0.0177), atol=1e-4)
Exemplo n.º 20
0
    def test_example(self):
        from pfhedge import Hedger
        from pfhedge.instruments import AmericanBinaryOption
        from pfhedge.instruments import BrownianStock

        deriv = AmericanBinaryOption(BrownianStock(), strike=1.03)
        model = BSAmericanBinaryOption(deriv)
        hedger = Hedger(model, model.features())
        price = hedger.price(deriv)

        assert torch.allclose(price, torch.tensor(0.6219), atol=1e-4)
Exemplo n.º 21
0
    def test_example(self):
        from pfhedge import Hedger
        from pfhedge.instruments import BrownianStock
        from pfhedge.instruments import EuropeanBinaryOption

        deriv = EuropeanBinaryOption(BrownianStock())
        model = BSEuropeanBinaryOption(deriv)
        hedger = Hedger(model, model.features())
        price = hedger.price(deriv)

        assert torch.allclose(price, torch.tensor(0.5004), atol=1e-4)
Exemplo n.º 22
0
    def test_compute_pnl(self):
        torch.manual_seed(42)
        deriv = EuropeanOption(BrownianStock())
        hedger = Hedger(Naked(), ["zero"])

        pnl = hedger.compute_pnl(deriv)
        payoff = deriv.payoff()
        assert torch.allclose(pnl, -payoff)

        result = hedger.compute_pnl(deriv)
        expect = -deriv.payoff()
        assert torch.allclose(result, expect)
Exemplo n.º 23
0
    def test_repr(self):
        liability = EuropeanOption(BrownianStock())
        m = WhalleyWilmott(liability)
        assert repr(m) == ("WhalleyWilmott(\n"
                           "  (bs): BSEuropeanOption()\n"
                           "  (clamp): Clamp()\n"
                           ")")

        liability = EuropeanOption(BrownianStock())
        m = WhalleyWilmott(liability, a=2)
        assert repr(m) == ("WhalleyWilmott(\n"
                           "  a=2\n"
                           "  (bs): BSEuropeanOption()\n"
                           "  (clamp): Clamp()\n"
                           ")")

        liability = LookbackOption(BrownianStock())
        m = WhalleyWilmott(liability)
        assert repr(m) == ("WhalleyWilmott(\n"
                           "  (bs): BSLookbackOption()\n"
                           "  (clamp): Clamp()\n"
                           ")")
Exemplo n.º 24
0
    def test_payoff(self):
        liability = AmericanBinaryOption(BrownianStock(), strike=2.0)
        liability.underlier.prices = torch.tensor([
            [1.0, 1.0, 1.0, 1.0],
            [1.0, 1.0, 1.0, 2.0],
            [1.9, 2.0, 2.1, 1.0],
        ])
        result = liability.payoff()
        expect = torch.tensor([0.0, 1.0, 1.0, 1.0])
        assert torch.allclose(result, expect)

        liability = AmericanBinaryOption(BrownianStock(),
                                         strike=1.0,
                                         call=False)
        liability.underlier.prices = torch.tensor([
            [2.0, 2.0, 2.0, 2.0],
            [2.0, 2.0, 2.0, 1.0],
            [1.1, 1.0, 0.9, 2.0],
        ])
        result = liability.payoff()
        expect = torch.tensor([0.0, 1.0, 1.0, 1.0])
        assert torch.allclose(result, expect)
Exemplo n.º 25
0
    def test(self, volatility):
        liability = EuropeanOption(BrownianStock(volatility=volatility))
        liability.underlier.prices = torch.arange(1.0, 7.0).reshape(3, 2)

        f = Volatility().of(liability)

        result = f[0]
        expect = torch.full((2, 1), volatility)
        assert torch.allclose(result, expect)
        result = f[1]
        expect = torch.full((2, 1), volatility)
        assert torch.allclose(result, expect)
        result = f[2]
        expect = torch.full((2, 1), volatility)
        assert torch.allclose(result, expect)
Exemplo n.º 26
0
    def test(self):
        torch.manual_seed(42)
        liability = EuropeanOption(BrownianStock())
        liability.underlier.prices = torch.arange(1.0, 7.0).reshape(3, 2)

        f = Zero().of(liability)

        result = f[0]
        expect = torch.zeros((2, 1))
        assert torch.allclose(result, expect)
        result = f[1]
        expect = torch.zeros((2, 1))
        assert torch.allclose(result, expect)
        result = f[2]
        expect = torch.zeros((2, 1))
        assert torch.allclose(result, expect)
Exemplo n.º 27
0
    def test_forward(self):
        m = BSEuropeanOption()
        x = torch.tensor([0.0, 1.0, 0.2]).reshape(1, -1)
        result = m(x).item()
        assert np.isclose(result, 0.5398278962)

        m = BSEuropeanOption(call=False)
        x = torch.tensor([0.0, 1.0, 0.2]).reshape(1, -1)
        result = m(x).item()
        assert np.isclose(result, -0.4601721)

        liability = EuropeanOption(BrownianStock(), call=False)
        m = BSEuropeanOption(liability)
        x = torch.tensor([0.0, 1.0, 0.2]).reshape(1, -1)
        result = m(x).item()
        assert np.isclose(result, -0.4601721)
Exemplo n.º 28
0
    def test(self):
        liability = EuropeanOption(BrownianStock())
        liability.simulate()

        module = torch.nn.Linear(2, 1)
        x1, x2 = Moneyness(), ExpiryTime()
        f = ModuleOutput(module, [x1, x2]).of(liability)

        result = f[0]
        expect = module(torch.cat([x1[0], x2[0]], 1))
        assert torch.allclose(result, expect)
        result = f[1]
        expect = module(torch.cat([x1[1], x2[1]], 1))
        assert torch.allclose(result, expect)
        result = f[2]
        expect = module(torch.cat([x1[2], x2[2]], 1))
        assert torch.allclose(result, expect)
Exemplo n.º 29
0
    def test(self):
        maturity = 3 / 365
        dt = 1 / 365
        liability = EuropeanOption(BrownianStock(dt=dt), maturity=maturity)
        liability.underlier.prices = torch.arange(1.0, 7.0).reshape(3, 2)

        f = ExpiryTime().of(liability)

        result = f[0]
        expect = torch.full((2, 1), 3 / 365)
        assert torch.allclose(result, expect)
        result = f[1]
        expect = torch.full((2, 1), 2 / 365)
        assert torch.allclose(result, expect)
        result = f[2]
        expect = torch.full((2, 1), 1 / 365)
        assert torch.allclose(result, expect)
Exemplo n.º 30
0
    def test_parity(self, volatility, strike, maturity, n_paths, init_price):
        """
        Test put-call parity.
        """
        stock = BrownianStock(volatility)
        co = EuropeanOption(stock, strike=strike, maturity=maturity, call=True)
        po = EuropeanOption(stock,
                            strike=strike,
                            maturity=maturity,
                            call=False)
        co.simulate(n_paths=n_paths, init_price=init_price)
        po.simulate(n_paths=n_paths, init_price=init_price)

        s = stock.prices[-1, :]
        c = co.payoff()
        p = po.payoff()

        assert ((c - p) == s - strike).all()