최소제곱법 - Method of Least Squares

김민욱·2025년 6월 9일

최소제곱법 - Method of Least Squares

근사적으로 구하려는 해와 실제 해의 오차의 제곱 합이 최소가 되는 해를 구하는 방법이다.

예를 들어,

{x+2y=33x+2y=5x+y=2.09⇔{x+2y=3−4y=−4−y=−0.91\begin{cases}x+2y=3\\3x+2y=5\\x+y=2.09\end{cases}\Lrarr\begin{cases}x+2y=3\\-4y=-4\\-y=-0.91\end{cases}

이 연립 방정식은 일관적이지 않은 계로 해가 없다.
이 때 해 (x0,y0)(x_0, y_0)가 존재하지만 계가 정확하지 않다고 가정해보자.
즉, 오류가 존재할 것이다.

(x0,y0)(x_0, y_0)의 근사치를 찾는 방법 중 하나가 최소제곱법(Method of Least Squares)이다.

연립 방정식
{a11x1+a12x2+⋯+a1nxn=b1a21x1+a22x2+⋯+a2nxn=b2⋯am1x1+am2x2+⋯+amnxn=bm⟺Ax=b\begin{cases}a_{11}x_1+a_{12}x_2+\cdots+a_{1n}x_n=b_1\\a_{21}x_1+a_{22}x_2+\cdots+a_{2n}x_n=b_2\\\cdots\\a_{m1}x_1+a_{m2}x_2+\cdots+a_{mn}x_n=b_m\end{cases}\Longleftrightarrow A\mathbf{x=b}

에서 x∈Rn\mathbf x\in \mathbb R^n의 잔차(residual)를 다음과 같이 정의한다.

r(x)=b−Axr(\mathbf x)=\mathbf b-A\mathbf x

계에 대한 최소제곱해(least squares solution) x\mathbf x는 ∣∣r(x)∣∣||r(\mathbf x)|| 또는 ∣∣r(x)∣∣2||r(\mathbf x)||^2를 최소화 하는 x\mathbf x이다.

∣∣r(x)∣∣2=∑i=1m(ai1x1+ai2x2+⋯+ainxn−bi)2||r(\mathbf x)||^2 = \sum_{i=1}^m(a_{i1}x_1+a_{i2}x_2+\cdots+a_{in}x_n-b_i)^2

여기서 벡터 x^\mathbf{\hat{x}}가 Ax=bA\mathbf{x=b}의 최소제곱해라면 x^\mathbf{\hat{x}}는 정규 방정식 ATAx=ATbA^TA\mathbf{x}=A^T\mathbf{b}의 해이다(필요충분조건).

따라서 정규 방정식 ATAx=ATbA^TA\mathbf x=A^T\mathbf b는 항상 일관적이다.

예제 풀이

예제 1.

다음 연립 방정식의 최소제곱해를 구하여라.
{x+2y=33x+2y=5x+y=2.09\begin{cases}x+2y=3\\3x+2y=5\\x+y=2.09\end{cases}

풀이.

{x+2y=33x+2y=5x+y=2.09⟺Ax=b⟺(1    23    21    1)(xy)=(352.09)\begin{cases}x+2y=3\\3x+2y=5\\x+y=2.09\end{cases}\Longleftrightarrow A\mathbf{x=b}\Longleftrightarrow \begin{pmatrix}1\;\;2\\3\;\;2\\1\;\;1\end{pmatrix}\begin{pmatrix}x\\y\end{pmatrix}=\begin{pmatrix}3\\5\\2.09\end{pmatrix}

ATAx=ATbA^TA\mathbf x=A^T\mathbf b임을 이용한다.

(1    3    12    2    1)(1    23    21    1)(xy)=(1    3    12    2    1)(352.09)\begin{pmatrix}1\;\;3\;\;1\\2\;\;2\;\;1\end{pmatrix}\begin{pmatrix}1\;\;2\\3\;\;2\\1\;\;1\end{pmatrix}\begin{pmatrix}x\\y\end{pmatrix}=\begin{pmatrix}1\;\;3\;\;1\\2\;\;2\;\;1\end{pmatrix}\begin{pmatrix}3\\5\\2.09\end{pmatrix}

(11    9 9      9)(xy)=(20.0918.09)⟺{x=1y=1.01\begin{pmatrix}11\;\;9\\\ 9\;\;\;9\end{pmatrix}\begin{pmatrix}x\\y\end{pmatrix}=\begin{pmatrix}20.09\\18.09\end{pmatrix}\Longleftrightarrow \begin{cases}x=1\\y=1.01\end{cases}

예제 2.

다음 데이터에 최소제곱이 맞는
ⅰ) 상수 함수를 구하여라.
ⅱ) 일차 함수를 구하여라.
ⅲ) 이차 함수를 구하여라.

  x  | 0 | 1 | 2 | 3
---------------------
f(x) | 1 | 0 | 1 | 2

풀이 2-1.

f(x)=c⇒{c=1c=0c=1c=2⇒(1111)(c)=(1012)f(x) = c \Rightarrow \begin{cases}c=1\\c=0\\c=1\\c=2\end{cases}\Rightarrow \begin{pmatrix}1\\1\\1\\1\end{pmatrix}(c)=\begin{pmatrix}1\\0\\1\\2\end{pmatrix}

ATAx=ATbA^TA\mathbf x=A^T\mathbf b임을 이용한다.

(1  1  1  1)(1111)(c)=(1  1  1  1)(1012)⇔4c=4⇔c=1\begin{pmatrix}1\;1\;1\;1\end{pmatrix}\begin{pmatrix}1\\1\\1\\1\end{pmatrix}(c)=\begin{pmatrix}1\;1\;1\;1\end{pmatrix}\begin{pmatrix}1\\0\\1\\2\end{pmatrix} \Lrarr4c=4\Lrarr c=1

∴f(x)=1\therefore f(x)=1

풀이 2-2.

f(x)=c1+c2x⇒{c1=1c1+c2=0c1+2c2=1c1+3c2=2⇒(1    01    11    21    3)(c1c2)=(1012)f(x) = c_1+c_2x \Rightarrow \begin{cases}c_1=1\\c_1+c_2=0\\c_1+2c_2=1\\c_1+3c_2=2\end{cases}\Rightarrow \begin{pmatrix}1\;\;0\\1\;\;1\\1\;\;2\\1\;\;3\end{pmatrix}\begin{pmatrix}c_1\\c_2\end{pmatrix}=\begin{pmatrix}1\\0\\1\\2\end{pmatrix}

(1  1  1  10  1  2  3)(1    01    11    21    3)(c1c2)=(1  1  1  10  1  2  3)(1012)\begin{pmatrix}1\;1\;1\;1\\0\;1\;2\;3\end{pmatrix}\begin{pmatrix}1\;\;0\\1\;\;1\\1\;\;2\\1\;\;3\end{pmatrix}\begin{pmatrix}c_1\\c_2\end{pmatrix}=\begin{pmatrix}1\;1\;1\;1\\0\;1\;2\;3\end{pmatrix}\begin{pmatrix}1\\0\\1\\2\end{pmatrix}

(4      6  6    14)(c1c2)=(48)⇔{c1=0.4c2=0.4\begin{pmatrix}4\;\;\;6\\\;6\;\;14\end{pmatrix}\begin{pmatrix}c_1\\c_2\end{pmatrix}=\begin{pmatrix}4\\8\end{pmatrix}\Lrarr \begin{cases}c_1=0.4\\c_2=0.4\end{cases}

∴f(x)=0.4+0.4x\therefore f(x)=0.4+0.4x

풀이 2-3.

f(x)=c1+c2x+c3x2⇒{c1=1c1+c2+c3=0c1+2c2+4c3=1c1+3c2+9c3=2⇒(1    0    01    1    11    2    41    3    9)(c1c2c3)=(1012)f(x) = c_1+c_2x+c_3x^2 \Rightarrow \begin{cases}c_1=1\\c_1+c_2+c_3=0\\c_1+2c_2+4c_3=1\\c_1+3c_2+9c_3=2\end{cases}\Rightarrow \begin{pmatrix}1\;\;0\;\;0\\1\;\;1\;\;1\\1\;\;2\;\;4\\1\;\;3\;\;9\end{pmatrix}\begin{pmatrix}c_1\\c_2\\c_3\end{pmatrix}=\begin{pmatrix}1\\0\\1\\2\end{pmatrix}

(1  1  1  10  1  2  30  1  4  9)(1    0    01    1    11    2    41    3    9)(c1c2)=(1  1  1  10  1  2  30  1  4  9)(1012)\begin{pmatrix}1\;1\;1\;1\\0\;1\;2\;3\\0\;1\;4\;9\end{pmatrix}\begin{pmatrix}1\;\;0\;\;0\\1\;\;1\;\;1\\1\;\;2\;\;4\\1\;\;3\;\;9\end{pmatrix}\begin{pmatrix}c_1\\c_2\end{pmatrix}=\begin{pmatrix}1\;1\;1\;1\\0\;1\;2\;3\\0\;1\;4\;9\end{pmatrix}\begin{pmatrix}1\\0\\1\\2\end{pmatrix}

(4      6      14  6    14      3614    36    98)(c1c2c3)=(4822)⇔{c1=0.9c2=−1.1c3=0.5\begin{pmatrix}4\;\;\;6\;\;\;14\\\;6\;\;14\;\;\;36\\14\;\;36\;\;98\end{pmatrix}\begin{pmatrix}c_1\\c_2\\c_3\end{pmatrix}=\begin{pmatrix}4\\8\\22\end{pmatrix}\Lrarr \begin{cases}c_1=0.9\\c_2=-1.1\\c_3=0.5\end{cases}

∴f(x)=0.9−1.1x+0.5x2\therefore f(x)=0.9-1.1x+0.5x^2


<참고자료>
Steve J. Leon, Linear Algebra with Application, 10th edition, 2021.

0개의 댓글