Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r code for using Ornstein-Uhlenbeck to estimate time for mean reversion

Tags:

r

I am looking for an example of the r code for using Ornstein-Uhlenbeck to estimate time for mean reversion when considering cointegrated securities

like image 262
Stephen Avatar asked Oct 22 '10 18:10

Stephen


3 Answers

Ref: Package ‘ouch’ (link)

Title: Ornstein-Uhlenbeck models for phylogenetic comparative hypotheses

prev_sprd <- c(sprd[2:length(sprd)], 0)
d_sprd <- sprd - prev_sprd
prev_sprd_mean <- prev_sprd - mean(prev_sprd)
sprd.zoo <- merge(d_sprd, prev_sprd_mean)
sprd_t <- as.data.frame(sprd.zoo)

with intercept:

result <- lm(d_sprd ~ prev_sprd_mean, data = sprd_t)
half_life <- -log(2)/coef(result)[2]
half_life

or if no intercept:

result = lm(d_sprd ~ prev_sprd_mean + 0,  data = sprd_t)   
half_life1 = -log(2)/coef(result)[1]
half_life1

also try:

Statistical Methods for Financial Engineering, B. Remillard

On the Simulation and Estimation of the Mean-Reverting Ornstein-Uhlenbeck Process

Why is this important?

If we enter into a mean-reverting position, and 3 or 4 half-life’s later the spread still has not reverted to zero, we have reason to believe that maybe the regime has changed, and our mean-reverting model may not be valid anymore

ref:

  1. http://epchan.blogspot.co.uk/2007/01/what-is-your-stop-loss-strategy.html

  2. http://cran.r-project.org/web/packages/peacots/peacots.pdf

like image 159
adam.888 Avatar answered Oct 14 '22 08:10

adam.888


There are several packages on CRAN that have the Ornstein-Uhlenbeck procedure. I would suggest using rseek to find them, then see which package best suits your needs.

like image 40
Joshua Ulrich Avatar answered Oct 14 '22 09:10

Joshua Ulrich


I suggestion reading through this thread on the r-sig-finance list which directly addresses your question.

like image 39
Shane Avatar answered Oct 14 '22 09:10

Shane