σμ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
  • 日本語
  • 中文
scikit-learn skill›Module 3 · Honest evaluation›Topic 3.4·~1 hrAdvanced

Purged and embargoed cross-validation

Skill positionModule 3 of 4 · Topic 4 of 6
3.4 Purged and embargoed cross-validation
—Lab score— / 100Total XP—Streak—

Cross-validation that respects the clock

Cross-validation reuses the data: it cuts it into folds, tests on one fold and trains on the rest. For independent rows that works, but financial labels overlap in time. If the label of row jjj is the return from jjj to j+Hj+Hj+H, its window is [j,j+H][j,j+H][j,j+H]. A test fold that covers rows aaa to b−1b-1b−1 has labels covering [a,b−1+H][a,b-1+H][a,b−1+H]. Any training row whose window touches that range, which means a−H≤j≤b−1+Ha-H\le j\le b-1+Ha−H≤j≤b−1+H, shares returns with the test labels. Keeping such rows lets the model see part of the answer.

[j, j+H]∩[a, b−1+H]≠∅  ⟺  a−H≤j≤b−1+H⟹a−H≤j≤b−1+H+e[j,\,j+H]\cap[a,\,b-1+H]\neq\varnothing\iff a-H\le j\le b-1+H\qquad\Longrightarrow\qquad a-H\le j\le b-1+H+e[j,j+H]∩[a,b−1+H]=∅⟺a−H≤j≤b−1+H⟹a−H≤j≤b−1+H+e(1)

Purging removes the overlapping training rows. An embargo removes a further stretch of eee rows after the test fold, because returns are autocorrelated and volatility clusters, so the first rows after a test block still carry information about it. In a walk-forward scheme training only comes before the test, and the same idea becomes a gap: train on rows up to a−H−1a-H-1a−H−1, so the gap is HHH (plus eee). scikit-learn's TimeSeriesSplit has a gap argument for this. On the monthly stock panel with H=3H=3H=3, the walk-forward information coefficient is 0.03260.03260.0326 with no embargo and 0.03270.03270.0327 with 333 months of embargo: the cleaning changes little because the label overlap is short, but you cannot know that without doing it.

Counting the rowsTake n=100n=100n=100 rows, a label horizon H=5H=5H=5, a test fold [40,60)[40,60)[40,60) and an embargo e=2e=2e=2. Before the fold, the rows 353535 to 393939 overlap its labels: 555 rows. After it, the rows 606060 to 666666 (up to b−1+H+e=66b-1+H+e=66b−1+H+e=66) are removed: 777 rows. The fold keeps 100−20−12=68100-20-12=68100−20−12=68 training rows. In a walk-forward version with the same test fold, the training rows stop at 343434, so there are 353535 of them, which is what scikit-learn's TimeSeriesSplit with a gap of 555 returns.
  • Purge, then embargo: drop training rows whose labels touch the test labels, then a few more after the test.
  • A gap in walk-forward: train only on rows at least HHH before the test.
  • Do it even if it changes little: a clean evaluation costs a few rows, a leak can cost a strategy.

Lab · what's graded

  • Rows purged before the fold10 pts
  • Rows purged after the fold15 pts
  • The training size that remains15 pts
  • A walk-forward fold with a gap10 pts
  • Which rows are purged10 pts
  • A purged split10 pts
  • Match the vocabulary15 pts
  • Why an embargo as well15 pts
0/ 8 answered
Tasks
1Numeric answer10 ptsNot answered

The label of row jjj covers [j,j+H][j,j+H][j,j+H] with H=5H=5H=5, and the test fold is rows 404040 to 595959. Training rows jjj with j≥a−H=35j\ge a-H=35j≥a−H=35 before the fold overlap its labels. How many rows from 353535 to 393939 are purged?

k=k =k=accepted: ± 0.01
2Numeric answer15 ptsNot answered

After the fold (b=60b=60b=60), rows up to b−1+H+eb-1+H+eb−1+H+e are removed with H=5H=5H=5 and an embargo e=2e=2e=2. How many rows from 606060 to b−1+H+eb-1+H+eb−1+H+e are removed?

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

There are 100100100 rows, a test fold of 202020 rows, 555 rows purged before it and 777 rows after it. How many training rows remain?

n=n =n=accepted: ± 0.01
4Numeric answer10 ptsNot answered

A walk-forward fold tests rows 404040 to 595959 and trains only on rows up to a−H−1a-H-1a−H−1, with H=5H=5H=5 (a gap of 555). How many training rows does it have, counting from row 000?

n=n =n=accepted: ± 0.01
5Choice10 ptsNot answered

In purged cross-validation, which training rows are dropped?

6Order the steps10 ptsNot answered

Put the steps of building one purged fold in order.

Drag to reorder, or focus an item, press Space to lift it, the arrow keys to move it and Space to drop it.

  1. 1Remove the training rows whose windows touch that range, plus the embargo
  2. 2Choose the test rows a to b - 1
  3. 3Fit on the remaining training rows and score the test rows
  4. 4Work out the range of label windows the test rows cover
7Match15 ptsNot answered

Match each term with its meaning.

Pick the partner of each item on the left.

Label horizon H
Purging
Embargo
Gap in TimeSeriesSplit
8Explain in words15 ptsNot answered

Purging already drops training rows whose labels overlap the test labels. In one or two sentences, explain why an embargo after the test fold is added on top.

Math modeKeyboard or LaTeX inputTask 1 of 8 · 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