コード例 #1
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
コード例 #2
0
 def test_payoff(self):
     liability = EuropeanBinaryOption(BrownianStock(), strike=2.0)
     liability.underlier.prices = torch.tensor([
         [1.0, 1.0, 1.0, 1.0],
         [3.0, 1.0, 1.0, 1.0],
         [1.9, 2.0, 2.1, 3.0],
     ])
     result = liability.payoff()
     expect = torch.tensor([0.0, 1.0, 1.0, 1.0])
     assert torch.allclose(result, expect)
コード例 #3
0
    def test_repr(self):
        m = BSEuropeanBinaryOption()
        assert repr(m) == "BSEuropeanBinaryOption()"

        liability = EuropeanBinaryOption(BrownianStock(), strike=1.1)
        m = BSEuropeanBinaryOption(liability)
        assert repr(m) == "BSEuropeanBinaryOption(strike=1.1)"

        with pytest.raises(ValueError):
            # not yet supported
            liability = EuropeanBinaryOption(BrownianStock(),
                                             strike=1.1,
                                             call=False)
            m = BSEuropeanBinaryOption(liability)
            assert repr(m) == "BSEuropeanBinaryOption(call=False, strike=1.1)"
コード例 #4
0
 def test_repr(self):
     liability = EuropeanBinaryOption(BrownianStock(), maturity=1.0)
     expect = "EuropeanBinaryOption(BrownianStock(...), maturity=1.00e+00)"
     assert repr(liability) == expect
     liability = EuropeanBinaryOption(BrownianStock(),
                                      maturity=1.0,
                                      call=False)
     expect = (
         "EuropeanBinaryOption(BrownianStock(...), call=False, maturity=1.00e+00)"
     )
     assert repr(liability) == expect
     liability = EuropeanBinaryOption(BrownianStock(),
                                      maturity=1.0,
                                      strike=2.0)
     expect = (
         "EuropeanBinaryOption(BrownianStock(...), strike=2.0, maturity=1.00e+00)"
     )
     assert repr(liability) == expect
コード例 #5
0
    def test_parity(self, volatility, strike, maturity, n_paths, init_price):
        """
        Test put-call parity.
        """
        stock = BrownianStock(volatility)
        co = EuropeanBinaryOption(stock,
                                  strike=strike,
                                  maturity=maturity,
                                  call=True)
        po = EuropeanBinaryOption(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)

        c = co.payoff()
        p = po.payoff()

        assert (c + p == 1.0).all()
コード例 #6
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)
コード例 #7
0
 def test_device(self, device):
     liability = EuropeanBinaryOption(BrownianStock(device=device))
     assert liability.device == torch.device(device)
コード例 #8
0
 def test_error_put(self):
     with pytest.raises(ValueError):
         # not yet supported
         liability = EuropeanBinaryOption(BrownianStock(), call=False)
         BSEuropeanBinaryOption(liability)