Path: blob/master/6.1 Ornstein-Uhlenbeck process and applications.ipynb
1675 views
Ornstein-Uhlenbeck process and applications
Contents
The Ornstein-Uhlenbeck process is described by the following SDE:
The parameters are:
: mean reversion coefficient
: long term mean
: volatility coefficient
This process is Gaussian, Markovian and (unconditionally) stationary.
The previous SDE can be solved! Let's do it!
First of all we can define the new process such that the differential is equal . The SDE becomes:
Let us introduce the function: . This function has derivatives:
We can use the Itô formula:
For convenience, let us replace the time variable with , and then let us integrate both sides on :
At this point we can return to the original variable and obtain the final solution:
Moments:
The mean of is:
where the Itô integral is a martingale, .
The covariance is:
where in the first line I remove the non-random variables, in the third line I use the independence property (expectiation of the product is equal to the product of expectation) and in the fourth line I use the Itô isometry.
The variance is:
As we obtain the asymptotic mean: and the asymptotic variance: .
Numerical simulation
We can discretize the SDE using the Euler-Maruyama numerical method (see Notebook 1.2). This discretization is commented in the code below.
Another possibility is to generate the dynamics from the solution of the SDE. Let us consider the solution of the OU SDE obtained above. We can compute and consider the initial value at time .
with .
Mean and standard deviation
Since we have the possibility to generate several paths, let us consider the values at time T. We compute theoretical mean and standard deviation and compare them with the values obtained from the data:
Plot
Covariance
Let us also compare the theoretical covariance with the covariance obtained from the data.
The estimate of the parameters doesn't look very accurate.
Maximum Likelihood Estimation
We know that
Let us define .
At this point it is possible to write the log-likelihood function and impose the condition that first order derivatives are equal to zero. All these steps are done in [1]. Here I just recall the final formulas:
The parameters are:
With the MLE we obtain parameters quite similar to those obtained by OLS.
Expected time to reach the long term mean
Computation of the first hitting time density of an Ornstein-Uhlenbeck process is a long standing problem, which still remains open. As explained in the introduction of the paper [5], the first hitting time of an OU process has many applications in applied mathematics, especially in mathematical finance and for the design of trading strategies.
Let us define the first hitting time for the OU process:
Here I follow the presentation of the article [4], where the author considers the standardized problem with , and .
This is a common choice, and many articles cited in [4] follow this approach (see [6]). In [5] the authors use , (which in my opinion looks better).
The standardized form can be obtained with the following change of variables:
(the bar is then removed for brevity).
Let denote the density function of . For , the density has a nice expression:
Thanks to the change of variable, the expected time it takes for a process starting at to reach , is equal to the (scaled) expected time it takes for a process to reach starting at (set implies ).
Let us estimate the expected time numerically
Now we can define the density and the new starting point C.
When comparing with the histogram, we need to take into account of the time scale: . Therefore the density becomes: .
We can now compute the theoretical mean and standard deviation:
Bond Pricing by Vasicek model
The Vasicek model describes the evolution of the interest rate process assuming it follows an OU process under the risk neutral dynamics (I indicate with the risk neutral measure).
We write:
The price of a zero-coupon bond has the following formula:
Thanks to the Feynman-Kac formula, we know that the previous expression is a solution of the following PDE:
with terminal conditions:
Following [2] (page 59), we can find the closed formula for the bond price under the Vasicek model:
with
Closed formula:
Monte Carlo
PDE
As done in the notebook 2.2, we can discretize the PDE using the Upwind scheme:
For convenience, let's call and . We can rewrite the equation above as:
and, given , we solve for . I also introduce the artificial lateral boundary conditions:
although the resulting values are too extreme. (a high-resolution grid mitigates this problem).
See also notebook 2.1 for the description of the application of the finite difference implicit scheme.
Plot
Tracking the OU process
In this section I consider a state space model with a state process following an OU process and an observation (or measurement) process .
In order to estimate the true state process, I will make use of the Kalman filter. I have alread presented the topic in the notebook 5.1 and showed its applications in the notebooks 5.2 and 5.3.
In order to create an observation process, let's add some noise to the true state process we have simulated at the beginning of the book.
Plot true process vs measurement process
Gaussian linear state space model:
State equation:
Measurment equation:
Kalman filter:
Let us identify the Kalman filter elements. You can find them in the notebook 5.1 or in the wiki page. In order to get , we need to rewrite the state equation as
such that and then ignore the first line. The matrix .
Predict step:
Auxiliary variables:
Update step:
Log-likelihood:
The log-likelihood function is:
Train and test split
MLE estimation
The estimated parameters are very good! Below we can see the error of the estimation:
Kalman smoother
Let's implement the RTS smoother.
In addition to the smooth state and variance process, here I also estimate the smooth covariance (called Cs_smooth
in the code).
The formulas to obtain are long and can be found in [3] or in the Appendix of [7]. Here I just write the python code:
Plot
Let's have a look at the errors:
Looking at the mean linear error value, we can see that the average distance of the estimated process from the true hidden process is smaller that the average standard deviation.
Iterative approach for parameters estimation
This method is described by Shumway and Stoffer (1982) in [7] and is an off-line calculation that makes use of smoother estimators for the Kalman Filter. The Shumway and Stoffer algorithm has been widely tested (for instance in [3]).
Let's recall the formulas for this iterative algorithm (taken from [3]). Let's start with an initial guess and then compute the new parameters:
Very long formulas... where I introduced:
The iteration runs until the improvements (measured by relative errors) are smaller than a specified error.
Comparison of the estimated values with the values obtained from the true state process:
We can see that this algorithm works very well.
It is a good alternative to the numerical maximization of the loglikelihood function used in the previous section.
Trading strategy
Let us assume that our OU process describes the dynamics of an asset, or a portfolio of assets. We can implement a trading strategy that takes advantage from the mean reversion. This strategy is better known as pair trading (or, more in general, statistical arbitrage).
The mechanics of the strategy (taken from [8]) is the following:
set two bands above and below the asymptotic mean (usually at one asymptotic standard deviation) for opening the position.
buy if the lower band is crossed
sell if the upper band is crossed
set two (smaller) bands to close the position (usually at 1/10 of the standard deviation).
Now let us simulate again the process:
Plot:
The histogram is computed considering the of each generated path. The obtained distribution is a multimodal distribution where each peak is roughly a multiple of the band size.
From the Cumulative amount of cash plot, we can see that we make money when we open the position. When the position is closed, a small amount of money is lost. At terminal time the position must be closed.
The sharpe ratio is defined as . There are alternative definitions involving net returns and log-returns, however here it makes more sense to consider the (when the initial value is 0, returns are not defined).
First time to exit the strip
Let us assume the process at time is contained in the strip formed by the two asymptotic standard deviations i.e.
What is the expected time to exit this strip?
We can estimate the time using the Monte Carlo simulated data, by simply taking the average of the first times the process touches the above or below lines. (since argmax return zero if it can find a True value, check if there are zeros and if there are, replace them with a high value)
ODE approach
The expected time is the solution of the following differential equation:
with boundary conditions:
Here I called the levels of the asymptiotic standard deviation. This formula comes from [9], Chapter 10.9.
Discretization
This is a boudary value problem, that can be solved by a finite difference method. The discretization scheme is very similatr to the scheme I used for the Vasicek PDE:
Again, let's call and . We can rewrite the equation above as:
That in matrix form becomes
where is the usual tridiagonal matrix.
Plot expected time
Distribution of the first exit time from the strip
If we call the first exit time, let us indicate with the cumulative distribution function of representing the probability of to exit the strip before . Since we are interested in the forward evolution, let us define (with abuse of notation) . The distribution is solution of the following (forward) PDE (see [9] Chapter 10.8):
with boundary conditions:
where again are the upper and lower barriers.
The equation is almost identical to the PDE for bond pricing we found in Sectiton Bond, therefore the discretization is the same. The only difference is the time that goes forward.
Plot
References
[1] Thijs van den Berg, Calibrating the Ornstein-Uhlenbeck(Vasicek) model
[2] Brigo and Mercurio, Interest Rate Models - Theory and Practice: With Smile, Inflation and Credit
[3] Robert J. Elliott , John Van Der Hoek & William P. Malcolm, Pairs trading, Quantitative Finance, (2007)
[4] Finch, S., Ornstein–Uhlenbeck process, (2004).
[5] Alexander Lipton, Vadim Kaushansky, On the First Hitting Time Density of an Ornstein-Uhlenbeck Process, (2018)
[6] L. M. Ricciardi and S. Sato, First-passage-time density and moments of the Ornstein-Uhlenbeck process, J. Appl. Probab. 25 (1988)
[7] Shumway, R.H. and Stoffer, D.S., An approach to time series smoothing and forecasting using the EM algorithm, Journal of Time Series, (1982)
[8] Cartea, A., Jaimungal, S., and Peñalva, J., Algorithmic and high frequency trading. Cambridge University Press, (2015)
[9] Paul Wilmott, Paul Wilmott on quantitative finance, (2006)