import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as ss
def plot_gaussian_and_exponential(lmbda):
mean = 1/lmbda # MEAN = 0.5
sigma = (1/lmbda) # VAR = 0.25
x = np.linspace(-2, 4, num=100)
ex = ss.expon(scale= (1/lmbda))
norm = ss.norm(mean, sigma)
ex_y = ex.pdf(x)
norm_y = norm.pdf(x)
plt.figure(figsize=(12,8))
plt.plot(x, ex_y, label="exponential")
plt.plot(x, norm_y, label = "normal")
plt.legend()
plot_gaussian_and_exponential(lmbda= 2)
def generate_uniform_random_numbers(count):
return np.random.uniform(0, 1, count)
def invCDFtoNorm(nums, mu, sigma):
norm = ss.norm(mu, sigma)
normal_dist = norm.ppf(nums)
x = np.linspace(-12, 12, 100)
y = norm.pdf(x)
plt.figure(figsize=(12,8))
plt.hist(normal_dist, bins = 500, density= True, label="Mapping from Inverse CDF")
plt.plot(x, y, label="PDF")
plt.legend()
mu = 0
sigma = 3
invCDFtoNorm(generate_uniform_random_numbers(100000), mu, sigma)
def invCDFtoRayleigh(nums, sigma):
rayleigh = ss.rayleigh()
rayleigh_dist = rayleigh.ppf(nums)
x = np.linspace(0, 5, 100)
y = rayleigh.pdf(x)
plt.figure(figsize=(12,8))
plt.hist(rayleigh_dist, bins = 500, density= True,label="Mapping from Inverse CDF")
plt.plot(x, y, label= "PDF")
plt.legend()
sigma = 1
invCDFtoRayleigh(generate_uniform_random_numbers(100000), sigma)
def invCDFtoExponential(nums, lmbda):
expo = ss.expon(scale = (1/lmbda))
expo_dist = expo.ppf(nums)
x = np.linspace(0, 8, 100)
y = expo.pdf(x)
plt.figure(figsize=(12,8))
plt.hist(expo_dist, bins = 500, density= True,label="Mapping from Inverse CDF")
plt.plot(x, y, label="PDF")
plt.legend()
lmbda = 1.5
invCDFtoExponential(generate_uniform_random_numbers(100000), lmbda)
def generate_random_sum():
s=0;
arr = np.random.uniform(0,1, 500)
s = sum(arr)
return s
def generate_bar_plot():
randm = []
for i in range(0, 50000):
randm.append(generate_random_sum())
norm = ss.norm(250, 6.5)
x = np.linspace(220, 270, 100)
y = norm.pdf(x)
plt.figure(figsize=(12,8))
plt.hist(randm, bins = 500, density= True)
plt.plot(x, y, label="PDF of Normal Distribution")
plt.legend()
generate_bar_plot()