Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-336

JS Divergence from Counts

Problem

Normalize two count lists into distributions and return the Jensen-Shannon divergence in bits. The mixture is m = (p + q)/2 and the divergence is 0.5*KL(p||m) + 0.5*KL(q||m). Return 0.0 if either total is zero.

Hint

Normalize both lists, average them, and average the two KL contributions.

Test cases

case 1
in: [[1,1],[1,1]]
expected: 0
case 2
in: [[1,1],[1,3]]
expected: 0.0487949406953985
case 3
in: [[2,0],[0,2]]
expected: 1
case 4
in: [[3,0],[1,1]]
expected: 0.31127812445913283
case 5
in: [[0,0],[1,1]]
expected: 0
Starter codePython
import math
def js_from_counts(p_counts, q_counts):
    # Your code here
    pass

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