EuropeanOption

class pfhedge.instruments.EuropeanOption(underlier, call=True, strike=1.0, maturity=0.08, dtype=None, device=None)[source]

European option.

The payoff of a European call option is given by:

\[\mathrm{payoff} = \max(S - K, 0) ,\]

where \(S\) is the underlier’s spot price at maturity and \(K\) is the strike.

The payoff of a European put option is given by:

\[\mathrm{payoff} = \max(K - S, 0) .\]
Parameters
  • underlier (BasePrimary) – The underlying instrument of the option.

  • call (bool, default=True) – Specifies whether the option is call or put.

  • strike (float, default=1.0) – The strike price of the option.

  • maturity (float, default=20/250) – The maturity of the option.

dtype

The dtype with which the simulated time-series are represented.

Type

torch.dtype

device

The device where the simulated time-series are.

Type

torch.device

Examples

>>> import torch
>>> from pfhedge.instruments import BrownianStock
>>> from pfhedge.instruments import EuropeanOption
>>>
>>> _ = torch.manual_seed(42)
>>> derivative = EuropeanOption(BrownianStock(), maturity=5/250)
>>> derivative.simulate(n_paths=2)
>>> derivative.underlier.spot
tensor([[1.0000, 1.0016, 1.0044, 1.0073, 0.9930, 0.9906],
        [1.0000, 0.9919, 0.9976, 1.0009, 1.0076, 1.0179]])
>>> derivative.payoff()
tensor([0.0000, 0.0179])

Using custom dtype and device.

>>> derivative = EuropeanOption(BrownianStock())
>>> derivative.to(dtype=torch.float64, device="cuda:0")
EuropeanOption(
  ...
  (underlier): BrownianStock(..., dtype=torch.float64, device='cuda:0')
)

Make self a listed derivative.

>>> from pfhedge.nn import BlackScholes
>>>
>>> pricer = lambda derivative: BlackScholes(derivative).price(
...     log_moneyness=derivative.log_moneyness(),
...     time_to_maturity=derivative.time_to_maturity(),
...     volatility=derivative.ul().volatility)
>>> derivative = EuropeanOption(BrownianStock(), maturity=5/250)
>>> derivative.list(pricer, cost=1e-4)
>>> derivative.simulate(n_paths=2)
>>> derivative.ul().spot
tensor([[1.0000, 0.9788, 0.9665, 0.9782, 0.9947, 1.0049],
        [1.0000, 0.9905, 1.0075, 1.0162, 1.0119, 1.0220]])
>>> derivative.spot
tensor([[0.0113, 0.0028, 0.0006, 0.0009, 0.0028, 0.0049],
        [0.0113, 0.0060, 0.0130, 0.0180, 0.0131, 0.0220]])

Add a knock-out clause with a barrier at 1.03:

>>> _ = torch.manual_seed(42)
>>> derivative = EuropeanOption(BrownianStock(), maturity=5/250)
>>> derivative.simulate(n_paths=8)
>>> derivative.payoff()
tensor([0.0000, 0.0000, 0.0113, 0.0414, 0.0389, 0.0008, 0.0000, 0.0000])
>>>
>>> def knockout(derivative, payoff):
...     max = derivative.underlier.spot.max(-1).values
...     return payoff.where(max < 1.03, torch.zeros_like(max))
>>>
>>> derivative.add_clause("knockout", knockout)
>>> derivative
EuropeanOption(
  strike=1., maturity=0.0200
  clauses=['knockout']
  (underlier): BrownianStock(sigma=0.2000, dt=0.0040)
)
>>> derivative.payoff()
tensor([0.0000, 0.0000, 0.0113, 0.0000, 0.0000, 0.0008, 0.0000, 0.0000])
list(pricer, cost=0.0)

Make self a listed derivative.

After this method self will be a exchange-traded derivative which can be transacted at any time with the spot price given by self.spot.

See an example in EuropeanOption for a usage.

Parameters
  • pricer (Callable[[BaseDerivative], Tensor]]) – A function that takes self and returns the spot price tensor of self.

  • cost (float, optional) – The transaction cost rate.

log_moneyness(time_step=None)

Returns log-moneyness of self.

Log-moneyness reads \(\log(S / K)\) where \(S\) is the spot price of the underlying instrument and \(K\) is the strike of the derivative.

Returns

torch.Tensor

moneyness(time_step=None, log=False)

Returns the moneyness of self.

Moneyness reads \(S / K\) where \(S\) is the spot price of the underlying instrument and \(K\) is the strike of the derivative.

Parameters
  • time_step (int, optional) – The time step to calculate the moneyness. If None (default), the moneyness is calculated at all time steps.

  • log (bool, default=False) – If True, returns log moneyness.

Shape:
  • Output: \((N, T)\) where \(N\) is the number of paths and \(T\) is the number of time steps. If time_step is given, the shape is \((N, 1)\).

Returns

torch.Tensor

simulate(n_paths=1, init_state=None)

Simulate time series associated with the underlier.

Parameters
  • n_paths (int) – The number of paths to simulate.

  • init_state (tuple[torch.Tensor | float], optional) – The initial state of the underlier.

  • **kwargs – Other parameters passed to self.underlier.simulate().

time_to_maturity(time_step=None)

Returns the time to maturity of self.

Parameters

time_step (int, optional) – The time step to calculate the time to maturity. If None (default), the time to maturity is calculated at all time steps.

Shape:
  • Output: \((N, T)\) where \(N\) is the number of paths and \(T\) is the number of time steps. If time_step is given, the shape is \((N, 1)\).

Returns

torch.Tensor

to(*args, **kwargs)

Moves and/or casts the buffers of the instrument.

This can be called as

to(device=None, dtype=None)
to(tensor)
to(instrument)

Its signature is similar to torch.nn.Module.to(). It only accepts floating point dtypes. See Instrument dtype and device for details.

Note

This method modifies the instrument in-place.

See also

  • float(): Cast to torch.float32.

  • double(): Cast to torch.float64.

  • half(): Cast to torch.float16.

  • bfloat16(): Cast to torch.bfloat16.

  • cuda(): Move to CUDA memory.

  • cpu(): Move to CPU memory.

Parameters
  • dtype (torch.dtype) – The desired floating point dtype of the buffers in this instrument.

  • device (torch.device) – The desired device of the buffers in this instrument.

  • tensor (torch.Tensor) – Tensor whose dtype and device are the desired dtype and device of the buffers in this instrument.

  • instrument (BaseInstrument) – Instrument whose dtype and device are the desired dtype and device of the buffers in this instrument.

Returns

self

ul(index=0)

Alias for self.underlier.