σμ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.1·~50 minAdvanced

Clustering for regimes and peers

Skill positionModule 4 of 4 · Topic 1 of 4
4.1 Clustering for regimes and peers
—Lab score— / 100Total XP—Streak—

Groups without labels

Clustering looks for groups of similar rows without being told what the groups are. K-means is the workhorse. Pick kkk centres, then repeat two steps: assign each point to its nearest centre, and move each centre to the mean of its points. It stops when nothing changes. The quantity it lowers is the inertia, the sum of squared distances from each point to its centre. Because k-means uses distances, features must be on a common scale, and because it always finds kkk groups, it will find them even in data that has none. In finance clustering is used to label regimes (calm, stormy, crisis) and to find peers, stocks that behave alike.

inertia=∑i∥xi−ck(i)∥2,si=bi−aimax⁡(ai, bi)\mathrm{inertia}=\sum_{i}\left\|x_i-c_{k(i)}\right\|^{2},\qquad s_i=\frac{b_i-a_i}{\max(a_i,\,b_i)}inertia=i∑​​xi​−ck(i)​​2,si​=max(ai​,bi​)bi​−ai​​(1)

How good is a clustering? The silhouette of a point compares aaa, its mean distance to the other points of its own cluster, with bbb, its mean distance to the points of the nearest other cluster: s=(b−a)/max⁡(a,b)s=(b-a)/\max(a,b)s=(b−a)/max(a,b). It is near 111 for a point deep inside a tight, well separated cluster, near 000 on a border and negative if the point is probably in the wrong cluster. The mean silhouette helps to compare values of kkk, but it never proves that clusters are real. Always re-run with other starting points and other features, and ask whether the groups mean something for trading.

Six points and real stocksTake the points 1,2,3,10,11,121,2,3,10,11,121,2,3,10,11,12 with starting centres 222 and 999. The points 1,2,31,2,31,2,3 go to centre 222 and 10,11,1210,11,1210,11,12 to centre 999. The new centres are 222 and 111111. The inertia is (1+0+1)+(1+0+1)=4(1+0+1)+(1+0+1)=4(1+0+1)+(1+0+1)=4. For the point 333, a=(2+1)/2=1.5a=(2+1)/2=1.5a=(2+1)/2=1.5 and b=(7+8+9)/3=8b=(7+8+9)/3=8b=(7+8+9)/3=8, so s=(8−1.5)/8=0.8125s=(8-1.5)/8=0.8125s=(8−1.5)/8=0.8125. On the real data, 545454 stocks described by their beta to the average stock and their yearly volatility fall into 303030 and 242424 stocks for k=2k=2k=2 (silhouette 0.530.530.53), into 25,22,725,22,725,22,7 for k=3k=3k=3 (0.520.520.52) and have a silhouette of 0.460.460.46 for k=4k=4k=4. For SPY, clustering days by the 202020-day volatility and return finds 72%72\%72% of calm days (volatility 12.4%12.4\%12.4%), 25.7%25.7\%25.7% of middling days (20.3%20.3\%20.3%) and 2.3%2.3\%2.3% of crisis days (66.3%66.3\%66.3% and a 202020-day return of −12.7%-12.7\%−12.7%). A hidden Markov model gave different shares, because the features and the method differ.
  • Two steps: assign to the nearest centre, move the centre to the mean, repeat.
  • Scale first: distances treat every feature alike, so a large-unit feature would dominate.
  • Clusters are a lens, not a fact: check stability and meaning before you trade on them.

Lab · what's graded

  • A new centre10 pts
  • The inertia10 pts
  • Distance inside the cluster10 pts
  • A silhouette15 pts
  • Days that are not calm5 pts
  • Why scale the features10 pts
  • The steps of k-means10 pts
  • Match the vocabulary10 pts
  • Why clusters need checking20 pts
0/ 9 answered
Tasks
1Numeric answer10 ptsNot answered

The points 10,11,1210,11,1210,11,12 were assigned to the second centre. Where does that centre move to, the mean of its points?

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

The centres are 222 and 111111. The points 1,2,31,2,31,2,3 are at squared distances 1,0,11,0,11,0,1 from 222 and 10,11,1210,11,1210,11,12 at 1,0,11,0,11,0,1 from 111111. What is the inertia?

i=i =i=accepted: ± 0.01
3Numeric answer10 ptsNot answered

The point 333 lies in the cluster {1,2,3}\{1,2,3\}{1,2,3}. What is aaa, its mean distance to the other two points 111 and 222? Give one decimal.

a=a =a=accepted: ± 0.05
4Numeric answer15 ptsNot answered

For the point 333, a=1.5a=1.5a=1.5 and the mean distance to the other cluster {10,11,12}\{10,11,12\}{10,11,12} is b=(7+8+9)/3=8b=(7+8+9)/3=8b=(7+8+9)/3=8. What is the silhouette s=(b−a)/max⁡(a,b)s=(b-a)/\max(a,b)s=(b−a)/max(a,b)? Give three decimals.

s=s =s=accepted: ± 0.002
5Numeric answer5 ptsNot answered

Clustering SPY days into three groups, 72%72\%72% are calm. What share of days is not calm? Give two decimals as a fraction.

f=f =f=accepted: ± 0.005
6Choice10 ptsNot answered

Stocks are clustered on beta (values near 111) and market value in dollars (values near 101110^{11}1011). What happens if you do not scale?

7Order the steps10 ptsNot answered

Put the steps of k-means 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. 1Repeat until the assignments stop changing
  2. 2Choose k starting centres
  3. 3Move each centre to the mean of its points
  4. 4Assign each point to its nearest centre
8Match10 ptsNot answered

Match each term with its meaning.

Pick the partner of each item on the left.

Centroid
Inertia
Silhouette
Peer group
9Explain in words20 ptsNot answered

K-means on SPY days gives 72%72\%72%, 25.7%25.7\%25.7% and 2.3%2.3\%2.3%, while a hidden Markov model gave other shares. In one or two sentences, explain why you should not treat a clustering as the true set of regimes.

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