Information Theory - Entropy

NameEquationInterpretation
EntropyH(P)=Ex∼P[−ln⁡P(x)]H(P) = E_{x\sim P} [- \ln P(x)]Average of negative log probability.
Cross-EntropyH(P,Q)=Ex∼P[−ln⁡Q(x)]H(P, Q) = E_{x \sim P} [- \ln Q(x)]The average number of nats (cf. bits) to represent xx using the imperfect code defined by QQ (data compression view).
KL DivergenceKL(P,Q)=H(P,Q)−H(P)=Ex∼P[−ln⁡Q(x)P(x)]\begin{aligned} KL(P, Q) &= H(P, Q) - H(P) \\ &= E_{x\sim P} [-\ln \frac{Q(x)}{P(x)}] \end{aligned}How many more nats (cf. bits) is required to represent xx with code QQ compared with the optimal code (PP). PP is called the target distribution.
*Conditional EntropyH(P(x∥y))=Ex,y∼P(x,y)[−ln⁡P(x,y)P(y)]=H(P(x,y))−H(P(y))\begin{aligned} H(P(x\|y)) &= E_{x, y \sim P(x,y)}[-\ln \frac{P(x, y)}{P(y)}] \\ &= H(P(x, y)) - H(P(y)) \end{aligned}
*Mutual InformationI(x,y)=KL(P(x,y),P(x)P(y))=Ex,y∼P(x,y)[−ln⁡P(x)P(y)P(x,y)]=H(P(y))−H(P(y∥x))=H(P(x))−H(P(x∥y))\begin{aligned} I(x, y) &= KL(P(x, y), P(x)P(y)) \\ &= E_{x, y \sim P(x, y)}[-\ln\frac{P(x)P(y)}{P(x, y)}] \\ &= H(P(y)) - H(P(y \| x)) \\ &= H(P(x)) - H(P(x \| y)) \end{aligned}How much knowing xx helps to know yy.

KL Divergence is always non-negative (hence MI also is)

H(P,Q)≥H(P)⇔KL(P,Q)≥0⇔I(P(x,y))≥0\begin{align*} H(P, Q) \geq H(P) &\Leftrightarrow KL(P, Q) \geq 0 \\ &\Leftrightarrow I(P(x, y)) \geq 0 \end{align*}
KL(P,Q)=Ex∼P[−ln⁡Q(x)P(x)]≥−ln⁡Ex∼P[Q(x)P(x)]      (∵Jensen’s inequality)=−ln⁡∑xP(x)Q(x)P(x)=−ln⁡∑xQ(x)=0\begin{align*} KL(P, Q) &= {\color{blue}\mathbb{E}_{x \sim P}} [{\color{brown}- \ln} \frac{Q(x)}{P(x)}] \\ &\geq {\color{brown} -\ln} {\color{blue}\mathbb{E}_{x \sim P}}[\frac{Q(x)}{P(x)}] ~~~~~~(\because \text{Jensen's inequality})\\ &= -\ln \sum_x P(x) \frac{Q(x)}{P(x)} \\ &= -\ln \sum_x Q(x) = 0 \end{align*}

Example: Binary classification

Let's say we do a coin-toss (biased coin). The coin can take either Head or Tail. We want to obtain a model that predicts if the coin gives Head or Tail.

Using a random variable xx, we can write each event as x=Hx = H or x=Tx = T. Let's define our model:

Q(x)≔{p(x=H)1−p(x=T),Q(x) \coloneqq \begin{cases} p & (x = H) \\ 1 - p & (x = T) \end{cases},

where 0≤p≤10 \leq p \leq 1 is a parameter that the model learns.

👉 Entropy
Although this is not a standard path to take, but let's compute the entropy of QQ.

H(Q)=Ex∼Q[−ln⁡Q(x)]=∑x∈{H,T}−Q(x)ln⁡Q(x)=−Q(x=H)ln⁡Q(x=H)−Q(x=T)ln⁡Q(x=T)=−pln⁡p−(1−p)ln⁡(1−p)\begin{align} H(Q) &= \mathbb{E}_{x \sim Q} [-\ln Q(x)] \\ &= \sum_{x \in \{H, T\}} - Q(x) \ln Q(x) \\ &= -Q(x = H) \ln Q(x = H) - Q(x = T) \ln Q(x = T) \\ &= -p \ln p - (1 - p) \ln (1 - p) \end{align}

Now, what is max and min of this entropy? To find that out, let's think about the shape of −pln⁡p−(1−p)ln⁡(1−p)-p \ln p - (1 - p) \ln (1 - p).

Shape of the entropy w.r.t. pp
What does −pln⁡p−(1−p)ln⁡(1−p)-p \ln p - (1-p) \ln (1-p) look like? We can have a few observations about the form of equation, and make a guess.

  • There is a symmetry (−▲ln⁡▲-\blacktriangle \ln \blacktriangle)
  • −pln⁡p- p \ln p is a convex function (imagine its shape)
  • Adding two convex functions gives a new convex function
  • Given that the symmetry is around p=1/2p = 1/2 (p p~ vs  1−p ~1 - p~ in (0≤p≤10 \leq p \leq 1)),
    the maximum should occur at p=1/2p = 1/2

Can we compute lim⁡x→+0xln⁡x\lim_{x \rightarrow +0} x \ln x?

xln⁡x=ln⁡x1/xx \ln x = \frac{\ln x}{1/x}

Using L'Hopital's rule, we can compute the limit:

lim⁡x→+0ln⁡x1/x=lim⁡x→+01/x−1/x2=lim⁡x→+0−x=0\lim_{x \rightarrow +0} \frac{\ln x}{1/x} = \lim_{x \rightarrow +0} \frac{1/x}{-1/x^2} = \lim_{x \rightarrow +0} -x = 0

What is ddx(xln⁡x)\frac{d}{dx}(x \ln x)?

ddx(xln⁡x)=ln⁡x+x⋅1x=ln⁡x+1\frac{d}{dx}(x \ln x) = \ln x + x \cdot \frac{1}{x} = \ln x + 1

Thus,

  • xln⁡xx \ln x starts at (x,y)=(0,0)(x, y) = (0, 0) with slope −∞-\infty
  • ↘\searrow As we increase xx, the yy-value goes down and reaches the minimum at x=e−1x = e^{-1}
  • ↗\nearrow As we increase xx further, the yy-value goes up and reaches 00 again at x=1x = 1

As a result:

  • Min: H(Q)=0H(Q) = 0 when p=0p = 0 or p=1p = 1.
  • Max: H(Q)=ln⁡2H(Q) = \ln 2 when p=1/2p = 1/2.

What does this mean? (How to interpret this?)
Can we see it as perplexity (?): exp⁡(ln⁡2)=2\exp{(\ln 2)} = 2 -> Two possible outcomes...

👉 Cross-entropy: The first thing to try
Now, let's try to define cross-entropy for the target distribution P(x)P(x). Cross entropy is:

H(P,Q)≔Ex∼P[−ln⁡Q(x)]=∑x∈{H,T}−P(x)ln⁡Q(x)=−P(x=H)ln⁡Q(x=H)−P(x=T)ln⁡Q(x=T)=−P(x=H)ln⁡p−P(x=T)ln⁡(1−p)\begin{align} H(P, Q) &\coloneqq \mathbb{E}_{x \sim P} \big[-\ln Q(x) \big] \\ &= \sum_{x \in \{H, T\}} - P(x) \ln Q(x) \\ &= -P(x = H) \ln Q(x = H) - P(x = T) \ln Q(x = T) \\ &= -P(x = H) \ln p - P(x = T) \ln (1 - p) \end{align}

Now, what should the target distribution P(x)P(x) look like? Woah, this is such a non-standard quenstion, but let's do it anyway.

Let's say this is the target distribution that is unknown to the model:

P(x)≔{ptgt(x=H)1−ptgt(x=T),P(x) \coloneqq \begin{cases} p^\textrm{tgt} & (x = H) \\ 1 - p^\textrm{tgt} & (x = T) \end{cases},

Then, the cross entropy would be

H(P,Q)=−P(x=H)ln⁡p−P(x=T)ln⁡(1−p)=−ptgtln⁡p−(1−ptgt)ln⁡(1−p)\begin{align} H(P, Q) &= -P(x = H) \ln p - P(x = T) \ln (1 - p) \\ &= -{\color{magenta}p^\textrm{tgt}} \ln p - (1 - {\color{magenta}p^\textrm{tgt}}) \ln (1 - p) \end{align}

I guess we can try to maximize this w.r.t. pp. That should give us p=ptgtp = p^\textrm{tgt} in the end... But again, this is quite a non-standard way to go.

👉 Cross-entropy: a rather standard thing to do
Okay, finally something that is a very standard question to ask. Given a dataset that stores actuall events observed D={(x′=T), (x′=H), (x′=H), ⋯ }\mathcal{D} = \{(x'=T),~(x'=H),~(x'=H),~\cdots\}, what does the empirical cross entropy look like? In this case, let's define empirical target distribution for each sample (why does that make sense?).

  • When the sample is x′=Hx' = H:     P(x)≔1 if x=H, else 0P(x) \coloneqq 1 \text{ if } x = H, \text{ else } 0.
  • When the sample with x′=Tx' = T:      P(x)≔1 if x=T, else 0P(x) \coloneqq 1 \text{ if } x = T, \text{ else } 0.
  • Note that this is a mapping from a single sample x′x' to a distribution P(x)P(x).
    • But why can we change the (target) distribution P(x)P(x) for each sample? How is that reasonable...?

With these,

H(P,Q)≔Ex∼P[−ln⁡Q(x)]=∑x′∈D−P(x′)ln⁡Q(x′)=∑x′∈D−P(x=x′)ln⁡Q(x=x′)=∑x′∈D{−P(x=H)ln⁡Q(x=H)if x′=H−P(x=T)ln⁡Q(x=T)if x′=T=∑x′∈D{−ln⁡pif x′=H−ln⁡(1−p)if x′=T.\begin{align} H(P, Q) &\coloneqq \mathbb{E}_{x \sim P} \big[-\ln Q(x) \big] \\ &= \sum_{x' \in \mathcal{D}} - P(x') \ln Q(x') \\ &= \sum_{x' \in \mathcal{D}} - P(x=x') \ln Q(x=x') \\ &= \sum_{x' \in \mathcal{D}} \begin{cases} - P(x=H) \ln Q(x=H) & \text{if } x' = H \\ - P(x=T) \ln Q(x=T) & \text{if } x' = T \end{cases} \\ &= \sum_{x' \in \mathcal{D}} \begin{cases} - \ln p & \text{if } x' = H \\ - \ln (1-p) & \text{if } x' = T \end{cases}. \end{align}

We can end this here. But a weird trick that people always do is to simplify this using a binary label yy. Let's define yy as

y′≔{1if x′=H0if x′=T.y' \coloneqq \begin{cases} 1 & \text{if } x' = H \\ 0 & \text{if } x' = T \end{cases}.

Then, we can write the above as

H(P,Q)=∑x′∈D−y′ln⁡p−(1−y′)ln⁡(1−p).H(P, Q) = \sum_{x' \in \mathcal{D}} - y' \ln p - (1-y') \ln (1 - p).

Notice that only one of these two terms will be non-zero for each x′x'. Personally, I hate this simple form as this makes it look unnecessarily complicated, but this is a standard formulation that people commonly use.

Notice that we are summing over the data samples, not the all possible values of xx.

Reference