# -*- 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 the_lambda = 0.5 mean_expon = 1/the_lambda random_expon = np.random.exponential(1/the_lambda,m) r_ave_expon = running_average(random_expon) mu = 2 sigma = 3 mean_normal = mu random_normal = np.random.normal(mu,sigma,m) r_ave_normal = running_average(random_normal) # 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_expon plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('k') plt.ylabel('Random Sample') title_str = r'$Exp[0.5]-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_expon, 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_expon plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('k') plt.ylabel('Running Average') title_str = r'$Exp[0.5]-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_expon, 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_normal plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('k') plt.ylabel('Random Sample') title_str = r'$Normal[2,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='blue',s=1) plt.axhline(mean_normal, 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_normal plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('k') plt.ylabel('Running Average') title_str = r'$Normal[2,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='blue', lw=1) plt.axhline(mean_normal, lw=2, ls = '--',color = 'black',alpha=0.5) plt.savefig('Law_of_large_numbers_expon_normal.png', dpi=300)