Sign up ×
Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. It's 100% free, no registration required.

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

share|improve this question
use rseek in Cran or look at… or… – ash Nov 19 '12 at 18:41

3 Answers 3

up vote 5 down vote accepted

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]
share|improve this answer

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

share|improve this answer

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:

See chapter 4 in the above document for details.

share|improve this answer
Ive used the SimDiffProc library to do the same but I feel the simulations are wrong. The Euler simulation process gives better results. – Mahesh Feb 23 '13 at 0:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.