# -*- coding: utf-8 -*- """ Created on Tue Sep 6 15:05:56 2022 @author: Rene """ import numpy as np from scipy.stats import norm import matplotlib.pyplot as plt mu_0 = 120 the_sigma = 2 n = 3 SD_X_bar = the_sigma/(n**0.5) LB = mu_0-4*SD_X_bar UB = mu_0+4*SD_X_bar x_points = np.linspace(LB,UB,100) y_points = norm.pdf(x_points,mu_0,SD_X_bar) the_alpha = 0.05 crit_threshold = norm.ppf(1-the_alpha,mu_0,SD_X_bar) data = np.array([123,122,122.5]) x_bar = np.mean(data) p_value = 1-norm.cdf(x_bar,mu_0,SD_X_bar) off_set_x = 0.25 off_set_y = 0.01 max_y = 0.4 # Comparing dataset with the theoretical pdf and mu_0 with x_bar # Setting the attributes for the Figure plt.rc('font', family='serif', size='10') Figure = plt.figure() Figure.set_figwidth(9) Figure.set_figheight(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('t') plt.ylabel(r'$N(\mu_0,\sigma / \sqrt{n} )$ density') plt.plot(x_points,y_points,ls='-',lw=0.5,color='blue',label = r'$N(\mu_0,\sigma / \sqrt{n} )$ density') plt.axvline(mu_0, lw=1, ls = '--',color = 'blue',alpha=0.5) plt.axvline(crit_threshold, lw=1, ls = '--',color = 'black',alpha=0.5) eq = r'$\mu_0 =$'f'{mu_0:2.0f}' plt.text(mu_0-0.1,0.4,eq,color="black",horizontalalignment='right',verticalalignment='top') eq = r'$c_{\alpha} =$'f'{crit_threshold:2.1f}' plt.text(crit_threshold-0.1,0.4,eq,color="black",horizontalalignment='right',verticalalignment='top') eq = r'Critical Region $K = [c_{\alpha},\infty)$' plt.text(crit_threshold+0.1,0.4,eq,color="black",horizontalalignment='left',verticalalignment='top') plt.text(LB,0.4,"Speed Sample = {122,123,122.5}",color="black", horizontalalignment='left',verticalalignment='top') eq = r'$t_0 = \overline{x}_{3} = $'f'{x_bar:2.1f}' plt.text(LB,0.37,eq,color="black", horizontalalignment='left',verticalalignment='top') n_x_values = np.linspace(crit_threshold,UB,100) n_y_values = norm.pdf(n_x_values,mu_0,SD_X_bar) plt.fill_between(n_x_values, np.zeros(100), n_y_values, color = 'red',alpha=0.25) n_x_values = np.linspace(x_bar,UB,100) n_y_values = norm.pdf(n_x_values,mu_0,SD_X_bar) plt.fill_between(n_x_values, np.zeros(100), n_y_values, color = 'lightgreen',alpha=0.5) eq = r'Type 1 Error = ' plt.text(crit_threshold+0.1,0.22,eq,color="black", horizontalalignment='left',verticalalignment='top') eq = r'$Pr (H_0 = False\,|\,H_0 = True ) = $ ' plt.text(crit_threshold+0.1,0.2,eq, horizontalalignment='left',verticalalignment='top') eq = r'$Pr(T > c_{\alpha}\,|\,\mu_0 = $'f'{mu_0:2.0f}'+'$)$ = ' plt.text(crit_threshold+0.1,0.18,eq, horizontalalignment='left',verticalalignment='top') eq = r'$\alpha = $'f'{the_alpha:2.2f}' plt.text(crit_threshold+0.1,0.16,eq, horizontalalignment='left',verticalalignment='top') plt.arrow(crit_threshold+0.35, 0.145, 0, -0.1, head_width=0.1, head_length=0.01, color='red') eq = eq = r'$t_0 = \overline{x}_{3} = $'f'{x_bar:2.1f}' plt.text(x_bar-0.1,0.12,eq, horizontalalignment='left',verticalalignment='top') x_values = [x_bar,x_bar] y_values = [0,norm.pdf(x_bar,mu_0,SD_X_bar)] plt.plot(x_values,y_values,ls='-',lw=1,color='black') x_values = [x_bar,x_bar] y_values = [0,0.10] plt.plot(x_values,y_values,ls=':',lw=1,color='black') eq = r'p-value = ' plt.text(x_bar+0.2,0.08,eq, horizontalalignment='left',verticalalignment='bottom') eq = r'$Pr(T > t_0\,|\,\mu_0 = $'f'{mu_0:2.0f}'+'$)$ = ' plt.text(x_bar+0.2,0.0575,eq, horizontalalignment='left',verticalalignment='bottom') eq = f'{p_value:2.4f}' plt.text(x_bar+0.2,0.04,eq, horizontalalignment='left',verticalalignment='bottom') plt.arrow(x_bar+0.35, 0.035, 0, -0.015, head_width=0.1, head_length=0.01, color='red') text_str = r'Dutch Speed Example: $T\sim N(\mu_0, \sigma/\sqrt{n}):\mu_0=120,\,n=3,\,\alpha=$ 'f'{100*the_alpha:2.0f}'+'%' Figure.suptitle(text_str,size='14') plt.savefig("Dutch_Speed_example.png", dpi=300)