Take the 2-minute tour ×
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
1  
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

3 Answers 3

up vote 4 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]
  }
  return(x);
}
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:
http://cran.r-project.org/web/packages/Sim.DiffProcGUI/index.html

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

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

Your Answer

 
discard

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.