σμ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
  • 日本語
  • 中文
SciPy skill›Module 3 · Matrices, signals and structure›Topic 3.8·~45 minIntermediate

Clustering assets by correlation

Skill positionModule 3 of 3 · Topic 8 of 10
3.8 Clustering assets by correlation
—Lab score— / 100Total XP—Streak—

Letting the correlations draw the groups

Hierarchical clustering starts from a distance between every pair of assets. A correlation is not a distance, but dᵢⱼ=√(1/2(1-ρᵢⱼ)) is: it is 0 for ρ=1, about 0.71 for ρ=0 and 1 for ρ=-1, and it obeys the triangle inequality. squareform(d, checks=False) turns the square matrix into the condensed list of n(n-1)/2 distances that scipy.cluster.hierarchy.linkage(y, method='average') expects. The result Z is a tree written as n-1 rows [left, right, height, size]: at each step the two closest groups are merged, and the distance between two groups is the average of the distances between their members.

Two functions read the tree. hierarchy.leaves_list(Z) gives the order of the leaves in which similar assets are neighbours, which reorders a correlation matrix into visible blocks. hierarchy.fcluster(Z, k, criterion='maxclust') cuts the tree into at most k groups and returns a label for each asset. On twelve stocks and ETFs over 1,500 days, with no sector information at all, five groups come out: tech (AAPL, MSFT, NVDA), banks (JPM, BAC, GS), energy (XOM, CVX), health (JNJ, PFE) and bonds (TLT, IEF). hierarchy.cophenet(Z, y) measures how faithful the tree is: it correlates the original distances with the distances the tree implies, and here it gives 0.96. A value near 1 means the tree is a good summary; a low one means the structure is not really a hierarchy.

  • Correlation is not a distance: use √(1/2(1-ρ)), then squareform(..., checks=False).
  • leaves_list orders, fcluster cuts: one gives neighbours, the other gives groups.
  • Cophenetic correlation near 1 means the tree keeps the distances; check it before trusting the groups.

Lab · what's graded

  • Correlation distance15 pts
  • The average-linkage tree15 pts
  • The leaf order15 pts
  • Cutting the tree20 pts
  • The cophenetic correlation15 pts
  • The twelve-asset study20 pts
Explorer · lab-sp-22
▾ lab-sp-22
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sp-22$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sp-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