Cross-entropy

Source: this video
Consider we are encoding a language, consisting of n symbols. Suppose the probability of each symbol is q1,q2,…,qn respectively. If we want to minimize the number of bits for each message (in average) for this language, we should represent the ith symbol with

log12⁡qi

bits.
For example, if a symbol ↑ has probability 50%, we should represent this symbol with 0 or 1, since it is so frequent that it is worth spend one bit in it:
Pasted image 20260922093651.png

The average number of bits needed for a message is, precisely, the Shannon entropy:

∑iqilog12⁡qi

So far, so good.

But what if the language has changed? Better said, what if the probabilities of each symbol were not correctly computed? Suppose that the real probabilities were p1,…,pn. In average, for a message we will be using, instead,

∑ipilog12⁡qi

bits. And this quantity is called cross-entropy for the two distributions {qi} and {pi}.
Pasted image 20260922094801.png

See also this for another explanation.