Least Squares
Data Fitting
Regression Analysis
Statistical Methods
Mathematical Modelling

Least Squares method in practice

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

The Least Squares method is a mathematical optimization technique predominantly used in statistics and data fitting to approximate the solution of overdetermined systems. Such systems have more equations than unknowns—common in real-world scenarios where measurements or data points are numerous but the underlying model governing them is simpler.

Introduction

In essence, the Least Squares method seeks to minimize the sum of the squares of the residuals (the differences between observed and calculated values). Although it can be applied to various contexts, it's principally utilized in linear regression to determine the best-fit line through a set of data points.

Theoretical Foundations

At its core, the Least Squares method addresses the problem of fitting a model to a data set. In its linear form, the objective is to find coefficients that minimize the Euclidean length of the vector of residuals. Mathematically, given a set of nn data points (xi,yi)(x_i, y_i), the goal is to find the parameters β0\beta_0 and β1\beta_1 such that:

y_i=β_0+β_1x_i+ϵ_iy\_i = \beta\_0 + \beta\_1 x\_i + \epsilon\_i

where ϵi\epsilon_i represents the error term. The sum of squared errors (SSE) is given by:

SSE=_i=1n(y_iy^_i)2SSE = \sum\_{i=1}^{n} (y\_i - \hat{y}\_i)^2

The parameters β0\beta_0 and β1\beta_1 are estimated by minimizing this SSE.

Algebraic Solution

The solutions for β0\beta_0 and β1\beta_1, in the case of simple linear regression, are obtained using the normal equations:

β_1=_i=1n(x_ixˉ)(y_iyˉ)_i=1n(x_ixˉ)2\beta\_1 = \frac{\sum\_{i=1}^{n}(x\_i - \bar{x})(y\_i - \bar{y})}{\sum\_{i=1}^{n}(x\_i - \bar{x})^2}

β_0=yˉβ_1xˉ\beta\_0 = \bar{y} - \beta\_1 \bar{x}

where $\bar\{x\}$ and $\bar\{y\}$ are the means of the xx and yy values, respectively.

Practical Example

Let's consider a simple data set to illustrate the Least Squares method:

Data Pointxxyy
112
223
335
444
556

The task is to fit a linear model using the Least Squares method. Calculating the means:

xˉ=1+2+3+4+55=3\bar{x} = \frac{1+2+3+4+5}{5} = 3

yˉ=2+3+5+4+65=4\bar{y} = \frac{2+3+5+4+6}{5} = 4

With these, we find β1\beta_1 and β0\beta_0:

β_1=(13)(24)+(23)(34)+(33)(54)+(43)(44)+(53)(64)(13)2+(23)2+(33)2+(43)2+(53)2=0.8\beta\_1 = \frac{(1-3)(2-4) + (2-3)(3-4) + (3-3)(5-4) + (4-3)(4-4) + (5-3)(6-4)}{(1-3)^2 + (2-3)^2 + (3-3)^2 + (4-3)^2 + (5-3)^2} = 0.8

β_0=40.8×3=1.6\beta\_0 = 4 - 0.8 \times 3 = 1.6

Thus, the estimated regression line is y=1.6+0.8xy = 1.6 + 0.8x.

Table Summary

ConceptExplanation/Formula
Data PointsObservations xix_i and yiy_i
ObjectiveMinimize SSE=i=1n(yiy^i)2SSE = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2
Slope (β1\beta_1)β1=(xixˉ)(yiyˉ)(xixˉ)2\beta_1 = \frac{\sum(x_i - \bar{x})(y_i-\bar{y})}{\sum(x_i-\bar{x})^2}
Intercept (β0\beta_0)β0=yˉβ1xˉ\beta_0 = \bar{y} - \beta_1 \bar{x}
Best-Fit Liney^=β0+β1x\hat{y} = \beta_0 + \beta_1 x

Applications

Regression Analysis: The most recognized application, used to model relationships between variables. • Forecasting: Used in time-series analysis to predict future points. • Signal Processing: Applied in filter design and adaptive filtering. • Image Reconstruction: Employed for denoising and restoring images.

Limitations

Despite its widespread usage, the Least Squares method assumes that: • The relationship between variables is linear. • The errors are normally distributed and homoscedastic. • There's no multicollinearity among independent variables.

For non-linear or more complex models, extensions like polynomial regression or alternative methods like Ridge regression, which addresses multicollinearity by imposing a penalty on the size of coefficients, can be employed.

In conclusion, the Least Squares method remains a cornerstone of statistical analysis, econometrics, and predictive modeling, providing foundational tools for estimating parameters and making informed predictions across a spectrum of disciplines.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.