Example #1
0
    def test_dtype(self, dtype):
        liability = EuropeanOption(BrownianStock(dtype=dtype))
        assert liability.dtype == dtype
        liability.simulate()
        assert liability.payoff().dtype == dtype

        liability = EuropeanOption(BrownianStock()).to(dtype=dtype)
        liability.simulate()
        assert liability.payoff().dtype == dtype
Example #2
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)
Example #3
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()
Example #4
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)
Example #5
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)