高斯分布
基本概念
服从一元高斯分布的一组样本
X
=
(
x
1
,
x
2
,
…
,
x
n
)
X=(x_1,x_2,\dots,x_n)
X=(x1,x2,…,xn),其中每一个样本都是一个随机数值。但是在多元高斯分布中,每一个样本不是一个数值,而是多维的随机向量,每一个样本都是p维:
x
=
[
x
1
x
2
⋮
x
p
]
\pmb{x}=\begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_p \end{bmatrix}
xx=⎣
⎡x1x2⋮xp⎦
⎤
假设有n个样本,每一个都是p维,那么可以表示成:
X
=
[
x
11
x
12
⋯
x
1
p
x
21
x
22
⋯
x
2
p
⋮
⋮
x
n
1
x
n
2
⋯
x
n
p
]
\pmb{X}=\begin{bmatrix} x_{11} & x_{12} & \cdots & x_{1p} \\ x_{21} & x_{22} & \cdots & x_{2p} \\ \vdots & & & \vdots \\ x_{n1} & x_{n2} & \cdots & x_{np} \end{bmatrix}
XX=⎣
⎡x11x21⋮xn1x12x22xn2⋯⋯⋯x1px2p⋮xnp⎦
⎤
如何去表示这样的样本矩阵?使用多维高斯分布!!
均值参数表示成:
μ
=
[
μ
1
μ
2
⋮
μ
p
]
\pmb{\mu}=\begin{bmatrix} \mu_1 \\ \mu_2 \\ \vdots \\ \mu_p \end{bmatrix}
μμ=⎣
⎡μ1μ2⋮μp⎦
⎤
多维高斯分布的方差参数表示成协方差矩阵(协方差矩阵表示里面不同随机变量的相关性):
Σ
=
[
σ
11
σ
12
⋯
σ
1
p
σ
21
σ
22
⋯
σ
2
p
⋮
⋮
σ
n
1
x
n
2
⋯
σ
p
p
]
\pmb{\Sigma}=\begin{bmatrix} \sigma_{11} & \sigma_{12} & \cdots & \sigma_{1p} \\ \sigma_{21} & \sigma_{22} & \cdots & \sigma_{2p} \\ \vdots & & & \vdots \\ \sigma_{n1} & x_{n2} & \cdots & \sigma_{pp} \end{bmatrix}
ΣΣ=⎣
⎡σ11σ21⋮σn1σ12σ22xn2⋯⋯⋯σ1pσ2p⋮σpp⎦
⎤
概率密度函数:
p
(
x
∣
θ
)
=
1
(
2
π
)
p
2
∣
Σ
∣
1
2
e
x
p
[
−
1
2
(
x
−
μ
)
T
Σ
−
1
(
x
−
μ
)
]
p(x|\theta)=\frac{1}{(2\pi)^{\frac{p}{2}}|\Sigma|^{\frac{1}{2}}}exp[-\frac{1}{2}(x-\mu)^T\Sigma^{-1}(x-\mu)]
p(x∣θ)=(2π)2p∣Σ∣211exp[−21(x−μ)TΣ−1(x−μ)]
二元高斯分布的实例:
可以看到,不同协方差矩阵和不同均值的高斯分布在形状上是不一样的。而且呈椭圆分布,越接近椭圆中心,样本密度越大;反之越稀疏。而且,协方差矩阵的特征值与椭圆的长短轴的长度有关系,而协方差矩阵的特征向量和椭圆的长短轴的方向有关:
# -*- coding: utf-8 -*-
# @Use :
# @Time : 2022/8/24 19:22
# @FileName: Gaussian.py
# @Software: PyCharm
import numpy as np
import matplotlib.pyplot as plt
mean_1 = np.array([0, 0])
conv_1 = np.array([[1, 0],
[0, 1]])
mean_2 = np.array([0, -5])
conv_2 = np.array([[4, 0],
[0, 0.25]])
mean_3 = np.array([4, 4])
conv_3 = np.array([[4, -3],
[-3, 0.25]])
x_1, y_1 = np.random.multivariate_normal(mean=mean_1, cov=conv_1, size=2000).T
x_2, y_2 = np.random.multivariate_normal(mean=mean_2, cov=conv_2, size=2000).T
x_3, y_3 = np.random.multivariate_normal(mean=mean_3, cov=conv_3, size=2000).T
plt.plot(x_1, y_1, 'ro', alpha=0.05)
plt.plot(x_2, y_2, 'bo', alpha=0.05)
plt.plot(x_3, y_3, 'go', alpha=0.05)
plt.gca().axes.set_xlim(-10, 10)
plt.gca().axes.set_ylim(-10, 10)
plt.grid(ls='--')
plt.show()
#求协方差矩阵的特征值和特征向量
evalue. evevtor = linalg.eig(conv1)
evalue. evevtor = linalg.eig(conv2)
evalue. evevtor = linalg.eig(conv3)