stepik: https://stepik.org/a/239757
Research-oriented implementation of the Normal Equations method for solving Simple Linear Regression in closed form.
This repository provides a mathematically transparent implementation of the closed-form solution to the ordinary least squares (OLS) problem for simple linear regression.
We consider the model:
where:
-
$x_i$ — scalar input -
$y_i$ — scalar target -
$\beta_0$ — intercept -
$\beta_1$ — slope -
$\varepsilon_i$ — noise term
The objective is to minimize the residual sum of squares:
Define the design matrix:
Parameter vector:
Target vector:
Then the least squares problem becomes:
For simple linear regression, the coefficients can also be written explicitly:
where:
The matrix
If multicollinearity or degeneracy occurs, numerical instability may arise.
For improved stability in high-dimensional settings, QR decomposition or SVD-based solvers are preferred.
The solver is implemented using NumPy and performs:
- Construction of
$X$ - Computation of
$X^T X$ - Matrix inversion
- Coefficient estimation
from solver import normal_equation
beta_0, beta_1 = normal_equation(x, y)