Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-125

KL Divergence of Binomials

Problem

Kullback-Leibler divergence from Binomial(n, q) to Binomial(n, p):

KL = n * (p * log(p / q) + (1 - p) * log((1 - p) / (1 - q))).

Terms with p = 0 or p = 1 contribute zero. Return 0.0 for invalid parameters or q outside (0, 1).

Hint

The divergence is n times the per-trial Bernoulli divergence.

Test cases

case 1
in: [1,0.5,0.5]
expected: 0
case 2
in: [10,0.5,0.25]
expected: 1.4384103622589042
case 3
in: [5,0.2,0.8]
expected: 4.158883083359672
case 4
in: [4,0,0.5]
expected: 2.772588722239781
Starter codePython
import math


def kl_divergence_binomial(n, p, q):
    # Your code here
    pass

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