# -*- coding: utf-8 -*- """ Created on Wed Mar 4 18:10:08 2020 @author: Johan Rene van Dorp """ import numpy as np import matplotlib.pyplot as plt from scipy.stats import uniform from scipy.stats import expon from scipy.stats import norm a = 0 b = 1/3 unif_x = np.linspace(-1,2,1000,endpoint=True) unif_cdf = uniform.cdf(unif_x,a,b) lb = 0 ub = 21 the_lambda = 0.25 exp_x = np.linspace(lb,ub,1000,endpoint=True) exp_pdf = expon.pdf(exp_x, loc = 0, scale = 1/the_lambda) exp_cdf = expon.cdf(exp_x, loc = 0, scale = 1/the_lambda) lb= -4 ub = 10 the_mu = 3 the_sigma = 2.5 norm_x = np.linspace(lb,ub,1000,endpoint=True) norm_pdf = norm.pdf(norm_x, loc = the_mu, scale = the_sigma) norm_cdf = norm.cdf(norm_x, loc = the_mu, scale = the_sigma) # Plotting the Uniform PDF and CDF plt.rc('font', family='serif', size='12') Figure = plt.figure(1) Figure.set_figwidth(9) Figure.set_figheight(5) Figure.subplots_adjust(left=0.1, bottom=0.1, right=0.95, top=0.85, hspace=0.1, wspace=0.25) #*************************************************************************************** # Adding the left panel with the theoretical pmf LB_x = -0.5 UB_x = 1 off_set_x = 0.05*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = 0 UB_y = 3.0 off_set_y = 0.1*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) Panel = Figure.add_subplot(1,2,1) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('Probability Density Function $f(x)$') text_str = r'PDF - Uniform $U[0,\frac{1}{3}]$' plt.title(text_str) plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) x_values = [-1, a] y_values = [0, 0] plt.plot(x_values, y_values,color='red') x_values = [a, b] y_values = [1/(b-a), 1/(b-a)] plt.plot(x_values, y_values,color='red') x_values = [b, 2] y_values = [0, 0] plt.plot(x_values, y_values,color='red') x_values = [0, 0] y_values = [0, 1/(b-a)] plt.plot(x_values, y_values,color='black',linestyle=':') x_values = [b, b] y_values = [0, 1/(b-a)] plt.plot(x_values, y_values,color='black',linestyle=':') text_str = r'$\frac{1}{3}$' plt.text(b+0.05,0.25,text_str,horizontalalignment = 'left',verticalalignment = 'center') # Adding the right panel with the empirical pmf LB_x = -0.5 UB_x = 1 off_set_x = 0.05*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = 0 UB_y = 1 off_set_y = 0.1*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) Panel = Figure.add_subplot(1,2,2) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('Cumulative Distribution Function $F(x)$') text_str = r'CDF - Uniform $U[0,\frac{1}{3}]$' plt.title(text_str) plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(1, lw=1, ls = '--',color = 'darkgray',alpha=0.5) plt.plot(unif_x,unif_cdf,color='red') x_values = [b, b] y_values = [0, 1] plt.plot(x_values, y_values,color='black',linestyle=':') text_str = r'$\frac{1}{3}$' plt.text(b+0.05,0.25/(1/(b-a)),text_str,horizontalalignment = 'left',verticalalignment = 'center') plt.savefig('Uniform_Example.png', dpi=300) #Plotting the Exponential PDF and CDF plt.rc('font', family='serif', size='12') Figure = plt.figure(2) Figure.set_figwidth(9) Figure.set_figheight(5) Figure.subplots_adjust(left=0.1, bottom=0.1, right=0.95, top=0.85, hspace=0.1, wspace=0.25) #*************************************************************************************** # Adding the left panel with the theoretical pmf LB_x = -5 UB_x = 20 off_set_x = 0.05*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = 0 UB_y = 0.25 off_set_y = 0.1*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) Panel = Figure.add_subplot(1,2,1) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('Probability Density Function $f(x)$') text_str = r'PDF - $Exp(\lambda)$, $\lambda = \frac{1}{4}$' plt.title(text_str) plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) x_values = [-6, 0] y_values = [0, 0] plt.plot(x_values, y_values,color='red') plt.plot(exp_x, exp_pdf,color='red') x_values = [0, 0] y_values = [0, the_lambda] plt.plot(x_values, y_values,color='black',linestyle=':') # Adding the right panel with the empirical pmf LB_x = -5 UB_x = 20 off_set_x = 0.05*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = 0 UB_y = 1 off_set_y = 0.1*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) Panel = Figure.add_subplot(1,2,2) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('Cumulative Distribution Function $F(x)$') text_str = r'CDF - $Exp(\lambda)$, $\lambda = \frac{1}{4}$' plt.title(text_str) plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.plot(exp_x,exp_cdf,color='red') plt.axhline(1, lw=1, ls = '--',color = 'darkgray',alpha=0.5) x_values = [-6, 0] y_values = [0, 0] plt.plot(x_values, y_values,color='red') plt.savefig('Exponential_Example.png', dpi=300) #Plotting the Normal PDF and CDF plt.rc('font', family='serif', size='12') Figure = plt.figure(3) Figure.set_figwidth(9) Figure.set_figheight(5) Figure.subplots_adjust(left=0.1, bottom=0.1, right=0.95, top=0.85, hspace=0.1, wspace=0.25) #*************************************************************************************** # Adding the left panel with the normal pdf LB_x = -3 UB_x = 9 off_set_x = 0.05*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = 0 UB_y = 0.2 off_set_y = 0.1*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) Panel = Figure.add_subplot(1,2,1) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('Probability Density Function $f(x)$') text_str = r'PDF - $N(\mu,\sigma)$, $\mu =3, \sigma = 2.5$' plt.title(text_str) plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.plot(norm_x, norm_pdf,color='red') x_values = [the_mu, the_mu] y_values = [0, norm.pdf(the_mu, loc = the_mu, scale = the_sigma)] plt.plot(x_values, y_values,color='black',ls=':') text_str = r'3' plt.text(the_mu+0.1,0.01,text_str,horizontalalignment = 'left',verticalalignment = 'center') # Adding the right panel with the normal cdf LB_x = -3 UB_x = 9 off_set_x = 0.05*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = 0 UB_y = 1 off_set_y = 0.1*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) Panel = Figure.add_subplot(1,2,2) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('Cumulative Distribution Function $F(x)$') text_str = r'PDF - $N(\mu,\sigma)$, $\mu =3, \sigma = 2.5$' plt.title(text_str) plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.plot(norm_x,norm_cdf,color='red') plt.axhline(1, lw=1, ls = '--',color = 'darkgray',alpha=0.5) x_values = [the_mu, the_mu] y_values = [0, 1] plt.plot(x_values, y_values,color='black',ls=':') text_str = r'3' plt.text(the_mu+0.1,0.01/0.20,text_str,horizontalalignment = 'left',verticalalignment = 'center') plt.savefig('Normal_Example.png', dpi=300)