σμ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
  • 日本語
  • 中文
Linear Algebra skill›Module 4 · Least squares and factorizations›Topic 4.1·~55 minIntermediate

Least squares and the normal equations

Skill positionModule 4 of 4 · Topic 1 of 4
4.1 Least squares and the normal equations
—Lab score— / 100Total XP—Streak—

More equations than unknowns

A regression has many observations and few coefficients: Ax=bA\mathbf{x}=\mathbf{b}Ax=b with AAA tall (nnn rows, ppp columns, n>pn>pn>p) has no exact solution, because b\mathbf{b}b is almost never in the column space. Least squares picks the x^\hat{\mathbf{x}}x^ that makes the error as small as possible, min⁡x∥Ax−b∥2\min_{\mathbf{x}}\|A\mathbf{x}-\mathbf{b}\|^2minx​∥Ax−b∥2. Geometrically, Ax^A\hat{\mathbf{x}}Ax^ is the projection of b\mathbf{b}b onto the column space, so the residual b−Ax^\mathbf{b}-A\hat{\mathbf{x}}b−Ax^ is orthogonal to every column of AAA.

ATA x^=ATb,x^=(ATA)−1ATb,b^=Ax^A^{T}A\,\hat{\mathbf{x}}=A^{T}\mathbf{b},\qquad \hat{\mathbf{x}}=(A^{T}A)^{-1}A^{T}\mathbf{b},\qquad \hat{\mathbf{b}}=A\hat{\mathbf{x}}ATAx^=ATb,x^=(ATA)−1ATb,b^=Ax^(1)

Orthogonality of the residual, AT(b−Ax^)=0A^{T}(\mathbf{b}-A\hat{\mathbf{x}})=\mathbf{0}AT(b−Ax^)=0, rearranges into the normal equations above. They always have a solution, and it is unique when the columns of AAA are independent (then ATAA^{T}AATA is invertible). In regression notation A=XA=XA=X holds the regressors, with a first column of ones for the intercept, b=y\mathbf{b}=\mathbf{y}b=y is the response and β^=(XTX)−1XTy\hat{\boldsymbol\beta}=(X^{T}X)^{-1}X^{T}\mathbf{y}β^​=(XTX)−1XTy are the OLS coefficients. In code you still solve the system rather than invert XTXX^{T}XXTX.

Fitting a line to three pointsFit y=c0+c1ty=c_0+c_1ty=c0​+c1​t to the points (0,1)(0,1)(0,1), (1,2)(1,2)(1,2), (2,2)(2,2)(2,2). Then A=(101112)A=\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix}A=​111​012​​ and b=(1,2,2)\mathbf{b}=(1,2,2)b=(1,2,2), so ATA=(3335)A^{T}A=\begin{pmatrix}3&3\\3&5\end{pmatrix}ATA=(33​35​) and ATb=(5,6)A^{T}\mathbf{b}=(5,6)ATb=(5,6). The normal equations 3c0+3c1=53c_0+3c_1=53c0​+3c1​=5, 3c0+5c1=63c_0+5c_1=63c0​+5c1​=6 give c1=0.5c_1=0.5c1​=0.5 and c0=7/6≈1.167c_0=7/6\approx1.167c0​=7/6≈1.167. The fitted values are 1.167, 1.667, 2.1671.167,\ 1.667,\ 2.1671.167, 1.667, 2.167 and the residuals −0.167, 0.333, −0.167-0.167,\ 0.333,\ -0.167−0.167, 0.333, −0.167: they sum to zero and are orthogonal to ttt.
  • Normal equations: ATAx^=ATbA^{T}A\hat{\mathbf{x}}=A^{T}\mathbf{b}ATAx^=ATb; solve them, do not invert.
  • Residual: b−Ax^\mathbf{b}-A\hat{\mathbf{x}}b−Ax^, orthogonal to every column of AAA; with an intercept column it sums to zero.
  • Independent columns are needed for a unique answer; collinear regressors make ATAA^{T}AATA singular.
Squaring the condition numberForming ATAA^{T}AATA squares the condition number: κ(ATA)=κ(A)2\kappa(A^{T}A)=\kappa(A)^2κ(ATA)=κ(A)2. If κ(A)=105\kappa(A)=10^{5}κ(A)=105, the normal equations have κ=1010\kappa=10^{10}κ=1010 and lose ten of your sixteen digits. The next topic shows how QR solves the same problem without ever forming ATAA^{T}AATA.

Lab · what's graded

  • The normal matrix15 pts
  • The slope15 pts
  • The intercept15 pts
  • The size of the error10 pts
  • What the residual is orthogonal to15 pts
  • The steps of a least squares fit15 pts
  • Why residuals sum to zero15 pts
0/ 7 answered
Tasks
1Matrix · vector15 ptsNot answered

Fit y=c0+c1ty=c_0+c_1ty=c0​+c1​t to the points (0,1)(0,1)(0,1), (1,2)(1,2)(1,2), (2,2)(2,2)(2,2) with A=(101112)A=\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix}A=​111​012​​ and b=(1,2,2)\mathbf{b}=(1,2,2)b=(1,2,2). Fill in ATAA^{T}AATA.

ATA=A^T A =ATA=
2Numeric answer15 ptsNot answered

Fit y=c0+c1ty=c_0+c_1ty=c0​+c1​t to the points (0,1)(0,1)(0,1), (1,2)(1,2)(1,2), (2,2)(2,2)(2,2) with A=(101112)A=\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix}A=​111​012​​ and b=(1,2,2)\mathbf{b}=(1,2,2)b=(1,2,2). Solve ATAc=ATbA^{T}A\mathbf{c}=A^{T}\mathbf{b}ATAc=ATb, where ATb=(5,6)A^{T}\mathbf{b}=(5,6)ATb=(5,6). What is the slope c1c_1c1​?

c1=c_1 =c1​=accepted: ± 0.001
3Numeric answer15 ptsNot answered

Fit y=c0+c1ty=c_0+c_1ty=c0​+c1​t to the points (0,1)(0,1)(0,1), (1,2)(1,2)(1,2), (2,2)(2,2)(2,2) with A=(101112)A=\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix}A=​111​012​​ and b=(1,2,2)\mathbf{b}=(1,2,2)b=(1,2,2). What is the intercept c0c_0c0​? Give it to three decimals.

c0=c_0 =c0​=accepted: ± 0.001
4Numeric answer10 ptsNot answered

Fit y=c0+c1ty=c_0+c_1ty=c0​+c1​t to the points (0,1)(0,1)(0,1), (1,2)(1,2)(1,2), (2,2)(2,2)(2,2) with A=(101112)A=\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix}A=​111​012​​ and b=(1,2,2)\mathbf{b}=(1,2,2)b=(1,2,2). The residuals are (−1/6, 1/3, −1/6)(-1/6,\ 1/3,\ -1/6)(−1/6, 1/3, −1/6). What is the sum of squared residuals? Give it to three decimals.

SSR=SSR =SSR=accepted: ± 0.001
5Choice15 ptsNot answered

The residual b−Ax^\mathbf{b}-A\hat{\mathbf{x}}b−Ax^ of a least squares fit is orthogonal to which of these? Select all that apply.

6Order the steps15 ptsNot answered

Put the steps of fitting a regression by the normal equations 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. 1Form ATAA^{T}AATA and ATbA^{T}\mathbf{b}ATb
  2. 2Compute the fitted values Ax^A\hat{\mathbf{x}}Ax^ and the residual
  3. 3Build AAA with a column of ones and the regressors
  4. 4Solve ATAx=ATbA^{T}A\mathbf{x}=A^{T}\mathbf{b}ATAx=ATb for the coefficients
7Explain in words15 ptsNot answered

In a regression with an intercept the residuals add up to zero. In two sentences, say why, using orthogonality.

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