Least Squares Line
We want to find a straight line that fits the given set of points best in a sense of least
squares approximation. If a line is not perpendicular to the x-axis, it can be represented
either explicitly as y=kx+c or implicitly as y-kx-c=0. Assume this line is used to
approximate the given set of points pi (i=1,2,¼,n). Then, the squared error
with respect to pi=(xi,yi) is
(yi-kxi-
c)2. Accordingly, the total squared error
is given by
We now want to optimize k and c such that f is minimized. This is equivalent to
solving the following system of linear equations:
Alternatively, we may represent the above equations as
where,
|
a00= |
å
| xi2, a01= |
å
| xi, b0= |
å
| xiyi |
|
|
a10= |
å
| xi, a11=n, b1= |
å
| yi |
|
Since it is assumed that the line is not perpendicular to the x-axis, we have
Therefore,
We now consider how to find the projection point of pi on the line y=kx+c. Let
We want to find x such that the distance between pi and (x,y), which is on
y=kx+c, is minimized. This is equivalent to solving
|
|
df dx
|
=2(x-xi)+2(kx+c-yi)k=0. |
|
Therefore,
Finally, if the line is perpendicular to the x-axis, we may represent it as
x=[`k] y + [`c]. Accordingly, we can solve the following linear system of equations
to obtain optimized [`k] and [`c]:
|
|
_ a
|
00
|
|
_ k
|
+ |
_ a
|
01
|
|
_ c
|
= |
_ b
|
0
|
|
|
|
|
_ a
|
10
|
|
_ k
|
+ |
_ a
|
11
|
|
_ c
|
= |
_ b
|
1
|
|
|
where,
|
|
_ a
|
00
|
= |
å
| yi2, |
_ a
|
01
|
= |
å
| yi, |
_ b
|
0
|
= |
å
| xiyi |
|
|
|
_ a
|
10
|
= |
å
| yi, |
_ a
|
11
|
=n, |
_ b
|
1
|
= |
å
| xi |
|
In applications, we actually compare two determinants
|
|
ê ê
ê
|
| |
ê ê
ê
|
and |
ê ê ê
ê ê ê
|
| |
ê ê ê
ê ê ê
|
|
|
and choose the method that yields the larger absolute value. This approach will
guarantee the numerical stability.
RETURN