Skip to content
AI360Xpert
Beta

Source and Channel Coding

Two limits run communication: entropy floors compression, capacity ceilings transmission. Shannon says both are reachable, separately.

Four symbols compress to 1.75 average bits against the entropy floor while parity fights a 0.1-flip channel under capacity 0.53
Four symbols compress to 1.75 average bits against the entropy floor while parity fights a 0.1-flip channel under capacity 0.53

Why Does This Exist?

Files waste space and noisy links corrupt bits; ML ships both (datasets, weights, streaming predictions). Ad-hoc codes either bloat 2x or collapse at the first flipped bit. Shannon's two theorems replace folklore with floors and ceilings plus codes that reach them.

Source coding: nn symbols need geqnH\\geq nH bits on average, Huffman achieves <H+1< H + 1. Channel coding: reliable rate sits leqC=maxp(x)I(X;Y)\\leq C = \\max_{p(x)} I(X;Y), achievable with long codes. Separation: do them in sequence. They quantify entropy and mutual information as operating limits.

The one idea you need first

Entropy HH is the incompressible core:500 fair coins need 500 bits, period. Capacity CC is the pipe width after noise: a binary symmetric channel flipping 10% carries C=1−H(0.1)approx0.531C = 1 - H(0.1) \\approx 0.531 bits per use. Codes approach, never cross.

Think of It Like This

Packing versus armored trucks

Source coding packs luggage: Huffman folds common shirts small, rare coats big, averaging near the suitcase's true volume. Channel coding hires armored trucks: parity guards and repetition survive hijacked (flipped) boxes at the cost of fewer boxes per trip.

Packing never protects; armor never shrinks. Where it stops: luggage packing is exact and offline, while ML streams codes online with latency budgets Shannon ignores.

How It Actually Works

Kraft inequality sum2−lileq1\\sum 2^{-l_i} \\leq 1 constrains prefix lengths; Huffman greedily merges rarest symbols, optimal among prefix codes. Arithmetic codes reach H+2/nH + 2/n bits total. Channel: random coding plus joint-typicality decoding proves achievability below CC; Fano's inequality proves the converse above it. Modern LDPC, turbo, and polar codes operate tenths of a dB from CC; quantization plus entropy coding (weights, JPEG) lives the source side daily.

Worked example

Symbols [A,B,C,D][A,B,C,D] with p=[0.5,0.25,0.125,0.125]p = [0.5, 0.25, 0.125, 0.125]. Entropy H=0.5(1)+0.25(2)+2(0.125)(3)=0.5+0.5+0.75=1.75H = 0.5(1) + 0.25(2) + 2(0.125)(3) = 0.5 + 0.5 + 0.75 = 1.75 bits. Huffman lengths [1,2,3,3][1,2,3,3] average 0.5(1)+0.25(2)+0.125(3)+0.125(3)=1.750.5(1)+0.25(2)+0.125(3)+0.125(3) = 1.75: meets the floor exactly. Fixed 2-bit codes waste 0.250.25 bits per symbol (14%). Channel BSC p=0.1p = 0.1: H(0.1)=−(0.1log20.1+0.9log20.9)approx0.469H(0.1) = -(0.1\\log_2 0.1 + 0.9\\log_2 0.9) \\approx 0.469, Capprox0.531C \\approx 0.531. Triple repetition with majority vote pushes error 0.1to3(0.01)(0.9)+0.001=0.0280.1 \\to 3(0.01)(0.9)+0.001 = 0.028 at rate 1/31/3: below capacity, working, improvable.

Code

import numpy as np
p = np.array([0.5, 0.25, 0.125, 0.125])H = float(-(p * np.log2(p)).sum())lengths = np.array([1, 2, 3, 3])print(round(H, 2), round(float((p * lengths).sum()), 2))  # -> (1.75, 1.75)

Entropy and Huffman average coincide at 1.75. The floor is met, not just approached.

Watch Out For

Coding to the wrong distribution

Huffman on yesterday's frequencies bloats today's drifted stream past fixed-length: the H+1H+1 guarantee assumes matched pp. Model drift silently taxes every byte.

Track frequencies online (adaptive Huffman, arithmetic with updates) or re-fit per block. If average length creeps above H+1H + 1, the code is stale.

Transmitting above capacity and retrying harder

No code, retried forever, beats CC: Fano's converse is a wall, not a challenge. Pipelines that push 0.8 bits through a 0.53 pipe "with better engineering" burn latency for guaranteed residual error.

Compress first (lower HH), widen the pipe, or accept distortion (rate-distortion). If error floors persist across codes, physics decided.

The Quick Version

  • Source floor HH with Huffman under H+1H+1; example hits 1.75 exactly.
  • Channel ceiling C=maxI(X;Y)C = \\max I(X;Y); BSC at 10% gives 0.531 bits per use.
  • Separation: compress to HH, then protect below CC.
  • Repetition 3x cuts 0.1 error to 0.028 at rate one-third.
  • Match codes to live frequencies; stale codes bloat past fixed-length.
  • Above capacity no retry scheme converges; change the pipe or the fidelity.