# -*- coding: utf-8 -*- """ Created on Sat Jan 8 15:19:57 2022 @author: Rene """ import numpy as np import matplotlib.pyplot as plt from scipy.stats import expon # Evaluating cdf of the uniform distribution on the interval [a,b] the_lambda=2 expon_x = np.linspace(-1,4,1000,endpoint=True) expon_cdf = expon.cdf(expon_x,scale = 1/the_lambda) # Sampling from a Exponential Distribution with parameter lambda = 2 m = 100 sample = np.random.exponential(scale = 1/the_lambda,size = m) # Transforming the Uniform Sample to a discrete distribution Sample x_points = [0.25,0.75,1.25] y_points = np.zeros(3) y_points = expon.cdf(x_points,scale = 1/the_lambda) 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 Uniform PDF and CDF plt.rc('font', family='serif', size='12') Figure = plt.figure(1) 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) #*************************************************************************************** LB_x = 0 UB_x = 2.5 off_set_x = 0.1*(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 - $Exp(\lambda)$, $\lambda = 2$' 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.axhline(1, lw=1, ls = '--',color = 'darkgray',alpha=0.5) plt.plot(expon_x,expon_cdf,color='red',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.95,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.05,y_points[i],text_str,horizontalalignment = 'left',verticalalignment = 'center', color = 'green') x_values = [x_points[i], x_points[i]] y_values = [0, y_points_emp[i]] plt.plot(x_values, y_values,color='blue') text_str = r'$\widehat{p} = $'+ f'{y_points_emp[i]:0.3f}' plt.text(x_points[i]-0.05,y_points_emp[i],text_str,horizontalalignment = 'right',verticalalignment = 'center', color = 'blue') plt.savefig('Exponential_Sampler.png', dpi=300)