M2-001M2: Classical Machine LearningLinear RegressionEasy
⚡Flashcard Drill➔Mastery:
Formulate the Closed-Form Normal Equation for Linear Regression and Its Necessary Preconditions
📐 Mathematical Definition
⚡ One-Sentence Core Answer
The OLS closed-form solution is , which strictly requires to be invertible (full column rank with no perfect multicollinearity).
📌 Key Takeaways
- •Normal equation: , derived by setting the gradient of squared error .
- •Invertibility precondition: must have full column rank , which requires sample size .
- •Rank deficiency: If features are collinear or , is singular and has no unique inverse, requiring pseudoinverse or Ridge regularization.
📐 Mathematical Derivations
The Ordinary Least Squares objective is . Taking the gradient with respect to : . Setting yields the Normal Equations: . If the Gram matrix is non-singular (positive definite), multiplying by gives the unique closed-form estimator: . The Hessian is , confirming a global convex minimum.
🏭 Production Trade-offs
Computational complexity: Computing via Cholesky decomposition costs time and memory. In modern big data regimes where feature dimension or sample size , closed-form matrix inversion is completely intractable. Instead, iterative first-order optimization (Stochastic Gradient Descent / Mini-batch SGD) with complexity per epoch is used exclusively in production.
⚠️ Common Interview Pitfalls
- ✕Inverting directly using
np.linalg.invrather than solving the linear system via Choleskyscipy.linalg.solve(X.T @ X, X.T @ y, assume_a='pos'). - ✕Attempting closed-form inversion when without regularization.
🎯 Interviewer Follow-up Inquiries
- ?Why is QR decomposition numerically superior to forming 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.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
写出线性回归的闭式解,并说明它成立的前提。
最小二乘解 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 求逆(条件数平方)
- 在共线/宽数据下仍用普通最小二乘(应改岭回归或伪逆)