You are on page 1of 2

Chi-Square Test

The Pearson’s Chi-Squared test, or just Chi-Squared test for short, is named for Karl Pearson,
although there are variations on the test. The test calculates a statistic that has a chi-squared
distribution, named for the Greek capital letter Chi (X) pronounced “ki” as in kite.

The Hypotheses

The H0 (Null Hypothesis): There is no relationship between variable one and variable two.
The H1 (Alternative Hypothesis): There is a relationship between variable 1 and variable 2.

In [8]: # chi-squared test with similar proportions


from scipy.stats import chi2_contingency
from scipy.stats import chi2
In [7]: # contingency table
table = [[250, 200],
[50, 1000]]
print('OBSERVED')
print(table)

stat, p, dof, expected = chi2_contingency(table)


print('dof=%d' % dof)
print('EXPECTED')
print(expected)

# interpret test-statistic
prob = 0.1
critical = chi2.ppf(prob, dof)
print('probability=%.3f, critical=%.3f, stat=%.3f' % (prob, critical, stat))

if abs(stat) >= critical:


print('Dependent (reject H0)')
else:
print('Independent (fail to reject H0)')

# interpret p-value
alpha = 1.0 - prob
print('significance=%.3f, p=%.3f' % (alpha, p))
if p <= alpha:
print('Dependent (reject H0)')
else:
print('Independent (fail to reject H0)')

OBSERVED
[[250, 200], [50, 1000]]
dof=1
EXPECTED
[[ 90. 360.]
[210. 840.]]
probability=0.100, critical=0.016, stat=504.767
Dependent (reject H0)
significance=0.900, p=0.000
Dependent (reject H0)

The rejection rule is: reject null hypothesis if p- value is less than 0.05 , 0.01 or 0.1.
If you reject the null hypothesis, that means the alternative hypothesis will be accepted. But if you
fail to, that means the claim of the null hypothesis after your research is valid.

In [ ]:

You might also like