σμ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.4·~1 hr 30 minIntermediate

Capstone: OLS regression from scratch

Skill positionModule 4 of 4 · Topic 4 of 4
4.4 Capstone: OLS regression from scratch
—Lab score— / 100Total XP—Streak—

A regression is a projection, and QR computes it

Everything in this skill meets in one computation. The regression of a response y on regressors in the columns of X projects y onto the column space of X; the coefficients β̂ are the weights of that projection, and the residual is orthogonal to every regressor. To find the weights without squaring the condition number, factor X=QR and solve the triangular system Rβ̂=Qᵀy by back substitution. This is what a library does when you call a least squares routine.

X=QR, Rβ̂=Qᵀy, R²=1-(‖e‖²)/(‖y-ȳ1‖²)

In finance the response is the daily return of a stock and the regressors are the returns of the market and other factors. The intercept α is the average return not explained by the factors, each slope β is the sensitivity to a factor, and R² is the share of the stock's variance the factors explain. You compute them from daily simple returns rₜ=Pₜ/Pₜ₋₁-1, using only days where all series have a return.

What to expect on AppleRegressing AAPL on SPY (the market) and MSFT from 2018 gives about β_SPY≈1.01, β_MSFT≈0.17, R²≈0.58 and an α of about 0.035 percent per day over 2,171 days. A beta near one says Apple moves about one for one with the market, and the R² says the market and Microsoft explain a bit over half of its daily variance; the rest is Apple-specific. Treat the α with care: the prices are adjusted and only surviving companies are in the file, which flatters it.
  • Modified Gram-Schmidt builds Q and R; subtract each projection from the updated vector.
  • Back substitution solves a triangular system from the last row up.
  • The column of ones gives the intercept; without it the fit is forced through the origin.
Correlated factors share the creditSPY and MSFT move together, so the data matrix is not perfectly conditioned. The betas are still well determined here because the two are far from identical, but adding a third factor that is nearly a combination of them would make individual betas swing wildly while the fitted values barely change. Use the condition number of X as a warning light.

Lab · what's graded

  • Modified Gram-Schmidt25 pts
  • Back substitution15 pts
  • An OLS fit from QR30 pts
  • Betas on real returns30 pts
Explorer · lab-la-10
▾ lab-la-10
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-la-10$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-la-10 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes

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