# -*- coding: utf-8 -*- """ Created on Wed Mar 4 18:10:08 2020 @author: Johan Rene van Dorp """ import numpy as np import matplotlib.pyplot as plt def Example_6_3_pdf(x,a,b): n = len(x) values = np.zeros(n) for i in np.arange(0,n): if not((x[i]b)): values[i] = 2*(x[i]-a)/(b-a)**2 return values def Example_6_3_cdf(x,a,b): n = len(x) values = np.zeros(n) for i in np.arange(0,n): if (x[i]<=a): values[i] = 0 elif (x[i]<=b): values[i] = ((x[i]-a)/(b-a))**2 else: values[i] = 1 return values def Example_6_3_cdf_inv(u,a,b): n = len(u) values = np.zeros(n) for i in np.arange(0,n): values[i] = (b-a)*u[i]**0.5+a return values # Evaluationg the cdf of example 6_3 a = 1 b = 3 six_3_x = np.linspace(-1,4,1000,endpoint=True) six_3_cdf = Example_6_3_cdf(six_3_x,a,b) # Sampling from the distribution of example 6_3 m = 100 sample = np.random.uniform(0,1,m) sample = Example_6_3_cdf_inv(sample,a,b) # Transforming the Uniform Sample to a discrete distribution Sample x_points = [1.75,2.25,2.75] y_points = Example_6_3_cdf(x_points,a,b) y_points_emp = np.zeros(3) y_points_emp[0] = len(sample[sample <= x_points[0]])/m y_points_emp[1] = len(sample[sample <= x_points[1]])/m y_points_emp[2] = len(sample[sample <= x_points[2]])/m #Plotting the Exponential PDF and CDF plt.rc('font', family='serif', size='12') Figure = plt.figure() Figure.set_figwidth(9) Figure.set_figheight(5) Figure.subplots_adjust(left=0.1, bottom=0.1, right=0.95, top=0.85, hspace=0.1, wspace=0.25) #*************************************************************************************** # Adding the right panel with the empirical pmf LB_x = 0 UB_x = 3.25 off_set_x = 0.05*(UB_x-LB_x) x_lims = (LB_x-off_set_x,UB_x+off_set_x) LB_y = 0 UB_y = 1 off_set_y = 0.1*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) Panel = Figure.add_subplot(1,1,1) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('x') plt.ylabel('Cumulative Distribution Function $F(x)$') text_str = r'CDF - Example 6.3' plt.title(text_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(six_3_x,six_3_cdf,color='red') plt.axhline(1, lw=1, ls = '--',color = 'darkgray',alpha=0.5) plt.scatter(x_points,y_points,color = 'green',alpha=0.5,s=100) plt.scatter(x_points,y_points_emp,color = 'blue') text_str = r'sample size = '+ f'{m:0.0f}' plt.text(0.025,0.9,text_str,horizontalalignment = 'left',verticalalignment = 'center', color = 'red') for i in np.arange(0,3): text_str = f'{y_points[i]:0.2f}' plt.text(x_points[i]+0.1,y_points[i],text_str,horizontalalignment = 'left',verticalalignment = 'center', color = 'green') x_values = [x_points[i], x_points[i]] y_values = [0, y_points[i]] plt.plot(x_values, y_values,color='green',lw=5,alpha=0.5) x_values = [x_points[i], x_points[i]] y_values = [0, y_points_emp[i]] plt.plot(x_values, y_values,color='blue',lw=2,alpha=0.5) text_str = r'$\widehat{p} = $'+ f'{y_points_emp[i]:0.3f}' plt.text(x_points[i]-0.1,y_points_emp[i],text_str,horizontalalignment = 'right',verticalalignment = 'center', color = 'blue') plt.savefig('Example_6_3_Sampler.png', dpi=300)