# -*- 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 S = np.zeros(m) T = np.zeros(m) for i in np.arange(0,m): x = np.random.poisson(mu,n) mu_hat=np.mean(x) y_hat=(n-np.count_nonzero(x))/n S[i]=y_hat T[i]=np.exp(-mu_hat) mu_S = np.mean(S) mu_T = np.mean(T) # Calculating the histogram data for the left panel Number_of_bins = 10 LB = 0 UB = 0.3 bins = np.linspace(LB, UB, Number_of_bins+1) S_hist_data = dl.Estimate_empirical_histogram_table(bins,S) T_hist_data = dl.Estimate_empirical_histogram_table(bins,T) # 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.01 off_set_y = 0.25 max_y = 16 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 = S_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('S') plt.ylabel('Density') text_str = '# Bins = '+str(Number_of_bins) plt.text(0.3,13,text_str,color = 'red',size = 10,horizontalalignment='right') text_str = 'm = '+str(m) plt.text(0.3,12,text_str,color = 'red',size = 10,horizontalalignment='right') plt.axvline(p, lw=1, ls = '--',color = 'black',alpha=0.5) text_str = 'p = '+f'{p:2.2f}' plt.text(p+0.01,max_y-1,text_str,color = 'blue',size = 10) Panel.set_title("Estimator S") # Adding the left panel with the histogram of Estimator S hist_data = T_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('T') plt.ylabel('Density') text_str = '# Bins = '+str(Number_of_bins) plt.text(0.3,13,text_str,color = 'red',size = 10,horizontalalignment='right') text_str = 'm = '+str(m) plt.text(0.3,12,text_str,color = 'red',size = 10,horizontalalignment='right') plt.axvline(p, lw=1, ls = '--',color = 'black',alpha=0.5) text_str = 'p = '+f'{p:2.2f}' plt.text(p+0.01,max_y-1,text_str,color = 'blue',size = 10) Panel.set_title("Estimator T") plt.savefig("Hist_S_T.png", dpi=1200)