Learn

Entropy and Compression: What Can a Pattern Save?

Information entropy describes uncertainty under a stated probability model. It helps explain average lossless coding limits, but it does not predict a universal compressed size for every individual file.

Begin with the model

Compression can exploit regularity only when the representation gives the decoder enough information to recover the intended input. Entropy formalizes one part of that problem: the average uncertainty associated with outcomes under a probability model. It is not a measure of how meaningful a document is to a person.

For a discrete source with probabilities p, the Shannon entropy in bits is the sum of p times log2(1/p) over outcomes with positive probability. A zero-probability outcome contributes zero by convention. MIT’s lecture on data compression develops the connection between probability and coding limits. Data compression and Shannon entropy.

Work through an original four-symbol example

Imagine a source producing four symbols: red with probability one half, blue with probability one quarter, and green and gold with probability one eighth each. A fixed-width representation would use two bits per symbol.

Consider this prefix code: red is 0, blue is 10, green is 110, and gold is 111. No codeword is a prefix of another codeword, so a decoder can identify symbols as it reads the stream. The expected length is one half times one bit, plus one quarter times two bits, plus two times one eighth times three bits. That totals 1.75 bits per symbol.

For this particular distribution, the entropy calculation also gives 1.75. The example shows how assigning shorter codes to more likely outcomes can reduce average length. It does not claim that a real file always reaches that average, or that every probability distribution admits an equally neat single-symbol code.

Average length is not an individual promise

A file containing an unexpectedly high number of green symbols could use more bits with this code than a file dominated by red. The model describes expected behavior over its source distribution. A different workload may not follow it.

Real formats also need framing, model information, and other overhead. If the reader does not already know the code table, the encoder must somehow communicate it or agree on it in advance. For very small messages, that surrounding information can outweigh the savings from shorter symbol codes.

Patterns can exist beyond individual symbols

Two files can have the same counts of individual bytes but different arrangements. One might repeat a long phrase; the other might scatter the same bytes without that repeated phrase. A model that considers only individual byte frequencies misses that difference.

This is why an observed byte-frequency estimate should not be presented as the definitive limit for every compressor. A method can exploit longer context or other structure that a simpler model ignores. DEFLATE combines repeated-sequence references with Huffman coding, providing a real example of more than one mechanism within a format. DEFLATE specification.

Why a universal lossless shrinker cannot work

There are more possible fixed-length input strings than there are shorter strings available to represent all of them uniquely. If every input had to shrink and remain uniquely decodable, the representations would run out. Some inputs must stay the same length or grow once the full encoding is counted.

That counting argument is useful when evaluating extraordinary claims. Ask which input family is supported, which model is assumed, and what overhead is included. A strong result on structured records may be valuable without applying to all possible byte sequences.

Continue with dictionary compression for repeated values and phrases, or lossless and lossy compression for the difference between exact recovery and controlled information loss.