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(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)
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()