pfhedge.stochastic.generate_heston¶
- pfhedge.stochastic.generate_heston(n_paths, n_steps, init_state=None, kappa=1.0, theta=0.04, sigma=0.2, rho=- 0.7, dt=0.004, dtype=None, device=None)[source]¶
Returns time series following Heston model.
The time evolution of the process is given by:
\[\begin{split}dS(t) = S(t) \sqrt{V(t)} dW_1(t) \,, \\ dV(t) = \kappa (\theta - V(t)) dt + \sigma \sqrt{V(t)} dW_2(t) \,.\end{split}\]The correlation between \(dW_1\) and \(dW_2\) is \(\rho\).
Time-series is generated by Andersen’s QE-M method (See Reference for details).
References
Heston, S.L., 1993. A closed-form solution for options with stochastic volatility with applications to bond and currency options. The review of financial studies, 6(2), pp.327-343.
Andersen, Leif B.G., Efficient Simulation of the Heston Stochastic Volatility Model (January 23, 2007). Available at SSRN: https://ssrn.com/abstract=946405 or http://dx.doi.org/10.2139/ssrn.946404
- Parameters
n_paths (int) – The number of simulated paths.
n_steps (int) – The number of time steps.
init_state (tuple[torch.Tensor | float], optional) – The initial state of the time series. This is specified by a tuple \((S(0), V(0))\). If
None
(default), it uses \((1.0, \theta)\).kappa (float, default=1.0) – The parameter \(\kappa\).
theta (float, default=0.04) – The parameter \(\theta\).
sigma (float, default=2.0) – The parameter \(\sigma\).
rho (float, default=-0.7) – The parameter \(\rho\).
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 (seetorch.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 (seetorch.set_default_tensor_type()
).device
will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
- Shape:
spot: \((N, T)\) where \(N\) is the number of paths and \(T\) is the number of time steps.
variance: \((N, T)\).
- Returns
A namedtuple
(spot, variance)
.- Return type
Examples
>>> from pfhedge.stochastic import generate_heston ... >>> _ = torch.manual_seed(42) >>> spot, variance = generate_heston(2, 5) >>> spot tensor([[1.0000, 0.9941, 0.9905, 0.9846, 0.9706], [1.0000, 1.0031, 0.9800, 0.9785, 0.9735]]) >>> variance tensor([[0.0400, 0.0408, 0.0411, 0.0417, 0.0422], [0.0400, 0.0395, 0.0452, 0.0434, 0.0446]])