(一)连续随机数的生成-从混合高斯分布中采样

从混合高斯分布中采样

  • 1. 从混合高斯分布中采样
  • 2. Python编程
    • 2.1. 伪代码
    • 2.2. Python编程实现

1. 从混合高斯分布中采样

Example 1: Mixture of Gaussian distribution

Let X 1 ∼ N ( μ 1 , σ 1 2 ) , X 2 ∼ N ( μ 2 , σ 2 2 ) X_1 \sim N\left(\mu_1, \sigma_1^2\right), X_2 \sim N\left(\mu_2, \sigma_2^2\right) X1N(μ1,σ12),X2N(μ2,σ22) with σ 1 > 0 \sigma_1>0 σ1>0 and σ 2 > 0 \sigma_2>0 σ2>0, and let X 1 X_1 X1 and X 2 X_2 X2 be independent. Let Θ ∼ Ber ⁡ ( p ) \Theta \sim \operatorname{Ber}(p) ΘBer(p) with p ∈ ( 0 , 1 ) p \in(0,1) p(0,1) and let Θ \Theta Θ be independent of X 1 X_1 X1 and X 2 X_2 X2. Then, ( Θ ) X 1 + ( 1 − Θ ) X 2 (\Theta) X_1+(1-\Theta) X_2 (Θ)X1+(1Θ)X2 is a simplest Gaussian mixture model.\

Assignment: Θ X 1 + ( 1 − Θ ) X 2 \Theta X_1+(1-\Theta) X_2 ΘX1+(1Θ)X2 has a probability density
p 2 π σ 1 exp ⁡ ( − ( x − μ 1 ) 2 2 σ 1 2 ) + 1 − p 2 π σ 2 exp ⁡ ( − ( x − μ 2 ) 2 2 σ 2 2 ) .  \frac{p}{\sqrt{2 \pi} \sigma_1} \exp \left(-\frac{\left(x-\mu_1\right)^2}{2 \sigma_1^2}\right)+\frac{1-p}{\sqrt{2 \pi} \sigma_2} \exp \left(-\frac{\left(x-\mu_2\right)^2}{2 \sigma_2^2}\right) \text {. } 2π σ1pexp(2σ12(xμ1)2)+2π σ21pexp(2σ22(xμ2)2)
(Hint: P ( Θ X 1 + ( 1 − Θ ) X 2 ⩽ x ) = P ( X 1 ⩽ x ∣ Θ = 1 ) P ( Θ = 1 ) + P ( X 2 ⩽ x ∣ Θ = 0 ) P ( Θ = 0 ) P\left(\Theta X_1+(1-\Theta) X_2\leqslant x\right)=P\left(X_1 \leqslant x \mid \Theta=1\right) P(\Theta=1)+P\left(X_2 \leqslant x| \Theta=0\right) P(\Theta=0) P(ΘX1+(1Θ)X2x)=P(X1xΘ=1)P(Θ=1)+P(X2x∣Θ=0)P(Θ=0) and the independence of Θ \Theta Θ of X 1 X_1 X1 and X 2 X_2 X2.)

2. Python编程

2.1. 伪代码

We now aim to sample a random quantity from the distribution of the above Gaussian Mixture Model.
Pseudo-code

  • Sample a random quantity Θ ∼ Ber ⁡ ( p ) \Theta \sim \operatorname{Ber}(p) ΘBer(p)
    • If Θ = 1 \Theta=1 Θ=1, sample a random quantity X ∼ N ( μ 1 , σ 1 2 ) X\sim N\left(\mu_1, \sigma_1^2\right) XN(μ1,σ12),
    • If Θ = 0 \Theta=0 Θ=0, sample a random quantity X ∼ N ( μ 2 , σ 2 2 ) X\sim N\left(\mu_2, \sigma_2{ }^2\right) XN(μ2,σ22).
  • Return Y = X Y=X Y=X.

2.2. Python编程实现

Assignment: Realize the above psendocode by programming to create 1000 random quantities, and draw a histogram. (Hint: To draw a random quantity X X X from N ( μ , σ 2 ) N\left(\mu, \sigma^2\right) N(μ,σ2), first draw a random quantity Z ∼ N ( 0 , 1 ) Z\sim N(0,1) ZN(0,1) and take X = μ + σ Z X=\mu+\sigma Z X=μ+σZ.)\

import numpy as np
import matplotlib.pyplot as plt
# Parameters for the mixture distribution
p = 0.7
mu1, sigma1 = 5, 2
mu2, sigma2 = 10, 3

# Generate random quantities
theta = np.random.choice([0, 1], size=1000, p=[1 - p, p])
X = np.zeros(1000)

for i in range(1000):
    if theta[i] == 1:
        X[i] = np.random.normal(mu1, sigma1)
    else:
        X[i] = np.random.normal(mu2, sigma2)

# Plot histogram
plt.hist(X, bins=30, density=True, alpha=0.6, color='g', label='Sampled Data')
plt.xlabel('Value')
plt.ylabel('Density')
plt.title('Histogram of Mixture of Gaussian Distribution')
plt.legend()
plt.grid(True)
plt.show()

你可能感兴趣的:(数据科学和Python实现,python)