# -*- coding: utf-8 -*- """ Created on Thu Feb 3 18:47:48 2022 @author: Rene """ import pandas as pd import numpy as np import matplotlib.pyplot as plt from scipy.stats import expon from scipy.stats import gamma the_lambda = 0.5 x = np.arange(0,101)/100 LB_x = 0 UB_x = 12 x = (UB_x-LB_x)*x+LB_x pdf_exp = expon.pdf(x,scale = 1/the_lambda) cdf_exp = expon.cdf(x,scale = 1/the_lambda) pdf_gamma = gamma.pdf(x,a = 2,scale = 1/the_lambda) cdf_gamma = gamma.cdf(x,a = 2,scale = 1/the_lambda) # Plotting a four panel figure plt.rc('font', family='serif', size='10') plt.figure(figsize=(10, 5)) Figure = plt.figure(0) Figure.set_figwidth(9) Figure.set_figheight(4.75) Figure.subplots_adjust(hspace=0.5, wspace=0.25) # Adding a panel to the figure Panel = Figure.add_subplot(2,2,1) LB_x = 0 UB_x = 10 off_set_x = 0.025*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = 0 UB_y = max(pdf_exp) off_set_y = 0.05*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) the_x = x the_y = pdf_exp plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('PDF $f(x)$') title_str = r'PDF : $X \sim Exp(0.5)$' plt.title(title_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(the_x,the_y, color='blue', lw=1) x_values = [2,2] y_values = [0,expon.pdf(2,scale = 1/the_lambda)] plt.plot(x_values,y_values, color='black', lw=1, ls=':') # Adding a panel to the figure Panel = Figure.add_subplot(2,2,2) LB_x = 0 UB_x = 10 off_set_x = 0.025*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = 0 UB_y = max(pdf_exp) off_set_y = 0.05*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) the_x = x the_y = pdf_gamma plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('PDF $f(x)$') title_str = r'PDF : $X \sim Gamma(2,0.5)$' plt.title(title_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(the_x,the_y, color='red', lw=1) x_values = [4,4] y_values = [0,gamma.pdf(4,a = 2,scale = 1/the_lambda)] plt.plot(x_values,y_values, color='black', lw=1, ls=':') # Adding a panel to the figure Panel = Figure.add_subplot(2,2,3) LB_x = 0 UB_x = 10 off_set_x = 0.025*(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.05*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) the_x = x the_y = cdf_exp plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('CDF $F(x) = Pr(X \leq x)$') title_str = r'CDF : $X \sim Exp(0.5)$' plt.title(title_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=2, ls = '--',color = 'lightgray',alpha=0.5) plt.plot(the_x,the_y, color='blue', lw=1) x_values = [2,2] y_values = [0,expon.cdf(2,scale = 1/the_lambda)] plt.plot(x_values,y_values, color='black', lw=1, ls=':') x_values = [0,2] y_values = [expon.cdf(2,scale = 1/the_lambda),expon.cdf(2,scale = 1/the_lambda)] plt.plot(x_values,y_values, color='black', lw=1, ls=':') text_str = f'{expon.cdf(2,scale = 1/the_lambda):4.3f}' plt.text(0.25,expon.cdf(2,scale = 1/the_lambda),text_str,color = 'black', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') # Adding a panel to the figure Panel = Figure.add_subplot(2,2,4) LB_x = 0 UB_x = 10 off_set_x = 0.025*(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.05*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) the_x = x the_y = cdf_gamma plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('CDF $F(x) = Pr(X \leq x)$') title_str = r'CDF : $X \sim Gamma(2,0.5)$' plt.title(title_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(the_x,the_y, color='red', lw=1) x_values = [4,4] y_values = [0,gamma.cdf(4,a = 2,scale = 1/the_lambda)] plt.plot(x_values,y_values, color='black', lw=1, ls=':') x_values = [0,4] y_values = [gamma.cdf(4,a = 2,scale = 1/the_lambda),gamma.cdf(4,a = 2,scale = 1/the_lambda)] plt.plot(x_values,y_values, color='black', lw=1, ls=':') text_str = f'{gamma.cdf(4,a = 2,scale = 1/the_lambda):4.3f}' plt.text(0.25,gamma.cdf(4,a = 2,scale = 1/the_lambda),text_str,color = 'black', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') plt.savefig('EXP_GAMMA_PMF_CDF.png', dpi=300)