python以不同的概率从不同的分布中抽样

标签 python numpy probability sampling

我正在尝试实现一个从三个不同的多元高斯分布返回 100 个样本的函数。

numpy 提供了一种从单一多元高斯样本中采样的方法。但是我找不到从三个具有不同采样概率的不同多元变量中采样的方法。

我的要求是以 $[0.7, 0.2, 0.1]$ 的概率从三个均值和协方差均值和协方差均值如下的多元高斯分布中采样

G_1  mean = [1,1] cov =[ [ 5, 1] [1,5]]
G_2  mean = [0,0] cov =[ [ 5, 1] [1,5]]
G_3  mean = [-1,-1] cov =[ [ 5, 1] [1,5]]

有什么想法吗?

最佳答案

假设您创建了一个生成器数组:

generators = [
    np.random.multivariate_normal([1, 1], [[5, 1], [1, 5]]),             
    np.random.multivariate_normal([0, 0], [[5, 1], [1, 5]]), 
    np.random.multivariate_normal([-1, -1], [[5, 1], [1, 5]])]

现在您可以创建生成器索引的加权随机数,因为 np.random.choice支持加权采样:

draw = np.random.choice([0, 1, 2], 100, p=[0.7, 0.2, 0.1])

(draw 是一个长度为 100 的条目数组,每个条目来自 {0, 1, 2},概率为 0.7, 0.2, 0.1,分别。)

现在只生成样本:

[generators[i] for i in draw]

关于python以不同的概率从不同的分布中抽样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39677967/

相关文章:

python - 如何使基本的倒排索引程序更像pythonic

python - 根据梯度符号为数据分配颜色 (Python)

python - 使用Python从视频中识别车牌

python - numpy.array 的数据是如何存储的?

python - 将 int 索引设置为 10 位而不是 11 位的 Django 迁移外键

python - 使用来自 excel (XLRD) 的数据使用 matplotlib 绘制图形

python - 用盐加密/解密python中的数据

python - 用于选择具有某些相关概率的 N 个值的代码

algorithm - 编写一个 C++ 程序,在给定 rand1() 的情况下生成 0 到 5 之间的随机分布,随机返回 0 或 1

python - SciPy - 子类化 rv_continuous 时出现 'Object too deep for desired array'