+1 vote
in Programming Languages by (6k points)
I want to compute chi-square and p-value for a 2x2 matrix. What Python library should I use to calculate these values?

1 Answer

0 votes
by (11.3k points)

You can perform either a Fisher exact test or Chi-square test to calculate p-value. For chi-square, you need to perform Chi-square test. Scipy library has functions for both of these tests.
Fisher exact test on a 2x2 contingency table

>>> obs
array([[60, 14],
       [19, 80]])
>>> from scipy.stats import fisher_exact
>>> oddsratio, pvalue = fisher_exact(obs)
>>> pvalue
1.4292844716515043e-16
>>> oddsratio
18.045112781954888

Chi-square test of independence of variables in a contingency table.

>>> obs
array([[60, 14],
       [19, 80]])
>>> chi, pvalue, dof, ex = chi2_contingency(obs)
>>> chi
62.900798319007976
>>> pvalue
2.1738410013620548e-15
>>> dof
1L
>>>

...