Let us consider the task of finding the line that best fits a set of points in the plane.
We will, however, insist the the points' center of mass is at the origin,
as this turns out to simplify the solution.
To be more specific, let the points be given as (xi,yi) for i=1,…,n,
where n≥2 is the number of points.
The center of mass restriction means that we have
i=1∑nxi=0andi=1∑nyi=0.
We will furthermore require that not all xi are equal to zero or,
equivalently, that ∑i=1nxi2>0
(this is also the reason for the n≥2 restriction).
We initially stated that we wanted to find the line that fits the points best.
There are several ways to define what is meant by best, but here we want to find the line
y=ax+b such that the following error function is minimized:
J=i=1∑n(axi+b−yi)2.
That is, we want to minimize the sum of the squares of the vertical distances between
the points and the line, or least squares for short.
To find the stationary point of J, we first set the partial derivative with respect to b to zero: