ModuleOutput

class pfhedge.features.ModuleOutput(module, inputs)[source]

The feature computed as an output of a torch.nn.Module.

Parameters
  • module (torch.nn.Module) – Module to compute the value of the feature. The input and output shapes should be \((N, *, H_{\mathrm{in}}) \to (N, *, H_{\mathrm{out}})\) where \(N\) is the number of simulated paths of the underlying instrument, \(H_{\mathrm{in}}\) is the number of input features, \(H_{\mathrm{out}}\) is the number of output features, and \(*\) means any number of additional dimensions.

  • inputs (list[Feature]) – The input features to the module.

Examples

>>> from torch.nn import Linear
>>> from pfhedge.instruments import BrownianStock
>>> from pfhedge.instruments import EuropeanOption
>>> derivative = EuropeanOption(BrownianStock())
>>> derivative.simulate(n_paths=3)
>>>
>>> m = Linear(2, 1)
>>> f = ModuleOutput(m, inputs=["moneyness", "expiry_time"]).of(derivative)
>>> f.get(0).size()
torch.Size([3, 1, 1])
>>> f
ModuleOutput(
  inputs=['moneyness', 'expiry_time']
  (module): Linear(in_features=2, out_features=1, bias=True)
)
>>> from pfhedge.nn import BlackScholes
>>>
>>> _ = torch.manual_seed(42)
>>> derivative = EuropeanOption(BrownianStock())
>>> derivative.simulate(n_paths=3)
>>> m = BlackScholes(derivative)
>>> f = ModuleOutput(m, ["log_moneyness", "expiry_time", "volatility"])
>>> f = f.of(derivative)
>>> f.get(0).size()
torch.Size([3, 1, 1])