# -*- 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 uniform # Evaluating cdf of the uniform distribution on the interval [a,b] a = 0 b = 1 unif_x = np.linspace(-1,2,1000,endpoint=True) unif_cdf = uniform.cdf(unif_x,a,b) # Sampling from a Uniform Distribution on the interval [a,b] a = 0 b = 1 m = 100 sample = np.random.uniform(a,b,m) # Transforming the Uniform Sample to a discrete distribution Sample x_points = [0.25,0.5,0.75] y_points = np.zeros(3) y_points[0] = len(sample[sample <= x_points[0]])/m y_points[1] = len(sample[sample <= x_points[1]])/m y_points[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 = 1 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 - Uniform $U[0,1]$' 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(unif_x,unif_cdf,color='red',alpha = 0.5) x_values = [b, b] y_values = [0, 1] plt.plot(x_values, y_values,color='black',linestyle=':') plt.scatter(x_points,x_points,color = 'green',alpha=0.5,s=100) plt.scatter(x_points,y_points,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'{x_points[i]:0.2f}' plt.text(x_points[i]+0.025,x_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='blue') text_str = r'$\widehat{p} = $'+ f'{y_points[i]:0.3f}' plt.text(x_points[i]-0.025,y_points[i],text_str,horizontalalignment = 'right',verticalalignment = 'center', color = 'blue') plt.savefig('Uniform_Sampler.png', dpi=300)