σμ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.1·~45 minIntermediate

Linear algebra essentials in scipy.linalg

Skill positionModule 3 of 3 · Topic 1 of 5
3.1 Linear algebra essentials in scipy.linalg
—Lab score— / 100Total XP—Streak—

Matrices as the machinery of portfolios

A portfolio problem is a matrix problem. The covariance matrix Σ\SigmaΣ holds the variances and co-movements of the assets, and almost every task asks you to do something with it. scipy.linalg.solve(A, b) solves Ax=bA\mathbf x=\mathbf bAx=b for the unknown x\mathbf xx. You should not compute the inverse inv(A) and multiply: the direct solve does less work and loses less accuracy. The Cholesky factorization, linalg.cholesky(S, lower=True), splits a covariance matrix as Σ=LL⊤\Sigma=LL^{\top}Σ=LL⊤, where LLL is lower triangular, which means zeros above the diagonal. If z\mathbf zz is a vector of independent standard normal numbers, then LzL\mathbf zLz has covariance Σ\SigmaΣ, and this is how you simulate correlated returns.

The eigenvalues of Σ\SigmaΣ tell you how many independent sources of risk there really are. linalg.eigvalsh(S) returns them for a symmetric matrix, smallest first. For two assets with correlation ρ\rhoρ the correlation matrix has eigenvalues 1+ρ1+\rho1+ρ and 1−ρ1-\rho1−ρ, so highly correlated assets have one big eigenvalue and one tiny one. The ratio of the largest to the smallest eigenvalue is the condition number. A large condition number means the matrix is nearly singular, that is, nearly impossible to invert, and a small change in the data can swing the answer of a solve a lot. Optimisers that take the inverse of Σ\SigmaΣ are sensitive to exactly this.

Ax=b ⇒ x=solve(A,b),Σ=LL⊤,r=Lz,κ(Σ)=λmax⁡λmin⁡A\mathbf x=\mathbf b\ \Rightarrow\ \mathbf x=\text{solve}(A,\mathbf b),\qquad \Sigma=LL^{\top},\quad \mathbf r=L\mathbf z,\qquad \kappa(\Sigma)=\frac{\lambda_{\max}}{\lambda_{\min}}Ax=b ⇒ x=solve(A,b),Σ=LL⊤,r=Lz,κ(Σ)=λmin​λmax​​(1)
Two assets by handTake Σ=(4223)\Sigma=\begin{pmatrix}4&2\\2&3\end{pmatrix}Σ=(42​23​). Its Cholesky factor is L=(2012)L=\begin{pmatrix}2&0\\1&\sqrt2\end{pmatrix}L=(21​02​​), because LL⊤LL^{\top}LL⊤ gives 444, 222, 222 and 1+2=31+2=31+2=3. For independent draws z=(1,1)z=(1,1)z=(1,1) the correlated draw is Lz=(2, 1+2)=(2, 2.414)Lz=(2,\,1+\sqrt2)=(2,\,2.414)Lz=(2,1+2​)=(2,2.414). To solve (2113)x=(35)\begin{pmatrix}2&1\\1&3\end{pmatrix}\mathbf x=\begin{pmatrix}3\\5\end{pmatrix}(21​13​)x=(35​), eliminate to find x=(0.8, 1.4)\mathbf x=(0.8,\,1.4)x=(0.8,1.4). A correlation matrix with ρ=0.9\rho=0.9ρ=0.9 has eigenvalues 1.91.91.9 and 0.10.10.1, so the first factor carries 1.9/2=95%1.9/2=95\%1.9/2=95% of the total variance. A matrix with eigenvalues 111 and 333 has condition number 333. On real data, SPY, QQQ and TLT over 5,4045{,}4045,404 days have correlations of 0.920.920.92 between SPY and QQQ and about −0.3-0.3−0.3 between each of them and TLT. The correlation matrix has eigenvalues 0.080.080.08, 0.860.860.86 and 2.062.062.06, so one factor carries 69%69\%69% of the variance, and the condition number is 262626.
  • Solve, do not invert: solve(A, b) is faster and more accurate than inv(A) @ b.
  • Cholesky makes correlation: multiply independent normals by LLL to get returns with covariance Σ\SigmaΣ.
  • Watch the condition number: highly correlated assets make Σ\SigmaΣ nearly singular and solutions unstable.

Lab · what's graded

  • The Cholesky factor20 pts
  • Solving a system10 pts
  • A condition number10 pts
  • A factor's share of variance10 pts
  • What each routine is for15 pts
  • Simulating correlated returns10 pts
  • Why not invert25 pts
0/ 7 answered
Tasks
1Matrix · vector20 ptsNot answered

Find the lower triangular LLL with LL⊤=Σ=(4223)LL^{\top}=\Sigma=\begin{pmatrix}4&2\\2&3\end{pmatrix}LL⊤=Σ=(42​23​). Enter LLL (use 1.41421.41421.4142 for 2\sqrt22​).

L=L =L=
2Numeric answer10 ptsNot answered

Solve 2x1+x2=32x_1+x_2=32x1​+x2​=3 and x1+3x2=5x_1+3x_2=5x1​+3x2​=5. What is x2x_2x2​? Give one decimal.

x=x =x=accepted: ± 0.02
3Numeric answer10 ptsNot answered

A symmetric matrix has eigenvalues 111 and 333. What is its condition number, the largest eigenvalue divided by the smallest?

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

Two assets with correlation 0.90.90.9 have correlation-matrix eigenvalues 1.91.91.9 and 0.10.10.1. What percent of the total variance does the first factor carry?

s=s =s=accepted: ± 0.5
5Match15 ptsNot answered

Match each scipy.linalg routine with its job in portfolio work.

Pick the partner of each item on the left.

solve
cholesky
eigvalsh
lstsq
6Order the steps10 ptsNot answered

Put the steps of simulating correlated daily returns 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. 1Check that the sample covariance is close to the target
  2. 2Multiply the normal draws by L
  3. 3Estimate the covariance matrix from history
  4. 4Draw independent standard normal numbers
  5. 5Factor it with Cholesky
7Explain in words25 ptsNot answered

In one or two sentences, explain why solve(A, b) is preferred to inv(A) @ b, and what a large condition number warns about.

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