Skip to content
All library documents

Simulating Ornstein–Uhlenbeck Processes with Euler and Exact Methods

Article Quant Q&A · Author: Add

Summary

The document presents approaches for simulating an Ornstein–Uhlenbeck process in R. It describes Euler–Maruyama discretization, parameterized by a long-run mean, mean-reversion speed, volatility, initial value, and simulation horizon. It also points readers to R packages for stochastic differential equation simulation, including tools with graphical interfaces.

A second approach samples exact conditional distributions at each time step, avoiding the approximation in Euler simulation. The answer provides a covariance check comparing a simulated estimate with its theoretical value, illustrating one way to assess a simulation. That example is evidence for the implementation under its chosen settings, rather than a broad comparison of methods. The document does not discuss calibration from market data or trading applications, and numerical accuracy still depends on implementation and simulation sampling.

Key ideas

  • Euler–Maruyama provides a straightforward discretized simulation of an Ornstein–Uhlenbeck process.
  • The process is described using a long-run mean, a mean-reversion speed, and a volatility parameter.
  • R packages offer alternative simulation tools for diffusion processes.
  • An exact conditional-distribution method avoids Euler’s approximate transition distribution.
  • Comparing simulated and theoretical covariance provides a check on process simulation.

Tags

Full text
# R code for Ornstein-Uhlenbeck process


# R code for Ornstein-Uhlenbeck process












Can any one help me with some R code to run Ornstein-Uhlenbeck process?

## Answer by Alexey Kalmykov (score 13, accepted)

https://quant.stackexchange.com/a/4565

The code of Euler Maruyama simulation method is pretty simple (`nu` is long run mean, `lambda` is mean reversion speed):

```
ornstein_uhlenbeck <- function(T,n,nu,lambda,sigma,x0){
  dw  <- rnorm(n, 0, sqrt(T/n))
  dt  <- T/n
  x <- c(x0)
  for (i in 2:(n+1)) {
    x[i]  <-  x[i-1] + lambda*(nu-x[i-1])*dt + sigma*dw[i-1]
  }
  return(x);
}
```

## Answer by Joshua Ulrich (score 12)

https://quant.stackexchange.com/a/1261

Take a look at the sde package; specifically the `dcOU` and `dsOU` functions. You may also find some examples on the R-SIG-Finance mailing list, which would be in the results of a search on www.rseek.org.

## Answer by vonjd (score 5)

https://quant.stackexchange.com/a/4564

You can also use the Sim.DiffProc package.

Have a look at this document: Sim.DiffProc: A Package for Simulation of Diffusion Processes in R

See esp. chapter 2.1.2

There is even a Graphical User Interface (GUI) available for some functions: http://cran.r-project.org/web/packages/Sim.DiffProcGUI/index.html

See chapter 4 in the above document for details.

## Answer by St&#233;phane Laurent (score 2)

https://quant.stackexchange.com/a/37212

The Euler method is simple but it gives an approximate distribution. The method implemented below gives an exact distribution of $X_{t_i}$ and exact conditional distributions $(X_{t_j} \mid X_{t_i})$.

```
rOU <- function(npaths, T, nsteps, x0, theta1, theta2, theta3){
  dt <- T/nsteps
  r <- theta1/theta2
  s <- theta3*sqrt(-expm1(-2*theta2*dt)/2/theta2)
  e <- exp(-theta2*dt)
  out <- rbind(x0, matrix(NA_real_, nsteps, npaths))
  for(i in 2:(nsteps+1)){
    out[i,] <- rnorm(npaths, r+e*(out[i-1,]-r), s)
  }
  out
}
```

Let's check the covariance $Cov(X_{t_1}, X_{t_2})$:

```
 theta1 <- 1; theta2 <- 2; theta3 <- 3
> nsteps <- 10
> sims <- rOU(npaths=500000, T=1, nsteps=nsteps, x0=0, 
+             theta1=theta1, theta2=theta2, theta3=theta3)
> # check covariance
> t1 <- 1/2; t2 <- 1
> cov(sims[1+nsteps*t1,], sims[1+nsteps*t2,]) # estimated
[1] 0.713272
> theta3^2/2/theta2 * 
+   (exp(-theta2*(t2-t1)) - exp(-theta2*(t2+t1))) #exact
[1] 0.7157078
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.