Variable Code Length and Expected Cost
A variable-length code gives different symbols different lengths. Its expected length weights each symbol length by its probability.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Work three lengths
If A has probability one half and length one, and B and C each have probability one quarter and length two, the expected length is 1.5 bits per symbol. Compare weighted expectation.
Check decoding constraints
Choosing short names independently can make concatenation ambiguous. A prefix condition is one possible approach. See prefix code lengths.[1][2]
Compute expected bits per encoded symbol
Suppose code lengths are one, two and two and their symbol probabilities are 0.5, 0.25 and 0.25. The expected length is \(0.5+0.5+0.5=1.5\) bits per symbol. The average concerns a distribution, not the length of one codeword. See weighted expectation.
Symbol A: P=.5, length=1, contribution=.5
B and C: each P=.25, length=2, contribution=.5
Expected length 1.5
Include headers and padding when comparing actual files
A short encoded file can use more bytes despite a favorable theoretical rate. Inspect framing overhead and compare the frequency mix of real messages.