Skip to content

第十六讲:投影矩阵和最小二乘

上一讲中,我们知道了投影矩阵P=A(ATA)1ATPb将会把向量投影在A的列空间中。

举两个极端的例子:

  • 如果bC(A),则Pb=b
  • 如果bC(A),则Pb=0

一般情况下,b将会有一个垂直于A的分量,有一个在A列空间中的分量,投影的作用就是去掉垂直分量而保留列空间中的分量。

在第一个极端情况中,如果bC(A)则有b=Ax。带入投影矩阵p=Pb=A(ATA)1ATAx=Ax,得证。

在第二个极端情况中,如果bC(A)则有bN(AT),即ATb=0。则p=Pb=A(ATA)1ATb=0,得证。

向量b投影后,有b=e+p,p=Pb,e=(IP)b,这里的pbC(A)中的分量,而ebN(AT)中的分量。

回到上一讲最后提到的例题:

我们需要找到距离图中三个点 (1,1),(2,2),(3,2) 偏差最小的直线:y=C+Dt

python
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn import linear_model
import numpy as np
import pandas as pd
import seaborn as sns

x = np.array([1, 2, 3]).reshape((-1,1))
y = np.array([1, 2, 2]).reshape((-1,1))
predict_line = np.array([-1, 4]).reshape((-1,1))

regr = linear_model.LinearRegression()
regr.fit(x, y)
ey = regr.predict(x)

fig = plt.figure()
plt.axis('equal')
plt.axhline(y=0, c='black')
plt.axvline(x=0, c='black')

plt.scatter(x, y, c='r')
plt.scatter(x, regr.predict(x), s=20, c='b')
plt.plot(predict_line, regr.predict(predict_line), c='g', lw='1')
[ plt.plot([x[i], x[i]], [y[i], ey[i]], 'r', lw='1') for i in range(len(x))]

plt.annotate('(1, 1)', xy=(1, 1), xytext=(-15, -30), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.annotate('(2, 2)', xy=(2, 2), xytext=(-60, -5), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.annotate('(3, 2)', xy=(3, 2), xytext=(-15, -30), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))

plt.annotate('$e_1$', color='r', xy=(1, 1), xytext=(0, 2), textcoords='offset points', size=20)
plt.annotate('$e_2$', color='r', xy=(2, 2), xytext=(0, -15), textcoords='offset points', size=20)
plt.annotate('$e_3$', color='r', xy=(3, 2), xytext=(0, 1), textcoords='offset points', size=20)

plt.annotate('$p_1$', xy=(1, 7/6), color='b', xytext=(-7, 30), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.annotate('$p_2$', xy=(2, 5/3), color='b', xytext=(-7, -30), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.annotate('$p_3$', xy=(3, 13/6), color='b', xytext=(-7, 30), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.draw()
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn import linear_model
import numpy as np
import pandas as pd
import seaborn as sns

x = np.array([1, 2, 3]).reshape((-1,1))
y = np.array([1, 2, 2]).reshape((-1,1))
predict_line = np.array([-1, 4]).reshape((-1,1))

regr = linear_model.LinearRegression()
regr.fit(x, y)
ey = regr.predict(x)

fig = plt.figure()
plt.axis('equal')
plt.axhline(y=0, c='black')
plt.axvline(x=0, c='black')

plt.scatter(x, y, c='r')
plt.scatter(x, regr.predict(x), s=20, c='b')
plt.plot(predict_line, regr.predict(predict_line), c='g', lw='1')
[ plt.plot([x[i], x[i]], [y[i], ey[i]], 'r', lw='1') for i in range(len(x))]

plt.annotate('(1, 1)', xy=(1, 1), xytext=(-15, -30), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.annotate('(2, 2)', xy=(2, 2), xytext=(-60, -5), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.annotate('(3, 2)', xy=(3, 2), xytext=(-15, -30), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))

plt.annotate('$e_1$', color='r', xy=(1, 1), xytext=(0, 2), textcoords='offset points', size=20)
plt.annotate('$e_2$', color='r', xy=(2, 2), xytext=(0, -15), textcoords='offset points', size=20)
plt.annotate('$e_3$', color='r', xy=(3, 2), xytext=(0, 1), textcoords='offset points', size=20)

plt.annotate('$p_1$', xy=(1, 7/6), color='b', xytext=(-7, 30), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.annotate('$p_2$', xy=(2, 5/3), color='b', xytext=(-7, -30), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.annotate('$p_3$', xy=(3, 13/6), color='b', xytext=(-7, 30), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))
plt.draw()

png

python
plt.close(fig)
plt.close(fig)

根据条件可以得到方程组 {C+D=1C+2D=2C+3D=2,写作矩阵形式 [111213][CD]=[122],也就是我们的Ax=b,很明显方程组无解。

我们需要在b的三个分量上都增加某个误差e,使得三点能够共线,同时使得e12+e22+e32最小,找到拥有最小平方和的解(即最小二乘),即Axb2=e2最小。此时向量b变为向量p=[p1p2p3](在方程组有解的情况下,Axb=0,即bA的列空间中,误差e为零。)我们现在做的运算也称作线性回归(linear regression),使用误差的平方和作为测量总误差的标准。

注:如果有另一个点,如(0,100),在本例中该点明显距离别的点很远,最小二乘将很容易被离群的点影响,通常使用最小二乘时会去掉明显离群的点。

现在我们尝试解出x^=[C^D^]p=[p1p2p3]

ATAx^=ATbATA=[36614]ATb=[511][36614][C^D^]=[511]

写作方程形式为{3C^+16D^=56C^+14D^=11,也称作正规方程组(normal equations)。

回顾前面提到的“使得误差最小”的条件,e12+e22+e32=(C+D1)2+(C+2D2)2+(C+3D2)2,使该式取最小值,如果使用微积分方法,则需要对该式的两个变量C,D分别求偏导数,再令求得的偏导式为零即可,正是我们刚才求得的正规方程组。(正规方程组中的第一个方程是对C求偏导的结果,第二个方程式对D求偏导的结果,无论使用哪一种方法都会得到这个方程组。)

解方程得C^=23,D^=12,则“最佳直线”为y=23+12t,带回原方程组解得p1=76,p2=53,p3=136,即e1=16,e2=13,e3=16

于是我们得到p=[7653136],e=[161316],易看出b=p+e,同时我们发现pe=0pe

误差向量e不仅垂直于投影向量p,它同时垂直于列空间,如 [111],[123]

接下来我们观察ATA,如果A的各列线性无关,求证ATA是可逆矩阵。

先假设ATAx=0,两边同时乘以xTxTATAx=0,即(Ax)T(Ax)=0。一个矩阵乘其转置结果为零,则这个矩阵也必须为零((Ax)T(Ax)相当于Ax长度的平方)。则Ax=0,结合题设中的“A的各列线性无关”,可知x=0,也就是ATA的零空间中有且只有零向量,得证。

我们再来看一种线性无关的特殊情况:互相垂直的单位向量一定是线性无关的。

  • 比如[100][010][001],这三个正交单位向量也称作标准正交向量组(orthonormal vectors)。
  • 另一个例子[cosθsinθ][sinθcosθ]

下一讲研究标准正交向量组。

本站没有备案,因为不需要备案