All problems
KL Calibration Divergence
Problem
Compute the KL divergence used to score calibration ranges: normalize p and q to distributions, floor q at eps, and return sum p_i * ln(p_i / q_i), skipping terms where p_i is zero. Lower divergence means the quantized distribution better matches the reference.
The signature is kl_calibration_divergence(p, q, eps=1e-9).
Hint
KL is asymmetric: it is zero only when the two normalized histograms match.
Test cases
case 1
in: [[1,0],[1,0]]
expected: 0
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.14384103622589042
case 4
in: [[1,0],[0,1]]
expected: 20.72326583694641
Starter codePython
import math
def kl_calibration_divergence(p, q, eps=1e-9):
# Your code here
pass4 test cases · run your code against them in the editor