Lab 01

Special Continuous Distributions

Author

Gül İnan

Published

December 22, 2022

Normal Distribution

# Multiple curves in one plot
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats

#------------------------------------------------------------
# control the parameter settings
mu_values = [-2, 0, 2]
linestyles = ['-', '--', '-.']
colors = ['black', 'red', 'blue']

#------------------------------------------------------------
# plot the distributions
fig, ax = plt.subplots(figsize=(10, 7))

for m, ls, cl in zip(mu_values, linestyles, colors):
    x = stats.norm.rvs(loc = m, scale = 1, size = 10000)
    x1 = np.sort(x)
    plt.plot(x1, stats.norm.pdf(x1, loc = m, scale = 1), ls=ls, c=cl,
             label=r'$\mu=%.1f,\ \sigma^2=%.1f$' % (m,1))

plt.xlim(-5, 5)
plt.ylim(0, 0.5)

plt.xlabel('$x$')
plt.ylabel(r'$f(x|\mu)$')
plt.title('Normal Distribution (mu changes, sigma fixed)')
plt.legend(loc=1)
plt.show()

# Multiple curves in one plot
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats

#------------------------------------------------------------
# control the parameter settings
# takes std as input argument, not variance
sigma_values = [1, 2, 3]
linestyles = ['-', '--', '-.']
colors = ['black', 'red', 'blue']

#------------------------------------------------------------
# plot the distributions
fig, ax = plt.subplots(figsize=(7, 7))

for s, ls, cl in zip(sigma_values, linestyles, colors):
    x = stats.norm.rvs(loc = 0, scale = s, size = 10000)
    x1 = np.sort(x)
    plt.plot(x1, stats.norm.pdf(x1, loc = 0, scale = s), ls=ls, c=cl,
             label=r'$\mu=%.1f,\ \sigma^2=%.1f$' % (0,s*s))

plt.xlim(-20, 20)
plt.ylim(0, 0.5)

plt.xlabel('$x$')
plt.ylabel(r'$f(x|\sigma^2)$')
plt.title('Normal Distribution (mu fixed, sigma2 changes)')
plt.legend(loc=0)
plt.show()