在实际应用中,由于数据不完整或噪声等原因,我们经常会遇到一些
不相容的方程组 。这种情况下,虽然没有精确解,但我们仍然需要找到一个近似解,使得方程的左右两边差距尽可能小。为了解决这个问题,我们引入
最小二乘法 ( least squares solution ) ~(\textbf{least squares solution})~ ( least squares solution ) ,它通过
最小化误差的平方和 来找到最优的近似解。
法方程
A T A x ^ = A T b ~\mathbf{A}^T \mathbf{A} \hat{\mathbf{x}} = \mathbf{A}^T \mathbf{b}~ A T A x ^ = A T b 是一个
n × n ~n\times n~ n × n 的线性方程组,如果
A T A ~\mathbf{A}^T\mathbf{A}~ A T A 可逆(即列向量线性无关),我们可以直接求解:
x = ( A T A ) − 1 A T b (4) \colorbox{#F0F8FF}{$\mathbf{x} = (\mathbf{A}^T \mathbf{A})^{-1} \mathbf{A}^T \mathbf{b}$}\tag{4} x = ( A T A ) − 1 A T b ( 4 ) 如果
A T A ~\mathbf{A}^T\mathbf{A}~ A T A 不可逆,则最小二乘解不唯一。这个结论可由下面定理给出:
当使用最小二乘解
x ^ ~\hat{\mathbf{x}}~ x ^ 计算
A x ^ ~\mathbf{A}\hat{\mathbf{x}}~ A x ^ 作为
b ~\mathbf{b}~ b 的近似时,
b ~\mathbf{b}~ b 与
A x ^ ~\mathbf{A}\hat{\mathbf{x}}~ A x ^ 的距离称为
最小二乘误差 ( least-squares error ) ~(\textbf{least-squares error})~ ( least-squares error ) 。数学上,这个误差由欧几里得范数(
ℓ 2 ~\ell_2~ ℓ 2 范数)表示,即:
∥ b − A x ^ ∥ \|\mathbf{b} - \mathbf{A}\hat{\mathbf{x}}\| ∥ b − A x ^ ∥ 借用前面
3.1 中的示例,我们来计算最小二乘解的最小二乘误差。已知:
b = [ 2 0 11 ] , A x ^ = [ 4 0 0 2 1 1 ] [ 1 2 ] = [ 4 4 3 ] \mathbf{b} = \begin{bmatrix} 2 \\ 0 \\ 11 \end{bmatrix} ,\quad \mathbf{A}\hat{\mathbf{x}} = \begin{bmatrix} 4 & 0 \\ 0 & 2 \\ 1 & 1 \end{bmatrix} \begin{bmatrix} 1 \\ 2 \end{bmatrix} = \begin{bmatrix} 4 \\ 4 \\ 3 \end{bmatrix} b = 2 0 11 , A x ^ = 4 0 1 0 2 1 [ 1 2 ] = 4 4 3 计算
r = b − A x ^ ~\mathbf{r} = \mathbf{b} - \mathbf{A}\hat{\mathbf{x}}~ r = b − A x ^ (
r \mathbf{r}~ r 也被称作
残差向量 ,
Residual Vector \textbf{Residual Vector} Residual Vector ):
b − A x ^ = [ 2 0 11 ] − [ 4 4 3 ] = [ − 2 − 4 8 ] \mathbf{b} - \mathbf{A}\hat{\mathbf{x}} = \begin{bmatrix} 2 \\ 0 \\ 11 \end{bmatrix} - \begin{bmatrix} 4 \\ 4 \\ 3 \end{bmatrix} = \begin{bmatrix} -2 \\ -4 \\ 8 \end{bmatrix} b − A x ^ = 2 0 11 − 4 4 3 = − 2 − 4 8 计算最小二乘误差:
∥ b − A x ^ ∥ = ( − 2 ) 2 + ( − 4 ) 2 + 8 2 = 4 + 16 + 64 = 84 \| \mathbf{b} - \mathbf{A}\hat{\mathbf{x}} \| = \sqrt{(-2)^2 + (-4)^2 + 8^2} = \sqrt{4 + 16 + 64} = \sqrt{84} ∥ b − A x ^ ∥ = ( − 2 ) 2 + ( − 4 ) 2 + 8 2 = 4 + 16 + 64 = 84 当矩阵
A ~\mathbf{A}~ A 的列正交时,求解最小二乘问题
A x = b ~\mathbf{A}\mathbf{x} = \mathbf{b}~ Ax = b 的过程会很简单。最小二乘解的核心思想是找到向量
b ~\mathbf{b}~ b 在矩阵
A ~\mathbf{A}~ A 的列空间
Col A ~\text{Col}\mathbf{A}~ Col A 上的正交投影
b ^ ~\hat{\mathbf{b}}~ b ^ ,即:
b ^ = proj Col A b \mathbf{\hat{b}} = \text{proj}_{\text{Col}\mathbf{A}} \mathbf{b} b ^ = proj Col A b 如果矩阵
A ~\mathbf{A}~ A 的列是正交的(即
a i ⋅ a j = 0 , i ≠ j ~\mathbf{a}_i\cdot \mathbf{a}_j = 0,~~i\neq j~ a i ⋅ a j = 0 , i = j ),那么直接应用用投影计算公式即可:
b ^ = ∑ i b ⋅ a i a i ⋅ a i a i \mathbf{\hat{b}} = \sum_i \frac{\mathbf{b} \cdot \mathbf{a}_i}{\mathbf{a}_i \cdot \mathbf{a}_i} \mathbf{a}_i b ^ = i ∑ a i ⋅ a i b ⋅ a i a i 下面的示例中
A ~\mathbf{A}~ A 的列向量正交,我们来计算最小二乘解问题。条件如下:
A = [ 1 − 6 1 − 2 1 1 1 7 ] , b = [ − 1 2 1 6 ] \mathbf{A} = \begin{bmatrix} 1 & -6 \\ 1 & -2 \\ 1 & 1 \\ 1 & 7 \end{bmatrix}, \quad \mathbf{b} = \begin{bmatrix} -1 \\ 2 \\ 1 \\ 6 \end{bmatrix} A = 1 1 1 1 − 6 − 2 1 7 , b = − 1 2 1 6 由于列向量
a 1 ~\mathbf{a}_1~ a 1 和
a 2 ~\mathbf{a}_2~ a 2 正交,最小二乘解可通过如下投影公式得到:
b ^ = c 1 a 1 + c 2 a 2 = b ⋅ a 1 a 1 ⋅ a 1 a 1 + b ⋅ a 2 a 2 ⋅ a 2 a 2 = 2 [ 1 1 1 1 ] + 45 90 [ − 6 − 2 1 7 ] = [ − 1 1 5 / 2 11 / 2 ] \begin{align*}\mathbf{\hat{b}} &= c_1\mathbf{a}_1 + c_2\mathbf{a}_2 \\[2ex] &= \frac{\mathbf{b} \cdot \mathbf{a_1}}{\mathbf{a_1} \cdot \mathbf{a_1}} \mathbf{a_1} + \frac{\mathbf{b} \cdot \mathbf{a_2}}{\mathbf{a_2} \cdot \mathbf{a_2}} \mathbf{a_2}\\[3ex] &= 2\begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} + \frac{45}{90}\begin{bmatrix} -6 \\ -2 \\ 1 \\ 7 \end{bmatrix} = \begin{bmatrix} -1 \\ 1 \\ 5/2 \\ 11/2 \end{bmatrix} \end{align*} b ^ = c 1 a 1 + c 2 a 2 = a 1 ⋅ a 1 b ⋅ a 1 a 1 + a 2 ⋅ a 2 b ⋅ a 2 a 2 = 2 1 1 1 1 + 90 45 − 6 − 2 1 7 = − 1 1 5/2 11/2 x ^ \hat{\mathbf{x}}~ x ^ 由系数
c 1 , c 2 ~c_1,~c_2~ c 1 , c 2 构成:
x ^ = [ c 1 c 2 ] = [ 2 1 / 2 ] \hat{\mathbf{x}} = \begin{bmatrix}c_1 \\ c_2 \end{bmatrix} = \begin{bmatrix}2 \\ 1/2\end{bmatrix} x ^ = [ c 1 c 2 ] = [ 2 1/2 ] 当矩阵
A ~\mathbf{A}~ A 的列向量线性无关时,使用 QR 分解来计算最小二乘解效率更高。相比于直接求解法方程
A T A x = A T b ~\mathbf{A}^T\mathbf{A}\mathbf{x} = \mathbf{A}^T\mathbf{b}~ A T Ax = A T b ,QR 分解可以避免求逆矩阵,更适用于大规模矩阵或
病态矩阵( ill-conditioned matrix ) (\textbf{ill-conditioned matrix}) ( ill-conditioned matrix ) 。