ENع
←Information and entropy
Lesson 66 · Noise, errors and OFDM

Information and entropy: how many bits a message really needs

14 min10 steps5 questionsAdvanced
About this lesson

A rare symbol carries more information than a common one. Follow Shannon's own example: a source sends A, B, C and D with probabilities 1/2, 1/4, 1/8 and 1/8. A plain 2-bit code spends 16 bits on an 8-symbol message; measuring each symbol's surprise gives the entropy, 1.75 bits per symbol, and a prefix-free code matched to the odds sends the same message in 14 bits. You will see why no code can beat the entropy, meet binary entropy, and compute H in Python with numpy. How to build the best code for any odds is taught in the DSA lesson on Huffman coding.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 66 · Interactive
01 / 10

A source picks symbols

A source sends one of four symbols, A, B, C or D. A comes half the time, B a quarter, C and D an eighth each. A probability is how often a symbol comes, from 0 to 1.

symbol   probability p
  A        1/2 = 0.5
  B        1/4 = 0.25
  C        1/8 = 0.125
  D        1/8 = 0.125
           sum = 1

each symbol is picked on its own
(independent of the ones before)
message: A B A C A B A D
Loading figure…
or ← →
Read the lesson step by step
  1. A source picks symbols

    A source sends one of four symbols, A, B, C or D. A comes half the time, B a quarter, C and D an eighth each. A probability is how often a symbol comes, from 0 to 1.

    symbol   probability p
      A        1/2 = 0.5
      B        1/4 = 0.25
      C        1/8 = 0.125
      D        1/8 = 0.125
               sum = 1
    
    each symbol is picked on its own
    (independent of the ones before)
    message: A B A C A B A D
  2. Fixed length: 2 bits each

    Four symbols fit in 2 bits: 00, 01, 10, 11, because 2² = 4. Every symbol costs 2 bits, so the 8-symbol message costs 16 bits. Can we spend fewer?

    fixed-length code
      A 00   B 01   C 10   D 11
    
    bits per symbol = log2(4) = 2
    
    message  A B A C A B A D
    bits     8 × 2 = 16
  3. Rare means surprising

    A rare symbol tells you more. Shannon measured this as information, or surprise: I = log2(1/p) bits. A (p = 1/2) carries 1 bit, B 2 bits, C and D 3 bits each.

    information (surprise) of one symbol
      I = log2(1 / p)   bits
    
      A  log2(2) = 1 bit
      B  log2(4) = 2 bits
      C  log2(8) = 3 bits
      D  log2(8) = 3 bits
    
    p = 1  ->  I = 0 (no surprise at all)
    1 bit  = one fair coin toss
  4. Entropy: the average surprise

    Weight each surprise by how often it comes and add: H = ½·1 + ¼·2 + ⅛·3 + ⅛·3 = 1.75 bits per symbol. This average is the entropy of the source.

    entropy  H = Σ p · log2(1 / p)
    
      A  0.5   × 1 = 0.5
      B  0.25  × 2 = 0.5
      C  0.125 × 3 = 0.375
      D  0.125 × 3 = 0.375
                   -----
               H = 1.75 bits per symbol
    
    all four equally likely: H = 2 bits
  5. A code that fits the odds

    Give each symbol as many bits as its surprise: A 0, B 10, C 110, D 111. No codeword is the start of another, so this is a prefix-free code.

    codeword length = surprise
      A  0     1 bit
      B  10    2 bits
      C  110   3 bits
      D  111   3 bits
    
    prefix-free: no codeword begins
    another one ("0" does not begin
    "10", "110" or "111")
    (the code from Shannon's 1948 paper)
  6. Send it again: 14 bits

    Now the common A costs 1 bit and the rare C and D cost 3. The same 8 symbols take 14 bits instead of 16: 12.5% fewer, and nothing is lost.

    message  A   B   A   C   A   B   A   D
    code     0   10  0   110 0   10  0   111
    
    bits = 4×1 + 2×2 + 1×3 + 1×3
         = 14   (fixed code: 16)
    
    stream: 01001100100111
  7. Reading it back

    The receiver needs no gaps between codewords. It reads bits until they match a codeword, writes that letter and starts again. Prefix-free makes the first match the only one.

    stream 01001100100111
    
    read "0"    -> A
    read "10"   -> B
    read "0"    -> A
    read "110"  -> C
    read "0"    -> A
    read "10"   -> B
    read "0"    -> A
    read "111"  -> D
    
    decoded: ABACABAD
  8. No code beats the entropy

    14 bits / 8 symbols = 1.75 bits per symbol: exactly H. Shannon proved no decodable code (one the receiver can always split back into letters) can average fewer than H bits. Huffman's method (see the DSA lesson) finds the best code.

    Shannon's source coding theorem
      best average length L:  H ≤ L < H + 1
    
    here  L = Σ p · length
            = 0.5·1 + 0.25·2 + 0.125·3·2
            = 1.75 = H
    
    L = H exactly because every p
    is a power of 1/2
  9. Two symbols: binary entropy

    A source with two symbols, picked with odds p and 1 − p, has H(p) bits. A fair coin (p = 0.5) gives the most: 1 bit. At p = 0.1 it is only 0.469 bits.

    H(p) = −p·log2 p − (1−p)·log2(1−p)
    
      p = 0.5   H = 1 bit (the most)
      p = 0.1   H = 0.1·3.322 + 0.9·0.152
                  = 0.469 bits
      p = 0     H = 0 (always the same)
    
    predictable source -> few bits needed
  10. Entropy in Python

    With numpy, entropy is one line. p holds the four probabilities. np.log2 takes the base-2 log of every entry, and −(p·log2 p).sum() adds the weighted surprises: 1.75.

    import numpy as np
    p = np.array([4, 2, 1, 1]) / 8
    H = -(p * np.log2(p)).sum()
    print(H)  # 1.75
    
    >>> p
    array([0.5  , 0.25 , 0.125, 0.125])
    >>> -np.log2(p)
    array([1., 2., 3., 3.])
    
    print(H) shows: 1.75
    (Python 3 with numpy)

Quiz Unlocks after the last step.