Path: blob/master/1.4 SDE - Heston model.ipynb
1675 views
In order to generate correlated Gaussian distributed random variables
where is the vector to simulate, the vector of means and the covariance matrix,
we first need to simulate a vector of uncorrelated standard Normal random variables,
then find a square root of , i.e. a matrix such that .
The vector is given by
A popular choice to calculate is the Cholesky decomposition.
Example:
Let us consider the following 2-dimensional covariance matrix:
where for simplicity I chose , such that corresponds to the correlation matrix. I also set .
In python we can use the built in function scipy.stats.multivariate_normal
as follows:
If we want to use the algorithm proposed above, we have to find the matrix C.
In two dimensions it has the simple form:
By a simple matrix multiplication we can verify that:
Therefore, in 2 dimensions there is no need to use the Cholesky decomposition. We simply have:
For higher dimensional problems, the function scipy.linalg.cholesky
doc can help:
Heston model
The Heston process is described by the SDE:
The stock price follows a "geometric Brownian motion" with a stochastic volatility. The square of the volatility (the variance) follows a CIR process. Have a look at the notebook 1.2 for more information on the CIR process.
The parameters are:
drift of the stock process
mean reversion coefficient of the variance process
long term mean of the variance process
volatility coefficient of the variance process
correlation between and i.e.
We will also require that (Feller condition).
Path generation
The Heston process can be discretized using the Euler-Maruyama method proposed in the notebook 2.1.
In the next cell I present the algorithm that produces sample paths of the Heston process. In this example, I prefer to use the log-variables and in order to avoid negative values for the process .
This is an arbitrary choice!
Comment: From a theoretical point of view, using log-variables is the best choice, because it avoids negative values without any tweak of the original process. From the practical point of view, it is better not to use log-variables! The reason is that the algorithm can produce NaNs or overflows when the time steps are not small enough (e.g. for ). This happens quite frequently.
Distribution of log-returns
The python code presented above is very slow when we want to generate a big number of paths. For this reason I implemented it in Cython.
For a short introduction to Cython, have a look at the notebook A2. This is the Cython code, that makes also use of some C functions.
CYTHON implementation
The function
Heston_paths_log
contains the code presented above, but translated in Cython. The code returns only the "good" paths. It means that if there are overflows, they are ignored. Since this method returns a "random" number of paths, I will prefer to use the next method.The function
Heston_paths
implements the discretization of the original process . The CIR process is discretized using the method (4) presented in the notebook 1.2 i.e. by taking the absolute value of the variance at each time step.
I import the following cython functions:
Let us re-define the parameters of the process, together with the parameters of the CIR density.
The next cell just wants to check if there are overflows for small value of (big time steps).
OK.... now we are going to use the other function:
The distribution of the log-returns is far from being normal!! (as you can see also from the q-q plot)
This is a good property, because we can describe better the features of the empirical distributions obtained from market data!
You can play with the correlation parameter in order to obtain different skewness values.
For negative , we generate a log-return distribution with negative skewness.
For positive , we generate a log-return distribution with positive skewness.
In the pictures above we can see the difference between the Normal distribution and the Heston distribution!
I used the function Heston_pdf
, which implements the Fourier inversion of the Heston characteristic function using the Gil-Pelaez formula.
For more information on Fourier inversion see the notebook 1.3.
Characteristic function
The characteristic function (CF) for the Heston model was presented for the first time in the original paper of Heston (1993) [1].
The Heston CF is very useful for the computation of option prices. All the other methods, such as Monte Carlo or PDE, are quite slow, while Fourier inversion (as we have seen in the notebook 1.3) is super fast!
Issue with the Heston CF
The Heston CF works well for short time . However, when the time increases this CF exhibits discontinuities due to the multivalued complex functions (the complex square root and the complex logarithm need to have a specified Principal brach). A consequence is that the numerical integration becomes unstable.
In the important articles [2] and [3], different (but equivalent) forms of the Heston CF are presented. These CFs consider different Riemann surfaces and resolve the problem.
In the following I will show how the CF proposed by Schoutens (in [2]) performs better than the Heston CF.
In these notebooks, all the functions that consider the Heston CF are implemented using the CF cf_Heston_good
proposed by Schoutens.
Fourier inversion
References
[1] Heston, S. L. (1993). "A closed-form solution for options with stochastic volatility and applications to bond and currency options". Review of Financial Studies
[2] Schoutens, W., Simons, E., & Tistaert, J. (2004). "A perfect calibration! Now what?". Wilmott Magazine link
[3] Yiran Cui, Sebastian del Baño Rollin, Guido Germano, (2017), "Full and fast calibration of the Heston stochastic volatility model" European Journal of operational research. link