6.1.7Algorithmic & Quant Trading

Understand mean-reversion quant models

2,384 words11 min readdifficulty · medium

What Is Mean Reversion?

Three pillars:

  1. Stationary process: The mean and variance don't drift wildly over time (unlike a trending stock that keeps climbing).
  2. Reverting force: Economic forces (profit-taking, value buyers) pull prices back.
  3. Quantifiable deviation: We measure "how far is too far" using standard deviations (z-scores) or Bollinger Bands.

The Mathematical Foundation: Ornstein-Uhlenbeck Process

Mean reversion is modeled by the Ornstein-Uhlenbeck (OU) process, the continuous-time version of an autoregressive process.

Derivation from First Principles

Step 1: Model the price change dXtdX_t as two competing forces:

  • Drift toward mean: The term θ(μXt)\theta(\mu - X_t) says "if Xt>μX_t > \mu, drift down; if Xt<μX_t < \mu, drift up." The farther from μ\mu, the stronger the pull.
  • Random shock: σdWt\sigma dW_t is the unpredictable noise (news, liquidity shocks).

Why this step? We separate systematic reversion (predictable) from random walk (unpredictable).

Step 2: Solve for the expected value E[XtX0]E[X_t | X_0] (where will the price be on average?).

Ignoring the stochastic term for the expectation, we have: dE[Xt]dt=θ(μE[Xt])\frac{dE[X_t]}{dt} = \theta(\mu - E[X_t])

This is a first-order linear ODE. Rearrange: dE[Xt]μE[Xt]=θdt\frac{dE[X_t]}{\mu - E[X_t]} = \theta \, dt

Integrate both sides: lnμE[Xt]=θt+C-\ln|\mu - E[X_t]| = \theta t + C

At t=0t=0, E[X0]=X0E[X_0] = X_0, so C=lnμX0C = -\ln|\mu - X_0|.

Exponentiate: μE[Xt]=(μX0)eθt\mu - E[X_t] =(\mu - X_0)e^{-\theta t}

Why this step? We're solving how the expected price evolves. The exponential decay means deviations shrink over time.

Step 3: Rearrange to get the forecast:

Why this step? This formula is our trading signal. If current price deviates from μ\mu, we know the expected direction and can size our bet.


Practical Implementation: Z-Score Strategy

The OU process is continuous-time. In practice, we work with discrete returns.

Trading Rules:

  1. Entry: If zt<2z_t < -2 (price is 2 SD below mean), BUY (expect reversion up).
  2. Entry: If zt>+2z_t > +2 (price is 2 SD above mean), SELL/SHORT (expect reversion down).
  3. Exit: Close position when ztz_t crosses back to0 (price returns to mean).
  4. Stop-loss: If zt>3|z_t| > 3 (regime change? model breakdown?), exit to limit losses.

Why ±2\pm 2 SD? Assuming normal distribution, ~95% of data falls within 2 SD. Breaching this is rare → high probability of reversion. But we must confirm stationarity.

Figure — Understand mean-reversion quant models

Example1: Single-Stock Mean Reversion

Setup: Stock XYZ has been trading around μ=100\mu = 100 for months. Today, bad earnings → price drops to Pt=85P_t = 85. Historical volatility σ=5\sigma = 5.

Step 1: Compute z-score. zt=851005=155=3z_t = \frac{85 - 100}{5} = \frac{-15}{5} = -3

Why this step? We quantify how extreme the deviation is. z=3z = -3 is 3 SD below mean, highly unusual.

Step 2: Decision z<2z < -2 triggers a BUY signal. We expect reversion to μ=100\mu = 100.

Why this step? The model says extreme deviations are temporary. We're betting the panic is overdone.

Step 3: Estimate time to revert. If we estimated θ=0.3\theta = 0.3 per day (from historical fitting), half-life: t1/2=ln20.32.3 dayst_{1/2} = \frac{\ln 2}{0.3} \approx 2.3 \text{ days}

In ~2-3 days, we expect half the deviation to close. Target exit: z0z \approx 0, price around 9710397-103.

Why this step? Knowing the reversion speed helps set holding period and profit expectations.

Outcome: Three days later, stock rebounds to 9898 as earnings panic fades. z0.4z \approx -0.4. Exit for 15%\approx 15\% gain.


Example 2: Pairs Trading (Relative Mean Reversion)

Setup: Stocks A and B are in the same sector (e.g., Coca-Cola vs. Pepsi). Historically, their ==price spread St=PAPBS_t = P_A - P_B== hovers around μS=5\mu_S = 5. Today, PA=50P_A = 50, PB=38P_B = 38, so St=12S_t = 12. σS=2\sigma_S = 2.

Step 1: Compute z-score of the spread. zS=1252=3.5z_S = \frac{12 - 5}{2} = 3.5

Why this step? The spread is 3.5 SD above normal. A has outperformed B excessively.

Step 2: Trade the pair to bet on convergence.

  • Short A (overpriced relative to B).
  • Long B (underpriced relative to B).
  • This is market-neutral: if the whole sector drops, we lose on B but gain on the short A. We only care about the spread narrowing.

Why this step? By trading the ratio, we isolate mean-reversion and hedge out market risk.

Step 3: Exit when zS0z_S \approx 0 (spread returns to μS=5\mu_S = 5). Say PA=48P_A = 48, PB=43P_B = 43. Spread = 5.

Profit calculation:

  • Short A: Sold at 50, bought back at 48 → gain +2+2 per share.
  • Long B: Bought at 38, sold at 43 → gain +5+5 per share.
  • Net (assuming equal position sizes): +7+7 per unit pair.

Why this step? Pairs trading profits from relative mispricing, not absolute direction.


Testing for Mean Reversion: The ADF Test

Not all price series revert. A trending stock is non-stationary. We use the Augmented Dickey-Fuller (ADF) test.

Why this step? Applying mean-reversion to a trending stock loses money (you keep shorting an uptrend). ADF prevents this.


Common Mistakes


Active Recall Checkpoints

Recall Feynman Explain-to-a-12-Year-Old

Imagine you have a toy that always wants to stay in the middle of your room. If you push it to the corner, it rolls back to the center by itself. That's mean reversion!

Stock prices are like that toy sometimes. When bad news pushes a price way down, it's like the toy is in the corner. Our model says, "Hey, this is too far from the middle—it'll probably roll back." So we buy the stock when it's in the corner, then sell when it rolls back to the middle and make money.

But we have to check: Is the toy really programed to go back to the middle, or did someone move the middle? If the company is actually broken (like the toy's battery died), it won't come back, and we lose. That's why we test with the ADF test to make sure the "rolling back" pattern is real.


Connections

  • Statistical Arbitrage Strategies – mean reversion is the engine behind stat-arb
  • Bollinger Bands – practical visualization of mean ± 2 SD thresholds
  • Cointegration Testing – pairs trading requires cointegration, not just correlation
  • Kalman Filter – advanced: dynamically estimate time-varying μ\mu and θ\theta
  • Risk Management in Quant Trading – position sizing using z-score magnitude
  • Stationarity Tests (ADF, KPSS) – prerequisites before trusting mean-reversion signals

#flashcards/stock-market

What is the core assumption of mean-reversion models? :: Prices that deviate significantly from their historical mean are experiencing temporary overeactions and will revert back toward the mean over time.

Write the Ornstein-Uhlenbeck SDE and explain each term.
dXt=θ(μXt)dt+σdWtdX_t = \theta(\mu - X_t)dt + \sigma dW_t. θ\theta = speed of reversion, μ\mu = long-term mean, XtX_t = current price, σ\sigma = volatility, dWtdW_t = random Brownian motion.
What does a z-score measure in mean-reversion trading?
The number of standard deviations the current price is from the moving average. zt=PtPˉnσnz_t = \frac{P_t - \bar{P}_n}{\sigma_n}.
When do you BUY in a z-score mean-reversion strategy?
When zt<2z_t < -2 (price is 2+ standard deviations below the mean), expecting reversion upward.
What is the half-life of mean reversion and how is it calculated?
The time it takes for half the price deviation to decay back toward the mean. t1/2=ln2θt_{1/2} = \frac{\ln 2}{\theta}, where θ\theta is the speed parameter.
Why use the ADF test before applying mean-reversion?
To test if the price series is stationary (mean-reverting). If it has a unit root (random walk), mean-reversion strategies will fail.
What is pairs trading and how does it use mean reversion?
Trading two correlated stocks by betting their price spread will revert to its historical mean. Long the underperformer, short the outperformer, profiting from convergence while hedging market risk.

What is the key risk of mean-reversion models? :: Regime shifts—when the fundamental mean permanently changes due to company deterioration, market structure change, or crisis. The price won't revert to the old mean.

Concept Map

assumes

driven by

measures

via

modeled by

has

has

has

split into

solve ODE

shows

strategy

Mean Reversion

Stationary Process

Reverting Force

Quantifiable Deviation

Z-scores / Bollinger Bands

Ornstein-Uhlenbeck SDE

Theta = reversion speed

Mu = long-term mean

Sigma dW_t random shock

Drift toward mean

Mean-Reversion Forecast

Deviations decay exponentially

Fade the extremes: buy low sell high

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Mean reversion ka matlab hai ki jab stock price apne average se bahut zyada door chali jati hai—ya to bahut upar ya bahut neeche—to woh wapas average ki taraf ane ki koshish karti hai. Socho agar ek rubber band ko stretch karo, to woh snap karke wapas relax position mein aa jata hai. Yahi concept markets mein bhi kaam karta hai.

Quant models isko statistically measure karte hain. Hum calculate karte hain ki current price apne historical mean se kitne "standard deviations" door hai (isko z-score kehte hain). Agar z-score -2 se neeche ho (matlab price bahut gir gayi hai), to model kehta hai "buy karo, kyunki yeh wapas upar ayegi." Agar z-score +2 se upar ho (price bahut upar chali gayi), to "sell karo, neeche aayegi." Lekin sabse important cheez hai yeh confirm karna ki stock actually mean-reverting hai ya nahi—uske liye ADF test use karte hain. Agar test fail ho jaye, matlab stock trend kar raha hai aur mean reversion kaam nahi karega.

Pairs trading ek popular strategy hai jisme do similar stocks lete hain (jaise Coca-Cola aur Pepsi). Unka price difference (spread) track karte hain. Jab spread abnormally wide ho jaye, to overpriced wale ko short karo aur underpriced waale ko long karo. Jab spread normal ho jaye, exit karo. Yeh strategy market-neutral hoti hai—matlab overall market up jaye ya down, humein farak nahi padta, hum sirf relative difference se profit lete hain. Lekin dhyan rahe: agar company fundamentally bigad gayi (management change, product failure), to mean permanently shift ho sakta hai aur model fail ho jayega. Isliye quant trading mein risk management aur regime detection bahut zaroori hai.

Test yourself — Algorithmic & Quant Trading

Connections