机器学习笔记之高斯分布——使用极大似然估计计算最优参数
目录
本节将介绍一个在统计机器学习中占据重要地位的分布——高斯分布。
高斯分布介绍
高斯分布( Gaussian Distribution \text{Gaussian Distribution} Gaussian Distribution),也称正态分布( Normal Distribution \text{Normal Distribution} Normal Distribution)。该分布在数学、物理及工程等领域都占据重要地位的分布。从随机变量 X \mathcal X X的维度进行划分,可以将高斯分布分为一维正态分布和多元正态分布。
一维正态分布
假设一维随机变量 X X X服从一个数学期望为 μ \mu μ,方差为 σ 2 \sigma^2 σ2的高斯分布,将其记作:
X ∼ N ( μ , σ 2 ) \mathcal X \sim \mathcal N(\mu,\sigma^2) X∼N(μ,σ2)
对应高斯分布的概率密度函数( Probability Density Function,PDF \text{Probability Density Function,PDF} Probability Density Function,PDF)表示如下:
f ( x ) = 1 2 π σ exp [ − ( x − μ ) 2 2 σ 2 ] f(x) = \frac{1}{\sqrt{2\pi}\sigma}\exp \left[-\frac{(x - \mu)^2}{2\sigma^2}\right] f(x)=2πσ1exp[−2σ2(x−μ)2]
其中( μ , σ \mu,\sigma μ,σ(标准差))为正态分布 N ( μ , σ 2 ) \mathcal N(\mu,\sigma^2) N(μ,σ2)的充分统计量(若某一数据集合确定服从于高斯分布,只需要知道该分布的样本均值和方差 → \to → 可以从该分布中生成任意一个样本)。
充分统计量在'指数族分布'中详细介绍。
数学期望 μ \mu μ决定了概率密度函数的有效位置;方差 σ 2 \sigma^2 σ2决定了概率密度函数的有效范围。我们称 μ = 0 , σ = 1 \mu=0,\sigma=1 μ=0,σ=1的分布为标准正态分布( Standard Normal Distribution \text{Standard Normal Distribution} Standard Normal Distribution)。具体代码如下:
import math
import matplotlib.pyplot as plt
def norm(x,mu,sigma):
return (1 / (sigma * math.sqrt(2 * math.pi))) * math.exp(-1 * (((x - mu) ** 2) / (sigma ** 2)))
if __name__ == '__main__':
x = np.linspace(-10,10,500)
y = [norm(i,0,1) for i in x]
y_1 = [norm(i,0,4.0) for i in x]
y_2 = [norm(i,-2,4.0) for i in x]
plt.plot(x,y)
plt.plot(x,y_1)
plt.plot(x,y_2)
plt.show()
返回结果如下:
多维正态分布
多元正态分布是一维正态分布向多维的推广。其充分统计量是期望和协方差矩阵,分别记作: μ , Σ \mu,\Sigma μ,Σ。如果 Σ \Sigma Σ是 非奇异矩阵,多元正态分布的概率密度函数表示如下:
f x ( x 1 , x 2 , . . . , x p ) = 1 ( 2 π ) p ∣ Σ ∣ exp [ − 1 2 ( x − μ ) ⊤ Σ − 1 ( x − μ ) ] f_{\mathbf x}(x_1,x_2,...,x_p) = \frac{1}{\sqrt{(2\pi)^p|\Sigma|}}\exp \left[-\frac{1}{2}(\mathbf x - \mu)^{\top}\Sigma^{-1}(\mathbf x - \mu) \right] fx(x1,x2,...,xp)=(2π)p∣Σ∣1exp[−21(x−μ)⊤Σ−1(x−μ)]
其中:
x ∈ X \mathbf x \in \mathcal X x∈X,是数据集合 X \mathcal X X的一个样本,共包含 p p p个维度: ( x 1 , x 2 , . . . , x p ) (x_1,x_2,...,x_p) (x1,x2,...,xp)。 ∣ Σ ∣ |\Sigma| ∣Σ∣表示协方差矩阵 Σ \Sigma Σ的行列式结果。具体代码如下:
from scipy.stats import multivariate_normal
def draw_pic(mu,sigma):
x = np.linspace(-2,2,50)
y = np.linspace(-2,2,50)
X,Y = np.meshgrid(x,y)
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y
Gaussians_l = []
Gaussians_l.append(multivariate_normal(mu,sigma))
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Gaussians_l[0].pdf(pos), cmap='viridis', linewidth=0)
plt.show()
if __name__ == '__main__':
mu = [0, 0]
sigma = [[0.5, 0], [0, 0.5]]
draw_pic(mu,sigma)
返回图片结果:
回顾:数据集合与概率模型
上一节介绍到,数据集合是由 大量重复试验 产生的样本所组成的集合。并介绍了向量维度是 描述样本特征的一组值的数量。
现已知一个数据集合 X \mathcal X X,它共包含 N N N个样本,每个样本包含 p p p个维度,使用数学符号表示该数据集合如下:
X = ( x ( 1 ) x ( 2 ) x ( 3 ) ⋮ x ( N ) ) = ( x 1 ( 1 ) , x 2 ( 1 ) , ⋯ , x p ( 1 ) x 1 ( 2 ) , x 2 ( 2 ) , ⋯ , x p ( 2 ) x 1 ( 3 ) , x 2 ( 3 ) , ⋯ , x p ( 3 ) ⋮ x 1 ( N ) , x 2 ( N ) , ⋯ , x p ( N ) ) N × p \mathcal X = \begin{pmatrix} x^{(1)} \\ x^{(2)} \\ x^{(3)}\\ \vdots \\ x^{(N)} \end{pmatrix} = \begin{pmatrix} x_1^{(1)},x_2^{(1)},\cdots,x_p^{(1)} \\ x_1^{(2)},x_2^{(2)},\cdots,x_p^{(2)} \\ x_1^{(3)},x_2^{(3)},\cdots,x_p^{(3)} \\ \vdots \\ x_1^{(N)},x_2^{(N)},\cdots,x_p^{(N)} \end{pmatrix}_{N\times p} X=
x(1)x(2)x(3)⋮x(N)
=
x1(1),x2(1),⋯,xp(1)x1(2),x2(2),⋯,xp(2)x1(3),x2