25 lines
709 B
Python
25 lines
709 B
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
# Define the Gaussian distribution
|
|
mu, sigma = 0, 5 # High variance
|
|
gaussian = np.random.normal(mu, sigma, 1000000) # Generate a lot of samples
|
|
|
|
# Rejection sampling
|
|
gaussian = gaussian[(gaussian > -1) & (gaussian < 1)]
|
|
|
|
# Generate uniform distribution
|
|
uniform = np.random.uniform(-1, 1, len(gaussian)) # Same number of samples as the Gaussian
|
|
|
|
# Plot the distributions
|
|
plt.figure(figsize=(12,6))
|
|
|
|
plt.subplot(1, 2, 1)
|
|
plt.hist(gaussian, bins=30, density=True, alpha=0.6, color='g')
|
|
plt.title('Gaussian Rejection Sampling')
|
|
|
|
plt.subplot(1, 2, 2)
|
|
plt.hist(uniform, bins=30, density=True, alpha=0.6, color='b')
|
|
plt.title('Uniform Distribution')
|
|
|
|
plt.show() |