KL Divergence

Easy
~12 min
code completion

KL Divergence

Kullback–Leibler divergence measures how much a probability distribution differs from a reference :

Key properties:

  • , with equality iff
  • Not symmetric: in general
  • Appears in VAE training (ELBO = reconstruction loss ) and PPO policy updates
  • For this problem assume all values in p and q are strictly positive (no zeros).

    Your task:

    Implement kl_divergence(p, q) that computes .

    Example Tests

    Identical distributions: KL is zero

    Input: {"p":[0.25,0.25,0.25,0.25],"q":[0.25,0.25,0.25,0.25]}

    Expected: 0

    Similar distributions: small KL

    Input: {"p":[0.5,0.5],"q":[0.4,0.6]}

    Expected: 0.02041

    Very different distributions: larger KL

    Input: {"p":[0.9,0.1],"q":[0.5,0.5]}

    Expected: 0.36806

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.