Skip to content

第十五讲:子空间投影

R2空间讲起,有向量a,b,做ba上的投影p,如图:

python
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

plt.style.use("seaborn-dark-palette")

fig = plt.figure()
plt.axis('equal')
plt.axis([-7, 7, -6, 6])
plt.arrow(-4, -1, 8, 2, head_width=0.3, head_length=0.5, color='r', length_includes_head=True)
plt.arrow(0, 0, 2, 4, head_width=0.3, head_length=0.5, color='b', length_includes_head=True)
plt.arrow(0, 0, 48/17, 12/17, head_width=0.3, head_length=0.5, color='gray', length_includes_head=True)
plt.arrow(48/17, 12/17, 2-48/17, 4-12/17, head_width=0.3, head_length=0.5, color='g', length_includes_head=True)
# plt.plot([48/17], [12/17], 'o')
# y=1/4x
# y=-4x+12
# x=48/17
# y=12/17
plt.annotate('b', xy=(1, 2), xytext=(-30, 15), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.annotate('a', xy=(-1, -0.25), xytext=(15, -30), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.annotate('e=b-p', xy=(2.5, 2), xytext=(30, 0), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.annotate('p=xa', xy=(2, 0.5), xytext=(-20, -40), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.grid()
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

plt.style.use("seaborn-dark-palette")

fig = plt.figure()
plt.axis('equal')
plt.axis([-7, 7, -6, 6])
plt.arrow(-4, -1, 8, 2, head_width=0.3, head_length=0.5, color='r', length_includes_head=True)
plt.arrow(0, 0, 2, 4, head_width=0.3, head_length=0.5, color='b', length_includes_head=True)
plt.arrow(0, 0, 48/17, 12/17, head_width=0.3, head_length=0.5, color='gray', length_includes_head=True)
plt.arrow(48/17, 12/17, 2-48/17, 4-12/17, head_width=0.3, head_length=0.5, color='g', length_includes_head=True)
# plt.plot([48/17], [12/17], 'o')
# y=1/4x
# y=-4x+12
# x=48/17
# y=12/17
plt.annotate('b', xy=(1, 2), xytext=(-30, 15), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.annotate('a', xy=(-1, -0.25), xytext=(15, -30), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.annotate('e=b-p', xy=(2.5, 2), xytext=(30, 0), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.annotate('p=xa', xy=(2, 0.5), xytext=(-20, -40), textcoords='offset points', size=20, arrowprops=dict(arrowstyle="->"))
plt.grid()

png

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

从图中我们知道,向量e就像是向量b,p之间的误差,e=bp,eppa上,有p=ax

所以有aTe=aT(bp)=aT(bax)=0。关于正交的最重要的方程:

aT(bxa)=0xaTa=aTbx=aTbaTap=aaTbaTa

从上面的式子可以看出,如果将b变为2bp也会翻倍,如果将a变为2ap不变。

设投影矩阵为P,则可以说投影矩阵作用与某个向量后,得到其投影向量,projectionp=Pb

易看出P=aaTaTa,若an维列向量,则P是一个n×n矩阵。

观察投影矩阵P的列空间,C(P)是一条通过a的直线,而rank(P)=1(一列乘以一行:aaT,而这一列向量a是该矩阵的基)。

投影矩阵的性质:

  • P=PT,投影矩阵是一个对称矩阵。
  • 如果对一个向量做两次投影,即PPb,则其结果仍然与Pb相同,也就是P2=P

为什么我们需要投影?因为就像上一讲中提到的,有些时候Ax=b无解,我们只能求出最接近的那个解。

Ax总是在A的列空间中,而b却不一定,这是问题所在,所以我们可以将b变为A的列空间中最接近的那个向量,即将无解的Ax=b变为求有解的Ax^=ppbA的列空间中的投影,x^不再是那个不存在的x,而是最接近的解)。

现在来看R3中的情形,将向量b投影在平面A上。同样的,p是向量b在平面A上的投影,e是垂直于平面A的向量,即b在平面A法方向的分量。 设平面A的一组基为a1,a2,则投影向量p=x1^a1+x2^a2,我们更倾向于写作p=Ax^,这里如果我们求出x^,则该解就是无解方程组最近似的解。

现在问题的关键在于找e=bAx^,使它垂直于平面,因此我们得到两个方程 {a1T(bAx^)=0a2T(bAx^)=0,将方程组写成矩阵形式 [a1Ta2T](bAx^)=[00],即AT(bAx^)=0

比较该方程与R2中的投影方程,发现只是向量a变为矩阵A而已,本质上就是ATe=0。所以,eAT的零空间中(eN(AT)),从前面几讲我们知道,左零空间列空间,则有eC(A),与我们设想的一致。

再化简方程得ATAx=ATb,比较在R2中的情形,aTa是一个数字而ATA是一个n阶方阵,而解出的x可以看做两个数字的比值。现在在R3中,我们需要再次考虑:什么是x^?投影是什么?投影矩阵又是什么?

  • 第一个问题:x^=(ATA)1ATb
  • 第二个问题:p=Ax^=A(ATA)1ATb,回忆在R2中的情形,下划线部分就是原来的aaTaTa
  • 第三个问题:易看出投影矩阵就是下划线部分P=A(ATA)1AT

这里还需要注意一个问题,P=A(ATA)1AT是不能继续化简为P=AA1(AT)1AT=I的,因为这里的A并不是一个可逆方阵。 也可以换一种思路,如果A是一个n阶可逆方阵,则A的列空间是整个Rn空间,于是bRn上的投影矩阵确实变为了I,因为b已经在空间中了,其投影不再改变。

再来看投影矩阵P的性质:

  • P=PT:有 [A(ATA)1AT]T=A[(ATA)1]TAT,而(ATA)是对称的,所以其逆也是对称的,所以有A((ATA)1)TAT=A(ATA)1AT,得证。
  • P2=P:有 [A(ATA)1AT][A(ATA)1AT]=A(ATA)1[(ATA)(ATA)1]AT=A(ATA)1AT,得证。

最小二乘法

接下看看投影的经典应用案例:最小二乘法拟合直线(least squares fitting by a line)。

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

python
plt.style.use("seaborn-dark-palette")

fig = plt.figure()
plt.axis('equal')
plt.axis([-1, 4, -1, 3])
plt.axhline(y=0, c='black', lw='2')
plt.axvline(x=0, c='black', lw='2')

plt.plot(1, 1, 'o', c='r')
plt.plot(2, 2, 'o', c='r')
plt.plot(3, 2, 'o', c='r')

plt.annotate('(1, 1)', xy=(1, 1), xytext=(-40, 20), 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=(-18, 20), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))

plt.grid()
plt.style.use("seaborn-dark-palette")

fig = plt.figure()
plt.axis('equal')
plt.axis([-1, 4, -1, 3])
plt.axhline(y=0, c='black', lw='2')
plt.axvline(x=0, c='black', lw='2')

plt.plot(1, 1, 'o', c='r')
plt.plot(2, 2, 'o', c='r')
plt.plot(3, 2, 'o', c='r')

plt.annotate('(1, 1)', xy=(1, 1), xytext=(-40, 20), 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=(-18, 20), textcoords='offset points', size=14, arrowprops=dict(arrowstyle="->"))

plt.grid()

png

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

根据条件可以得到方程组 {C+D=1C+2D=2C+3D=2,写作矩阵形式 [111213][CD]=[122],也就是我们的Ax=b,很明显方程组无解。但是ATAx^=ATb有解,于是我们将原是两边同时乘以AT后得到的新方程组是有解的,ATAx^=ATb也是最小二乘法的核心方程。

下一讲将进行最小二乘法的验算。

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