σμ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.2·~45 minAdvanced

Dimensionality reduction with PCA and kernel PCA

Skill positionModule 4 of 4 · Topic 2 of 4
4.2 Dimensionality reduction with PCA and kernel PCA
—Lab score— / 100Total XP—Streak—

Fewer numbers that keep the story

Stocks move together, so 54 monthly return series carry much less than 54 independent pieces of information. Principal component analysis, PCA, finds new axes, the principal components, that are ordered by how much of the variance of the data they carry. The first axis points along the direction of greatest spread, the second is perpendicular to it and carries the next most, and so on. In terms of the covariance matrix, the axes are its eigenvectors and the variances along them are its eigenvalues. The share of variance of component kkk is λk/∑jλj\lambda_k/\sum_j\lambda_jλk​/∑j​λj​. Keeping the first few components gives a short description of the data with a small loss.

Σvk=λkvk,rk=λk∑jλj,k(x,y)=e−γ∥x−y∥2\Sigma v_k=\lambda_k v_k,\qquad r_k=\frac{\lambda_k}{\sum_j\lambda_j},\qquad k(x,y)=e^{-\gamma\|x-y\|^{2}}Σvk​=λk​vk​,rk​=∑j​λj​λk​​,k(x,y)=e−γ∥x−y∥2(1)

PCA works on centred data, and on features with comparable scales, so standardise first. Its axes are linear, which suits the market because most stocks share one common move. Kernel PCA does PCA in a space built by a kernel such as the RBF kernel e−γd2e^{-\gamma d^{2}}e−γd2, which scores how close two points are, so it can follow curved structures. Two warnings apply. A component is a mix of all the inputs, so naming it is guesswork. And the leading components are the biggest sources of variance, not the most predictive ones. In finance the first component is the market, the next few are sectors, and a model fitted on components should still be checked on later data.

A 2 by 2 example and 54 stocksTake the covariance matrix (2112)\begin{pmatrix}2&1\\1&2\end{pmatrix}(21​12​). Its eigenvalues are 333 and 111, so the first component carries 3/(3+1)=75%3/(3+1)=75\%3/(3+1)=75% of the variance and the second 25%25\%25%. The first axis is (1,1)/2(1,1)/\sqrt2(1,1)/2​. The point (1,1)(1,1)(1,1) projects on it at (1+1)/2=1.414(1+1)/\sqrt2=1.414(1+1)/2​=1.414, while the point (1,−1)(1,-1)(1,−1) projects at 000 and is lost by a one-component model, with a squared error of 222. For the RBF kernel with γ=0.5\gamma=0.5γ=0.5 and squared distance 222, the similarity is e−1=0.368e^{-1}=0.368e−1=0.368. On the 319319319 monthly returns of the 545454 real stocks, the first component carries 29.5%29.5\%29.5% of the variance, the second 8.4%8.4\%8.4% and the third 5.5%5.5\%5.5%. It takes 222222 components to reach 80%80\%80% and 323232 to reach 90%90\%90%, so a short description saves 323232 of the 545454 numbers at the 80%80\%80% level.
  • Axes by variance: the first component is the direction of greatest spread, each next one is perpendicular.
  • Standardise first: PCA follows variance, so a feature with big numbers would dominate.
  • Variance is not prediction: the biggest components are not necessarily the useful ones.

Lab · what's graded

  • The share of the first component15 pts
  • A projection10 pts
  • A reconstruction error10 pts
  • An RBF kernel value10 pts
  • Numbers saved5 pts
  • Why standardise before PCA10 pts
  • The steps of PCA10 pts
  • Match the vocabulary15 pts
  • A big component is not a useful one15 pts
0/ 9 answered
Tasks
1Numeric answer15 ptsNot answered

A covariance matrix has eigenvalues 333 and 111. What share of the variance does the first component carry? Give two decimals as a fraction.

r=r =r=accepted: ± 0.005
2Numeric answer10 ptsNot answered

The first axis is (1,1)/2(1,1)/\sqrt2(1,1)/2​. What is the coordinate of the point (1,1)(1,1)(1,1) on it, (1+1)/2(1+1)/\sqrt2(1+1)/2​? Give three decimals.

p=p =p=accepted: ± 0.003
3Numeric answer10 ptsNot answered

The point (1,−1)(1,-1)(1,−1) projects to 000 on the first axis, so a one-component model rebuilds it as (0,0)(0,0)(0,0). What is the squared reconstruction error?

e=e =e=accepted: ± 0.01
4Numeric answer10 ptsNot answered

The RBF kernel is e−γd2e^{-\gamma d^{2}}e−γd2. With γ=0.5\gamma=0.5γ=0.5 and a squared distance d2=2d^{2}=2d2=2, what is the similarity? Give three decimals.

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

For the 545454 real stocks, 222222 components carry 80%80\%80% of the variance. How many of the 545454 numbers does keeping only those 222222 save?

s=s =s=accepted: ± 0.01
6Choice10 ptsNot answered

A feature in dollars of volume (millions) and a feature in monthly return (percent) are fed to PCA without scaling. What happens?

7Order the steps10 ptsNot answered

Put the steps of PCA 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 data on the first few axes
  2. 2Centre and standardise the features
  3. 3Sort the axes by eigenvalue and keep the largest
  4. 4Find the eigenvectors and eigenvalues of the covariance matrix
8Match15 ptsNot answered

Match each term with its meaning.

Pick the partner of each item on the left.

Principal component
Eigenvalue
Kernel
Loading
9Explain in words15 ptsNot answered

The first component of the 545454 stocks is the market and carries 29.5%29.5\%29.5% of the variance. In one or two sentences, explain why it does not follow that it is the most useful component for predicting returns.

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