All problems
Chi-Square Statistic for Counts
Problem
Return the chi-square statistic sum((o - e)^2 / e) for a 2D table of observed counts, where e = row_total * col_total / total. Return 0.0 for an empty table, a zero total, or any zero expected count.
Hint
Compute the row and column totals once, then loop over the cells.
Test cases
case 1
in: [[[10,20],[30,40]]]
expected: 0.7936507936507936
case 2
in: [[[5,5],[5,5]]]
expected: 0
case 3
in: [[[1,2,3],[3,2,1]]]
expected: 2
case 4
in: [[[10,0],[0,10]]]
expected: 20
case 5
in: [[[10,20,30]]]
expected: 0
Starter codePython
def chi_square_counts(observed):
# Your code here
pass5 test cases · run your code against them in the editor