# -*- coding: utf-8 -*- """ Created on Thu Feb 3 18:47:48 2022 @author: Rene """ import numpy as np import matplotlib.pyplot as plt def running_average(sample): n = len(sample) r_average=np.zeros(n) sum = 0 for i in np.arange(0,n): sum = sum+sample[i] r_average[i] = sum/(i+1) return(r_average) m = 500 a = 1 b = 3 mean_unif = (a+b)/2 random_unif = np.random.uniform(a,b,m) r_ave_unif = running_average(random_unif) alpha=4 beta=2 mean_gamma=alpha/beta random_gamma=np.random.gamma(alpha, 1/beta, m) r_ave_gamma=running_average(random_gamma) # 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 = 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 UB_y = 10 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'$U[1,3]-Sample$' 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.scatter(the_x,the_y, color='red', s=1) plt.axhline(mean_unif, lw=2, ls = '--',color = 'black',alpha=0.5) # Adding a panel to the figure Panel = Figure.add_subplot(2,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 = 1 UB_y = 3 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_ave_unif plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('k') plt.ylabel('Running Average') title_str = r'$U[1,3]-Sample$' 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) plt.axhline(mean_unif, lw=2, ls = '--',color = 'black',alpha=0.5) # Adding a panel to the figure Panel = Figure.add_subplot(2,2,3) 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 UB_y = 10 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_gamma plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('k') plt.ylabel('Random Sample') title_str = r'$Gamma[4,2]-Sample$' 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.scatter(the_x,the_y, color='blue',s=1) plt.axhline(mean_gamma, lw=2, ls = '--',color = 'black',alpha=0.5) # Adding a panel to the figure Panel = Figure.add_subplot(2,2,4) 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 = 1 UB_y = 3 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_ave_gamma plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('k') plt.ylabel('Running Average') title_str = r'$Gamma[4,2]-Sample$' 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) plt.axhline(mean_gamma, lw=2, ls = '--',color = 'black',alpha=0.5) plt.savefig('Law_of_large_numbers_unif_gamma.png', dpi=300)