Skip to content
DeepForgeby svx
All problems
HardStatisticsst-046

G-Test Statistic

Problem

Return the G-test statistic 2 * sum(o * ln(o/e)) for a 2D table of observed counts, where e = row_total * col_total / total; cells with observed count zero contribute zero. Return 0.0 for an empty table, a zero total, or any zero expected count.

Hint

Only cells with o greater than 0 contribute to the sum of o * ln(o/e).

Test cases

case 1
in: [[[10,20],[30,40]]]
expected: 0.8043486460964835
case 2
in: [[[5,5],[5,5]]]
expected: 0
case 3
in: [[[10,0],[0,10]]]
expected: 27.725887222397812
case 4
in: [[[20,30,50]]]
expected: 0
case 5
in: [[[1,2],[3,4]]]
expected: 0.08043486460964827
Starter codePython
import math
def g_test(observed):
    # Your code here
    pass

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