# Shannon's source coding theorem

In information theory, Shannon's source coding theorem (or noiseless coding theorem) establishes the statistical limits to possible data compression for data whose source is an independent identically-distributed random variable, and the operational meaning of the Shannon entropy.
Named after Claude Shannon, the source coding theorem shows that, in the limit, as the length of a stream of independent and identically-distributed random variable (i.i.d.) data tends to infinity, it is impossible to compress such data such that the code rate (average number of bits per symbol) is less than the Shannon entropy of the source, without it being virtually certain that information will be lost. However it is possible to get the code rate arbitrarily close to the Shannon entropy, with negligible probability of loss.
The source coding theorem for symbol codes places an upper and a lower bound on the minimal possible expected length of codewords as a function of the entropy of the input word (which is viewed as a random variable) and of the size of the target alphabet.
Note that, for data that exhibits more dependencies (whose source is not an i.i.d. random variable), the Kolmogorov complexity, which quantifies the minimal description length of an object, is more suitable to describe the limits of data compression. Shannon entropy takes into account only frequency regularities while Kolmogorov complexity takes into account all algorithmic regularities, so in general the latter is smaller. On the other hand, if an object is generated by a random process in such a way that it has only frequency regularities, entropy is close to complexity with high probability (Shen et al. 2017).

## Statements

Source coding is a mapping from (a sequence of) symbols from an information source to a sequence of alphabet symbols (usually bits) such that the source symbols can be exactly recovered from the binary bits (lossless source coding) or recovered within some distortion (lossy source coding). This is one approach to data compression.

### Source coding theorem

In information theory, the source coding theorem (Shannon 1948) informally states that (MacKay 2003, pg. 81, Cover 2006, Chapter 5):
   N i.i.d. random variables each with entropy H(X) can be compressed into more than N H(X) bits with negligible risk of information loss, as N → ∞; but conversely, if they are compressed into fewer than N H(X) bits it is virtually certain that information will be lost.
The $NH(X)$ coded sequence represents the compressed message in a biunivocal way, under the assumption that the decoder knows the source. From a practical point of view, this hypothesis is not always true. Consequently, when the entropy encoding is applied the transmitted message is $NH(X)+(inf.source)$. Usually, the information that characterizes the source is inserted at the beginning of the transmitted message.

### Source coding theorem for symbol codes

Let Σ$_1$, Σ$_2$ denote two finite alphabets and let Σ$^∗$ $_1$ and Σ$^∗$ $_2$ denote the set of all finite words from those alphabets (respectively).
Suppose that X is a random variable taking values in Σ$_1$ and let  f  be a uniquely decodable code from Σ$^∗$ $_1$ to Σ$^∗$ $_2$ where |Σ$_2$| = a. Let S denote the random variable given by the length of codeword  f (X).
If  f  is optimal in the sense that it has the minimal expected word length for X, then (Shannon 1948):
    ${\frac {H(X)}{\log _{2}a}}\leq \mathbb {E} [S]<{\frac {H(X)}{\log _{2}a}}+1$
Where $\mathbb {E}$ denotes the expected value operator.

## Proof: source coding theorem

Given X is an i.i.d. source, its time series X$_1$, ..., X$_n$ is i.i.d. with entropy H(X) in the discrete-valued case and differential entropy in the continuous-valued case. The Source coding theorem states that for any ε > 0, i.e. for any rate H(X) + ε larger than the entropy of the source, there is large enough n and an encoder that takes n i.i.d. repetition of the source, X$^{1:n}$, and maps it to n(H(X) + ε) binary bits such that the source symbols X$^{1:n}$ are recoverable from the binary bits with probability of at least 1 − ε.
Proof of Achievability. Fix some ε > 0, and let
    $p(x_{1},\ldots ,x_{n})=\Pr \left[X_{1}=x_{1},\cdots ,X_{n}=x_{n}\right].$
The typical set, A$^ε$ $_n$, is defined as follows:
    $A_{n}^{\varepsilon }=\left\{(x_{1},\cdots ,x_{n})\ :\ \left|-{\frac {1}{n}}\log p(x_{1},\cdots ,x_{n})-H_{n}(X)\right|<\varepsilon \right\}.$
The asymptotic equipartition property (AEP) shows that for large enough n, the probability that a sequence generated by the source lies in the typical set, A$^ε$ $_n$, as defined approaches one. In particular, for sufficiently large n, $P((X_{1},X_{2},\cdots ,X_{n})\in A_{n}^{\varepsilon })$ can be made arbitrarily close to 1, and specifically, greater than $1-\varepsilon$ (See AEP for a proof).
The definition of typical sets implies that those sequences that lie in the typical set satisfy:
    $2^{-n(H(X)+\varepsilon )}\leq p\left(x_{1},\cdots ,x_{n}\right)\leq 2^{-n(H(X)-\varepsilon )}$
* The probability of a sequence $(X_{1},X_{2},\cdots X_{n})$ being drawn from A$^ε$ $_n$ is greater than 1 − ε.
* $\left|A_{n}^{\varepsilon }\right|\leq 2^{n(H(X)+\varepsilon )}$, which follows from the left hand side (lower bound) for $p(x_{1},x_{2},\cdots x_{n})$.
* $\left|A_{n}^{\varepsilon }\right|\geq (1-\varepsilon )2^{n(H(X)-\varepsilon )}$, which follows from upper bound for $p(x_{1},x_{2},\cdots x_{n})$ and the lower bound on the total probability of the whole set A$^ε$ $_n$.
Since $\left|A_{n}^{\varepsilon }\right|\leq 2^{n(H(X)+\varepsilon )},n(H(X)+\varepsilon )$ bits are enough to point to any string in this set.
The encoding algorithm: the encoder checks if the input sequence lies within the typical set; if yes, it outputs the index of the input sequence within the typical set; if not, the encoder outputs an arbitrary n(H(X) + ε) digit number. As long as the input sequence lies within the typical set (with probability at least 1 − ε), the encoder does not make any error. So, the probability of error of the encoder is bounded above by ε.
Proof of converse: the converse is proved by showing that any set of size smaller than A$^ε$ $_n$ (in the sense of exponent) would cover a set of probability bounded away from 1.

## Proof: Source coding theorem for symbol codes

For 1 ≤ i ≤ n let s$_i$ denote the word length of each possible x$_i$. Define $q_{i}=a^{-s_{i}}/C$, where C is chosen so that q$_1$ + ... + q$_n$ = 1. Then
    ${\begin{aligned}H(X)&=-\sum _{i=1}^{n}p_{i}\log _{2}p_{i}\\&\leq -\sum _{i=1}^{n}p_{i}\log _{2}q_{i}\\&=-\sum _{i=1}^{n}p_{i}\log _{2}a^{-s_{i}}+\sum _{i=1}^{n}p_{i}\log _{2}C\\&=-\sum _{i=1}^{n}p_{i}\log _{2}a^{-s_{i}}+\log _{2}C\\&\leq -\sum _{i=1}^{n}-s_{i}p_{i}\log _{2}a\\&=\mathbb {E} S\log _{2}a\\\end{aligned}}$
where the second line follows from Gibbs' inequality and the fifth line follows from Kraft's inequality:
    $C=\sum _{i=1}^{n}a^{-s_{i}}\leq 1$
so log C ≤ 0.
For the second inequality we may set
    $s_{i}=\lceil -\log _{a}p_{i}\rceil$
so that
    $-\log _{a}p_{i}\leq s_{i}<-\log _{a}p_{i}+1$
and so
    $a^{-s_{i}}\leq p_{i}$
and
    $\sum a^{-s_{i}}\leq \sum p_{i}=1$
and so by Kraft's inequality there exists a prefix-free code having those word lengths. Thus the minimal S satisfies
    ${\begin{aligned}\mathbb {E} S&=\sum p_{i}s_{i}\\&<\sum p_{i}\left(-\log _{a}p_{i}+1\right)\\&=\sum -p_{i}{\frac {\log _{2}p_{i}}{\log _{2}a}}+1\\&={\frac {H(X)}{\log _{2}a}}+1\\\end{aligned}}$

## Extension to non-stationary independent sources
### Fixed rate lossless source coding for discrete time non-stationary independent sources

Define typical set A$^ε$ $_n$ as:
    $A_{n}^{\varepsilon }=\left\{x_{1}^{n}\ :\ \left|-{\frac {1}{n}}\log p\left(X_{1},\cdots ,X_{n}\right)-{\overline {H_{n}}}(X)\right|<\varepsilon \right\}.$
Then, for given δ > 0, for n large enough, Pr(A$^ε$ $_n$) > 1 − δ. Now we just encode the sequences in the typical set, and usual methods in source coding show that the cardinality of this set is smaller than $2^{n({\overline {H_{n}}}(X)+\varepsilon )}$. Thus, on an average, H$_n$(X) + ε bits suffice for encoding with probability greater than 1 − δ, where ε and δ can be made arbitrarily small, by making n larger.