Your question is Stable KL Divergence Computation. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Given two arrays of non-negative numbers p and q representing discrete probability distributions over the same support, write a function that returns the Kullback-Leibler divergence KL(p || q) using numerically stable computation. The function should normalize inputs if needed, reject invalid distributions, and avoid taking log(0) by using a small positive epsilon.
def kl_divergence(p, q):