如何在给定概率的情况下从两个高斯变量中取样并将其组合(伯努利)

2024-10-04 07:33:16 发布

您现在位置:Python中文网/ 问答频道 /正文

给定两个不同的高斯,如何创建两个高斯的混合,其中每个数据点来自第一个高斯,概率p=0.3,来自第二个高斯,概率0.7(使用概率的伯努利分布)

这是我的开始:

import numpy as np
mu1, sigma1 = 1, 2 # mean and standard deviation
g1 = np.random.normal(mu1, sigma1, 1000)

mu2, sigma2 = 4, 8 # mean and standard deviation 2
g2 = np.random.normal(mu2, sigma2, 1000)

p = .3  # probability
b = np.random.binomial(1, p, 1000)

# How to combine these to sample from mixture?

# Combining gaussians would be:
combined_gaussians = np.random.normal(mu1+mu2, np.sqrt(sigma1^2 + sigma2^2))

# In this case, can we just do a linear combination of the samples?:
combined_samples = b*g1+(1-b)g2


Tags: andtonprandommean概率standardnormal