高斯分布可视化

高斯分布

基本概念

服从一元高斯分布的一组样本 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= x1x2xp
假设有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= x11x21xn1x12x22xn2x1px2pxnp
如何去表示这样的样本矩阵?使用多维高斯分布!!

均值参数表示成:
μ = [ μ 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)

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KPer_Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值