# -*- coding: utf-8 -*- """ Created on Wed Sep 15 11:32:26 2021 @author: Rene """ import numpy as np from scipy.stats import norm import matplotlib.pyplot as plt # implementing two-tailed t-test for the popultation mean def one_sided_z_test(x, mu_0, the_sigma, alpha): n = len(x) x_bar = np.mean(x) z_quantile = norm.ppf(1-alpha,0,1) st_dev_x_bar = the_sigma/(n**0.5) z_statistic = (x_bar-mu_0)/st_dev_x_bar p_value = 1-norm.cdf(abs(z_statistic),0,1) results = np.zeros(3) results[0] = z_statistic results[1] = z_quantile results[2] = p_value return(results) def TypeII_error_one_sided_z_test(x,alpha,mu_0,mu_1,the_sigma): n = len(x) st_dev_x_bar = the_sigma/(n**0.5) z_quantile = norm.ppf(1-alpha,mu_0,st_dev_x_bar) TypeII_error=norm.cdf(z_quantile,mu_1,st_dev_x_bar) return(TypeII_error) # Evaluating x_values and y_values to fill for a tail def evaluate_z_fill_coordinates(LB,UB,m,mu,stdev): x = np.linspace(LB,UB,m) x_trans = (x-mu)/stdev y = norm.pdf(x_trans,0,1)/stdev return(x,y) data = np.array([123,122,122.5]) n = len(data) x_bar = np.mean(data) # H_0: mu = mu_0 H_1: mu not = mu_0 mu_0 = 120 alpha = 0.05 st_dev_known = 2 st_dev_x_bar = st_dev_known/(n**0.5) # Evaluating the pdf at the critical value if population mean equals mu_0 crit_value = norm.ppf(1-alpha,mu_0,st_dev_x_bar) # Evaluating the Type II Error for a specific-value of mu_1 LB = 117 UB = 126 mu_1 = np.linspace(LB,UB,100) beta = TypeII_error_one_sided_z_test(data,alpha,mu_0,mu_1,st_dev_known) off_set_x = 0.5 off_set_y = 0.02 max_y = 1 # Setting the attributes for the Figure plt.rc('font', family='serif', size='10') Figure = plt.figure() Figure.set_figwidth(9) Figure.set_figheight(4.5) Figure.subplots_adjust(hspace=0.4, wspace=0.4) x_lims = (LB-off_set_x,UB+off_set_x) y_lims = (0-off_set_y,max_y+off_set_y) plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel(r'$\mu_1$') plt.ylabel(r'$Type\,II\,Error = Pr (\overline{X}(\mu_1) < c_{\alpha})$') plt.axhline(1, lw=1, ls = '--',color = 'darkgray',alpha=0.5) plt.plot(mu_1, beta, color = 'blue', linestyle="-",lw=1) off_set_x = 0.15 text_str = r'$H_0: \mu = \mu_0$' plt.text(UB-1,max_y-0.05,text_str,horizontalalignment='left',color = 'black') text_str = r'$H_1: \mu > \mu_0$' plt.text(UB-1,0.925*max_y-0.05,text_str,horizontalalignment='left',color = 'black') text_str = r'$c_{\alpha} = $' + f'{crit_value:2.1f}' plt.text(UB-1,0.85*max_y-0.05,text_str,horizontalalignment='left',color = 'red') mu_one_value = 124 Type_II_Error = TypeII_error_one_sided_z_test(data,alpha,mu_0,mu_one_value,st_dev_known) x_values = [mu_one_value,mu_one_value] y_values = [0,Type_II_Error] plt.plot(x_values,y_values,ls=':',lw=1,color='black') x_values = [0,mu_one_value] y_values = [Type_II_Error,Type_II_Error] plt.plot(x_values,y_values,ls=':',lw=1,color='black') text_str = f'{100*Type_II_Error:2.2f}'+'%' plt.text(LB,Type_II_Error, text_str, color ='red',horizontalalignment='left',verticalalignment = 'bottom') mu_one_value = 123 Type_II_Error = TypeII_error_one_sided_z_test(data,alpha,mu_0,mu_one_value,st_dev_known) x_values = [mu_one_value,mu_one_value] y_values = [0,Type_II_Error] plt.plot(x_values,y_values,ls=':',lw=1,color='black') x_values = [0,mu_one_value] y_values = [Type_II_Error,Type_II_Error] plt.plot(x_values,y_values,ls=':',lw=1,color='black') text_str = f'{100*Type_II_Error:2.2f}'+'%' plt.text(LB,Type_II_Error, text_str, color ='red',horizontalalignment='left',verticalalignment = 'bottom') mu_one_value = 122 Type_II_Error = TypeII_error_one_sided_z_test(data,alpha,mu_0,mu_one_value,st_dev_known) x_values = [mu_one_value,mu_one_value] y_values = [0,Type_II_Error] plt.plot(x_values,y_values,ls=':',lw=1,color='black') x_values = [0,mu_one_value] y_values = [Type_II_Error,Type_II_Error] plt.plot(x_values,y_values,ls=':',lw=1,color='black') text_str = f'{100*Type_II_Error:2.2f}'+'%' plt.text(LB,Type_II_Error, text_str, color ='red',horizontalalignment='left',verticalalignment = 'bottom') mu_one_value = 121 Type_II_Error = TypeII_error_one_sided_z_test(data,alpha,mu_0,mu_one_value,st_dev_known) x_values = [mu_one_value,mu_one_value] y_values = [0,Type_II_Error] plt.plot(x_values,y_values,ls=':',lw=1,color='black') x_values = [0,mu_one_value] y_values = [Type_II_Error,Type_II_Error] plt.plot(x_values,y_values,ls=':',lw=1,color='black') text_str = f'{100*Type_II_Error:2.2f}'+'%' plt.text(LB,Type_II_Error, text_str, color ='red',horizontalalignment='left',verticalalignment = 'bottom') mu_one_value = 120 Type_II_Error = TypeII_error_one_sided_z_test(data,alpha,mu_0,mu_one_value,st_dev_known) x_values = [mu_one_value,mu_one_value] y_values = [0,Type_II_Error] plt.plot(x_values,y_values,ls=':',lw=1,color='black') x_values = [0,mu_one_value] y_values = [Type_II_Error,Type_II_Error] plt.plot(x_values,y_values,ls=':',lw=1,color='black') text_str = f'{100*Type_II_Error:2.2f}'+'%' plt.text(LB,Type_II_Error, text_str, color ='red',horizontalalignment='left',verticalalignment = 'bottom') text_str = r'Dutch Speed Example: $T\sim N(\mu_0, \sigma/\sqrt{n}):\mu_0=120,\,n=3,\,\alpha=$ 'f'{100*alpha:2.0f}'+'%' Figure.suptitle(text_str,size='14') plt.savefig('Dutch_Speed_example_Type_II_Error_OC.png', dpi=300)