We have data (x1,y1),…,(xn,yn) with xi,yi∈R (simple 1-d regression with intercept). The model is yi=α+βxi+ϵi.
Write the sum of squared residuals L(α,β)=∑i=1n(yi−α−βxi)2 and take partial derivatives with respect to α and β. Set both to zero.
Solve the resulting two equations to derive the classical closed forms
β^=∑i(xi−xˉ)2∑i(xi−xˉ)(yi−yˉ),α^=yˉ−β^xˉ.
Verify that β^ equals the sample covariance of (x,y) divided by the sample variance of x: β^=Cov(x,y)/Var(x).
Numerical sanity check. Generate n=100 points with xi∼N(0,1), yi=2+3xi+ϵi, ϵi∼N(0,1). Use numpy.polyfit or your closed-form formulas to estimate α^,β^. Both should be close to (2,3).
Hint
For part 1: ∂L/∂α=−2∑(yi−α−βxi)=0 gives ∑yi=nα+β∑xi. Similarly for β.