# -*- coding: utf-8 -*- """ Created on Fri Aug 12 13:20:49 2022 @author: Rene """ import numpy as np from scipy import optimize import matplotlib.pyplot as plt import pylab as p import math def CDF_T_k(t,k,n): num_values = np.arange(t-k+1,t+1,dtype=np.float32) denom_values = np.arange(n-k+1,n+1,dtype=np.float32) numerator = np.product(num_values) denominator = np.product(denom_values) value = numerator/denominator return(value) def solve_critical_value(alpha,k,n): def f(t): value = alpha - CDF_T_k(t,k,n) return(value) root = optimize.brentq(f, 1, n) return(root) n = 350 k = 5 t = 61 alpha = 0.05 t_alpha = solve_critical_value(alpha,k,n) Crit_value = math.trunc(t_alpha) m = 500 n_values = np.zeros(m-Crit_value+1) Type_II_error = np.zeros(m-Crit_value+1) for i in np.arange(1,(m-Crit_value)+2): n_values[i-1] = Crit_value+(i-1) Type_II_error[i-1] = 1-CDF_T_k(Crit_value,k,n_values[i-1]) data = [61,19,56,24,16] t_5 = max(data) p_5 = CDF_T_k(t_5,k,n) # Plotting the analysis results LB = 190 UB = 500 off_set_x = 5 off_set_y = 0.1 max_y = 1 # Setting the attributes for the Figure Figure = plt.figure() plt.rc('font', family='serif', size='10') plt.figure(figsize=(9, 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('t') y_title = 'Type II Error $= Pr(T_5 \geq $'+ f'{Crit_value:2.0f}'+'$ |N=n)$' plt.ylabel(y_title) plt.plot(n_values,Type_II_error,ls='-',lw=0.5,color='red') plt.axhline(1, lw=1, ls = ':',color = 'black',alpha=0.5) p.arrow(n, 0, 0, 0.90, head_width=5, head_length=0.05, color='green') p.arrow(n, 0.95, 0, -0.90, head_width=5, head_length=0.05,color='green') plt.axvline(n, lw=2, ls = ':',color = 'black',alpha=0.5) text_str = r'$1-\alpha = $'+ f'{1-alpha:2.2f}' plt.text(n-5,0.5,text_str, horizontalalignment = 'right', verticalalignment = 'bottom', color = 'green') plt.axvline(t_alpha, lw=2, ls = '--',color = 'blue',alpha=0.5) text_str = r'$t_{\alpha} = $' + f'{Crit_value:2.2f}' plt.text(t_alpha+5,-0.05,text_str, horizontalalignment = 'left', verticalalignment = 'center', color = 'blue') text_str = r"$H_0: N = $" + f'{n:2.0f}' plt.text(450,0.2,text_str, horizontalalignment = 'left', verticalalignment = 'center', color = 'green') text_str = r"$H_1: N < $" + f'{n:2.0f}' plt.text(450,0.1,text_str, horizontalalignment = 'left', verticalalignment = 'center', color = 'green') text_str = r"German Tank Example: $T_5 = max(X_1,\ldots,X_5)$" plt.suptitle(text_str,size='14') plt.savefig('German_Tank_Example_Type_II_350.png', dpi=300)