σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
statsmodels skill›Module 2 · Models for time series›Topic 2.5·~55 minAdvanced

Cointegration and pairs

Skill positionModule 2 of 3 · Topic 5 of 5
2.5 Cointegration and pairs
—Lab score— / 100Total XP—Streak—

Two prices that stay together

Prices wander: they behave like random walks, so each is non-stationary. Two wandering series are cointegrated when some combination of them does not wander, the spread st=yt−βxts_t=y_t-\beta x_tst​=yt​−βxt​ keeps returning to a mean. That is the idea behind pairs trading. The Engle–Granger test has two steps. First regress yyy on xxx by least squares to get the hedge ratio β^\hat\betaβ^​ and the spread. Then run an augmented Dickey–Fuller test on the spread: Δst=γst−1+εt\Delta s_t=\gamma s_{t-1}+\varepsilon_tΔst​=γst−1​+εt​, and ask whether γ<0\gamma<0γ<0. The t-statistic must be below special critical values, about −3.34-3.34−3.34 at 5% for two series, which are stricter than the usual ones because the spread was estimated. In statsmodels this is coint(y, x).

st=yt−β^xt,Δst=γst−1+εt,ϕ=1+γ,t1/2=ln⁡0.5ln⁡ϕs_t=y_t-\hat\beta x_t,\qquad \Delta s_t=\gamma s_{t-1}+\varepsilon_t,\qquad \phi=1+\gamma,\qquad t_{1/2}=\frac{\ln 0.5}{\ln\phi}st​=yt​−β^​xt​,Δst​=γst−1​+εt​,ϕ=1+γ,t1/2​=lnϕln0.5​(1)
A five-day spread by handTake x=1,2,3,4,5x=1,2,3,4,5x=1,2,3,4,5 and y=3,5,6,9,10y=3,5,6,9,10y=3,5,6,9,10. Regressing gives β^=18/10=1.8\hat\beta=18/10=1.8β^​=18/10=1.8, so the spread y−1.8xy-1.8xy−1.8x is 1.2,1.4,0.6,1.8,1.01.2,1.4,0.6,1.8,1.01.2,1.4,0.6,1.8,1.0 with mean 1.21.21.2 and standard deviation 0.4470.4470.447. Today's spread 1.01.01.0 has z-score (1.0−1.2)/0.447=−0.45(1.0-1.2)/0.447=-0.45(1.0−1.2)/0.447=−0.45: it is a bit below normal. If the spread follows st=0.9 st−1+εs_t=0.9\,s_{t-1}+\varepsilonst​=0.9st−1​+ε, a gap closes by half in ln⁡0.5/ln⁡0.9=6.58\ln0.5/\ln0.9=6.58ln0.5/ln0.9=6.58 days. A test regression Δs=γst−1\Delta s=\gamma s_{t-1}Δs=γst−1​ with γ^=−0.1\hat\gamma=-0.1γ^​=−0.1 and standard error 0.040.040.04 has t=−2.5t=-2.5t=−2.5, above the critical −3.34-3.34−3.34, so you cannot call the pair cointegrated. On real daily data since 2000 (6,6966{,}6966,696 days), regressing one log price on another gives R2R^2R2 of 0.850.850.85 to 0.930.930.93 for pairs such as KO and PEP, XOM and CVX, or HD and LOW, yet the Engle–Granger statistics are −2.69-2.69−2.69, −1.80-1.80−1.80 and −3.97-3.97−3.97, so only HD and LOW pass at 5% (p=0.008p=0.008p=0.008). Even that fragile: each half of the sample alone fails (t=−1.58t=-1.58t=−1.58 and −2.63-2.63−2.63) and the hedge ratio moves from 0.130.130.13 to 0.930.930.93. The full-sample spread has ϕ=0.998\phi=0.998ϕ=0.998, a half-life of about 332332332 days, too slow to trade. For comparison, in 100010001000 simulated pairs of independent random walks, 85%85\%85% show a slope with ∣t∣>1.96|t|>1.96∣t∣>1.96.
High R-squared is not evidenceTwo wandering prices always look related when you regress one on the other. Only the stationarity of the spread, tested with the Engle–Granger critical values, says the link is real. Check stability across sub-periods before trading it.
  • Two steps: regress for the hedge ratio, then test the spread for a unit root with Engle–Granger values.
  • The half-life tells you whether a real mean reversion is fast enough to trade.

Lab · what's graded

  • The hedge ratio10 pts
  • The mean spread10 pts
  • A z-score15 pts
  • A half-life15 pts
  • A Dickey–Fuller statistic15 pts
  • Reading the test10 pts
  • Passing is not enough25 pts
0/ 7 answered
Tasks
1Numeric answer10 ptsNot answered

For x=1,2,3,4,5x=1,2,3,4,5x=1,2,3,4,5 and y=3,5,6,9,10y=3,5,6,9,10y=3,5,6,9,10 the sums are Sxx=10S_{xx}=10Sxx​=10 and Sxy=18S_{xy}=18Sxy​=18. What is the hedge ratio β^=Sxy/Sxx\hat\beta=S_{xy}/S_{xx}β^​=Sxy​/Sxx​?

b=b =b=accepted: ± 0.01
2Numeric answer10 ptsNot answered

With β^=1.8\hat\beta=1.8β^​=1.8 the spread y−1.8xy-1.8xy−1.8x is 1.2,1.4,0.6,1.8,1.01.2,1.4,0.6,1.8,1.01.2,1.4,0.6,1.8,1.0. What is its mean?

m=m =m=accepted: ± 0.01
3Numeric answer15 ptsNot answered

Today's spread is 1.01.01.0, the mean is 1.21.21.2 and the standard deviation is 0.4470.4470.447. What is the z-score (1.0−1.2)/0.447(1.0-1.2)/0.447(1.0−1.2)/0.447? Give two decimals.

z=z =z=accepted: ± 0.01
4Numeric answer15 ptsNot answered

A spread follows st=0.9 st−1+εts_t=0.9\,s_{t-1}+\varepsilon_tst​=0.9st−1​+εt​. After how many days does a gap shrink by half, ln⁡0.5/ln⁡0.9\ln0.5/\ln0.9ln0.5/ln0.9? Use ln⁡0.5=−0.6931\ln0.5=-0.6931ln0.5=−0.6931, ln⁡0.9=−0.1054\ln0.9=-0.1054ln0.9=−0.1054 and give one decimal.

hl=hl =hl=accepted: ± 0.1
5Numeric answer15 ptsNot answered

In Δst=γst−1+εt\Delta s_t=\gamma s_{t-1}+\varepsilon_tΔst​=γst−1​+εt​ the estimate is γ^=−0.1\hat\gamma=-0.1γ^​=−0.1 with standard error 0.040.040.04. What is the t-statistic? Give one decimal.

t=t =t=accepted: ± 0.05
6Choice10 ptsNot answered

The statistic is -2.5 and the 5% Engle–Granger critical value is -3.34. What do you conclude?

7Explain in words25 ptsNot answered

HD and LOW pass the Engle–Granger test on the whole sample, but each half fails and the hedge ratio moves from 0.130.130.13 to 0.930.930.93. In one or two sentences, explain why this makes the pair risky to trade.

Math modeKeyboard or LaTeX inputTask 1 of 7 · Ctrl ⏎ to check

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub