# Python code to generate the PDF and CDF of a uniform random variable
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
# generate a data set of size 1000 consisting of values between -0.1 and 1.1
= np.linspace(-0.1,1.1,1000)
x #print(x)
# fit a uniform dist. on it, assuming that if 0 < x < 1, x is a uniform rand., otherwise it is not.
# calculate the uniform pdf values under these circumstances
= stats.uniform.pdf(x, 0, 1)
y #print(y)
Lab 01
Special Continuous Distributions
Uniform Distribution
# Plot the pdf
plt.plot(x, y) "PDF of Uniform rvs")
plt.title("x")
plt.xlabel("f(x)")
plt.ylabel( plt.show()
# Plot the CDF
= stats.uniform.cdf(x, 0, 1)
cum_prob
plt.plot(x, cum_prob) "CDF of Uniform rvs")
plt.title("x")
plt.xlabel("F(x)")
plt.ylabel( plt.show()
# Example X~Unif(0, 10)
= stats.uniform.cdf(3, 0, 10)
cum_prob1 print(cum_prob1)
= 1-stats.uniform.cdf(6, 0, 10)
cum_prob2 print(cum_prob2)
= stats.uniform.cdf(8, 0, 10)-stats.uniform.cdf(3, 0, 10)
cum_prob3 print(cum_prob3)
0.3
0.4
0.5
Exponential Distribution
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.expon.html
# Multiple curves in one plot
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
#------------------------------------------------------------
# control the parameter settings
= [2, 3, 4]
lambda_values = ['-', '--', '-.']
linestyles = ['black', 'red', 'blue']
colors
#------------------------------------------------------------
# plot the distributions
= plt.subplots(figsize=(7, 7))
fig, ax
for l, ls, cl in zip(lambda_values, linestyles, colors):
= stats.gamma.rvs(a = 1, scale = 1/l, size = 10000)
x = np.sort(x)
x1 = 1, scale = 1/l), ls=ls, c=cl,
plt.plot(x1, stats.gamma.pdf(x1, a =r'$\lambda=%.2f$' % (l))
label
0, 5)
plt.xlim(0, 4.5)
plt.ylim(
'$x$')
plt.xlabel(r'$f(x|\lambda)$')
plt.ylabel('Exponential Distribution')
plt.title(=0)
plt.legend(loc plt.show()
1/(l**2) for l in lambda_values] ##variance decreases (tails get shorter as variance decreases) [
[0.25, 0.1111111111111111, 0.0625]
# Gamma Distribution
# Python code to generate the PDF and CDF of a gamma random variable
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
# generate a data set of size 1000 for different values of shape and scale parameters
# determine shape parameter
= 3
alpha1 # determine alpha
= 3
lambda1 # determine scale parameter
= 1/lambda1
scale1 # mean = alpha * scale
# variance = alpha * scale^2
= stats.gamma.rvs(a = alpha1, scale = scale1, size = 10000)
x #print(x)
= np.sort(x)
x1 #print(x1)
= np.mean(x1)
mean_x = np.var(x1)
var_x print(mean_x, var_x)
0.9978994138880635 0.33920053823966356
# calculate the gamma pdf values under these circumstances
= stats.gamma.pdf(x1, a = alpha1, scale = scale1)
y #print(y)
# Plot the pdf
plt.plot(x1, y) "PDF of gamma rvs")
plt.title( plt.show()
Multiple curves in one plot
# Multiple curves in one plot
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
#------------------------------------------------------------
# control the parameter settings
= [1/2, 1/2, 1/2]
lambda_values = [2, 3, 4]
alpha_values = ['-', '--', '-.']
linestyles = ['black', 'red', 'blue']
colors
#------------------------------------------------------------
# plot the distributions
= plt.subplots(figsize=(7, 7))
fig, ax
for k, l, ls, cl in zip(alpha_values, lambda_values, linestyles, colors):
= stats.gamma.rvs(a = k, scale = 1/l, size = 10000)
x = np.sort(x)
x1 = k, scale = 1/l), ls=ls, c=cl,
plt.plot(x1, stats.gamma.pdf(x1, a =r'$\lambda=%.2f,\ \alpha=%.1f$' % (l, k))
label
0, 70)
plt.xlim(0, 0.2)
plt.ylim(
'$x$')
plt.xlabel(r'$f(x|\lambda, \alpha)$')
plt.ylabel('Gamma Distribution')
plt.title(=0)
plt.legend(loc plt.show()