Shannon Entropy Calculator

Shannon Entropy Calculator

Measure the uncertainty, or average information content, of a probability distribution, a set of counts or a piece of text, in bits, nats or dits.

H = −Σ p log₂ p

Enter probabilities that add up to 1

What Is Shannon Entropy?

Shannon entropy is the average amount of information, or surprise, in the outcome of a random process. Measured in bits, it's the average number of yes/no questions needed to pin down the outcome.

A fair coin has entropy 1 bit: one question settles it. A coin that always lands heads has entropy 0, because there's nothing to learn. Entropy is highest when every outcome is equally likely and falls as the distribution becomes more predictable.

Claude Shannon defined it in 1948 to set the limit on data compression: no lossless code can average fewer bits per symbol than the source's entropy. The same quantity measures diversity in ecology and uncertainty in machine learning.

Shannon Entropy Formula

As given in Shannon (1948) and Cover & Thomas, Elements of Information Theory, chapter 2. See Sources.

1. Entropy

H = −Σ pᵢ log_b pᵢ, with 0 × log 0 taken as 0

b = 2 gives bits, b = e gives nats, b = 10 gives dits (hartleys).

2. Maximum and efficiency

H_max = log_b K · efficiency = H ÷ H_max

3. Perplexity

perplexity = b^H

The number of equally likely outcomes that would have the same entropy: an "effective number of choices".

Worked Example: A Fair Die Against a Loaded One

Given:
A fair die, and a loaded die that lands on six 50% of the time and on each other face 10%.
Step 1:
Fair die:
H = log₂ 6 = 2.585 bits
Step 2:
Loaded die:
−(0.5 log₂ 0.5 + 5 × 0.1 log₂ 0.1) = 0.5 + 1.661 = 2.161 bits
Step 3:
Efficiency and perplexity:
83.6% of the maximum · like 4.47 equally likely faces
Result:
2.161 bits against 2.585

Loading the die removes about 0.42 bits of uncertainty per roll. A cheat who knows the bias can, on average, guess outcomes as if the die had only 4.5 sides.

Shannon Entropy of Familiar Sources

Entropy of common random sources in bits
SourceEntropy (bits)
Fair coin1
Coin landing heads 90% of the time0.469
Fair six-sided die2.585
One random letter A–Z4.7
One random digit 0–93.322
Card drawn from a 52-card deck5.7

Common Shannon Entropy Mistakes

  • Mixing logarithm bases. 1 nat ≈ 1.443 bits. Comparing an entropy in bits with one in nats gives nonsense.
  • Treating letter frequencies as the entropy of a language. Single-letter counts ignore context; English has roughly 1 to 1.5 bits per letter once context is used, far below the single-letter figure.
  • Using it to rate password strength. The entropy of the characters in one password says little; what matters is how the password was chosen.
  • Forgetting small-sample bias. Entropy estimated from a few observations is biased low, because rare outcomes may not appear at all.

Shannon Entropy FAQs

What units is entropy measured in?

Bits with log base 2, nats with natural logs, and dits (or hartleys) with base 10. Switch with the chips above.

What is the maximum entropy?

log K for K outcomes, reached when they are all equally likely.

Can entropy be negative?

No, not for a discrete distribution. The minimum is 0, when one outcome is certain.

How is text handled?

Each character, including spaces and punctuation, is counted, and the entropy of those frequencies is reported per character.

What is perplexity?

The base raised to the entropy: an effective number of equally likely choices. It’s widely used to score language models.

Is this the same as the Shannon diversity index?

Yes, mathematically, usually with natural logs. Ecologists call it H′ and apply it to species counts.

How does entropy relate to compression?

It’s the lower limit on the average number of bits per symbol any lossless code can achieve for that source.

Do the probabilities need to add to exactly 1?

Yes, within rounding. Use counts mode if you have raw frequencies.

Sources

  • C. E. Shannon, "A mathematical theory of communication", Bell System Technical Journal 27 (1948), doi:10.1002/j.1538-7305.1948.tb01338.x.
  • T. M. Cover and J. A. Thomas, Elements of Information Theory, 2nd edition (Wiley, 2006), chapter 2.

Related Tools