Given an r×c contingency table of observed counts O, compute each expected count under independence:
Eij=NRiCjThen compute the chi-square statistic:
χ2=i=1∑rj=1∑cEij(Oij−Eij)2Here, Ri is row i's total, Cj is column j's total, and N is the grand total. Return chi2 as a Python float and expected as a NumPy array in a dictionary.
Input: C = [[10, 20], [20, 10]]
Output: {"chi2": 6.666667, "expected": [[15.0, 15.0], [15.0, 15.0]]}
Explanation: Equal row and column totals produce expected counts of 15 in every cell.
Input: C = [[20, 30], [40, 60]]
Output: {"chi2": 0.0, "expected": [[20.0, 30.0], [40.0, 60.0]]}
Input: C = [[25, 25], [25, 25]]
Output: {"chi2": 0.0, "expected": [[25.0, 25.0], [25.0, 25.0]]}
Use np.outer(row_totals, column_totals) / total for expected counts.
Sum (C - expected) ** 2 / expected across the complete table.
Sign in to take notes on this problem
Accepts: array
Given an r×c contingency table of observed counts O, compute each expected count under independence:
Eij=NRiCjThen compute the chi-square statistic:
χ2=i=1∑rj=1∑cEij(Oij−Eij)2Here, Ri is row i's total, Cj is column j's total, and N is the grand total. Return chi2 as a Python float and expected as a NumPy array in a dictionary.
Input: C = [[10, 20], [20, 10]]
Output: {"chi2": 6.666667, "expected": [[15.0, 15.0], [15.0, 15.0]]}
Explanation: Equal row and column totals produce expected counts of 15 in every cell.
Input: C = [[20, 30], [40, 60]]
Output: {"chi2": 0.0, "expected": [[20.0, 30.0], [40.0, 60.0]]}
Input: C = [[25, 25], [25, 25]]
Output: {"chi2": 0.0, "expected": [[25.0, 25.0], [25.0, 25.0]]}
Use np.outer(row_totals, column_totals) / total for expected counts.
Sum (C - expected) ** 2 / expected across the complete table.
Sign in to take notes on this problem
Accepts: array