One interpretation of entropy of a random variable is as a measure of how many unbiased, independent random bits in expectation we can extract from the random variable. Another one comes from compression of a sequence.

Assume that the sequence studied (denoted as ss) is a binary one. It consists of a concatenation of the outcomes of nn independent Bernoulli random variables. Without lose of generality, we assume that each bit comes up 11 with probability p≤1/2p \le 1 / 2. This is one of the simplest models of a sequence.

The fact that sequence could be a biased one makes it possible to represent it by a new sequence with shorter length in expectation. For example, suppose that p=14p = \frac{1}{4}, then for a pair of consecutive bits, it have

  1. probability 116\frac{1}{16} of being 1111,
  2. probability 316\frac{3}{16} of being 0101,
  3. probability 316\frac{3}{16} of being 1010,
  4. probability 916\frac{9}{16} of being 0000.

If we use 111111 to represent 1111, 110110 to represent 0101, 1010 to represent 1010, and 00 to represent 0000, then the expected number of representation bits per pair is

3⋅116+3⋅316+2⋅316+1⋅916=2716<2 \begin{array}{r} 3 \cdot \frac{1}{16} + 3 \cdot \frac{3}{16} + 2 \cdot \frac{3}{16} + 1 \cdot \frac{9}{16} = \frac{27}{16} < 2 \end{array}

In general, compression is not limited to the way described above. We call the rule with which we compress a string a compression function.

Compression Function

Definition. Let S={0,1}nS = \{0, 1\}^n be the set of binary sequence of length nn, and T={0,1}+T = \{ 0, 1\}^+ the set of binary sequence of any positive length. A compression function h:S→Th: S \rightarrow T is an injective (one-to-one) function from SS to TT.

In other words, each s∈Ss \in S is assigned a unique non-empty sequence (of arbitrary length) by hh.

Observe that the size of SS is 2n2^n. On the other hand, the number of sequences with length less than nn is ∑i=1n−12i=2n−1<2n\sum_{i = 1}^{n - 1} 2^{i} = 2^n - 1 < 2^n. For any hh, it is impossible for hh to map every s∈Ss \in S to a sequence with length less than nn. Under adversarial input, hh can not compress at all.

The hope is that when there is a distribution D\mathfrak{D} on SS, the expected length of the compressed sequences would be small:

E[∣h(s)∣]=∑s∈Sps⋅∣h(s)∣ \mathbf{E} [ |h(s)| ] = \sum_{s \in S} p_s \cdot |h(s)|

To illustrate the meaning of entropy, we study a simple case where D\mathfrak{D} is the joint distribution of nn i.i.d. Bernoulli random variables that come up 11 with probability p≤1/2p \le { 1 / 2 } (by symmetry, the claim holds for the case of p>1/2p > { 1 / 2 }).

The following theorem formalizes how good a compression function we can find.

Entropy as Lower and Upper Bounds

Theorem.

  1. ∀δ>0\forall \delta > 0, ∃\exists a compression hh and integer N>0N > 0, s.t., ∀n≥N\forall n \ge N, it holds that
E[∣h(s)∣]≤(1+δ)nH(p) \mathbf{E}[ |h(s)| ] \le (1 + \delta) n \mathbf{H}(p)
  1. ∀δ>0\forall \delta > 0, ∃\exists integer N>0N > 0, s.t., ∀n≥N\forall n \ge N, it holds that for any compression hh,
E[∣h(s)∣]≥(1−δ)nH(p) \mathbf{E}[ |h(s)| ] \ge (1 - \delta) n \mathbf{H}(p)

Intuitively, the entropy H(p)\mathbf{H}(p) is the measure of the average number of bits in expectation we need after compression to represent a Bernoulli random variable that comes up 11 with probability pp.

The high level idea of the proof is that, with high probability, the number of ones in a s∈Ss \in S is roughly npnp. There are roughly 2nH(p)2^{n\mathbf{H}(p)} such sequences. Therefore, we can use nH(p)n \mathbf{H}(p) bits to represent each sequence.

Proof. The claim is trivially true if p=1/2p = { 1 / 2 }. Otherwise, p<1/2p < { 1 / 2 }. We can pick some ϵ>0\epsilon > 0, such that p+ϵ<1/2p + \epsilon < { 1 / 2 }. Further, if nn is large enough, np+nϵ≤n/2−1np + n\epsilon \le n / 2 - 1. Hence, we may assume that ⌈np+nϵ⌉≤n/2\lceil np + n\epsilon \rceil \le n / 2 .

Denote ZZ be the number of ones in the string ss. By Hoeffding Inequality, it holds that

Pr⁡[∣Z−np∣≥nϵ]≤exp⁡(−2nϵ2) \Pr[ |Z - np| \ge n\epsilon ] \le \exp(-2n \epsilon^2)

By the fact that ZZ takes only integer values, we have

Pr⁡[Z≥⌈np+nϵ⌉]≤exp⁡(−2nϵ2) \Pr[Z \ge \lceil np + n\epsilon \rceil] \le \exp(-2 n \epsilon^2 )

We use the first bit output of hh as a flag. For a string ss with Z≥⌈np+nϵ⌉Z \ge \lceil np + n\epsilon \rceil, we set the first bit to 00 and then output the same sequence, i.e., h(s)=0∘sh(s) = 0 \circ s. In such case we do not compress the string at all and use n+1n + 1 bits to represent it.

We set the first bit output to 11 if Z<⌈np+nϵ⌉≤n/2Z < \lceil np + n \epsilon \rceil \le n / 2. The number of such sequences is bounded by

∑k=0⌈np+nϵ⌉−1(nk)≤n2(n⌈np+nϵ⌉−1)≤n22n⋅H(⌈np+nϵ⌉−1n)≤n22nH(p+ϵ) \sum_{k = 0}^{\lceil np + n\epsilon \rceil - 1} \binom{n}{k} \le \frac{n}{2} \binom{n}{\lceil np + n\epsilon \rceil - 1 } \le \frac{n}{2} 2^{ n \cdot \mathbf{H} \left( \frac{ \lceil np + n\epsilon \rceil - 1 }{n} \right) } \le \frac{n}{2} 2^{ n \mathbf{H} \left( p + \epsilon \right) }

The first inequality holds because (nk)\binom{n}{k} increases for k<n/2k < n / 2 and that the summation is over ⌈np+nϵ⌉≤n/2\lceil np + n \epsilon \rceil \le n / 2 terms. The last inequality holds since (⌈np+nϵ⌉−1)/n≤(np+nϵ)/n=p+ϵ<1/2(\lceil np + n\epsilon \rceil - 1) / n \le ( np + n\epsilon ) / n = p + \epsilon < 1 / 2 and H(⋅)\mathbf{H}( \cdot ) is a increasing function in the range [0,1/2][0, 1/2].

We compress these sequences by representing each of them with a unique sequence of at most

log⁡[(n/2)⋅2n⋅H(p+ϵ)]=n⋅H(p+ϵ)+log⁡n−1 \begin{array}{rl} \log \left[ (n / 2) \cdot 2^{ n \cdot\mathbf{H} \left( p + \epsilon \right) } \right] = n \cdot\mathbf{H} \left( p + \epsilon \right) + \log n -1 \end{array}

bits. Considering the leading flag bit 1 (to indicate the sequence is a compressed one), we output at most nH(p+ϵ)+log⁡nn \mathbf{H} \left( p + \epsilon \right) + \log n bits for sequences with Z<⌈np+nϵ⌉Z < \lceil np + n \epsilon \rceil. This can be written as [H(p+ϵ)+(1/n)⋅(log⁡n−1)]n\left[ \mathbf{H} \left( p + \epsilon \right) + (1 / n) \cdot (\log n - 1) \right] n bits and is smaller than

(1+δ/2)⋅H(p)⋅n (1 + \delta /{2} ) \cdot \mathbf{H} (p) \cdot n

is ϵ\epsilon is smaller enough and nn is large enough.

We are almost done with the proof. It is left to consider sequences with Z≥⌈np+nϵ⌉Z \ge \lceil np + n \epsilon \rceil. For those sequence, we don't compress them and and set the leading flag bit to 0. The outputs for those sequences consist of at most 1+n1 + n bits. However, by Hoeffding inequality, such sequences do not appear frequently and the expected output length can be made arbitrary small.

Specifically, ∀δ>0\forall \delta > 0, we can find large enough NN, such that ∀n>N\forall n > N, it holds

(n+1)⋅exp⁡(−2nϵ2)≤n⋅(δ/2)⋅H(p) (n + 1 ) \cdot \exp(-2 n \epsilon^2 ) \le n \cdot (\delta / 2) \cdot \mathbf{H}(p)

Then, in expectation, the bits outputted by hh is at most

(n+1)⋅exp⁡(−2nϵ2)+[n⋅H(p+ϵ)+log⁡n]⋅[1−exp⁡(−2nϵ2)]≤n⋅(δ/2)⋅H(p)+[n⋅H(p+ϵ)+log⁡n]≤n⋅(δ/2)⋅H(p)+(1+δ/2)⋅H(p)⋅n≤(1+δ)⋅H(p)⋅n \begin{aligned} &(n + 1) \cdot \exp(-2 n \epsilon^2 ) + [n \cdot \mathbf{H} \left( p + \epsilon \right) + \log n ] \cdot [ 1 - \exp(-2 n \epsilon^2 ) ] \\ \le& n \cdot (\delta / 2) \cdot \mathbf{H}(p) + [ n \cdot \mathbf{H} \left( p + \epsilon \right) + \log n ]\\ \le& n \cdot (\delta / 2) \cdot \mathbf{H}(p) + (1 + \delta /{2} ) \cdot \mathbf{H} (p) \cdot n\\ \le& \left( 1 + \delta \right) \cdot \mathbf{H}(p) \cdot n \end{aligned}

To prove the lower bound, first we need the following lemma.

Lemma. for s1,s2∈Ss_1, s_2 \in S, if s1s_1 has more ones than s2s_2, i.e., Pr⁡(s1)≥Pr⁡(s2)\Pr(s_1) \ge \Pr(s_2), then the hh that minimizes the expected output length E[∣h(s)∣]\mathbf{E}[ |h(s)| ] should assign s1s_1 a sequence at most as long as s2s_2, i.e., ∣h(s1)∣≤∣h(s2)∣|h(s_1)| \le |h(s_2)|.

Proof. Otherwise, if we swap the output sequences of h(s1)h(s_1) and h(s2)h(s_2), we lower value of E[∣h(s)∣]\mathbf{E}[ |h(s)| ].

□\square

Further, consider the number of s∈Ss \in S with ⌊np−nϵ⌋\lfloor np - n \epsilon \rfloor ones,

(n⌊np−nϵ⌋)≥1n+12n⋅H(⌊np−nϵ⌋/n)≥1n+12n⋅H((np−nϵ−1)/n)≥2⌊n⋅H(p−ϵ−1/n)−log⁡(n+1)⌋ \begin{aligned} \binom{n}{\lfloor np - n\epsilon \rfloor } &\ge \frac{1}{n + 1} 2^{ n \cdot \mathbf{H} \left( { \lfloor np - n\epsilon \rfloor } / { n} \right) } \\ &\ge \frac{1}{n + 1} 2^{ n \cdot \mathbf{H}( { (np - n\epsilon - 1) } / {n} )} \\ &\ge 2^{ \lfloor n \cdot \mathbf{H}( p - \epsilon -1 / n) - \log (n + 1) \rfloor } \end{aligned}

Let k=⌊n⋅H(p−ϵ−1/n)−log⁡(n+1)⌋k = \lfloor n \cdot \mathbf{H}( p - \epsilon -1 / n) - \log (n + 1) \rfloor. For large enough nn, it holds that

k≥(1−δ/2)⋅n⋅H(p) k \ge (1 - \delta / 2) \cdot n \cdot \mathbf{H}(p)

Further, since

∑i=1k−12i≤∑i=0k−12i=2k−1<2k, \sum_{i = 1}^{ k - 1 } 2^i \le \sum_{i = 0}^{ k - 1 } 2^i = 2^{ k } - 1 < 2^k,

there are strictly less than 2k2^k distinct binary sequences with length at most k−1k - 1.

Therefore, for the set of s∈Ss \in S with ⌊np−nϵ⌋\lfloor np - n\epsilon \rfloor ones, at least one of them has output length at least kk.

By the previous lemma, all sequences with more than ⌊np−nϵ⌋\lfloor np - n\epsilon \rfloor ones has length at least kk.

Denote ZZ be the number of ones in the string ss. By Hoeffding Inequality, it holds that

Pr⁡[∣Z−np∣≥nϵ]≤exp⁡(−2nϵ2) \Pr[ |Z - np| \ge n\epsilon ] \le \exp(-2n \epsilon^2)

By the fact that ZZ takes only integer values, we have

Pr⁡[Z≤⌊np−nϵ⌋]≤exp⁡(−2nϵ2) \Pr[Z \le \lfloor np - n\epsilon \rfloor] \le \exp(-2 n \epsilon^2 )

The expected output length of the sequences with more than ⌊np−nϵ⌋\lfloor np - n\epsilon \rfloor ones is at least

[1−exp⁡(−2nϵ2)]⋅k=[1−exp⁡(−2nϵ2)]⋅(1−δ/2)⋅n⋅H(p) \begin{aligned} &[ 1 - \exp(-2n\epsilon^2) ] \cdot k \\ =&[ 1 - \exp(-2n\epsilon^2) ] \cdot (1 - \delta / 2) \cdot n \cdot \mathbf{H}(p) \end{aligned}

This is at least (1−δ)⋅n⋅H(p)(1 - \delta) \cdot n \cdot \mathbf{H}(p) when nn is large enough.

■\blacksquare

Huffman Code

In this section, we show that the upper bound can be achieved by Huffman code.

∀δ>0\forall \delta > 0, ∃\exists a compression hh and integer N>0N > 0, s.t., ∀n≥N\forall n \ge N, it holds that

E[∣h(s)∣]≤(1+δ)nH(p) \mathbf{E}[ |h(s)| ] \le (1 + \delta) n \mathbf{H}(p)

We begin with an important property of Huffman Code. Suppose that we have an alphabet U\mathbb{U} such that probability associated with each element in U\mathbb{U} is 2−l2^{-l} for some integer ll. Then ∃h\exists h, such that

EX∈U[∣h(X)∣]=nH(X) \mathbf{E}_{X \in \mathbb{U} } [ |h(X)| ] = n \mathbf{H}(X)

For example, if U={0,1,2,3}\mathbb{U} = \{0, 1, 2, 3 \} and the distribution p={0.5,0.25,0.125,0.125}p = \{ 0.5, 0.25, 0.125, 0.125 \}, then we can have the following encoding

The expected coding length is

0.5⋅1+0.25⋅2+2⋅0.125⋅3=1.75 0.5 \cdot 1 + 0.25 \cdot 2 + 2 \cdot 0.125 \cdot 3 = 1.75

which is exactly the entropy of the random variable.

In general, if we have a random variable XX, we can round down its probability to the nearest integer negative power of 22. The expected code length is given by

∑ipi⌈log⁡1pi⌉≤H(p)+1 \sum_{i } p_i \left\lceil \log \frac{1}{p_i} \right\rceil \le \mathbf{H}(p) + 1

Intuitively, the ⌈log⁡1pi⌉\left\lceil \log \frac{1}{p_i} \right\rceil has enough slot to accommodate all elements with probabilities pip_i's.

In particular, we view {0,1}n\{0, 1\}^n as a large alphabet Σ\mathbf{\Sigma }. The alphabet has entropy n⋅H(p)n \cdot \mathbf{H}(p). We can have a encoding such that the expected output length is at most n⋅H(p)+1n \cdot \mathbf{H}(p) + 1. For large enough nn, this is at most (1+δ)n⋅H(p)(1 + \delta) n \cdot \mathbf{H}(p).

Reference

[1] M. Mitzenmacher and E. Upfal, Probability and computing: an introduction to randomized algorithms and probabilistic analysis. New York: Cambridge University Press, 2005.