# -*- 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 gamma import pandas as pd import matplotlib.patches as patches import Dist_Library m = 500 alpha=4 beta=2 x = np.arange(0,1001)/1000 LB_x = 0 UB_x = 11 x = (UB_x-LB_x)*x+LB_x pdf_gamma = gamma.pdf(x,a = alpha,scale = 1/beta) mean_gamma = alpha/beta random_gamma = np.random.gamma(alpha, 1/beta, m) h = 0.25 bounds = np.arange(0,20) bounds = bounds*(2*h) pmf_hat = Dist_Library.Estimate_empirical_histogram(bounds, random_gamma) #pdf_hat = pmf_hat/(2*h) df = pd.DataFrame({'The_Sample':random_gamma}) x1 = 2 lb_1 = x1-h ub_1 = x1+h cdf_lb = df[df<=lb_1].count()/m cdf_ub = df[df<=ub_1].count()/m density_x1 = (cdf_ub-cdf_lb)/(2*h) h = 0.25 x2 = 4 lb_2 = x2-h ub_2 = x2+h cdf_lb = df[df<=lb_2].count()/m cdf_ub = df[df<=ub_2].count()/m density_x2 = (cdf_ub-cdf_lb)/(2*h) # 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(1,2,1) LB_x = 0 UB_x = 10 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 = 0.5 off_set_y = 0.05*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) the_x = x the_y = pdf_gamma plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('Probability Density Function') title_str = r'$Gamma[2,4]-PDF$' 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') Panel.add_patch(patches.Rectangle( (lb_1, 0), 2*h, density_x1, facecolor = 'none', edgecolor = 'red', linewidth = 1, alpha = 1)) x_values = [x1,x1] y_values = [0,density_x1] plt.plot(x_values,y_values, color='black', lw=1, ls=':') Panel.add_patch(patches.Rectangle( (lb_2, 0), 2*h, density_x2, facecolor = 'none', edgecolor = 'red', linewidth = 1, alpha = 1)) x_values = [x2,x2] y_values = [0,density_x2] plt.plot(x_values,y_values, color='black', lw=1, ls=':') # Adding a panel to the figure Panel2 = Figure.add_subplot(1,2,2) LB_x = 0 UB_x = 10 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 = 0.5 off_set_y = 0.05*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) the_x = x the_y = pdf_gamma plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('Probability Density Function') title_str = r'$Gamma[2,4]-PDF$' 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') for i in np.arange(0,len(bounds)-1): Panel2.add_patch(patches.Rectangle( (bounds[i],0), 2*h, pmf_hat[i+1], facecolor = 'none', edgecolor = 'red', linewidth = 1, alpha = 1)) mid_point = (bounds[i]+bounds[i+1])/2 x_values = [mid_point,mid_point] y_values = [0,pmf_hat[i+1]] plt.plot(x_values,y_values, color='black', lw=1, ls=':') plt.savefig('Recover_Gamma_Density_2.png', dpi=300)