Given an r×c contingency table C, compute the chi-square test statistic and expected frequency table for testing independence.
Chi-Square Test for Independence:
Test Statistic:
χ2=∑E(O−E)2Expected Frequencies:
Eij=totalrowi⋅coljC: 2D array - Contingency table (observed frequencies)Input: C=[[10,20],[20,10]]
Output: chi2=6.667, expected=[[15,15],[15,15]]
Input: C=[[20,30],[40,60]]
Output: chi2=0.0, expected=[[20,30],[40,60]]
Input: C=[[25,25],[25,25]]
Output: chi2=0.0, expected=[[25,25],[25,25]]
Use np.sum() with axis parameter for row/column totals.
Use np.outer() to compute expected frequencies matrix.
Chi-square: np.sum((C - expected) ** 2 / expected).
Sign in to take notes on this problem
Accepts: array
Given an r×c contingency table C, compute the chi-square test statistic and expected frequency table for testing independence.
Chi-Square Test for Independence:
Test Statistic:
χ2=∑E(O−E)2Expected Frequencies:
Eij=totalrowi⋅coljC: 2D array - Contingency table (observed frequencies)Input: C=[[10,20],[20,10]]
Output: chi2=6.667, expected=[[15,15],[15,15]]
Input: C=[[20,30],[40,60]]
Output: chi2=0.0, expected=[[20,30],[40,60]]
Input: C=[[25,25],[25,25]]
Output: chi2=0.0, expected=[[25,25],[25,25]]
Use np.sum() with axis parameter for row/column totals.
Use np.outer() to compute expected frequencies matrix.
Chi-square: np.sum((C - expected) ** 2 / expected).
Sign in to take notes on this problem
Accepts: array