# R code for Ornstein-Uhlenbeck process

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

-
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

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);
}

-

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.

-

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.

-
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