Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 pcweicfa.blogspot.co.uk/2010/08/… or nunn.rc.fas.harvard.edu/groups/pica/wiki/70613/… – ash Nov 19 '12 at 18:41
up vote 7 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 www.rseek.org.

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.