Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-064

KL Between Two Uniforms

Problem

Return the KL divergence in bits from Uniform(a1, b1) to Uniform(a2, b2), assuming the first interval is contained in the second:

D = log2((b2 - a2) / (b1 - a1))

Hint

Within the shared support the density ratio is constant, so no integral is needed.

Test cases

case 1
in: [0,1,0,2]
expected: 1
case 2
in: [1,2,0,4]
expected: 2
case 3
in: [0,4,0,4]
expected: 0
case 4
in: [2,4,0,8]
expected: 2
Starter codePython
import math
def kl_uniform(a1, b1, a2, b2):
    # Your code here
    pass

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