Back to problems

Calculate Kullback-Leibler Divergence Between Two Distributions

Algorithm · Netflix · Easy

Task Write a function that receives two NumPy arrays, p and q, representing probability distributions. They have the same length, and corresponding positions are paired. Before calculating the result, ensure that each input distribution is normalized so that its elements add up to 1; do not assume the supplied arrays are already normalized. Then compute the Kullback-Leibler divergence from p to q using \[ D_{KL}(p\,\ \,q)=\sum_i p[i]\log\left(\frac{p[i]}{q[i]}\right). \]…

Checking your access…