KL Divergence
~12 mincode completion
Implement kl_divergence(p, q) that computes .
Examples
Identical distributions: KL is zero
- Input
- kl_divergence([0.25, 0.25, 0.25, 0.25], [0.25, 0.25, 0.25, 0.25])
- Output
- 0
Similar distributions: small KL
- Input
- kl_divergence([0.5, 0.5], [0.4, 0.6])
- Output
- 0.02041
Very different distributions: larger KL
- Input
- kl_divergence([0.9, 0.1], [0.5, 0.5])
- Output
- 0.36806
Hints
Hint 1
is the natural log, which is what this formula wants.
Hint 2
A common slip here: computed KL Q given P instead of P given Q.
Requirements
p: Probability distribution P (sums to 1), shape (n,)q: Reference distribution Q (sums to 1), shape (n,)Return Scalar KL divergence (non-negative).
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~12 min
••••••••••••••••
8 employers weight this skill
3 frontier labs, 2 big tech firms, 2 autonomy companies, 1 enterprise vendor. Top match scores 68.
Python
import numpy as np
def kl_divergence(p: np.ndarray, q: np.ndarray) -> float:
"""
Compute KL divergence D_KL(P || Q).
Args:
p: Probability distribution P (sums to 1), shape (n,)
q: Reference distribution Q (sums to 1), shape (n,)
Returns:
Scalar KL divergence (non-negative).
"""
# YOUR CODE HERE
pass