【统计学习】Linear Model with Least Squares

【统计学习】Linear Model with Least Squares

Basis Linear Model

对$\beta$求导
$X^T(y-X\beta)=0\\\hat{\beta}=(X^TX)^{-1}X^Ty$

Matrix Deduction

Alt text
Alt text
Alt text
Alt text
Alt text

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import numpy as np;
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt


#生成两个满足高斯分布的数据集
c=[]
x1=np.random.normal(1,10,100)
y1=np.random.normal(1,10,100)
x2=np.random.normal(12,5,100)
y2=np.random.normal(12,5,100)
for i in range(100):
c.append([x1[i],y1[i],0])
c.append([x2[i],y2[i],1])


#线性回归

#计算参数beta
c=(np.matrix(c))
y=c[:,[2]]
X=np.matrix(c[:,[0,1]])
bt=np.dot(X.T,X)
bt=np.linalg.pinv(bt)
bt=np.dot(bt,X.T)*y
print(bt)

#对样本的预测
haty=X*bt
print(haty)
plt.plot()

print(bt)

#绘制模拟分类边界
simulate_X=np.arange(-24,24,0.5)
S_X=[]
for x in simulate_X:
S_X.append([x,(0.5-x*bt[0,0])/bt[1,0]])
S_X=np.matrix(S_X)
plt.plot(S_X[:,0],S_X[:,1])
#plt.show()




plt.scatter(x1,y1)
plt.scatter(x2,y2)
plt.show()

Alt text

似然估计方式

首先定义 数据集因变量y、参数$\omega$、数据集自变量X


可以定义预测误差

并且误差$\epsilon$服从$\mu=0,\sigma^2$的高斯分布
也就是 $N((y_n-\langle x_n,\omega\rangle);0,\sigma^2)$
到这里可以写出似然函数

对数化

优化该函数

矩阵表示,也就是

Alt text

对$\omega$求偏导可得

于是

-------------End of this passage-------------