σμ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 4 · Structure and insight›Topic 4.2·~50 minAdvanced

Finding market regimes and peer groups

Skill positionModule 4 of 4 · Topic 2 of 7
4.2 Finding market regimes and peer groups
—Lab score— / 100Total XP—Streak—

Groups without labels: regimes and peers

K-means splits n points into k groups by repeating two steps: assign each point to the nearest centre, then move each centre to the mean of its points. It minimises the inertia, the sum of squared distances to the centres, Σᵢ‖xᵢ-c_(g(i))‖². Inertia always falls as k grows, so it cannot choose k alone; the silhouette compares, for each point, the distance to its own group with the distance to the nearest other group, and averages between -1 and 1 (high is well separated). Two practical rules: put the columns on one scale first, because distances add up raw units (StandardScaler), and run several starts (n_init=3) with a fixed random_state, because k-means finds a local optimum and its labels are arbitrary numbers.

For regimes, describe each SPY day by its 21-day return, its annualised 21-day volatility and its drawdown from the running high, standardise, cluster with k=3, and relabel the clusters by volatility so that 0 is the calmest. On 5,384 days the groups are a calm regime on 72% of days (volatility 11.6%), a middle one on 25% (22.4%) and a crisis regime on 2.7% (61.5%). The mean next-day return barely differs (0.04%, 0.05%, 0.14%), but the spread is 0.79%, 1.49% and 3.96%: regimes tell you about risk, not direction. For peers, measure how two assets co-move with the distance 1-ρ, take each asset's row of distances as its description, and cluster those rows. With no sector information, 59 assets fall into tech and consumer, defensives, banks and industrials, energy, and bonds with gold. Check stability before you trust a grouping: the adjusted Rand index of the groups from the first and the second half of the sample is only 0.38, so the borderline assets switch sides.

  • Scale first, restart often: StandardScaler, n_init=3, a fixed random_state; labels are arbitrary numbers, so rename them by something meaningful.
  • Inertia falls with k; the silhouette compares separation to spread, and neither chooses k by itself.
  • Regimes tell risk, not direction; check the stability of peer groups with the adjusted Rand index before using them.

Lab · what's graded

  • Regime descriptors10 pts
  • Fitting regimes20 pts
  • Choosing k15 pts
  • Summarising regimes15 pts
  • Peer groups15 pts
  • Stability of the groups10 pts
  • The study15 pts
Explorer · lab-sl-22
▾ lab-sl-22
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sl-22$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sl-22 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes

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