Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-015

Jensen-Shannon Divergence

Problem

Compute the Jensen-Shannon divergence in bits between p and q:

m = (p + q) / 2 JSD = 0.5 * D(p || m) + 0.5 * D(q || m)

where D is the KL divergence in bits. Skip zero probability terms.

Hint

JSD is symmetric and bounded by 1 bit.

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.0487949406953985
case 4
in: [[0.9,0.1],[0.5,0.5]]
expected: 0.1467931024360521
Starter codePython
import math
def jensen_shannon(p, q):
    # Your code here
    pass

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