Пример #1
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"
                                ")")
Пример #2
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))
Пример #3
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
Пример #4
0
    def test_shape(self):
        torch.distributions.Distribution.set_default_validate_args(False)

        deriv = EuropeanOption(BrownianStock())
        m = BlackScholes(deriv)

        N = 10
        H_in = len(m.features())
        M_1 = 12
        M_2 = 13

        x = torch.empty((N, H_in))
        assert m(x).size() == torch.Size((N, 1))

        x = torch.empty((N, M_1, H_in))
        assert m(x).size() == torch.Size((N, M_1, 1))

        x = torch.empty((N, M_1, M_2, H_in))
        assert m(x).size() == torch.Size((N, M_1, M_2, 1))
Пример #5
0
def test_bs():
    liability = EuropeanOption(BrownianStock(cost=1e-4))
    model = BlackScholes(liability)
    hedger = Hedger(model, model.features())
    _ = hedger.price(liability)
Пример #6
0
    print_as_comment(hedger)

    hedger.fit(deriv)
    price = hedger.price(deriv)

    print(">>> price")
    print_as_comment(price)

    # --- Black-Scholes and Whalley-Wilmott

    from pfhedge.nn import BlackScholes
    from pfhedge.nn import WhalleyWilmott

    deriv = EuropeanOption(BrownianStock(cost=1e-4))

    model = BlackScholes(deriv)
    hedger_bs = Hedger(model, model.features())
    hedger_bs
    print(">>> hedger_bs")
    print_as_comment(hedger_bs)

    model = WhalleyWilmott(deriv)
    hedger_ww = Hedger(model, model.features())
    hedger_ww
    print(">>> hedger_ww")
    print_as_comment(hedger_ww)

    price_bs = hedger_bs.price(deriv)
    price_ww = hedger_ww.price(deriv)

    print(">>> price_bs")
Пример #7
0
# Example to use Black-Scholes' delta-hedging strategy as a hedging model

import sys

import torch

sys.path.append("..")

from pfhedge import Hedger  # noqa: E402
from pfhedge.instruments import BrownianStock  # noqa: E402
from pfhedge.instruments import EuropeanOption  # noqa: E402
from pfhedge.nn import BlackScholes  # noqa: E402

if __name__ == "__main__":
    torch.manual_seed(42)

    # Prepare a derivative to hedge
    deriv = EuropeanOption(BrownianStock(cost=1e-4))

    # Create your hedger
    model = BlackScholes(deriv)
    hedger = Hedger(model, model.features())

    # Fit and price
    price = hedger.price(deriv, n_paths=10000)
    print(f"Price={price:.5e}")
Пример #8
0
from pfhedge.features import ExpiryTime  # noqa: E402
from pfhedge.features import Volatility  # noqa: E402
from pfhedge.features import ModuleOutput  # noqa: E402
from pfhedge.instruments import BrownianStock  # noqa: E402
from pfhedge.instruments import EuropeanOption  # noqa: E402
from pfhedge.nn import BlackScholes  # noqa: E402
from pfhedge.nn import MultiLayerPerceptron  # noqa: E402

if __name__ == "__main__":
    torch.manual_seed(42)

    # Prepare a derivative to hedge
    deriv = EuropeanOption(BrownianStock(cost=1e-4))

    # bs is a module that outputs Black-Scholes' delta
    bs = BlackScholes(deriv)
    delta = ModuleOutput(bs,
                         features=[LogMoneyness(),
                                   ExpiryTime(),
                                   Volatility()])

    # Create your hedger
    # Here `delta` is a feature that outputs Black-Scholes' delta
    model = MultiLayerPerceptron()
    hedger = Hedger(model, [delta, "prev_hedge"])

    # Fit and price
    hedger.fit(deriv, n_paths=10000, n_epochs=200)
    price = hedger.price(deriv, n_paths=10000)
    print(f"Price={price:.5e}")