Skip to content
DeepForgeby svx
All problems
HardStatisticsst-050

Cramer's V from Chi-Square

Problem

Return Cramer's V = sqrt(chi2 / (n * min(r-1, c-1))) for a 2D table of observed counts, where chi2 = sum((o - e)^2 / e) and e = row_total * col_total / total. Return 0.0 for an empty table, a zero total, a table with one row or one column, or any zero expected count.

Hint

Compute chi-square first, then normalize by n and the smaller dimension minus one.

Test cases

case 1
in: [[[10,20],[30,40]]]
expected: 0.0890870806374748
case 2
in: [[[10,0],[0,10]]]
expected: 1
case 3
in: [[[5,5],[5,5]]]
expected: 0
case 4
in: [[[10,20,30],[30,20,10]]]
expected: 0.408248290463863
case 5
in: [[[10,20,30]]]
expected: 0
Starter codePython
def cramers_v(observed):
    # Your code here
    pass

5 test cases · run your code against them in the editor