Our first hypothesis test - Student's t-test

Performing Experiments in Python

Luke Hayden

Instructor

From observed pattern to reliable result

Data contains patterns

  • Some expected

  • Others surprising

  • Random variation also

Dealing with this

  • How do we go from observation to result?
Performing Experiments in Python

Are these groups different?

Weights of two groups of adults

Sample A:

[66.1, 69.8,67.7,69.6,71.1]

Sample B:

[83.7,81.5, 80.6, 83.9, 84.4]
(p9.ggplot(df)+ 
p9.aes('Value', fill='Sample')+ 
p9.geom_density(alpha=0.5))

Are these different?

Performing Experiments in Python

Two hypotheses

Null hypothesis

A = B

  • Observed patterns are the product of random chance

A density plot of a unimodal distribution

Alternative hypothesis

A != B

  • Difference between samples represents a real difference between the populations

A density plot of a bimodal distribution

Performing Experiments in Python

Some statistical terms

p-value

  • Likelihood of pattern under null hypothesis

 

alpha

  • Crucial threshold of p-value

  • Usually alpha < 0.05: reject null hypothesis

Performing Experiments in Python

Student's t-test

  • Invented by William Sealy Gosset

  • Two basic types:

One-sample: Mean of population different from a given value?

Two-sample: Two means equal?

Coding a t-test

from scipy import stats 


stats.ttest_ind(Sample_A, Sample_B)

Performing Experiments in Python

Implementing a one-sample t-test

from scipy import stats 

Sample_A = df[df.Sample == "A"] 

t_result = stats.ttest_1sample(Sample_A, 65)

alpha = 0.05

if (t_result[1] < alpha):
    print("mean(A) != 65")

mean(A) != 65
Performing Experiments in Python

Implementing a two-sample t-test

from scipy import stats 

Sample_A = df[df.Sample == "A"] 
Sample_B = df[df.Sample == "B"] 

t_result = stats.ttest_ind(Sample_A, Sample_B)

alpha = 0.05

if (t_result[1] < alpha):
    print("A and B are different!")

A and B are different!
Performing Experiments in Python

Now let's try it out!

Performing Experiments in Python

Preparing Video For Download...