Quant techniques · mean reversion

Mean reversion in one regression: the Ornstein–Uhlenbeck half-life

You have found two stocks whose prices move together, and the gap between them keeps snapping back. Before you trade it, you need two numbers: how far the gap usually wanders, and how fast it comes back. The Ornstein–Uhlenbeck process gives both, and you can estimate it with an ordinary least-squares regression.

1dxt=θ(μ−xt) dt+σ dWtdx_t = \theta(\mu - x_t)\,dt + \sigma\,dW_t A pull towards μ, proportional to the distance from it, plus noise. θ is the speed of mean reversion.
2xt+1−xt=α+β xt+εtx_{t+1} - x_t = \alpha + \beta\, x_t + \varepsilon_t Sampled once a day, the OU process is an AR(1). Run this regression on the data.
3θ=−ln⁡(1+β),half-life=ln⁡2θ,μ=−α/β\theta = -\ln(1 + \beta), \qquad \text{half-life} = \dfrac{\ln 2}{\theta}, \qquad \mu = -\alpha/\beta Half-life: the expected time for a gap to shrink by half. For small β, half-life ≈ −ln 2 / β.
4s.d.∞=σ2θ=s.d.(ε)1−(1+β)2\text{s.d.}_\infty = \dfrac{\sigma}{\sqrt{2\theta}} = \dfrac{\text{s.d.}(\varepsilon)}{\sqrt{1 - (1+\beta)^2}} How far the spread wanders in the long run. Entry and exit bands are set in these units.
The model

A spring with noise

Take the spread xt=log⁡At−hlog⁡Btx_t = \log A_t - h \log B_t between two related assets, with h the hedge ratio. If the pair is a good one, the spread has a level μ it keeps returning to. The simplest model for that is the Ornstein–Uhlenbeck (OU) process in step 1: when x is above μ the drift is negative, when it is below the drift is positive, and the pull is stronger the further away it is. It is a spring, kicked around by Brownian noise.

Take expectations of the SDE and the noise drops out, leaving an ordinary differential equation. The expected gap decays exponentially:

E[xt]−μ=(x0−μ) e−θtE[x_t] - \mu = (x_0 - \mu)\, e^{-\theta t}

So after t=ln⁡2/θt = \ln 2/\theta the expected gap has halved. That time is the half-life, and it is the most useful single number for a mean-reverting trade. It tells you how long you will typically hold a position, and therefore how much the trade will cost in financing and how much risk you carry while waiting.

The variance also settles down. Starting from a fixed point,

Var(xt)=σ22θ(1−e−2θt)  ⟶  σ22θ\mathrm{Var}(x_t) = \frac{\sigma^2}{2\theta}\left(1 - e^{-2\theta t}\right) \;\longrightarrow\; \frac{\sigma^2}{2\theta}

A random walk’s variance grows forever. The OU process’s variance levels off, because the spring stops it wandering.

Estimation

From SDE to regression

Solve the SDE exactly over one time step Δt (say, one day):

xt+1=μ+(xt−μ) e−θΔt+εt,εt∼N ⁣(0, σ22θ(1−e−2θΔt))x_{t+1} = \mu + (x_t - \mu)\,e^{-\theta\Delta t} + \varepsilon_t, \qquad \varepsilon_t \sim N\!\left(0,\ \tfrac{\sigma^2}{2\theta}\left(1 - e^{-2\theta\Delta t}\right)\right)

That is an AR(1) with coefficient b=e−θΔtb = e^{-\theta\Delta t}. Subtracting xtx_t from both sides gives the regression in step 2, with β=b−1\beta = b - 1 and α=μ(1−b)\alpha = \mu(1 - b). Regress daily changes on yesterday’s level, read off the slope, and convert. A slope of −0.05 means 5% of the gap closes each day, so the half-life is −ln⁡2/ln⁡0.95≈13.5-\ln 2/\ln 0.95 \approx 13.5 days.

import numpy as np

def ou_fit(x, dt=1.0):
    """Fit an OU process to a series sampled every dt. Returns theta, mu, sigma, half-life."""
    x0, dx = x[:-1], np.diff(x)
    beta, alpha = np.polyfit(x0, dx, 1)              # dx = alpha + beta * x0
    b = 1 + beta
    if not 0 < b < 1:
        return None                                  # no mean reversion in this sample
    theta = -np.log(b) / dt
    mu = -alpha / beta
    resid = dx - (alpha + beta * x0)
    sd_inf = resid.std(ddof=2) / np.sqrt(1 - b**2)   # long-run s.d. of the spread
    sigma = sd_inf * np.sqrt(2 * theta)
    return dict(theta=theta, mu=mu, sigma=sigma, half_life=np.log(2) / theta, sd_inf=sd_inf)
true half-life (days)sample length

The spread xt with ±1 and ±2 stationary s.d.

Regression: xt+1 − xt against xt

–true half-life
–median estimate over 2,000 samples
–middle 80% of estimates

Set the true half-life to 7 days with a year of data and press “Fit 2,000 samples” a few times. The median estimate lands around 6 days, not 7. Change to 3 months of data and it gets much worse. This isn’t bad luck.

The catch

The regression overstates mean reversion

The least-squares estimate of an AR(1) coefficient is biased towards zero in small samples. Kendall (1954) worked out the leading term:

E[b^]≈b−1+3bnE[\hat b] \approx b - \frac{1 + 3b}{n}

A smaller b means faster decay, so the estimated half-life comes out too short and the spread looks more tradable than it is. With a true half-life of 7 days (b = 0.905) and n = 250, the bias is about −0.015, enough to move the median estimate from 6.9 to about 6.1 days. On 20,000 simulated years, the mean of b^\hat b was 0.8895 against Kendall’s 0.8900.

The cause is the intercept. Fitting μ from the same data pulls the fitted mean towards wherever the sample happened to spend its time, so the path looks like it keeps returning to its own average. The fewer half-lives your sample covers, the stronger the effect. As a rough rule, you want the sample to cover at least 10 to 20 half-lives before trusting the estimate.

Remedies, roughly from cheapest to best:

  • Subtract Kendall’s term: b^adj=b^+(1+3b^)/n\hat b_{\text{adj}} = \hat b + (1 + 3\hat b)/n.
  • Bootstrap or simulate the bias at the fitted parameters, as the figure does, and correct for it.
  • Use more data, or pool across many similar pairs.
Is it mean-reverting at all?

Testing the slope needs a different table

The natural test is whether β is below zero. But under the null hypothesis of a random walk (β = 0), the t-statistic of β does not have a normal or t distribution. It has the Dickey–Fuller distribution, which sits well to the left. With an intercept in the regression, the 5% critical value is about −2.86, not −1.65 or −1.96. Using the normal table finds mean reversion in plain random walks far too often. This regression with a Dickey–Fuller critical value is exactly the augmented Dickey–Fuller (ADF) test, minus the extra lagged terms.

For a pair, the spread itself depends on the estimated hedge ratio h. Estimating h and then testing the residuals is the Engle–Granger cointegration test, and it needs yet another, stricter, set of critical values (about −3.34 at 5% for two series), because the regression for h already picks the most stationary-looking combination.

Turning it into a trade

A textbook rule, in units of the long-run s.d. s=σ/2θs = \sigma/\sqrt{2\theta}:

  • Go short the spread when x>μ+2sx > \mu + 2s, long when x<μ−2sx < \mu - 2s.
  • Exit when the spread crosses back through μ (or a band such as ±0.5s).
  • Use a lookback for μ and s of a few half-lives, so the bands adapt without chasing noise.
  • Expect to hold for something like one to two half-lives. If the half-life is 60 days, the trade ties up capital for a quarter.

Where people slip in interviews

  • Reading the slope as θ. The regression slope is e−θΔt−1e^{-\theta\Delta t} - 1, which is close to −θΔt only when θΔt is small.
  • Mixing time units. A half-life of 5 from daily data is 5 days. Annualise θ by multiplying by 252, not the half-life.
  • Trusting short samples. A sample covering two or three half-lives will often find strong mean reversion in a random walk.
  • Using normal critical values. Unit-root t-statistics need Dickey–Fuller tables.
  • Forgetting that the hedge ratio is estimated too. That makes the spread look more stationary than it will be out of sample.
Practice

Try these before you open the answer

Warm-upA daily regression of Δx on x gives a slope of −0.05. What is the half-life?
Show answer
half-life=−ln⁡2ln⁡(1−0.05)=0.6930.0513≈13.5 days\text{half-life} = \frac{-\ln 2}{\ln(1 - 0.05)} = \frac{0.693}{0.0513} \approx 13.5 \text{ days}

The shortcut 0.693/0.05 = 13.9 days is close.

DecayA spread with a 10-day half-life is 2 s.d. above its mean. Where do you expect it to be in 20 days?
Show answer

Two half-lives, so the expected gap is divided by 4: 0.5 s.d. above the mean.

StationaryAn OU process has θ = 0.2 per day and σ = 1 per √day. What are its half-life and long-run standard deviation?
Show answer
half-life=ln⁡20.2≈3.47 days,s.d.∞=10.4≈1.58\text{half-life} = \frac{\ln 2}{0.2} \approx 3.47 \text{ days}, \qquad \text{s.d.}_\infty = \frac{1}{\sqrt{0.4}} \approx 1.58
AR(1)An AR(1) has b = 0.9 and residual s.d. 0.5. What is the long-run s.d. of the series?
Show answer
0.51−0.81=0.50.436≈1.15\frac{0.5}{\sqrt{1 - 0.81}} = \frac{0.5}{0.436} \approx 1.15
BiasTrue b = 0.95, and you have 100 daily observations. Roughly what does the regression give on average, and what half-life does that imply?
Show answer
E[b^]≈0.95−1+2.85100=0.9115,half-life≈0.6930.0927≈7.5 daysE[\hat b] \approx 0.95 - \frac{1 + 2.85}{100} = 0.9115, \qquad \text{half-life} \approx \frac{0.693}{0.0927} \approx 7.5 \text{ days}

The true half-life is 13.5 days, so you would think the spread mean-reverts almost twice as fast as it does.

TestingYour regression slope has a t-statistic of −2.3. Is that significant evidence of mean reversion at 5%?
Show answer

No. Under the random-walk null the statistic follows the Dickey–Fuller distribution, with a 5% critical value of about −2.86 (with an intercept). −2.3 does not reject a unit root.

StretchWhy does the OU variance level off at σ²/(2θ), and where does the 2 come from?
Show answer

Apply Itô to x2x^2 with μ = 0: d(x2)=2x dx+σ2dt=(−2θx2+σ2) dt+2σx dWd(x^2) = 2x\,dx + \sigma^2 dt = (-2\theta x^2 + \sigma^2)\,dt + 2\sigma x\, dW. Taking expectations, ddtE[x2]=−2θE[x2]+σ2\tfrac{d}{dt}E[x^2] = -2\theta E[x^2] + \sigma^2, which settles where the right side is zero. The 2 appears because the variance is a square, so it decays twice as fast as the mean.

The bias numbers come from 20,000 simulated one-year samples. The figure runs its own simulations in your browser.