import numpy as np
from scipy.stats import norm, t

import matplotlib.pyplot as plt
#import pretty_plots # script to set up LaTex and increase line-width and font size

import check_lab01 as p

Note: If you download this notebook to edit offline in the computer lab, you will need this file (right click -> save as) in the same directory as the .ipynb file.

ME 3263 - Laboratory #1

How can you measure something?

All measurements have traceable standards. There are seven base units in SI - meter (length), second (time), Mole (amount of substance), Ampere (electric current), Kelvin (temperature), Candela (Luminous intensity), and kilogram (mass) 1. Any measurement you make should have some method to check against a reference. In this lab, we will use calipers that measure dimensions i.e. meter*10\(^{-3}\) (length) 2. Calipers can always be verified to work with gage blocks.

Manufacturing variations

Different production methods will often exhibit varying degrees of precision and accuracy. It’s important to understand these differences, and how to measure them. In this lab, you will investigate several different sets of bar stock. Some samples have been cut with a bandsaw, some with a CNC mill. You will identify and examine the differences between the two methods.

Student’s t-test

To review, the two-sample t-test can be used to determine whether two groups of samples (e.g. groups 1 and 2 in the example below) have the same average length to within a desired confidence level. The two-sample t-test is based upon the means: \(\bar{x}_1\) and \(\bar{x}_2\), sample standard deviations: \(S_{x,1}\) and \(S_{x,2}\), and number of data points: \(n_1\) and \(n_2\). The t-value for this application is based upon the difference in means between sample sets:

\(t=\frac{|\bar{x}_1-\bar{x}_2|}{\sqrt{AB}}\)

where:

\(A=\frac{n_1+n_2}{n_1 n_2}\)

\(B=\frac{(n_1-1)S_{x,1}^2+(n_2-1)S_{x,2}^2}{n_1+n_2-2}\)

If this calculated t-value is above the critical t-value for the sample size and desired confidence level (\(t_{(\nu,P)}\)), the null hypothesis is rejected, meaning a significant difference has been detected between the two sample sets.

Example calculations using Python

Consider the following two groups of samples, measured to hundredths of a millimeter:

group

sample 1

sample 2

sample 3

sample 4

sample 5

1

104.14 mm

101.61 mm

99.06 mm

102.87 mm

106.68 mm

2

104.12 mm

101.63 mm

97.79 mm

101.58 mm

104.17 mm

Using Python, we can calculate the t-value for the difference between the means of these two samples:

G1=np.array([104.14, 101.61 ,  99.06, 102.87, 106.68])  #Entering the values of groups 1 and 2
G2=np.array([104.12, 101.63 ,  97.79, 101.58 , 104.17])

meanG1=np.sum(G1)/len(G1)    #Calculating their means: their sums divided by their number of elements
meanG2=np.sum(G2)/len(G2)

s2G1=np.sum((G1-meanG1)**2)/(len(G1)-1) #Calculating the sample variances using the expression we learned in class
s2G2=np.sum((G2-meanG2)**2)/(len(G2)-1)

A=(len(G1)+len(G2))/(len(G1)*len(G2)*1.0)   #calculating the A and B terms for the denominator of our t expression. Multiplying A by 1.0 is a simple way to make this integer a float
B=((len(G1)-1)*s2G1+(len(G2)-1)*s2G2)/(len(G1)+len(G2)-2)

tstat=np.abs(meanG1-meanG2)/np.sqrt(A*B)  #finally calculating our t-value

print('example groups 1 and 2 have t=%1.2f'%tstat)
example groups 1 and 2 have t=0.59

We can also use the SciPy stats library to find our critical t-values, rather than looking them up in a table. If you check the first code block of this notebook, you will see that we have imported norm and t from this library. t.interval is used to retrieve the critical t-value, as shown in the example below.

df=len(G1)+len(G2)-2  #Calculate our degrees of freedom (nu)

# Print out the table for df degrees of freedom (n1+n2-2)
print('df=%i'%df )
print('| p=0.05 | p=0.025 | p=0.01 | p=0.005 |')
print('|    --- |     --- |    --- |     --- |')
print("| %1.2f   | %1.2f    | %1.2f   | %1.2f    |"%(t.interval(0.95, df)[1],\
                                                     t.interval(0.975, df)[1],\
                                                     t.interval(0.99, df)[1],\
                                                     t.interval(0.995, df)[1])) #use t.interval with the desired c (P%) and degrees of freedom
df=8
| p=0.05 | p=0.025 | p=0.01 | p=0.005 |
|    --- |     --- |    --- |     --- |
| 2.31   | 2.75    | 3.36   | 3.83    |

The confidence interval is often expressed as a decimal \(c\) (e.g., \(c=0.95\)) rather than a percent (e.g., \(P=95\%\)). \(p\), the complement of \(c\), is also used (e.g., \(p=0.05\)). All three of these are functionally equivalent, and are simply different ways of notating the same quantity.

Example Problem #1

Groups 1 and 2 from the previous example have different measurements of mean length. Is it statistically significant?

Answer  = 1 # replace 1 with 'yes' or 'no'
p.check_p01(Answer)
Whoops, try again
0

Example Problem #2

Consider the following two groups of samples:

group

sample 1

sample 2

sample 3

sample 4

sample 5

1

103.5

99.0

104.5

110.7

98.4

2

108.8

117.9

113.8

107.7

112.7

Is there a statisitically significant difference between the two sets of measurements?

tstat # your work here
tvar=t.interval(0.95,degrees_of_freedom)[1]
Answer # = 'yes' or 'no'
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/tmp/ipykernel_1670/4010596980.py in <module>
      1 tstat # your work here
----> 2 tvar=t.interval(0.95,degrees_of_freedom)[1]
      3 Answer # = 'yes' or 'no'

NameError: name 'degrees_of_freedom' is not defined
# Leave this alone, execute to check the answer
p.check_p02(tstat,Answer)
Whoops, try again
0

Lab Procedure - Measuring tolerances and comparing groups and samples

You are given four groups, A-D, of 5 samples, 1-5. You will use 4-inch calipers to measure the lengths of the four groups of samples. Make sure each lab member takes measurements. Measure the length of each sample four times and fill in Table 1. A printable version of this table can be found here.

Table 1. Lengths of machined bar stock. Sets A-D, samples 1-5, length measurements 1-4. Calculate the average and standard deviation and note the lab member.

Set

Sample

Length 1

Length 2

Length 3

Length 4

Average

Stand. Dev.

User

A

1

___

___

___

___

______

______

______

A

2

___

___

___

___

______

______

______

A

3

___

___

___

___

______

______

______

A

4

___

___

___

___

______

______

______

A

5

___

___

___

___

______

______

______

B

1

___

___

___

___

______

______

______

B

2

___

___

___

___

______

______

______

B

3

___

___

___

___

______

______

______

B

4

___

___

___

___

______

______

______

B

5

___

___

___

___

______

______

______

C

1

___

___

___

___

______

______

______

C

2

___

___

___

___

______

______

______

C

3

___

___

___

___

______

______

______

C

4

___

___

___

___

______

______

______

C

5

___

___

___

___

______

______

______

D

1

___

___

___

___

______

______

______

D

2

___

___

___

___

______

______

______

D

3

___

___

___

___

______

______

______

D

4

___

___

___

___

______

______

______

D

5

___

___

___

___

______

______

______

Sample groups A-D were machined with two different methods, a saw and a computer numerical control (CNC) mill. There is a roughness associated with the process and a randomness associated with the mounting and cutting of the four groups of samples. The roughness affects the variation of the multiple measurements on one sample i.e. Length 1, 2, 3, and 4. The mounting and cutting process introduces variations between A1, A2, etc. The different cutting methods could lead to further variation between sample groups A, B, C and D.

Your Report

Describe the measurements in groups A, B, C, and D. Determine which, if any, groups are statistically equivalent (do they satisfy the null hypothesis?). Describe the difference in measurement errors (i.e., confidence intervals) between band saw and CNC sample sets. Remember that each lab group has received different sets of samples, so your results may not match your neighbors’.

Follow the established format for lab reports: Introduction, Methods, Results & Discussion, Conclusion