LocalVolatilityStock

class pfhedge.instruments.LocalVolatilityStock(sigma_fn, cost=0.0, dt=0.004, dtype=None, device=None)[source]

A stock of which spot prices follow the local volatility model.

See also

Parameters
  • sigma_fn (callable) – The local volatility function. Its signature is sigma_fn(time: Tensor, spot: Tensor) -> Tensor.

  • cost (float, default=0.0) – The transaction cost rate.

  • dt (float, default=1/250) – The intervals of the time steps.

  • dtype (torch.device, optional) – Desired device of returned tensor. Default: If None, uses a global default (see torch.set_default_tensor_type()).

  • device (torch.device, optional) – Desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

Buffers:
  • spot (torch.Tensor): The spot prices of the instrument. This attribute is set by a method simulate(). The shape is (N,T) where N is the number of simulated paths and T is the number of time steps.

property default_init_state

Returns the default initial state of simulation.

simulate(n_paths=1, time_horizon=0.08, init_state=None)[source]

Simulate the spot price and add it as a buffer named spot.

The shape of the spot is (N,T), where N is the number of simulated paths and T is the number of time steps. The number of time steps is determinded from dt and time_horizon.

Parameters
  • n_paths (int, default=1) – The number of paths to simulate.

  • time_horizon (float, default=20/250) – The period of time to simulate the price.

  • init_state (tuple[torch.Tensor | float], optional) – The initial state of the instrument. This is specified by a tuple (S(0),) where S(0) is the initial value of the spot price. If None (default), it uses the default value (See default_init_state). It also accepts a float or a torch.Tensor.

Examples

>>> from pfhedge.instruments import LocalVolatilityStock
...
>>> def sigma_fn(time: Tensor, spot: Tensor) -> Tensor:
...     a, b, sigma = 0.0001, 0.0004, 0.10
...     sqrt_term = (spot.log().square() + sigma ** 2).sqrt()
...     return ((a + b * sqrt_term) / time.clamp(min=1/250)).sqrt()
...
>>> _ = torch.manual_seed(42)
>>> stock = LocalVolatilityStock(sigma_fn)
>>> stock.simulate(n_paths=2, time_horizon=5 / 250)
>>> stock.spot
tensor([[1.0000, 1.0040, 1.0055, 1.0075, 1.0091, 1.0024],
        [1.0000, 1.0261, 1.0183, 1.0223, 1.0242, 1.0274]])
>>> stock.volatility
tensor([[0.1871, 0.1871, 0.1323, 0.1081, 0.0936, 0.0837],
        [0.1871, 0.1880, 0.1326, 0.1084, 0.0939, 0.0841]])
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

property variance

Returns the volatility of self.

It is a tensor filled with the square of self.sigma.