All problems
Contingency Coefficient
Problem
Return the Pearson contingency coefficient C = sqrt(chi2/(chi2 + n)) for a 2D table of observed counts, where chi2 uses expected counts row_total*col_total/n. Return 0.0 for an empty table, a zero total, or any zero expected count.
Hint
C measures association but has an upper bound below 1 that depends on table size.
Test cases
case 1
in: [[[10,20],[30,40]]]
expected: 0.08873565094161137
case 2
in: [[[5,5],[5,5]]]
expected: 0
case 3
in: [[[10,0],[0,10]]]
expected: 0.7071067811865476
case 4
in: [[[1,2,3],[3,2,1]]]
expected: 0.3779644730092272
case 5
in: [[[10,20,30]]]
expected: 0
Starter codePython
def contingency_coefficient(observed):
# Your code here
pass5 test cases · run your code against them in the editor