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 s) is a binary one.
It consists of a concatenation of the outcomes of n independent Bernoulli random variables.
Without lose of generality, we assume that each bit comes up 1 with probability p≤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=41, then for a pair of consecutive bits, it have
probability 161 of being 11,
probability 163 of being 01,
probability 163 of being 10,
probability 169 of being 00.
If we use 111 to represent 11, 110 to represent 01, 10 to represent 10, and 0 to represent 00, then the expected number of representation bits per pair is
3⋅161+3⋅163+2⋅163+1⋅169=1627<2
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}n be the set of binary sequence of length n, and T={0,1}+ the set of binary sequence of any positive length. A compression function h:S→T is an injective (one-to-one) function from S to T.
In other words, each s∈S is assigned a unique non-empty sequence (of arbitrary length) by h.
Observe that the size of S is 2n. On the other hand, the number of sequences with length less than n is ∑i=1n−12i=2n−1<2n.
For any h, it is impossible for h to map every s∈S to a sequence with length less than n.
Under adversarial input, h can not compress at all.
The hope is that when there is a distribution D on S, the expected length of the compressed sequences would be small:
E[∣h(s)∣]=s∈S∑ps⋅∣h(s)∣
To illustrate the meaning of entropy, we study a simple case where D is the joint distribution of ni.i.d. Bernoulli random variables that come up 1 with probability p≤1/2 (by symmetry, the claim holds for the case of p>1/2).
The following theorem formalizes how good a compression function we can find.
Entropy as Lower and Upper Bounds
Theorem.
∀δ>0, ∃ a compression h and integer N>0, s.t., ∀n≥N, it holds that
E[∣h(s)∣]≤(1+δ)nH(p)
∀δ>0, ∃ integer N>0, s.t., ∀n≥N, it holds that for any compression h,
E[∣h(s)∣]≥(1−δ)nH(p)
Intuitively, the entropy 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 1 with probability p.
The high level idea of the proof is that, with high probability, the number of ones in a s∈S is roughly np. There are roughly 2nH(p) such sequences. Therefore, we can use nH(p) bits to represent each sequence.
Proof. The claim is trivially true if p=1/2. Otherwise, p<1/2. We can pick some ϵ>0, such that p+ϵ<1/2. Further, if n is large enough, np+nϵ≤n/2−1. Hence, we may assume that ⌈np+nϵ⌉≤n/2 .
Denote Z be the number of ones in the string s. By Hoeffding Inequality, it holds that
Pr[∣Z−np∣≥nϵ]≤exp(−2nϵ2)
By the fact that Z takes only integer values, we have
Pr[Z≥⌈np+nϵ⌉]≤exp(−2nϵ2)
We use the first bit output of h as a flag. For a string s with Z≥⌈np+nϵ⌉, we set the first bit to 0 and then output the same sequence, i.e., h(s)=0∘s. In such case we do not compress the string at all and use n+1 bits to represent it.
We set the first bit output to 1 if Z<⌈np+nϵ⌉≤n/2. The number of such sequences is bounded by
The first inequality holds because (kn) increases for k<n/2 and that the summation is over ⌈np+nϵ⌉≤n/2 terms. The last inequality holds since (⌈np+nϵ⌉−1)/n≤(np+nϵ)/n=p+ϵ<1/2 and H(⋅) is a increasing function in the range [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+ϵ)+logn−1
bits. Considering the leading flag bit 1 (to indicate the sequence is a compressed one), we output at most nH(p+ϵ)+logn bits for sequences with Z<⌈np+nϵ⌉. This can be written as [H(p+ϵ)+(1/n)⋅(logn−1)]n bits and is smaller than
(1+δ/2)⋅H(p)⋅n
is ϵ is smaller enough and n is large enough.
We are almost done with the proof. It is left to consider sequences with Z≥⌈np+nϵ⌉. 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+n bits. However, by Hoeffding inequality, such sequences do not appear frequently and the expected output length can be made arbitrary small.
Specifically, ∀δ>0, we can find large enough N, such that ∀n>N, it holds
(n+1)⋅exp(−2nϵ2)≤n⋅(δ/2)⋅H(p)
Then, in expectation, the bits outputted by h is at most
To prove the lower bound, first we need the following lemma.
Lemma.for s1,s2∈S, if s1 has more ones than s2, i.e., Pr(s1)≥Pr(s2), then the h that minimizes the expected output length E[∣h(s)∣] should assign s1 a sequence at most as long as s2, i.e., ∣h(s1)∣≤∣h(s2)∣.
Proof. Otherwise, if we swap the output sequences of h(s1) and h(s2), we lower value of E[∣h(s)∣].
□
Further, consider the number of s∈S with ⌊np−nϵ⌋ ones,
Let k=⌊n⋅H(p−ϵ−1/n)−log(n+1)⌋. For large enough n, it holds that
k≥(1−δ/2)⋅n⋅H(p)
Further, since
i=1∑k−12i≤i=0∑k−12i=2k−1<2k,
there are strictly less than 2k distinct binary sequences with length at most k−1.
Therefore, for the set of s∈S with ⌊np−nϵ⌋ ones, at least one of them has output length at least k.
By the previous lemma, all sequences with more than ⌊np−nϵ⌋ ones has length at least k.
Denote Z be the number of ones in the string s. By Hoeffding Inequality, it holds that
Pr[∣Z−np∣≥nϵ]≤exp(−2nϵ2)
By the fact that Z takes only integer values, we have
Pr[Z≤⌊np−nϵ⌋]≤exp(−2nϵ2)
The expected output length of the sequences with more than ⌊np−nϵ⌋ ones is at least
=[1−exp(−2nϵ2)]⋅k[1−exp(−2nϵ2)]⋅(1−δ/2)⋅n⋅H(p)
This is at least (1−δ)⋅n⋅H(p) when n is large enough.
■
Huffman Code
In this section, we show that the upper bound can be achieved by Huffman code.
∀δ>0, ∃ a compression h and integer N>0, s.t., ∀n≥N, it holds that
E[∣h(s)∣]≤(1+δ)nH(p)
We begin with an important property of Huffman Code. Suppose that we have an alphabet U such that probability associated with each element in U is 2−l for some integer l. Then ∃h, such that
EX∈U[∣h(X)∣]=nH(X)
For example, if U={0,1,2,3} and the distribution 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
which is exactly the entropy of the random variable.
In general, if we have a random variable X, we can round down its probability to the nearest integer negative power of 2. The expected code length is given by
i∑pi⌈logpi1⌉≤H(p)+1
Intuitively, the ⌈logpi1⌉ has enough slot to accommodate all elements with probabilities pi's.
In particular, we view {0,1}n as a large alphabet Σ. The alphabet has entropy n⋅H(p). We can have a encoding such that the expected output length is at most n⋅H(p)+1. For large enough n, this is at most (1+δ)n⋅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.