σμ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.2·~50 minIntermediate

QR decomposition and Gram-Schmidt

Skill positionModule 4 of 4 · Topic 2 of 4
4.2 QR decomposition and Gram-Schmidt
—Lab score— / 100Total XP—Streak—

Building an orthonormal basis

Orthonormal columns are the best-behaved columns a matrix can have: QTQ=IQ^{T}Q=IQTQ=I, so QQQ keeps lengths and angles and never amplifies errors. Gram-Schmidt turns any independent columns a1,…,an\mathbf{a}_1,\dots,\mathbf{a}_na1​,…,an​ into orthonormal ones: take q1=a1/∥a1∥\mathbf{q}_1=\mathbf{a}_1/\|\mathbf{a}_1\|q1​=a1​/∥a1​∥, then for each next column subtract its projections onto the q\mathbf{q}q's found so far and normalise what remains.

A=QR,Rij=qi⋅aj (i≤j),qj=aj−∑i<jRijqiRjjA=QR,\qquad R_{ij}=\mathbf{q}_i\cdot\mathbf{a}_j\ (i\le j),\qquad \mathbf{q}_j=\frac{\mathbf{a}_j-\sum_{i<j}R_{ij}\mathbf{q}_i}{R_{jj}}A=QR,Rij​=qi​⋅aj​ (i≤j),qj​=Rjj​aj​−∑i<j​Rij​qi​​(1)

Recording the numbers you used gives the QR decomposition: QQQ has the orthonormal columns and RRR is upper triangular, with Rij=qi⋅ajR_{ij}=\mathbf{q}_i\cdot\mathbf{a}_jRij​=qi​⋅aj​ and the lengths RjjR_{jj}Rjj​ on its diagonal. For least squares, ∥Ax−b∥=∥QRx−b∥\|A\mathbf{x}-\mathbf{b}\|=\|QR\mathbf{x}-\mathbf{b}\|∥Ax−b∥=∥QRx−b∥ is minimised by Rx^=QTbR\hat{\mathbf{x}}=Q^{T}\mathbf{b}Rx^=QTb, a triangular system solved by back substitution, without ever forming ATAA^{T}AATA. This is why κ(R)=κ(A)\kappa(R)=\kappa(A)κ(R)=κ(A) instead of κ(A)2\kappa(A)^2κ(A)2.

Gram-Schmidt on two columnsTake A=(3142)A=\begin{pmatrix}3&1\\4&2\end{pmatrix}A=(34​12​), with columns a1=(3,4)\mathbf{a}_1=(3,4)a1​=(3,4) and a2=(1,2)\mathbf{a}_2=(1,2)a2​=(1,2). Then R11=∥a1∥=5R_{11}=\|\mathbf{a}_1\|=5R11​=∥a1​∥=5 and q1=(0.6, 0.8)\mathbf{q}_1=(0.6,\,0.8)q1​=(0.6,0.8). Next R12=q1⋅a2=2.2R_{12}=\mathbf{q}_1\cdot\mathbf{a}_2=2.2R12​=q1​⋅a2​=2.2, and a2−2.2 q1=(−0.32, 0.24)\mathbf{a}_2-2.2\,\mathbf{q}_1=(-0.32,\,0.24)a2​−2.2q1​=(−0.32,0.24) has length R22=0.4R_{22}=0.4R22​=0.4, so q2=(−0.8, 0.6)\mathbf{q}_2=(-0.8,\,0.6)q2​=(−0.8,0.6). Thus Q=(0.6−0.80.80.6)Q=\begin{pmatrix}0.6&-0.8\\0.8&0.6\end{pmatrix}Q=(0.60.8​−0.80.6​) and R=(52.200.4)R=\begin{pmatrix}5&2.2\\0&0.4\end{pmatrix}R=(50​2.20.4​). Check: det⁡A=2=det⁡R\det A=2=\det RdetA=2=detR.
  • Q: orthonormal columns, QTQ=IQ^{T}Q=IQTQ=I, no error amplification.
  • R: upper triangular; its diagonal holds the lengths left after removing earlier directions.
  • Modified Gram-Schmidt (or Householder reflections in libraries) keeps QQQ orthogonal in floating point; the textbook version loses orthogonality on nearly dependent columns.
Why libraries use QR for regressionRegressors in finance are often nearly collinear (many correlated factors, polynomial features). The normal equations would square that condition number and silently return garbage, while QR (or the SVD) copes. That is the algorithm behind the least squares routines of NumPy and statsmodels, and the one you will build in the capstone.

Lab · what's graded

  • The first diagonal entry10 pts
  • The matrix Q20 pts
  • The matrix R20 pts
  • A determinant from R15 pts
  • Least squares with QR15 pts
  • Why QR is more accurate20 pts
0/ 6 answered
Tasks
1Numeric answer10 ptsNot answered

Let A=(3142)A=\begin{pmatrix}3&1\\4&2\end{pmatrix}A=(34​12​) with columns a1=(3,4)\mathbf{a}_1=(3,4)a1​=(3,4) and a2=(1,2)\mathbf{a}_2=(1,2)a2​=(1,2). Find R11=∥a1∥R_{11}=\|\mathbf{a}_1\|R11​=∥a1​∥.

R11=R_{11} =R11​=accepted: ± 0.001
2Matrix · vector20 ptsNot answered

Let A=(3142)A=\begin{pmatrix}3&1\\4&2\end{pmatrix}A=(34​12​) with columns a1=(3,4)\mathbf{a}_1=(3,4)a1​=(3,4) and a2=(1,2)\mathbf{a}_2=(1,2)a2​=(1,2). Run Gram-Schmidt and fill in Q=(q1 q2)Q=(\mathbf{q}_1\ \mathbf{q}_2)Q=(q1​ q2​) (decimals are fine).

Q=Q =Q=
3Matrix · vector20 ptsNot answered

Let A=(3142)A=\begin{pmatrix}3&1\\4&2\end{pmatrix}A=(34​12​) with columns a1=(3,4)\mathbf{a}_1=(3,4)a1​=(3,4) and a2=(1,2)\mathbf{a}_2=(1,2)a2​=(1,2). Fill in the upper triangular RRR so that A=QRA=QRA=QR.

R=R =R=
4Numeric answer15 ptsNot answered

Let A=(3142)A=\begin{pmatrix}3&1\\4&2\end{pmatrix}A=(34​12​) with columns a1=(3,4)\mathbf{a}_1=(3,4)a1​=(3,4) and a2=(1,2)\mathbf{a}_2=(1,2)a2​=(1,2). Since QQQ is a rotation (det⁡Q=1\det Q=1detQ=1), ∣det⁡A∣=∣det⁡R∣|\det A|=|\det R|∣detA∣=∣detR∣. Compute it from the diagonal of R=(52.200.4)R=\begin{pmatrix}5&2.2\\0&0.4\end{pmatrix}R=(50​2.20.4​).

∣det⁡A∣=|\det A| =∣detA∣=accepted: ± 0.001
5Choice15 ptsNot answered

Given A=QRA=QRA=QR, which step finds the least squares solution of Ax≈bA\mathbf{x}\approx\mathbf{b}Ax≈b?

6Explain in words20 ptsNot answered

In two sentences, say why solving least squares with QR is more accurate than with the normal equations.

Math modeKeyboard or LaTeX inputTask 1 of 6 · 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