lab5
lab5
LAB
COURSE CODE: ENEE1020IU
Lab-works
Applications of Python
Part 5 – Hypothesis tests
(1 credit: 0 is for lecture, 1 is for lab-work)
Instructor: TRAN THANH TU
Email: [email protected]
[email protected] 117
Hypothesis tests with Python
Review
Parameter Hypothesis Distribution table
Population mean: Normal distribution
σ-known α
[email protected] 118
Hypothesis tests with Python
Review
Parameter Hypothesis Distribution table
Paired t-Test for assessing the average T-distribution
of differences α, df: n-1
Margin of error
Independent t-Test for assessing the Similar as Population mean: σ-known T-distribution
difference of two averages: α/2, df:
σ-unknown t
or
or
α/2, df: n1+n2−2
(p1=p2=p)
Hypothesis tests about the difference Normal distribution
between the proportions of two α/2
populations
[email protected] 119
Hypothesis tests with Python
Tests whether the means of two paired
Paired t-test samples are significantly different.
from scipy.stats import ttest_rel
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637,
-1.869]
data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075,
-0.169]
stat, p = ttest_rel(data1, data2) H0: the means of the samples are
equal.
print('stat=%.3f, p=%.3f' % (stat, p)) H1/Ha: the means of the samples
if p > 0.05: are unequal.
print('Probably the same distribution') In [1]: print('Slope: %.3f' % 1.123)
Slope: 1.123
else:
In [2]: print('Slope: %.3f' % 1.12345)
print('Probably different distributions') Slope: 1.123
In [3]: print('Slope: %.3f' % 1.1)
Slope: 1.100
In [4]: print('Slope: %.3f' % 1.1237)
Slope: 1.124 120
[email protected]
Hypothesis tests with Python
Tests whether two categorical variables
Chi-squared test are related or independent.
[email protected] 121
Hypothesis tests with Python
Tests whether the means of two
Independent t-test independent samples are significantly
different.
from scipy.stats import ttest_ind
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637,
-1.869]
data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075,
-0.169]
stat, p = ttest_ind(data1, data2) H0: the means of the samples are
equal.
print('stat=%.3f, p=%.3f' % (stat, p)) H1/Ha: the means of the samples
if p > 0.05: are unequal.
print('Probably the same distribution')
else:
print('Probably different distributions')
[email protected] 122
Hypothesis tests with Python
Tests whether the means of two or more
ANOVA independent samples are significantly
different.
from scipy.stats import f_oneway
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637,
-1.869]
data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075,
-0.169]
data3 = [-0.208, 0.696, 0.928, -1.148, -0.213, 0.229, 0.137, 0.269, -0.870, -
1.204]
stat, p = f_oneway(data1, data2, data3)
•H0: the means of the samples
print('stat=%.3f, p=%.3f' % (stat, p)) are equal.
if p > 0.05: •H1/Ha: one or more of the
means of the samples are
print('Probably the same distribution')
unequal.
else:
print('Probably different distributions')
[email protected] 123
Hypothesis tests with Python
Others:
https://docs.scipy.org/doc/scipy-0.14.0/reference/stats.html