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

Principal components of returns

Skill positionModule 4 of 4 · Topic 4 of 7
4.4 Principal components of returns
—Lab score— / 100Total XP—Streak—

One market, a few themes, and a long tail

Standardise each column of returns (mean 0, standard deviation 1) and call the result Z, with n days and p assets. The correlation matrix is C=ZᵀZ/n. A principal component is an eigenvector vⱼ of C: the direction in asset space along which the standardised returns vary most, then most among the directions at right angles to the first, and so on. Its eigenvalue λⱼ is the variance along that direction, the eigenvalues add up to p (the total variance of p standardised columns), and λⱼ/p is the explained variance ratio. The score Z vⱼ is the time series of that component. numpy.linalg.eigh returns eigenvalues in increasing order, so sort them down; the sign of an eigenvector is arbitrary, so fix a convention (for instance the largest component positive) before you compare with anything.

Keeping only the first k components rebuilds the data as Ẑ=Z VₖVₖᵀ. The mean squared error of the rebuild is the discarded variance divided by p: 1/pΣ_(j>k)λⱼ. On the last 2,500 trading days of 61 stocks and ETFs, the first component carries 37.4% of the variance and the next four 7.3%, 6.2%, 3.3% and 2.8%. The first score has a correlation of 0.998 with the equal-weight average return: it is the market, found without being told. The next components are sector and style themes. Yet the tail is heavy: 3 components give half of the variance, but 22 are needed for 80% and 34 for 90%, and the reconstruction error is 0.626 with one component, 0.430 with five and 0.213 with twenty. PCA compresses the common part of the market; it does not make 61 assets look like three.

  • Standardise, then eigen-decompose the correlation matrix; the eigenvalues add up to the number of assets.
  • Signs are arbitrary: fix a convention, and compare vectors by absolute cosine.
  • The first component of stock returns is the market; the tail of small components still matters.

Lab · what's graded

  • Standardising10 pts
  • The eigen-decomposition20 pts
  • Explained variance10 pts
  • Checking against scikit-learn15 pts
  • How many components10 pts
  • Reconstruction error15 pts
  • The study20 pts
Explorer · lab-sl-23
▾ lab-sl-23
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sl-23$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sl-23 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