Monday, August 24, 2015

mean - R code for Ornstein-Uhlenbeck process


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



Answer



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

No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...