Raw Bit Content and Lossy Counting
The simplest compressor just counts: if a source can produce different outcomes, give each a fixed-length label of bits. This raw bit content ignores probabilities completely. But if we accept a small chance of failure and throw away the least likely outcomes, the count drops dramatically. That simple idea leads straight to the source coding theorem.
Raw bit content
The raw bit content of an ensemble with alphabet is
It is the entropy of a uniform distribution, so . For a block of independent symbols, .
Throwing away unlikely outcomes
Allow the code to fail with probability at most . Then we only need labels for the smallest set of outcomes whose total probability is at least . Its size defines the essential bit content:
To find , sort outcomes by probability and keep the most probable ones until their total reaches .
import numpy as np
def essential_bits(p, delta):
q = np.sort(p)[::-1]
k = int(np.searchsorted(np.cumsum(q), 1 - delta - 1e-12)) + 1
return np.log2(k), k
p = np.array([0.25, 0.25, 0.2, 0.15, 0.1, 0.03, 0.01, 0.01])
print("H0 =", np.log2(len(p)), " H =", round(float(-np.sum(p * np.log2(p))), 4))
for delta in [0.0, 0.01, 0.05, 0.1, 0.3]:
bits, k = essential_bits(p, delta)
print(f"delta = {delta:4.2f}: keep {k} outcomes, H_delta = {bits:.3f} bits")H0 = 3.0 H = 2.4918
delta = 0.00: keep 8 outcomes, H_delta = 3.000 bits
delta = 0.01: keep 7 outcomes, H_delta = 2.807 bits
delta = 0.05: keep 5 outcomes, H_delta = 2.322 bits
delta = 0.10: keep 5 outcomes, H_delta = 2.322 bits
delta = 0.30: keep 3 outcomes, H_delta = 1.585 bits
Blocks of symbols
The effect becomes striking for long blocks. Take flips of a coin with . There are sequences, but sequences with many 1s are extremely rare. Sorting all sequences by probability, how many bits per flip do we need to cover 99% of the probability?
All sequences with the same number of 1s have the same probability , and there are of them, so we can work with counts instead of listing sequences:
import numpy as np
from scipy.special import gammaln
def essential_bits_per_symbol(n, f, delta):
k = np.arange(n + 1)
logc = (gammaln(n + 1) - gammaln(k + 1) - gammaln(n - k + 1)) / np.log(2) # log2 C(n,k)
logp = k * np.log2(f) + (n - k) * np.log2(1 - f) # log2 prob of one sequence
order = np.argsort(-logp) # most probable first (few 1s)
total, count_log2 = 0.0, -np.inf
for kk in order:
need = 1 - delta - total
mass = 2.0 ** (logc[kk] + logp[kk])
if mass >= need: # take only part of this class
count_log2 = np.logaddexp2(count_log2, np.log2(need) - logp[kk]) # log2 of need / p
break
total += mass
count_log2 = np.logaddexp2(count_log2, logc[kk])
return count_log2 / n
f = 0.1
print("H2(0.1) =", round(-f * np.log2(f) - (1 - f) * np.log2(1 - f), 4))
for n in [10, 100, 1000, 10000]:
print(f"n = {n:5d}: H_delta/n = {essential_bits_per_symbol(n, f, 0.01):.4f} (delta = 0.01)")H2(0.1) = 0.469
n = 10: H_delta/n = 0.7837 (delta = 0.01)
n = 100: H_delta/n = 0.6286 (delta = 0.01)
n = 1000: H_delta/n = 0.5315 (delta = 0.01)
n = 10000: H_delta/n = 0.4902 (delta = 0.01)
As grows, the essential bit content per symbol approaches the entropy , far below the raw bit content of 1 bit per flip, and the dependence on disappears. This is the source coding theorem in its counting form (The Source Coding Theorem).
In software
- Lossy counting is exactly what a fixed-length code with an "escape" does: frequent cases get short fixed labels and the rare rest is sent separately or treated as a failure.
scipy.special.gammalncomputes , so for huge without overflow;np.logaddexp2adds numbers stored as base-2 logarithms.
Exercises
Exercise 1: Raw bit content
What is the raw bit content of one character from a 64-symbol alphabet, and of a block of 10 such characters?
Answer
6 bits and 60 bits.
Solution
, and blocks multiply the number of outcomes, so the logarithms add.
Exercise 2: Essential bit content by hand
For and , what is ?
Answer
bits.
Solution
The two most probable outcomes cover 0.8 and the three most probable cover 0.9, so has 3 elements.
Exercise 3: A different bias (code)
Use the function from the lesson for and , . Compare with .
Answer
About 0.195 bits per flip, versus .
Solution
import numpy as np
from scipy.special import gammaln
def essential_bits_per_symbol(n, f, delta):
k = np.arange(n + 1)
logc = (gammaln(n + 1) - gammaln(k + 1) - gammaln(n - k + 1)) / np.log(2)
logp = k * np.log2(f) + (n - k) * np.log2(1 - f)
order = np.argsort(-logp)
total, count_log2 = 0.0, -np.inf
for kk in order:
need = 1 - delta - total
mass = 2.0 ** (logc[kk] + logp[kk])
if mass >= need:
count_log2 = np.logaddexp2(count_log2, np.log2(need) - logp[kk])
break
total += mass
count_log2 = np.logaddexp2(count_log2, logc[kk])
return count_log2 / n
f = 0.02
print(round(essential_bits_per_symbol(1000, f, 0.01), 4), round(-f * np.log2(f) - (1 - f) * np.log2(1 - f), 4))0.1947 0.1414
At the 99% set still needs extra room for fluctuations in the number of 1s; the gap shrinks as grows.
Test yourself
The raw bit content of an ensemble depends on…
- only the number of possible outcomes
- the probabilities of the outcomes
- the entropy
- the block length only
To build the smallest set covering probability , you…
- keep the most probable outcomes first
- keep the least probable outcomes first
- pick outcomes at random
- keep every other outcome
For long blocks of a biased coin, the essential bit content per symbol approaches…
- the entropy
- 1 bit
- 0 bits
- the raw bit content