# -*- 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 norm def running_sum(sample): n = len(sample) r_sum=np.zeros(n) sum = 0 for i in np.arange(0,n): sum = sum+sample[i] r_sum[i] = sum return(r_sum) m = 1000 a = -0.5 b = 0.5 mu = 0 sigma = (1/12)**0.5 x_points = np.arange(0,1001)/1000 LB_x = -15 UB_x = 15 x_points = (UB_x-LB_x)*x_points+LB_x norm_100_pdf = norm.pdf(x_points,100*mu,sigma*(100)**0.5) norm_500_pdf = norm.pdf(x_points,500*mu,sigma*(500)**0.5) norm_900_pdf = norm.pdf(x_points,900*mu,sigma*(900)**0.5) p_100 = 2*norm.cdf(-10,100*mu,sigma*(100)**0.5) p_500 = 2*norm.cdf(-10,500*mu,sigma*(500)**0.5) p_900 = 2*norm.cdf(-10,900*mu,sigma*(900)**0.5) random_unif = np.random.uniform(a,b,m) r_sum_unif = running_sum(random_unif) # 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) Figure.tight_layout() # Adding a panel to the figure Panel = Figure.add_subplot(1,2,1) LB_x = 0 UB_x = m off_set_x = 0.025*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = -0.6 UB_y = 0.6 off_set_y = 0.05*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) the_x = np.arange(0,m) the_y = random_unif plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('k') plt.ylabel('Random Sample') title_str = r'$Unif[-0.5,0.5]-Sample$' 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.scatter(the_x,the_y, color='blue',s=1) plt.axhline(mu, lw=2, ls = '--',color = 'black',alpha=0.5) plt.axhline(-0.5, lw=2, ls = ':',color = 'black',alpha=0.5) plt.axhline(+0.5, lw=2, ls = ':',color = 'black',alpha=0.5) # Adding a panel to the figure Panel = Figure.add_subplot(1,2,2) LB_x = 0 UB_x = m off_set_x = 0.025*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = -15 UB_y = 15 off_set_y = 0.05*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) the_x = np.arange(0,m) the_y = r_sum_unif plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('k') plt.ylabel('Running Average') title_str = r'Sum of $Uniform[-0.5,0.5]$ Sample' 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='red', lw=1,alpha=0.75) plt.plot(100+2000*norm_100_pdf,x_points, color='green', lw=1,ls = '--') plt.plot(500+2000*norm_500_pdf,x_points, color='green', lw=1,ls = '--') plt.plot(900+2000*norm_900_pdf,x_points, color='green', lw=1,ls = '--') plt.axhline(-10, lw=2, ls = '--',color = 'black',alpha=0.5) plt.axhline(10, lw=2, ls = '--',color = 'black',alpha=0.5) plt.axvline(100, lw=1, ls = ':',color = 'black',alpha=0.5) plt.axvline(500, lw=1, ls = ':',color = 'black',alpha=0.5) plt.axvline(900, lw=1, ls = ':',color = 'black',alpha=0.5) plt.scatter([100,500,900],[r_sum_unif[100],r_sum_unif[500],r_sum_unif[900]],color="blue") text_str = r'$p = $'+ f'{p_100:2.5f}' plt.text(110,15,text_str,color = 'red', size = 10,horizontalalignment = 'left', verticalalignment = 'center') text_str = r'$p = $'+ f'{p_500:2.2f}' plt.text(510,-15,text_str,color = 'red', size = 10,horizontalalignment = 'left', verticalalignment = 'center') text_str = r'$p = $'+ f'{p_900:2.2f}' plt.text(890,15,text_str,color = 'red', size = 10,horizontalalignment = 'right', verticalalignment = 'center') # adding a title to the plot text_str = 'Graphical Depiction of LOLN & CLT - Sum of Uniform[-0.5,0.5] RV\'s' plt.suptitle(text_str,size='12',y=0.99) plt.savefig('CLT_Accountant_Example.png', dpi=300)