2023-07-12 11:52:06 +00:00
|
|
|
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
|
2023-07-12 13:00:13 +00:00
|
|
|
plt.figure(figsize=(12, 6))
|
2023-07-12 11:52:06 +00:00
|
|
|
|
|
|
|
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()
|