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