# -*- coding: utf-8 -*- """ Created on Tue Aug 31 18:37:42 2021 @author: Rene """ import numpy as np import matplotlib.pyplot as plt import Dist_Library as dl p = 0.1 mu=-np.log(p) # Number of hours n = 30 # Generate m random samples from Poisson distribution and evaluate S and T Estimates m = 500 X_bar = np.zeros(m) Var_hat = np.zeros(m) for i in np.arange(0,m): x = np.random.poisson(mu,n) mu_bar=np.mean(x) var_hat=np.var(x) X_bar[i]=mu_bar Var_hat[i]=var_hat # Calculating the histogram data for the left panel Number_of_bins = 20 LB = 0 UB = 5 bins = np.linspace(LB, UB, Number_of_bins+1) X_bar_hist_data = dl.Estimate_empirical_histogram_table(bins,X_bar) Var_hat_hist_data = dl.Estimate_empirical_histogram_table(bins,Var_hat) # Plotting a two panel histogram plot of the durations # the waiting times plt.rc('font', family='serif', size='10') plt.figure(figsize=(10, 5)) Hist_Figure = plt.figure() Hist_Figure.subplots_adjust(hspace=0.4, wspace=0.4) off_set_x = 0.25 off_set_y = 0.05 max_y = 2 x_lims = (LB-off_set_x,UB+off_set_x) y_lims = (0-off_set_y,max_y+off_set_y) # Adding the left panel with the histogram of Estimator S hist_data = X_bar_hist_data the_bounds = np.append(hist_data["LB"][0],hist_data["UB"]) the_density = np.append(0,hist_data["Bin PDF"]) Panel = Hist_Figure.add_subplot(1,2,1) dl.add_pmf_density_hist_to_figure_panel(Panel,the_bounds,the_density,'indianred') plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('$\overline{x}$') plt.ylabel('Density') text_str = '# Bins = '+str(Number_of_bins) plt.text(5-0.15,1.5,text_str,color = 'red',size = 10,horizontalalignment='right') text_str = 'm = '+str(m) plt.text(5-0.15,1.4,text_str,color = 'red',size = 10,horizontalalignment='right') plt.axvline(mu, lw=1, ls = '--',color = 'black',alpha=0.5) text_str = '$\mu$ = '+f'{mu:2.2f}' plt.text(mu+0.15,max_y-0.15,text_str,color = 'blue',size = 10) text_str = 'Estimator ' + '$\overline{X}$' Panel.set_title(text_str) # Adding the left panel with the histogram of Estimator S hist_data = Var_hat_hist_data the_bounds = np.append(hist_data["LB"][0],hist_data["UB"]) the_density = np.append(0,hist_data["Bin PDF"]) Panel = Hist_Figure.add_subplot(1,2,2) dl.add_pmf_density_hist_to_figure_panel(Panel,the_bounds,the_density,'lightblue') plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('$s^2$') plt.ylabel('Density') text_str = '# Bins = '+str(Number_of_bins) plt.text(5-0.15,1.5,text_str,color = 'red',size = 10,horizontalalignment='right') text_str = 'm = '+str(m) plt.text(5-0.15,1.4,text_str,color = 'red',size = 10,horizontalalignment='right') plt.axvline(mu, lw=1, ls = '--',color = 'black',alpha=0.5) text_str = '$\mu$ = '+f'{mu:2.2f}' plt.text(mu+0.15,max_y-0.15,text_str,color = 'blue',size = 10) text_str = 'Estimator ' + '$S^2$' Panel.set_title(text_str) plt.savefig("Hist_X_bar_Var.png", dpi=1200)