In information theory, Shannon–Fano–Elias coding is a precursor to arithmetic coding, in which probabilities are used to determine codewords. It is named for Claude Shannon, Robert Fano, and Peter Elias.
Algorithm description Given a discrete random variable X of ordered values to be encoded, let p ( x ) {\displaystyle p(x)} be the probability for any x in X. Define a function
F ¯ ( x ) = ∑ x i < x p ( x i ) + 1 2 p ( x ) {\displaystyle {\bar {F}}(x)=\sum _{x_{i}<x}p(x_{i})+{\frac {1}{2}}p(x)}
Algorithm:
For each x in X, Let Z be the binary expansion of F ¯ ( x ) {\displaystyle {\bar {F}}(x)} . Choose the length of the encoding of x, L ( x ) {\displaystyle L(x)} , to be the integer ⌈ log 2 1 p ( x ) ⌉ + 1 {\displaystyle \left\lceil \log _{2}{\frac {1}{p(x)}}\right\rceil +1}
Choose the encoding of x, c o d e ( x ) {\displaystyle code(x)} , be the first L ( x ) {\displaystyle L(x)} most significant bits after the decimal point of Z.
Example Let X = {A, B, C, D}, with probabilities p = {1/3, 1/4, 1/6, 1/4}.
For A
F ¯ ( A ) = 1 2 p ( A ) = 1 2 ⋅ 1 3 = 0.1666 … {\displaystyle {\bar {F}}(A)={\frac {1}{2}}p(A)={\frac {1}{2}}\cdot {\frac {1}{3}}=0.1666\ldots }
In binary, Z(A) = 0.0010101010...
L ( A ) = ⌈ log 2 1 1 3 ⌉ + 1 = 3 {\displaystyle L(A)=\left\lceil \log _{2}{\frac {1}{\frac {1}{3}}}\right\rceil +1=\mathbf {3} }
code(A) is 001 For B
F ¯ ( B ) = p ( A ) + 1 2 p ( B ) = 1 3 + 1 2 ⋅ 1 4 = 0.4583333 … {\displaystyle {\bar {F}}(B)=p(A)+{\frac {1}{2}}p(B)={\frac {1}{3}}+{\frac {1}{2}}\cdot {\frac {1}{4}}=0.4583333\ldots }
In binary, Z(B) = 0.01110101010101...
L ( B ) = ⌈ log 2 1 1 4 ⌉ + 1 = 3 {\displaystyle L(B)=\left\lceil \log _{2}{\frac {1}{\frac {1}{4}}}\right\rceil +1=\mathbf {3} }
code(B) is 011 For C
F ¯ ( C ) = p ( A ) + p ( B ) + 1 2 p ( C ) = 1 3 + 1 4 + 1 2 ⋅ 1 6 = 0.66666 … {\displaystyle {\bar {F}}(C)=p(A)+p(B)+{\frac {1}{2}}p(C)={\frac {1}{3}}+{\frac {1}{4}}+{\frac {1}{2}}\cdot {\frac {1}{6}}=0.66666\ldots }
In binary, Z(C) = 0.101010101010...
L ( C ) = ⌈ log 2 1 1 6 ⌉ + 1 = 4 {\displaystyle L(C)=\left\lceil \log _{2}{\frac {1}{\frac {1}{6}}}\right\rceil +1=\mathbf {4} }
code(C) is 1010 For D
… excerpt ends here. Continue reading the full article.

