Shannon Entropy Calculator

H = −Σ p log₂ p, one row per symbol, with the surprisal, the contribution and the code length it implies.

Distribution

The text below is counted symbol by symbol. A character outside the basic plane counts as one symbol, not two.

Spaces and newlines are symbols like any other. Counting them changes every number on this page, so the choice is a control rather than a default.

Unit

The base of the logarithm is a choice of unit, nothing more: H in nats is H in bits × ln 2, and H in bans is H in bits × log₁₀ 2. The unit name is printed with every figure.

H (bits per symbol)

1.8230679823

H = −Σ p log₂ p over 4 symbols

  • symbols: 4
  • total: 11
  • total information: 20.0537 bits
Note:

Normalisation

Weights sum to 11 — normalised by dividing each by 11. Never assume the input sums to 1. Counts are divided by their total before any log is taken.

Surprisal, symbol by symbol

One row per symbol. The surprisal is the ideal code length for that symbol; the contribution column sums to H, and the f × ℓ column sums to what the Huffman code actually costs.
SymbolCount fp = f/NI = log₂(1/p) (bits)p · IAssigned ℓf × ℓ
I40.3636361.4594320.53070214
M10.0909093.4594320.31449433
P20.1818182.4594320.44716936
S40.3636361.4594320.53070228
total1111.82306821 bits

Maximum entropy for this alphabet

  • H = 1.8230679823 bits/symbol
  • H max = log₂ 4 = 2.0000000000 bits/symbol, reached only by a uniform distribution
  • normalised H = H / H max = 0.9115 (91.15% of the maximum)

0 ≤ H ≤ log₂ m. H is 0 exactly when the source is deterministic and log₂ m exactly when it is uniform.

The source-coding bound

  • H = 1.8230679823 bits/symbol
  • L = 1.9090909091 bits/symbol — the Huffman code for the same distribution
  • H ≤ L: 1.8231 ≤ 1.9091 ✓
  • L < H + 1: 1.9091 < 2.8231 ✓
  • redundancy L − H = 0.0860229268 bits/symbol
  • efficiency H / L = 95.4940%

Shannon proved the expansion is carried out to mₛ places, where log₂(1/pₛ) ≤ mₛ < 1 + log₂(1/pₛ). That inequality, applied symbol by symbol and averaged, is where H ≤ L < H + 1 comes from. It is a statement about Shannon–Fano coding in the original paper; it holds for Huffman codes because a Huffman code is never worse.

Shannon entropy of 4 symbols

4 symbols, 11 total occurrences. Divide by the total to get probabilities, then take −Σ p·log₂ p.

The surprisal column is the ideal code length; Huffman rounds it to whole bits. — Shannon entropy of 4 symbols
SymbolpSurprisal −log₂ p (bits)Contribution p·(−log₂ p)
I0.3636361.4594320.530702
M0.0909093.4594320.314494
P0.1818182.4594320.447169
S0.3636361.4594320.530702

H1.823068 bits/symbolthe probability-weighted mean of the surprisal column

Total information20.053748 bits11 symbols × H

Maximum possible H2.000000 bits/symbollog₂(4), reached only by the uniform distribution

H / log₂(m)0.911534how close to uniform this source is

Warning:

A symbol of probability 0 contributes exactly 0: the convention 0·log₂0 = 0 is a definition (the limit as p → 0), not an approximation. Evaluating it instead returns NaN, which is the usual bug on this page.

Source: C. E. Shannon, Bell System Technical Journal 27:379–423 (1948), Theorem 2

Note:

0 · log₂ 0 is defined as 0

It is the limit as p → 0, and it is a definition rather than an evaluation. Computing it directly gives NaN, so a symbol of probability 0 is special-cased before the logarithm is taken. A symbol that never occurs contributes nothing, and deleting it from the distribution gives the same entropy.

The Huffman code this entropy is compared against

Huffman code over 4 symbols

4 symbols, 11 total occurrences. Repeatedly merge the two lightest nodes until one remains.

Each step removes the two lightest nodes and inserts their parent. — Huffman code over 4 symbols
Step0 child (popped first)1 child (popped second)New nodeTie broken?
1"M" (1)"P" (2)N0 = 3no
2N0 (3)"S" (4)N1 = 7yes
3"I" (4)N1 (7)N2 = 11no
The canonical column has the same lengths, so the total cost is identical. — Huffman code over 4 symbols
SymbolFrequencyProbabilityCodeLengthfreq × lengthCanonical
I40.3636360140
M10.09090910033110
P20.18181810136111
S40.363636112810

Total encoded size21 bitsΣ frequency × code length, which also equals the sum of every internal node weight

Average code length1.909091 bits/symbol

Longest codeword3 bits

Kraft sum Σ 2^(−ℓ)1.000000exactly 1, so the code is complete

Moffat (2019) §2.2: symbols are numbered 0…m−1 in ascending code-point order, and the heap is keyed on the tuple (weight, isInternal ? 1 : 0, tieRank) with tieRank = −symbolIndex for leaves and the creation sequence number for internal nodes. So: on a tie between leaves the higher-numbered symbol is preferred; on a tie between a leaf and an internal node the leaf is preferred; on a tie between internal nodes the one formed earlier is preferred. The first node popped becomes the 0 (left) child.

Warning:

1 of the 3 merges had to break a weight tie, so this is ONE of several equally optimal codes — a textbook that breaks ties differently will show different bit patterns for the same total cost. The rule in force is stated above.

Source: D. A. Huffman, Proceedings of the IRE 40(9):1098–1101 (1952); tie-break per A. Moffat, ACM Comput. Surv. 51(4) §2.2 (2019)

Note:

Notation used on this page

  • H is an average over a distribution, not a property of a particular message.
  • Surprisal is I(x) = log(1/p) = −log p, in the unit selected above; entropy is its probability-weighted mean.
  • Counts and probabilities are the same input: weights are divided by their total before any logarithm is taken.
  • A symbol of probability 0 contributes exactly 0 by the convention 0·log 0 = 0, and its surprisal is reported as ∞.
  • ℓ is the codeword length the Huffman code for the same distribution assigns, shown for comparison with the surprisal.

Start from a worked example

What entropy measures

Entropy is the average number of bits you need to identify one symbol drawn from a known distribution. It is a property of the distribution, not of any particular message.

Two consequences follow immediately, and both catch people out. Shuffling a message does not change its entropy, because the counts do not change. And a message can be highly compressible while its first-order entropy is high, because entropy as computed here knows nothing about the order the symbols arrive in — see the last section.

Surprisal: the information in one symbol

The information carried by one occurrence of a symbol is its surprisal, I(x) = log₂(1/p) = −log₂ p. An event of probability ½ carries exactly one bit; an event of probability ¼ carries two; an event that is certain carries none, because you learn nothing by observing what you already knew.

Surprisal is also the ideal code length for that symbol: a symbol you expect one time in eight deserves three bits and no more. The table above prints it per symbol, next to the whole-bit length a real prefix code has to round it to.

The entropy formula

Entropy is the probability-weighted mean of surprisal: H = Σ pᵢ·I(xᵢ) = −Σ pᵢ log₂ pᵢ. Every step of that sum for the current input is above — normalise, take a logarithm per symbol, weight, add — with the numbers substituted rather than a bare formula printed. For MISSISSIPPI it lands at H = 1.8230679823 bits per symbol, and 11 symbols carry 20.0537 bits in total.

The form is not a modelling choice. Shannon’s Theorem 2 shows it is forced: “The only H satisfying the three above assumptions is of the form H = −K Σ pᵢ log pᵢ” — where the assumptions are that H is continuous in the pᵢ, that it increases with m for a uniform source, and that it is unchanged when a choice is broken into successive choices. The constant K “merely amounts to a choice of a unit of measure”; K = 1 with base-2 logarithms gives bits.

Never assume the input sums to 1. Counts are divided by their total before any log is taken. The normalisation is printed above the table rather than performed silently, because a tool that accepts both counts and probabilities and never says which it assumed is unusable for checking an answer.

Units: bits, nats and bans

Changing the base of the logarithm changes the unit and nothing else, so the toggle above converts rather than recomputes: H in another base is H in bits multiplied by a constant. The unit name is printed with every figure, because “H = 1.26” means different things in a statistics paper and a coding-theory paper.

The three log bases in common use
UnitLogarithmFrom bitsWhere you meet it
bit (shannon)log₂1the default everywhere on this site
natln× ln 2 ≈ 0.693147the natural unit; standard in statistics and machine learning
ban (hartley)log₁₀× log₁₀ 2 ≈ 0.301030Bletchley Park’s unit, and the one decimal digit of evidence

Bounds: 0 ≤ H ≤ log₂ m

Entropy is never negative and never exceeds log₂ m for an alphabet of m symbols. H = 0 happens exactly when the source is deterministic — one symbol has probability 1 — and H = log₂ m happens exactly when the distribution is uniform. Everything else sits strictly between, and the ratio H / H max is how far along that range a distribution is.

For MISSISSIPPI, m = 4, so H max = 2.0000000000 bits per symbol and H is 91.1534% of it. The distribution is close to uniform in the sense the ratio measures, and the two symbols that make it uneven — the single M against four Is — are exactly the ones a code can exploit.

Maximum entropy by alphabet size
Alphabet size mH max = log₂ m (bits/symbol)
10.0000000000
21.0000000000
31.5849625007
42.0000000000
52.3219280949
83.0000000000
164.0000000000
264.7004397181
2568.0000000000

Total information

A message of N symbols from this distribution carries N × H bits of information — 20.0537 bits here. That figure is a floor for an ideal code, not a file size: it assumes the decoder already knows the distribution, and it does not include the cost of transmitting the codebook, the file header or anything else.

It is also not an integer, and cannot be rounded down. A real encoding of these 11 symbols needs at least ⌈N × H⌉ bits, and a prefix code needs more than that — 21 bits here, as the next section explains.

0 · log₂ 0 = 0

A symbol of probability 0 contributes exactly 0 to the sum. That is a definition taken from the limit — p log₂ p → 0 as p → 0 — and not an evaluation: computing it directly gives 0 × (−∞) = NaN, so the zero case is handled before the logarithm is taken.

The consequence is worth stating, because it is the check that the convention is the right one: a distribution containing zero-weight symbols has exactly the same entropy as the same distribution with those symbols deleted. A symbol that never occurs tells you nothing when it does not occur.

The source-coding bound, H ≤ L < H + 1

The entropy figure means something because a code is measured against it. L is the average codeword length of the Huffman code for the same distribution, computed here by the same implementation that page uses, so the two can never disagree about the input.

For MISSISSIPPI: H = 1.8230679823, L = 1.9090909091, so H ≤ L holds with a redundancy of L − H = 0.0860229268 bits per symbol and an efficiency of H / L = 95.4940%. The upper bound holds too, with room to spare.

The bound is Shannon’s, not folklore. §9 of the 1948 paper: “The expansion is carried out to mₛ places, where mₛ is the integer satisfying: log₂(1/pₛ) ≤ mₛ < 1 + log₂(1/pₛ)”. That inequality, applied symbol by symbol and averaged, is where H ≤ L < H + 1 comes from. It is a statement about Shannon–Fano coding in the original paper; it holds for Huffman codes because a Huffman code is never worse.

Equality, L = H, happens exactly when every probability is a power of ½. Then each ideal length log₂(1/p) is already a whole number and there is nothing to round. The dyadic distribution A 8, B 4, C 2, D 2 gives H = L = 1.75 exactly; a uniform alphabet of 4 gives 2 and of 8 gives 3. Every other distribution pays something for the rounding.

The upper bound can be very loose. With weights 96, 1, 1, 1, 1 the entropy is 0.3222921891 bits per symbol while the Huffman code averages 1.08 — 235% above the floor. No prefix code can spend less than one whole bit on a symbol, and this source wants about 0.06 bits on its dominant symbol. Arithmetic coding is what closes that gap.

There is exactly one case where the strict upper bound fails, and it is a convention rather than a defect. For a single-symbol alphabet H = 0, and this toolkit emits a one-bit codeword because a zero-length codeword cannot be transmitted — so L = 1 and L < H + 1asks for 1 < 1, which is false. Under the alternative zero-length convention L = H = 0 and the bound holds trivially. The page reports the failure rather than rounding it away; a test that asserted the bound universally would be asserting something false.

This is not thermodynamic entropy

Shannon’s H and Boltzmann’s S = k log W have the same shape, and the connection between them is real and well studied. This calculator answers the information-theoretic question only: how many bits per symbol a source produces. Nothing on this page is a statement about heat, disorder or any physical system, and the numbers here should not be carried into a physics problem.

What this calculator does not model

This is first-order entropy: symbols are assumed independent, and only their counts are used. Real sources have structure, and conditional entropy H(X|Y) — how surprised you are by the next symbol given the previous one — is lower, often far lower.

English is the standard illustration. The first-order entropy of English letter frequencies is about 4.18 bits per letter against a maximum of log₂ 26 = 4.7004 bits, so letter frequency alone saves about half a bit. But letters are not independent: q is followed by u, this everywhere, and Shannon’s own estimate for English text with context taken into account is closer to one bit per letter. A first-order entropy of 4.18 bits per letter does not mean English needs 4.18 bits per letter; it means an encoder that ignores everything except letter frequency needs that much.

The same caveat applies to every number on this page, and it is why a compressor that models context beats one that only counts symbols. It is not a limitation of the formula — it is what the formula was asked.

Notation used on this page

  • Logarithms are base 2 unless the unit toggle says otherwise, and the unit is named with every figure.
  • p is a probability in [0, 1]; counts are normalised by their total before any logarithm is taken, and the normalisation is shown.
  • 0 · log 0 is defined as 0, by the limit.
  • I(x) = −log₂ p(x) is the surprisal, or self-information, of one symbol, and H is its probability-weighted mean.
  • L is the average codeword length of the Huffman code over the same distribution, computed by the same code as the Huffman page, with a one-symbol alphabet given the one-bit code 0.
  • Entropies are displayed to ten decimal places and computed in a form that is exact for dyadic distributions.
  • Whitespace is counted as a symbol unless the switch above is turned off, because that choice changes every number on the page.

Sources

Worked examples