Shannon–Fano–Elias coding

From testwiki
Jump to navigation Jump to search

Template:Short description Template:More citations needed In information theory, Shannon–Fano–Elias coding is a precursor to arithmetic coding, in which probabilities are used to determine codewords.[1] 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) be the probability for any x in X. Define a function

F¯(x)=xi<xp(xi)+12p(x)

Algorithm:

For each x in X,
Let Z be the binary expansion of F¯(x).
Choose the length of the encoding of x, L(x), to be the integer log21p(x)+1
Choose the encoding of x, code(x), be the first 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)=12p(A)=1213=0.1666
In binary, Z(A) = 0.0010101010...
L(A)=log2113+1=𝟑
code(A) is 001
For B
F¯(B)=p(A)+12p(B)=13+1214=0.4583333
In binary, Z(B) = 0.01110101010101...
L(B)=log2114+1=𝟑
code(B) is 011
For C
F¯(C)=p(A)+p(B)+12p(C)=13+14+1216=0.66666
In binary, Z(C) = 0.101010101010...
L(C)=log2116+1=𝟒
code(C) is 1010
For D
F¯(D)=p(A)+p(B)+p(C)+12p(D)=13+14+16+1214=0.875
In binary, Z(D) = 0.111
L(D)=log2114+1=𝟑
code(D) is 111

Algorithm analysis

Prefix code

Shannon–Fano–Elias coding produces a binary prefix code, allowing for direct decoding.

Let bcode(x) be the rational number formed by adding a decimal point before a binary code. For example, if code(C) = 1010 then bcode(C) = 0.1010. For all x, if no y exists such that

bcode(x)bcode(y)<bcode(x)+2L(x)

then all the codes form a prefix code.

By comparing F to the CDF of X, this property may be demonstrated graphically for Shannon–Fano–Elias coding.

The relation of F to the CDF of X

By definition of L it follows that

2L(x)12p(x)

And because the bits after L(y) are truncated from F(y) to form code(y), it follows that

F¯(y)bcode(y)2L(y)

thus bcode(y) must be no less than CDF(x).

So the above graph demonstrates that the bcode(y)bcode(x)>p(x)2L(x), therefore the prefix property holds.

Code length

The average code length is LC(X)=xXp(x)L(x)=xXp(x)(log21p(x)+1).
Thus for H(X), the entropy of the random variable X,

H(X)+1LC(X)<H(X)+2

Shannon Fano Elias codes from 1 to 2 extra bits per symbol from X than entropy, so the code is not used in practice.

See also

References

Template:Reflist Template:Compression Methods