Advanced Mathematics

Information and Entropy

Entropy H=−∑pilog⁡2piH = -\sum p_i\log_2 p_i measures the average surprise of a random outcome, in bits: the shortest average code length.

Needs a step first: 7

An outcome of probability pp carries −log⁡2p-\log_2 p bits of information (a fair coin flip is 1 bit, a 1-in-8 event 3 bits), and entropy is the expected value of that surprise. Shannon showed it is the least number of bits per symbol any code can average, so it sets the limit on compression. It is largest when all outcomes are equally likely and zero when one is certain. Cross-entropy, the loss that language models and classifiers are trained on, is built directly from it.

Where it shows up: It leads to cross-entropy and KL divergence, the losses most models are trained to minimise.

How to hold it: In your head

See entropy as average surprise in bits, largest when the outcomes are equally likely, and know that it is the floor under the average length of any code.

What knowing it looks like

  • Compute the entropy of a small distribution in bits.
  • Build a prefix code whose average length matches the entropy for probabilities that are powers of 12\tfrac12.
  • Explain why a uniform distribution has the most entropy.

Test yourself

A source emits A, B, C, D with probabilities 12,14,18,18\tfrac12, \tfrac14, \tfrac18, \tfrac18. Find its entropy in bits, and give a binary prefix code whose average length equals it.
Show the worked answer
H=12⋅1+14⋅2+18⋅3+18⋅3=0.5+0.5+0.375+0.375=1.75H = \tfrac12\cdot1 + \tfrac14\cdot2 + \tfrac18\cdot3 + \tfrac18\cdot3 = 0.5 + 0.5 + 0.375 + 0.375 = 1.75 bits. The code A = 0, B = 10, C = 110, D = 111 is prefix-free, and its average length is 12⋅1+14⋅2+18⋅3+18⋅3=1.75\tfrac12\cdot1 + \tfrac14\cdot2 + \tfrac18\cdot3 + \tfrac18\cdot3 = 1.75 bits, matching HH.

Needs a step first: 7

Learn it with an AI

Copy this prompt into any AI assistant. It will take you through step by step at your level and finish with the test above.

Be my maths tutor. I'm learning "Information and Entropy" (Undergraduate I tier): Entropy $H = -\sum p_i\log_2 p_i$ measures the average surprise of a random outcome, in bits: the shortest average code length.
I already know: Random Variables, Expectation and Variance, Exponentials and Logarithms.
Teach it Socratically: first ask what I already know; then explain the core idea with one concrete example and why it's true; then give me three problems, one at a time, easy to hard. Wait for my answer, check every step, and point out mistakes without giving the answer away.
Finish by testing me with this: A source emits A, B, C, D with probabilities $\tfrac12, \tfrac14, \tfrac18, \tfrac18$. Find its entropy in bits, and give a binary prefix code whose average length equals it.
Use LaTeX for formulas. When you say where it's used later (machine learning, quantum mechanics), be specific.

On the way to

Recommended