M2-001M2: Classical Machine LearningLinear RegressionEasy
⚡Flashcard Drill➔Mastery:

Formulate the Closed-Form Normal Equation for Linear Regression and Its Necessary Preconditions

📐 Mathematical Definition
w^=(X⊤X)−1X⊤y\hat w=(X^\top X)^{-1}X^\top y
⚡ One-Sentence Core Answer
The OLS closed-form solution is β^=(XTX)−1XTy\hat{\beta} = (X^T X)^{-1} X^T y, which strictly requires XTXX^T X to be invertible (full column rank with no perfect multicollinearity).

📌 Key Takeaways

  • •
    Normal equation: β^=(XTX)−1XTy\hat{\beta} = (X^T X)^{-1} X^T y, derived by setting the gradient of squared error ∇β∥y−Xβ∥2=0\nabla_\beta \|y - X\beta\|^2 = 0.
  • •
    Invertibility precondition: X∈RN×dX \in \mathbb{R}^{N \times d} must have full column rank Rank(X)=d\text{Rank}(X) = d, which requires sample size N≥dN \ge d.
  • •
    Rank deficiency: If features are collinear or N<dN < d, XTXX^T X is singular and has no unique inverse, requiring pseudoinverse X+X^+ or Ridge regularization.

📐 Mathematical Derivations

The Ordinary Least Squares objective is L(β)=12∥y−Xβ∥22=12(y−Xβ)T(y−Xβ)=12(yTy−2βTXTy+βTXTXβ)L(\beta) = \frac{1}{2} \|y - X\beta\|_2^2 = \frac{1}{2}(y - X\beta)^T (y - X\beta) = \frac{1}{2}(y^T y - 2\beta^T X^T y + \beta^T X^T X \beta). Taking the gradient with respect to β\beta: ∇βL(β)=−XTy+XTXβ\nabla_\beta L(\beta) = -X^T y + X^T X \beta. Setting ∇βL(β)=0\nabla_\beta L(\beta) = 0 yields the Normal Equations: XTXβ=XTyX^T X \beta = X^T y. If the Gram matrix XTXX^T X is non-singular (positive definite), multiplying by (XTX)−1(X^T X)^{-1} gives the unique closed-form estimator: β^=(XTX)−1XTy\hat{\beta} = (X^T X)^{-1} X^T y. The Hessian is ∇2L(β)=XTX⪰0\nabla^2 L(\beta) = X^T X \succeq 0, confirming a global convex minimum.

🏭 Production Trade-offs

Computational complexity: Computing (XTX)−1(X^T X)^{-1} via Cholesky decomposition costs O(Nd2+d3)O(N d^2 + d^3) time and O(d2)O(d^2) memory. In modern big data regimes where feature dimension d>100,000d > 100,000 or sample size N>108N > 10^8, closed-form matrix inversion is completely intractable. Instead, iterative first-order optimization (Stochastic Gradient Descent / Mini-batch SGD) with complexity O(Nd)O(Nd) per epoch is used exclusively in production.
⚠️ Common Interview Pitfalls
  • ✕
    Inverting XTXX^T X directly using np.linalg.inv rather than solving the linear system via Cholesky scipy.linalg.solve(X.T @ X, X.T @ y, assume_a='pos').
  • ✕
    Attempting closed-form inversion when N<dN < d without regularization.
🎯 Interviewer Follow-up Inquiries
  • ?
    Why is QR decomposition X=QRX = QR numerically superior to forming XTXX^T X directly in OLS?
  • ?
    How does the Gauss-Markov Theorem prove that OLS is the Best Linear Unbiased Estimator (BLUE)?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM1-089: Explain the Synthetic Control Method and Its Application in Geo-Testing and Policy Evaluation📋Back to BankNext →M2-002: Enumerate the Four Classical Assumptions of Linear Regression and the Consequences of Their Violation
中文速览与面试要点

写出线性回归的闭式解,并说明它成立的前提。

最小二乘解 w=(XᵀX)⁻¹Xᵀy,前提是 XᵀX 可逆(无完全共线性)。

核心要点

  • 推导:最小化残差平方和 L(w)=‖y−Xw‖²,对其求导置零得正规方程 ∂L/∂w=−2Xᵀ(y−Xw)=0 ⇒ XᵀXw=Xᵀy ⇒ w=(XᵀX)⁻¹Xᵀy。这个解存在的充要条件是 XᵀX 可逆,即 X 列满秩(各特征线性无关)。从概率视角,若假设 y=Xw+ε、ε~N(0,σ²I),则负对数似然正比于 ‖y−Xw‖²/(2σ²),故最小二乘 ≡ 高斯噪声下的 MLE——这解释了为什么最小二乘如此普遍。几何上,Xw 是 y 在 X 列空间上的 正交投影 (因为残差 e=y−Xw 满足 Xᵀe=0),投影矩阵 H=X(XᵀX)⁻¹Xᵀ 称为帽子矩阵。
  • 工程实现的关键是 不要真的求逆 :① (XᵀX)⁻¹ 的条件数是 κ(X)²,会平方放大舍入误差;② 更稳的做法是 QR 分解(X=QR,则 w=R⁻¹Qᵀy,条件数不变)或 SVD(w=VΣ⁺Uᵀy,可截断小奇异值);③ 当特征共线或 p>n(宽数据)时 XᵀX 奇异,需用伪逆(给出最小范数解)或岭回归(加 λI 使其可逆)。帽子矩阵的对角线 hᵢᵢ 是 杠杆值 ,衡量第 i 个样本对自身预测的影响;高杠杆点(hᵢᵢ 接近 1)对拟合影响极大,是回归诊断的重点。此外,线性回归的四大假设(线性、误差独立、同方差、正态)中,前三个影响估计的无偏性与有效性,正态只影响小样本推断。
  • 等价于高斯噪声假设下的 MLE
  • 实际用 QR/SVD/pinv 而非直接求逆

常见陷阱

  • 直接对 XᵀX 求逆(条件数平方)
  • 在共线/宽数据下仍用普通最小二乘(应改岭回归或伪逆)