pfhedge.stochastic.generate_local_volatility_process

pfhedge.stochastic.generate_local_volatility_process(n_paths, n_steps, sigma_fn, init_state=(1.0,), dt=0.004, dtype=None, device=None)[source]

Returns time series following the local volatility model.

The time evolution of the process is given by:

\[dS(t) = \sigma_{\mathrm{LV}}(t, S(t)) S(t) dW(t) ,\]

where \(\sigma_{\mathrm{LV}}\) is the local volatility function.

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

  • n_steps (int) – The number of time steps.

  • init_state (tuple[torch.Tensor | float], default=(0.0,)) – The initial state of the time series. This is specified by a tuple \((S(0),)\). It also accepts a torch.Tensor or a float.

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

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

  • dtype (torch.dtype, optional) – The desired data type of returned tensor. Default: If None, uses a global default (see torch.set_default_tensor_type()).

  • device (torch.device, optional) – The 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.

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

Returns

A namedtuple (spot, volatility).

Return type

(torch.Tensor, torch.Tensor)

Examples

>>> from pfhedge.stochastic import generate_local_volatility_process
...
>>> def sigma_fn(time: Tensor, spot: Tensor) -> Tensor:
...     a, b, sigma = 0.0001, 0.0004, 0.1000
...     sqrt_term = (spot.log().square() + sigma ** 2).sqrt()
...     return ((a + b * sqrt_term) / time.clamp(min=1/250)).sqrt()
...
>>> _ = torch.manual_seed(42)
>>> spot, volatility = generate_local_volatility_process(2, 5, sigma_fn)
>>> spot
tensor([[1.0000, 1.0040, 1.0055, 1.0075, 1.0091],
        [1.0000, 0.9978, 1.0239, 1.0184, 1.0216]])
>>> volatility
tensor([[0.1871, 0.1871, 0.1323, 0.1081, 0.0936],
        [0.1871, 0.1871, 0.1328, 0.1083, 0.0938]])