
$$ f(x) = ax + b $$
위와 같은 추세선을 그린다고 할 때 오차를 최소화하려고 한다. 3개의 sample을 대입한다,
$$ \begin{aligned} f(1) = a+b&&(1, 1)\\ f(2) = 2a+b&&(2, 3)\\ f(3) = 3a+b&&(3, 4)\\\,\\
\end{aligned}\\
(a+b-1)^2+(2a+b -3)^2+(3a+b-4)^2 = E $$
여기서 $E$를 최소화 시켜야 한다. 위를 $A\bold x = \bold b$의 형태의 norm으로 다시 쓸 수 있다.
$$ \left\Vert \begin{bmatrix} 1 & 1\\ 2 & 1\\ 3 & 1 \end{bmatrix} \begin{bmatrix} a\\\\b \end{bmatrix} - \begin{bmatrix} 1 \\ 3 \\ 4 \end{bmatrix} \right\Vert^2 = \Vert A\bold x - \bold b\Vert^2 = E $$
이때, $A\bold x\in \text{Col }A$이므로 $\text{Col } A$의 원소 중 $\bold b$와 거리가 최소인 것을 구하면 된다.

$\Vert A\bold x - \bold b\Vert$의 최소는 $\bold b$를 $\text{Col }A$에 정사영하면 구할 수 있다.
$$ \hat{\bold b} = \text{proj}_{\text{Col }A}\bold b = A\hat{\bold x} $$
Least-Square Solution $\hat{\bold x}$를 구해보자. $\text{Col A}$와 $\bold{b} - A\hat{\bold x}$는 직교해야 한다. 즉 $A$의 어떤 Column $a_j$와 $\bold{b} - A\hat{\bold x}$는 내적했을 때 0이 되어야 하므로 ${a_j}^T(\bold{b} - A\hat{\bold x}) = 0$이다. 따라서
$$ \begin{split}A^T(\bold{b} - A\hat{\bold x})&=0\\ A^T\bold b - A^TA\hat{\bold x}& = df\\ A^TA\hat{\bold x}& = A^T\bold b \end{split} $$
이를 통해 각각의 Least-Square Solution $\bold x$는 아래의 방정식을 만족하는 것을 알 수 있다.
$$ \textcolor{orange}{ \large{
A^T\bold x = A^T\bold b
}} $$
$$ \large{ \hat{\bold x} = (A^TA)^{-1}A^T\bold b } $$
위의 예제를 바탕으로 Matlab을 돌려본 결과
$$ \hat{\bold{x}} = \begin{bmatrix}1.50\\-0.33\end{bmatrix} $$