Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-327

Jensen-Shannon Distance

Problem

Compute the Jensen-Shannon distance between two distributions, the square root of the Jensen-Shannon divergence in bits. The divergence is 0.5*KL(p||m) + 0.5*KL(q||m) with the mixture m = (p + q)/2, and zero entries are skipped.

Hint

Build the average distribution first, then measure both KL terms relative to it.

Test cases

case 1
in: [[1,0],[0,1]]
expected: 1
case 2
in: [[0.5,0.5],[0.5,0.5]]
expected: 0
case 3
in: [[0.5,0.5],[0.25,0.75]]
expected: 0.22089576884901735
case 4
in: [[0.9,0.1],[0.1,0.9]]
expected: 0.7287004915675019
Starter codePython
import math
def js_distance(p, q):
    # Your code here
    pass

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