σμ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.4·~40 minIntermediate

Spatial distances and clustering

Skill positionModule 3 of 3 · Topic 4 of 5
3.4 Spatial distances and clustering
—Lab score— / 100Total XP—Streak—

Grouping assets by how they move together

A distance says how far apart two things are. For assets, close should mean that they move together, so we turn a correlation ρ\rhoρ into a distance: d=12(1−ρ)d=\sqrt{\tfrac12(1-\rho)}d=21​(1−ρ)​. Perfect correlation ρ=1\rho=1ρ=1 gives distance 000, no correlation gives 0.710.710.71, and perfect opposite movement ρ=−1\rho=-1ρ=−1 gives 111. Unlike 1−ρ1-\rho1−ρ, this formula behaves like a real distance, so it can be fed to clustering tools. scipy.spatial.distance.pdist computes all pairwise distances at once, and squareform converts between a full table and the compact list that the clustering functions expect. For finding the nearest neighbours of a point among many, scipy.spatial.cKDTree is much faster than comparing with every point.

Hierarchical clustering starts with every asset alone and repeatedly merges the two closest groups. How to measure the distance between two groups is the linkage rule. single uses the closest pair across the groups, complete uses the farthest pair and average uses the mean of all cross distances. hierarchy.linkage(distances, method) returns the whole merge history, with the height of each merge. hierarchy.fcluster(Z, t, 'distance') then cuts the tree at height ttt to give group labels, or with 'maxclust' into a chosen number of groups. In finance the groups become a map of diversification: assets in one cluster carry mostly the same risk, so holding five of them is not five bets.

dij=12(1−ρij),daverage(A,B)=1∣A∣∣B∣∑i∈A∑j∈Bdij,dsingle=min⁡dij,dcomplete=max⁡dijd_{ij}=\sqrt{\tfrac12(1-\rho_{ij})},\qquad d_{\text{average}}(A,B)=\frac{1}{|A||B|}\sum_{i\in A}\sum_{j\in B}d_{ij},\qquad d_{\text{single}}=\min d_{ij},\quad d_{\text{complete}}=\max d_{ij}dij​=21​(1−ρij​)​,daverage​(A,B)=∣A∣∣B∣1​i∈A∑​j∈B∑​dij​,dsingle​=mindij​,dcomplete​=maxdij​(1)
Three assets by handA correlation of 0.50.50.5 gives distance 0.25=0.5\sqrt{0.25}=0.50.25​=0.5, and 0.80.80.8 gives 0.1=0.316\sqrt{0.1}=0.3160.1​=0.316. Take three assets with distances AB=0.3AB=0.3AB=0.3, AC=0.8AC=0.8AC=0.8 and BC=0.7BC=0.7BC=0.7. The closest pair is AAA and BBB, which merge at height 0.30.30.3. The distance from the new group to CCC is min⁡(0.8,0.7)=0.7\min(0.8,0.7)=0.7min(0.8,0.7)=0.7 for single linkage, max⁡=0.8\max=0.8max=0.8 for complete linkage and (0.8+0.7)/2=0.75(0.8+0.7)/2=0.75(0.8+0.7)/2=0.75 for average linkage, so the second merge happens at 0.750.750.75 with average linkage. Cutting the tree at height 0.50.50.5 leaves two groups, {A,B}\{A,B\}{A,B} and {C}\{C\}{C}. On real data, 555555 large US stocks with at least 90%90\%90% of the history give JPMJPMJPM and BACBACBAC a correlation of 0.790.790.79, which is a distance of 0.3270.3270.327. Average-linkage clustering cut into 666 groups gives one block of 373737 stocks, a block of 888 that is exactly the health-care names (JNJ, PFE, MRK, ABT, AMGN, LLY, BMY, MDT), a block of 777 of defensive names (PG, KO, PEP, T, VZ, DUK, SO) and three loners.
  • Make correlation a distance: d=(1−ρ)/2d=\sqrt{(1-\rho)/2}d=(1−ρ)/2​ is 000 for twins and 111 for opposites.
  • Linkage is a choice: single, complete and average linkage can give different merge heights for the same data.
  • Clusters map diversification: pick across clusters, because assets within one cluster share most of their risk.

Lab · what's graded

  • From correlation to distance10 pts
  • From distance to correlation10 pts
  • An average-linkage height15 pts
  • A single-linkage height10 pts
  • Groups after a cut10 pts
  • What each routine does15 pts
  • Clusters and diversification30 pts
0/ 7 answered
Tasks
1Numeric answer10 ptsNot answered

Two assets have correlation ρ=0.8\rho=0.8ρ=0.8. What is d=12(1−ρ)d=\sqrt{\tfrac12(1-\rho)}d=21​(1−ρ)​? Give three decimals.

d=d =d=accepted: ± 0.002
2Numeric answer10 ptsNot answered

Two assets are at distance d=0.5d=0.5d=0.5. Using ρ=1−2d2\rho=1-2d^{2}ρ=1−2d2, what is their correlation?

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

AAA and BBB have just merged. Their distances to CCC are 0.80.80.8 and 0.70.70.7. At what height does CCC join them with average linkage?

h=h =h=accepted: ± 0.005
4Numeric answer10 ptsNot answered

With the same distances, at what height does CCC join {A,B}\{A,B\}{A,B} with single linkage?

h=h =h=accepted: ± 0.005
5Numeric answer10 ptsNot answered

The merges happen at heights 0.30.30.3 and 0.750.750.75. How many groups remain if you cut the tree at height 0.50.50.5?

g=g =g=accepted: ± 0.01
6Match15 ptsNot answered

Match each routine with what it does.

Pick the partner of each item on the left.

pdist
squareform
hierarchy.linkage
hierarchy.fcluster
cKDTree
7Explain in words30 ptsNot answered

Two portfolios each hold five stocks. One takes all five from the same cluster, the other takes one from each of five clusters. In one or two sentences, explain which is more diversified and why.

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