# -*- coding: utf-8 -*- """ Created on Fri Sep 10 16:59:10 2021 @author: Rene """ import numpy as np import pandas as pd from scipy.stats import norm import matplotlib.pyplot as plt df = pd.read_csv('BallBearings.csv') Diameter = df["Diameter"] # H_0: mu = mu_0 H_1: mu not = mu_0 mu_0 = 1 n = len(Diameter) s_n = np.std(Diameter,ddof=1) x_bar = np.mean(Diameter) # Assuming H_0 is True and with estimate for sigma evaluating theoretical pdf LB = mu_0-5*s_n UB = mu_0+5*s_n x_points = np.linspace(LB,UB,1000) y_points = norm.pdf(x_points,mu_0,s_n) off_set_x = 0 off_set_y = 0.5 max_y = 12 # 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(4.25) 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('x') plt.ylabel('density') plt.plot(x_points,y_points,ls='-',lw=0.5,color='blue',label = '$X \sim N(\mu,\sigma) \, pdf$') plt.scatter(Diameter,np.zeros(n),color="red", s = 10,label = 'Data Set') plt.axvline(mu_0, lw=1, ls = '--',color = 'blue',alpha=0.5) plt.axvline(x_bar, lw=1, ls = '--',color = 'red',alpha=0.5) text_str = '$\mu_0 = $' + f'{mu_0:2.0f}' plt.text(mu_0,11,text_str,horizontalalignment='right',color = 'blue') text_str = r'$\overline{x} = $' + f'{x_bar:2.2f}' plt.text(x_bar+0.005,11,text_str,horizontalalignment='left',color = 'red') text_str = r'$H_0: \mu = \mu_0$' plt.text(LB-off_set_x+0.005,11,text_str,horizontalalignment='left',color = 'red') text_str = r'$H_1: \mu \neq \mu_0$' plt.text(LB-off_set_x+0.005,10.25,text_str,horizontalalignment='left',color = 'red') text_str = r'$s_n = $' + f'{s_n:2.3f}' plt.text(LB-off_set_x+0.005,9.5,text_str,horizontalalignment='left',color = 'red') text_str = "Ball Bearing Example $X \sim N(\mu,\sigma), \mu = \mu_0 = $" + f'{mu_0:2.0f}' + ', $\sigma = s_n$, $n = $' + str(n) Figure.suptitle(text_str,size='14') plt.legend(loc = 'best') plt.savefig('Bearing_example_1.png', dpi=300)