Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-018

Hellinger Distance

Problem

Compute the Hellinger distance between two distributions:

H = sqrt(1 - sum(sqrt(p_i * q_i)))

Clamp the expression inside the square root at 0 to avoid tiny negative values from floating point.

Hint

sum(sqrt(p_i * q_i)) is the Bhattacharyya coefficient.

Test cases

case 1
in: [[0.5,0.5],[0.5,0.5]]
expected: 0
case 2
in: [[1,0],[0,1]]
expected: 1
case 3
in: [[0.5,0.5],[0.25,0.75]]
expected: 0.18459191128251476
case 4
in: [[0.1,0.2,0.7],[0.2,0.2,0.6]]
expected: 0.10249182368318223
Starter codePython
import math
def hellinger(p, q):
    # Your code here
    pass

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