Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-019

Chi-Square Distance

Problem

Compute the chi-square distance between two distributions:

chi2 = sum((p_i - q_i)^2 / q_i)

Assume q_i > 0 for every outcome.

Hint

The chi-square distance weights differences by the reference probability 1 / q_i.

Test cases

case 1
in: [[0.5,0.5],[0.25,0.75]]
expected: 0.3333333333333333
case 2
in: [[0.5,0.5],[0.5,0.5]]
expected: 0
case 3
in: [[0.25,0.75],[0.5,0.5]]
expected: 0.25
case 4
in: [[0.2,0.3,0.5],[0.25,0.25,0.5]]
expected: 0.01999999999999999
Starter codePython
def chi_square_distance(p, q):
    # Your code here
    pass

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