# -*- coding: utf-8 -*- """ Created on Thu Feb 3 18:47:48 2022 @author: Rene """ import numpy as np import matplotlib.pyplot as plt from scipy.stats import gamma from scipy.stats import norm the_lambda = 0.5 n = [1,2] #n = [3,4] #n = [16,25] #n = [30,50] #n = [100,1000] file_name = 'Central_Limit_Example_1.png' the_colors=['blue','red'] x = np.arange(0,1001)/1000 LB_x = -5 UB_x = 10 x = (UB_x-LB_x)*x+LB_x pdf_gamma_1 = gamma.pdf(x,a = n[0],scale = 1/(n[0]*the_lambda)) pdf_gamma_2 = gamma.pdf(x,a = n[1],scale = 1/(n[1]*the_lambda)) mu_1 = n[0]/(n[0]*the_lambda) sigma_1 = (n[0]/(n[0]*the_lambda)**2)**0.5 a = 1/sigma_1 b = -mu_1/sigma_1 x_std_1 = a*x+b pdf_std_1 = pdf_gamma_1/a mu_2 = n[1]/(n[1]*the_lambda) sigma_2 = (n[1]/(n[1]*the_lambda)**2)**0.5 a = 1/sigma_2 b = -mu_2/sigma_2 x_std_2 = a*x+b pdf_std_2 = pdf_gamma_2/a z = np.arange(0,1001)/1000 LB_z = -4 UB_z = 4 z = (UB_z-LB_z)*z+LB_z pdf_normal = norm.pdf(z,0,1) # 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 = 4 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 = 2 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_1 plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('PDF $f(x)$', size = 8) title_str = r'PDF of average of '+ f'{n[0]:2.0f}'+' RV\'s'+' : $X \sim Exp(0.5)$' plt.title(title_str, size = 10) 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=the_colors[0], lw=1) x_values = [2,2] y_values = [0,gamma.pdf(2,a = n[0],scale = 1/(n[0]*the_lambda))] plt.plot(x_values,y_values, color='black', lw=1, ls=':') text_str = r'pdf of $\overline{X}_n$' plt.text(0.1,1.7,text_str,color = 'black', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') text_str = r'mean = '+ f'{1/the_lambda:2.1f}' plt.text(0.1,1.4,text_str,color = 'red', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') st_dev = (((1/the_lambda)**2)/n[0])**0.5 text_str = r'st.dev. = '+ f'{st_dev:2.1f}' plt.text(0.1,1.1,text_str,color = 'red', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') text_str = r'$n = $'+ f'{n[0]:2.0f}' plt.text(4,1.7,text_str,color = 'red', size = 8,horizontalalignment = 'right', verticalalignment = 'bottom') # Adding a panel to the figure Panel = Figure.add_subplot(2,2,2) LB_x = -3 UB_x = 3 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_std_1 the_y = pdf_std_1 plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('PDF $f(x)$', size = 8) title_str = r'PDF of Standardized average of '+ f'{n[0]:2.0f}'+' RV\'s' plt.title(title_str, size = 10) 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=the_colors[1], lw=1) plt.plot(z,pdf_normal, color='black', lw=1, ls='--') text_str = r'$N(0,1)$' plt.text(1,0.4,text_str,color = 'black', size = 8,horizontalalignment = 'left', verticalalignment = 'top') text_str = r'pdf of $Z = (\overline{X}_n -\mu)/\sigma$' plt.text(-3,0.85,text_str,color = 'black', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') text_str = r'mean = '+ f'{0:2.0f}' plt.text(-3,0.70,text_str,color = 'red', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') text_str = r'st.dev. = '+ f'{1:2.0f}' plt.text(-3,0.55,text_str,color = 'red', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') text_str = r'$n = $'+ f'{n[0]:2.0f}' plt.text(3,0.85,text_str,color = 'red', size = 8,horizontalalignment = 'right', verticalalignment = 'bottom') # Adding a panel to the figure Panel = Figure.add_subplot(2,2,3) LB_x = 0 UB_x = 4 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 = 2 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_2 plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('PDF $f(x)$', size = 8) title_str = r'PDF of average of '+ f'{n[1]:2.0f}'+' RV\'s'+' : $X \sim Exp(0.5)$' plt.title(title_str, size = 10) 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=the_colors[0], lw=1) x_values = [2,2] y_values = [0,gamma.pdf(2,a = n[1],scale = 1/(n[1]*the_lambda))] plt.plot(x_values,y_values, color='black', lw=1, ls=':') text_str = r'n = '+ f'{n[0]:2.0f}' plt.text(0.1,1.8,text_str,color = 'red', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') text_str = r'mean = '+ f'{1/the_lambda:2.1f}' plt.text(0.1,1.6,text_str,color = 'red', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') st_dev = (((1/the_lambda)**2)/n[1])**0.5 text_str = r'st.dev. = '+ f'{st_dev:2.2f}' plt.text(0.1,1.4,text_str,color = 'red', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') text_str = r'$n = $'+ f'{n[1]:2.0f}' plt.text(4,1.7,text_str,color = 'red', size = 8,horizontalalignment = 'right', verticalalignment = 'bottom') # Adding a panel to the figure Panel = Figure.add_subplot(2,2,4) LB_x = -3 UB_x = 3 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_std_2 the_y = pdf_std_2 plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('PDF $f(x)$', size = 8) title_str = r'PDF of Standardized average of '+ f'{n[1]:2.0f}'+' RV\'s' plt.title(title_str, size = 10) 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=the_colors[1], lw=1) plt.plot(z,pdf_normal, color='black', lw=1, ls='--') text_str = r'$N(0,1)$' plt.text(1,0.4,text_str,color = 'black', size = 8,horizontalalignment = 'left', verticalalignment = 'top') text_str = r'pdf of $Z = (\overline{X}_n -\mu)/\sigma$' plt.text(-3,0.85,text_str,color = 'black', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') text_str = r'mean = '+ f'{0:2.0f}' plt.text(-3,0.70,text_str,color = 'red', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') text_str = r'st.dev. = '+ f'{1:2.0f}' plt.text(-3,0.55,text_str,color = 'red', size = 8,horizontalalignment = 'left', verticalalignment = 'bottom') text_str = r'$n = $'+ f'{n[1]:2.0f}' plt.text(3,0.85,text_str,color = 'red', size = 8,horizontalalignment = 'right', verticalalignment = 'bottom') plt.savefig(file_name, dpi=300)