gpt4 book ai didi

python - 如何使用 NumPy/SciPy 进行简单的高斯混合采样和 PDF 绘图?

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:44 26 4
gpt4 key购买 nike

我添加三个正态分布得到如下图所示的新分布,在python中如何根据这个分布进行采样?

import matplotlib.pyplot as plt
import scipy.stats as ss
import numpy as np


x = np.linspace(0, 10, 1000)
y1 = [ss.norm.pdf(v, loc=5, scale=1) for v in x]
y2 = [ss.norm.pdf(v, loc=1, scale=1.3) for v in x]
y3 = [ss.norm.pdf(v, loc=9, scale=1.3) for v in x]
y = np.sum([y1, y2, y3], axis=0)/3

plt.plot(x, y, '-')
plt.xlabel('$x$')
plt.ylabel('$P(x)$')

顺便说一句,有没有更好的方法来绘制这样的概率分布?

最佳答案

您似乎在问两个问题:如何从分布中采样以及如何绘制 PDF?

假设您正在尝试从代码中显示的 3 个正态分布的混合分布中进行采样,以下代码片段以朴素、直接的方式执行这种采样作为概念验证。

基本上,这个想法是

  1. 根据组件的概率权重,在组件索引中选择一个索引i,即0, 1, 2 ...
  2. 选择i后,选择相应的分布并从中获取样本点。
  3. 从 1 继续,直到收集到足够的样本点。

但是,要绘制 PDF,在这种情况下您实际上不需要样本,因为理论上的解决方案非常简单。在更一般的情况下,PDF 可以通过样本的直方图来近似。

下面的代码使用理论 PDF 执行采样和 PDF 绘图。​​

import numpy as np
import numpy.random
import scipy.stats as ss
import matplotlib.pyplot as plt

# Set-up.
n = 10000
numpy.random.seed(0x5eed)
# Parameters of the mixture components
norm_params = np.array([[5, 1],
[1, 1.3],
[9, 1.3]])
n_components = norm_params.shape[0]
# Weight of each component, in this case all of them are 1/3
weights = np.ones(n_components, dtype=np.float64) / 3.0
# A stream of indices from which to choose the component
mixture_idx = numpy.random.choice(len(weights), size=n, replace=True, p=weights)
# y is the mixture sample
y = numpy.fromiter((ss.norm.rvs(*(norm_params[i])) for i in mixture_idx),
dtype=np.float64)

# Theoretical PDF plotting -- generate the x and y plotting positions
xs = np.linspace(y.min(), y.max(), 200)
ys = np.zeros_like(xs)

for (l, s), w in zip(norm_params, weights):
ys += ss.norm.pdf(xs, loc=l, scale=s) * w

plt.plot(xs, ys)
plt.hist(y, normed=True, bins="fd")
plt.xlabel("x")
plt.ylabel("f(x)")
plt.show()

Overlaid image of two PDFs

关于python - 如何使用 NumPy/SciPy 进行简单的高斯混合采样和 PDF 绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49106806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com