σμ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
  • 日本語
  • 中文
Matrix Decompositions skill›Module 3 · Covariance and PCA›Topic 3.2·~1 hrIntermediate

PCA from scratch

Skill positionModule 3 of 5 · Topic 2 of 3
3.2 PCA from scratch
—Lab score— / 100Total XP—Streak—

Finding the directions of greatest variance

Principal component analysis asks for the single direction in which a cloud of data varies the most, then the next direction at right angles to it, and so on. For returns of NNN assets that cloud lives in NNN dimensions and is hard to look at, but its principal directions rank the independent sources of risk. The recipe is short: centre every column, form the covariance matrix, take its eigenvectors, sort them by eigenvalue and project the data onto the leading ones.

Σ=1T−1XcTXc=QΛQT,Z=XcQ,Var⁡(zj)=λj,sj=λj∑iλi\Sigma=\frac{1}{T-1}X_c^{T}X_c=Q\Lambda Q^{T},\qquad Z=X_cQ,\qquad \operatorname{Var}(z_j)=\lambda_j,\qquad s_j=\frac{\lambda_j}{\sum_i\lambda_i}Σ=T−11​XcT​Xc​=QΛQT,Z=Xc​Q,Var(zj​)=λj​,sj​=∑i​λi​λj​​(1)

Here XcX_cXc​ is the T×NT\times NT×N matrix of centred returns, the columns of QQQ are the principal directions (loadings) and the columns of ZZZ are the scores, the data in the new coordinates. The scores are uncorrelated and the variance of the jjj-th is λj\lambda_jλj​, so the eigenvalues split the total variance tr⁡Σ\operatorname{tr}\SigmatrΣ exactly. The signs of the directions are arbitrary: q\mathbf qq and −q-\mathbf q−q are the same line. Two cautions apply. Centring matters, because without it the first direction partly points at the mean. Units matter too: PCA of a covariance matrix is dominated by the most volatile variables, so when variables are on different scales you standardise them first, which means working with the correlation matrix.

Two assets by handTwo assets have covariance matrix Σ=(4223)\Sigma=\begin{pmatrix}4&2\\2&3\end{pmatrix}Σ=(42​23​). The trace is 7 and the determinant is 8, so λ2−7λ+8=0\lambda^2-7\lambda+8=0λ2−7λ+8=0 and λ=(7±17)/2\lambda=(7\pm\sqrt{17})/2λ=(7±17​)/2, that is 5.5625.5625.562 and 1.4381.4381.438. The first component therefore explains 5.562/7=79.5%5.562/7=79.5\%5.562/7=79.5% of the total variance. Its direction solves (4−5.562)q1+2q2=0(4-5.562)q_1+2q_2=0(4−5.562)q1​+2q2​=0, giving q1≈(0.788,0.615)\mathbf q_1\approx(0.788,0.615)q1​≈(0.788,0.615) up to sign. A centred observation (1,1)(1,1)(1,1) has a first score of 0.788+0.615=1.4030.788+0.615=1.4030.788+0.615=1.403 in absolute value. Both loadings have the same sign, so this component is a weighted average of the two assets.
  • Recipe: centre, covariance, eigendecompose, sort by eigenvalue, project onto the top directions.
  • Scores: uncorrelated, with variance λj\lambda_jλj​; the explained share is λj/∑λi\lambda_j/\sum\lambda_iλj​/∑λi​.
  • Cautions: signs are arbitrary; centre the data; standardise when units differ (correlation matrix).
What real stock returns sayFor the daily returns of twelve large US stocks since 2010 (4,184 days), the first principal component of the correlation matrix explains about 40% of the variance and every stock loads on it with the same sign: it is the market. The second explains about 12% and splits the stocks into groups, technology against defensive names. The remaining ten share the other 48% and are much harder to tell apart from noise, which is the subject of the next lessons.

Lab · what's graded

  • The PCA recipe15 pts
  • The first eigenvalue15 pts
  • The explained share15 pts
  • A score15 pts
  • When to standardise15 pts
  • Facts about scores10 pts
  • Why centre the data15 pts
0/ 7 answered
Tasks
1Order the steps15 ptsNot answered

Put the steps of PCA on a returns matrix 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. 1Project the centred data onto the top eigenvectors to get scores
  2. 2Form the covariance matrix
  3. 3Sort the eigenpairs by decreasing eigenvalue
  4. 4Subtract each column's mean
  5. 5Compute the eigenvectors and eigenvalues of that matrix
2Numeric answer15 ptsNot answered

Find the larger eigenvalue of Σ=(4223)\Sigma=\begin{pmatrix}4&2\\2&3\end{pmatrix}Σ=(42​23​) (trace 7, determinant 8). Give it to three decimals.

lambda1=lambda_1 =lambda1​=accepted: ± 0.005
3Numeric answer15 ptsNot answered

What percentage of the total variance does the first component explain? Give it to one decimal.

share=share =share=accepted: ± 0.1
4Numeric answer15 ptsNot answered

The first direction is q1=(0.788,0.615)\mathbf q_1=(0.788,0.615)q1​=(0.788,0.615). What is the absolute value of the first score of the centred observation (1,1)(1,1)(1,1)? Give it to three decimals.

score=score =score=accepted: ± 0.003
5Choice15 ptsNot answered

A PCA of the covariance matrix of a stock with 30% volatility and ten bond funds with 3% volatility is run. What happens, and what is the fix?

6Choice10 ptsNot answered

Select every true statement about PCA scores and loadings.

7Explain in words15 ptsNot answered

In one or two sentences, say why the data must be centred before computing principal components.

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