import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import uniform
from scipy.stats import norm
%matplotlib inline
# Gaussian Distribution
x = np.random.normal(5, 1, 10000)
# Law of law numbers -- This mean will be equal to the expected value when the number of samples is large
# Central Limit Theorem -- this mean is Gaussian Distributed.
print('Expected Value of x = {} with variance = {}'.format(np.mean(x), np.var(x)))
plt.hist(x)
plt.title('Samples drawn from a Gaussian Distribution')
plt.show()
Expected Value of x = 4.99984824615 with variance = 0.999953050944
# Uniform Distribution
x = np.random.rand(10000)
# Law of law numbers -- This mean will be equal to the expected value when the number of samples is large
# Central Limit Theorem -- This mean is Gaussian Distributed.
print('Expected Value of x = {} with variance = {}'.format(np.mean(x), np.var(x)))
plt.hist(x)
plt.title('Samples drawn from a Uniform Distribution')
plt.show()
Expected Value of x = 0.503530395661 with variance = 0.0839557828656
This method can be described in 2 steps as --
CAVEAT - We can only use this method when the inverse CDF is defined.
Graph of the inversion technique from ${\displaystyle x}$ to ${\displaystyle F(x)}$. On the bottom right we see the regular function and in the top left its inversion. [Image and Caption from Wikipedia]
# SIDE NOTE FOR ME -- `scipy.stats.rv_continuous` is the base class.
# Sample u from a uniform distribution
u = np.random.rand(10000)
# Sample x from the inverse CDF function
# In Scipy it's called ppf
samples = norm.ppf(u, 0, 1)
plt.hist(samples)
plt.title('Samples drawn from a Gaussian Distribution')
plt.show()
When the inverse CDF function is not defined, but the PDF is still available then we can use the accept reject method.
# CODE -- TODO