2
$\begingroup$

From what I've been reading, if there is no underlying difference between the 2 probabilities distributions we would have perfect entropy.

I'm putting an example below. Can anybody explain why the cross entropy of two exactly equal probability distributions is not 0 here?

### example of calculating cross entropy for identical distributions
from math import log2

### calculate cross-entropy
def cross_entropy(p, q):
    return -sum([p[i]*log2(q[i]) for i in range(len(p))])

### define data
p = [0.10, 0.40, 0.50]
# calculate cross entropy H(P, P)
ce_pp = cross_entropy(p, p)

print('H(P, P): %.3f bits' % ce_pp)

Result = 1.36

$\endgroup$
10
  • 1
    $\begingroup$ Considering just the definition of cross-entropy, for which values $p_i$ and $q_i$ will the cross-entropy be exactly 0? $\endgroup$
    – Sycorax
    Commented Feb 21, 2023 at 19:07
  • 1
    $\begingroup$ If the distributions are the same, cross entropy becomes entropy. It's the KL divergence that should be $0$ when they're the same. And, the distributions you give are not the same. $\endgroup$
    – gunes
    Commented Feb 21, 2023 at 19:09
  • $\begingroup$ I used P twice to calculate it but didn't use Q. I'm going to take out Q in an edit so that it's more clear $\endgroup$ Commented Feb 21, 2023 at 19:11
  • 1
    $\begingroup$ Let's take $i=1$. What is p[i]*log2(p[i])? $\endgroup$
    – jbowman
    Commented Feb 21, 2023 at 19:13
  • 1
    $\begingroup$ @Sycorax I apologize, I'm not very proficient in Math, I believe the answer to the question is that entropy in itself is just a measure of the information given by the distribution and that the KL divergence would be the one that makes sense being 0. $\endgroup$ Commented Feb 21, 2023 at 19:33

1 Answer 1

2
$\begingroup$

We can see that your understanding is not correct by inspecting the definition of cross-entropy: $$H(p,q)=-\sum_i p_i \log q_i .$$

  • The function $f(q) = -\log(q)$ is zero only for $q=1$. It's sufficient to observe that the function is monotonic and therefore has at most 1 zero.

  • The function $g(p) = p$ is zero only for $p=0$.

So we know that to achieve $H(p,q)=0$, we require some configuration of $p_i=0$ or $q_i=1$. The example in OP's post doesn't have any $p_i=0$ or $q_i=1$, so the claim in the question is false.

An example of $p,q$ that have $H(p,q)=0$ is $p = [0, 1]$ and $q=[0,1]$.

(Note that not every choice that has some $p_i=0$ or $q_i=1$ will have $H(p,q)=0$.)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.