# -*- 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 uniform from scipy.stats import expon from scipy.stats import norm def Chebishev_LB(a,sigma): value=1-(1/a**2)*sigma**2 return(value) x = np.zeros(100) alpha = np.linspace(0,1,100) LB_alpha = 1 UB_alpha = 6 alpha = (UB_alpha-LB_alpha)*alpha+LB_alpha sigma = 1 CB_a = alpha*sigma Cheb_LB = Chebishev_LB(CB_a,sigma) a = 0 b = 1 mean_unif = (a+b)/2 var_unif = (b-a)**2/12 sd_unif = var_unif**0.5 hw_unif = alpha*sd_unif UB = mean_unif+hw_unif LB = mean_unif-hw_unif Pr_unif = uniform.cdf(UB,a,b)-uniform.cdf(LB,a,b) the_lambda = 1 mean_exp = 1/the_lambda sd_exp = 1/the_lambda hw_exp = alpha*sd_exp UB = mean_exp+hw_exp LB = mean_exp-hw_exp Pr_exp = expon.cdf(UB,the_lambda)-expon.cdf(LB,the_lambda) mu = 0 sigma = 1 mean_norm = mu sd_norm = sigma hw_norm = alpha*sd_norm UB = mean_norm+hw_norm LB = mean_norm-hw_norm Pr_norm = norm.cdf(UB,mu,sigma)-norm.cdf(LB,mu,sigma) # 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,1,1) LB_x = LB_alpha UB_x = UB_alpha-1 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 = 1 off_set_y = 0.05*(UB_y-LB_y) y_lims = (LB_y-off_set_y,UB_y+off_set_y) the_x = CB_a the_y = Cheb_LB plt.xlim(x_lims) plt.ylim(y_lims) x_str = r'$\alpha$' plt.xlabel(x_str) y_str = r'$\Pr(|Y-\mu|) < \alpha \cdot \sigma$' plt.ylabel(y_str, size = 10) title_str="Graphical Depiction of Chebishev's inequality" plt.title(title_str, size = 12) plt.axvline(1, lw=2, ls = '--',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(1, lw=2, ls = '--',color = 'lightgray',alpha=0.5) plt.plot(the_x,the_y, color='red', lw=1,label = 'Chebishev LB') plt.plot(the_x,Pr_unif, color='green', lw=1,label = 'Unif[0,1]') plt.plot(the_x,Pr_exp, color='blue', lw=1,label = 'Exp[1]') plt.plot(the_x,Pr_norm, color='orange', lw=1,label = 'Normal[0,1]') plt.fill_between(the_x, x, the_y, color = 'lightcyan', label = 'INFEASIBLE') k = 2 y_k = Chebishev_LB(k,sigma) x_values = [k,k] y_values = [0,y_k] plt.plot(x_values,y_values, color='black', lw=1, ls=':') text_str = f'{y_k:2.2f}' plt.text(k,y_k+0.025,text_str,color = 'black', size = 10,horizontalalignment = 'center', verticalalignment = 'bottom') k = 3 y_k = Chebishev_LB(k,sigma) x_values = [k,k] y_values = [0,y_k] plt.plot(x_values,y_values, color='black', lw=1, ls=':') text_str = f'{y_k:2.2f}' plt.text(k,y_k+0.025,text_str,color = 'black', size = 10,horizontalalignment = 'center', verticalalignment = 'bottom') k = 4 y_k = Chebishev_LB(k,sigma) x_values = [k,k] y_values = [0,y_k] plt.plot(x_values,y_values, color='black', lw=1, ls=':') text_str = f'{y_k:2.2f}' plt.text(k,y_k+0.025,text_str,color = 'black', size = 10,horizontalalignment = 'center', verticalalignment = 'bottom') k = 5 y_k = Chebishev_LB(k,sigma) x_values = [k,k] y_values = [0,y_k] plt.plot(x_values,y_values, color='black', lw=1, ls=':') text_str = f'{y_k:2.2f}' plt.text(k,y_k+0.025,text_str,color = 'black', size = 10,horizontalalignment = 'center', verticalalignment = 'bottom') plt.legend(loc='best') plt.savefig('Chebishev.png', dpi=300)