Entropy
The lower bound of \(\bar \lambda\)
I highly recommend taking a look at the Huffman Coding post first. Huffman coding provides an optimal compression solution for a given data distribution, whereas Shannon-Fano coding does not.
It may be easier for us to first learn Huffman coding (a bottom-up approach to building the tree) in the algorithms class and then move on to Shannon-Fano coding (a top-down approach).
Take a look at the video.
After Claude Shannon introduced entropy in 1948, which defines the theoretical limit for optimal data compression, David A. Huffman built on this in 1952 by creating Huffman coding during his Ph.D.
Recap of Huffman coding
In the last post, we derived the average codeword length of Huffman coding.
\[ \bar \lambda = \mathbb E [ \lambda ] = \sum_{i=1}^N p(x_i) \lambda_i = \sum_{i=1}^N p(x_i) \left\lceil \log_2 \dfrac{1}{p(x_i)} \right\rceil \]
One thing we had a hard time deriving was \(\lambda\). We needed to check the frequency (or probability) of each character and aggregate them into a binary tree structure. After that, we traced the path from the root node to each leaf, encoding every character into a binary codeword. Finally, we mapped all of the codewords to the function len to obtain our most desired value, the length of each codeword.
The value \(\lambda\) can be interpreted as the number of bits (information) required to losslessly represent an arbitrary group (such as a character or color) within the given data. As you can feel from the word group, we were working on a discrete random variable.
We can guarantee the optimality of \(\lambda\) only with the given data distribution. If different data is provided, the frequency (probability) changes, causing the entire Huffman tree to differ from before.
This will reduce \(\bar \lambda\) exceptionally but not with an optimal length. If all characters are the same, the optimal length for the given data will be 0 because the leaf node will also be the root.
Entropy as a lower bound of \(\bar \lambda\)
What if I say we don’t need all of the cumbersome processes mentioned above? Take a look at the formula below.
\[ H(X) = \mathbb E[I(X)] = \sum_{i=1}^N p(x_i) \log_2 \dfrac{1}{p(x_i)} \]
Is entropy just another form of average codeword length?
The answer is no. This works as the theoretical lower bound for any data distribution (both discrete and continuous) when compressing. We can not get below this bound if we are performing a lossless compression.
We can see that the \(\lambda_i\) has turned into a \(\log\) form with a probability. Since \(x_i\) is the only parameter, we can define \(H\) as a scalar funciton for any random vaiable \(X\). As the probability of \(x_i\) increases we can compress the information into a smaller, more compact \(I(x_i)\).
We can set the theoretical lower bound for \(\bar \lambda\) as below.
\[ H(X) \leq \bar \lambda \]
Let’s prove this!
Proofing the lower bound of \(\bar \lambda\)
We need to know some basics of Jensen’s inequality and Kraft-McMillan inequality to prove this. Check out the Jensen’s inequality, Kraft-McMillan inequality posts to see more.
\[ \begin{align*} H(X) - \bar{\lambda} &= \sum_{i=1}^N \left( p(x_i) \dfrac{1}{\log_2 p(x_i)} - p(x_i) \lambda_i \right) \\[10pt] &= \sum_{i=1}^N p(x_i) \left( \dfrac{1}{\log_2 p(x_i)} - \lambda_i \right) \\[10pt] &= \sum_{i=1}^N p(x_i) \left( \dfrac{1}{\log_2 p(x_i)} \log_2 2^{-\lambda_i} \right) \\[10pt] &= \sum_{i=1}^N p(x_i) \log_2 \left( \dfrac{2^{-\lambda_i}}{p(x_i)} \right) \leq \log_2 \sum_{i=1}^N p(x_i) \dfrac{2^{-\lambda_i}}{p(x_i)} &\cdots \text{Jensen's inequality} \\[10pt] &= \log_2 \sum_{i=1}^N 2^{-\lambda_i} \leq \log_2 1 &\cdots \text{Kraft-McMillan inequality} \\[10pt] &= 0 \end{align*} \]
Well, that’s it! For fun, let’s take a look at the graph below.
Think \(\varphi(x)\) as \(-\log_2 p(x)\). You can clearly see that entropy (the blue cross) is working as a lower bound.
We have not talked about the upper bound of \(\bar \lambda\).
\[ H(X) \leq \bar \lambda \lt H(X)+1 \]
The red and blue crosses are the weighted averages of each function’s outputs. Try ripping the crosses apart from each other. Can you make it greater than 1? Probably not. Infinitely approaching all \(p(x_i)\) to values near powers of 2 from the left-hand side is the most probable way, but you won’t be able to get there.
If you do get there, it means you are allowing all \(x_i\) to have an extra bit, which is not optimal in the first place.