Channels

The dit.example_channels module is a catalog of canonical discrete memoryless channels (DMCs). Each constructor returns a conditional Distribution \(p(Y \mid X)\) – the representation consumed by channel_capacity() and by the channel-coding evaluation layer in dit.coding. Alphabets are integer-valued; an erasure is the integer just past the input alphabet (binary erasure 2, q-ary erasure q).

The catalog

Binary-input channels:

q-ary channels:

  • q_ary_symmetric_channel() – correct with probability 1 - p, else a uniform wrong symbol; capacity \(\log_2 q - H_b(p) - p \log_2(q - 1)\),

  • q_ary_erasure_channel() – erased with probability \(\epsilon\); capacity \((1 - \epsilon) \log_2 q\),

  • noisy_typewriter() – each letter maps to itself or the next, each with probability one half; capacity \(\log_2(n / 2)\).

Trivial endpoints:

Example

A channel can be fed directly to channel_capacity() or to a code’s probability_of_error():

In [1]: from dit.example_channels import binary_symmetric_channel

In [2]: from dit.algorithms import channel_capacity

In [3]: bsc = binary_symmetric_channel(0.1)

In [4]: channel_capacity(bsc)[0]
Out[4]: 0.5310044064107188

In [5]: from dit.coding import hamming

In [6]: hamming(3).probability_of_error(bsc, method='exact')
Out[6]: 0.14969440000000186

APIs

binary_symmetric_channel(p)[source]

The binary symmetric channel with crossover probability p.

Each transmitted bit is independently flipped with probability p. Its capacity is \(1 - H_b(p)\).

Parameters:

p (float) – The probability that a transmitted bit is flipped.

Returns:

channel – The conditional distribution p(Y | X) over {0, 1}.

Return type:

Distribution

binary_erasure_channel(epsilon)[source]

The binary erasure channel with erasure probability epsilon.

Each transmitted bit is independently erased with probability epsilon and otherwise received unchanged. Its capacity is \(1 - \epsilon\).

Parameters:

epsilon (float) – The probability that a transmitted bit is erased.

Returns:

channel – The conditional distribution p(Y | X) with output alphabet {0, 1, 2}, where 2 denotes an erasure.

Return type:

Distribution

z_channel(p)[source]

The Z-channel with crossover probability p.

A 0 is always received correctly; a 1 is flipped to 0 with probability p. This is the canonical binary asymmetric channel.

Parameters:

p (float) – The probability that a transmitted 1 is received as 0.

Returns:

channel – The conditional distribution p(Y | X) over {0, 1}.

Return type:

Distribution

binary_asymmetric_channel(p0, p1)[source]

The binary asymmetric channel.

A 0 is flipped to 1 with probability p0; a 1 is flipped to 0 with probability p1. The binary symmetric channel is the case p0 == p1 and the Z-channel is the case p0 == 0.

Parameters:
  • p0 (float) – The probability that a transmitted 0 is received as 1.

  • p1 (float) – The probability that a transmitted 1 is received as 0.

Returns:

channel – The conditional distribution p(Y | X) over {0, 1}.

Return type:

Distribution

binary_symmetric_erasure_channel(p, epsilon)[source]

The binary symmetric error-and-erasure channel.

A transmitted bit is erased with probability epsilon and flipped with probability p; the two events are disjoint, so p + epsilon <= 1.

Parameters:
  • p (float) – The probability that a transmitted bit is flipped.

  • epsilon (float) – The probability that a transmitted bit is erased.

Returns:

channel – The conditional distribution p(Y | X) with output alphabet {0, 1, 2}, where 2 denotes an erasure.

Return type:

Distribution

q_ary_symmetric_channel(q, p)[source]

The q-ary symmetric channel.

A symbol is received correctly with probability 1 - p and is otherwise received as one of the other q - 1 symbols, chosen uniformly. Its capacity is \(\log_2 q - H_b(p) - p \log_2(q - 1)\). The binary symmetric channel is the case q == 2.

Parameters:
  • q (int) – The alphabet size (q >= 2).

  • p (float) – The total probability that a symbol is received incorrectly.

Returns:

channel – The conditional distribution p(Y | X) over {0, ..., q - 1}.

Return type:

Distribution

q_ary_erasure_channel(q, epsilon)[source]

The q-ary erasure channel.

A symbol is erased with probability epsilon and otherwise received unchanged. Its capacity is \((1 - \epsilon) \log_2 q\). The binary erasure channel is the case q == 2.

Parameters:
  • q (int) – The alphabet size (q >= 2).

  • epsilon (float) – The probability that a symbol is erased.

Returns:

channel – The conditional distribution p(Y | X) with output alphabet {0, ..., q - 1, q}, where q denotes an erasure.

Return type:

Distribution

noisy_typewriter(n=26)[source]

The noisy typewriter channel (Cover & Thomas).

Each of the n letters is received either unchanged or as the next letter (cyclically), each with probability one half. Its capacity is \(\log_2(n / 2)\), achieved by using every other input letter.

Parameters:

n (int) – The alphabet size (n >= 2).

Returns:

channel – The conditional distribution p(Y | X) over {0, ..., n - 1}.

Return type:

Distribution

identity_channel(n=2)[source]

The noiseless channel over an n-symbol alphabet.

Every symbol is received unchanged, so the channel has capacity \(\log_2 n\).

Parameters:

n (int) – The alphabet size (n >= 1).

Returns:

channel – The conditional distribution p(Y | X) over {0, ..., n - 1}.

Return type:

Distribution

useless_channel(n=2)[source]

The zero-capacity channel over an n-symbol alphabet.

The output is uniform and independent of the input, so the channel carries no information and has capacity 0.

Parameters:

n (int) – The alphabet size (n >= 1).

Returns:

channel – The conditional distribution p(Y | X) over {0, ..., n - 1}.

Return type:

Distribution

Capacity

channel_capacity() takes a list of conditionals \(p(Y \mid X=x)\) (or a channel from this catalog) and returns the capacity together with a capacity-achieving input. For a joint distribution, use channel_capacity_joint().

The capacity is computed with the Blahut-Arimoto algorithm [Ari72, Bla72] (see also [CT06], Section 10.8). Write \(q = r P\) for the output distribution induced by an input \(r\), and \(D_x = D(P_x \| q)\). Then

\[\sum_x r(x) D_x = \I{X : Y} \leq C \leq \max_x D_x,\]

and the iteration stops once these two bounds agree to within max(atol, rtol * C) bits. The returned capacity is therefore certified rather than merely stationary. Three options trade speed for generality:

  • method='accelerated' (the default) takes the step \(r \leftarrow r\, e^{\mu D}\) of [MD04] with the adaptive \(\mu_k = D(r_k \| r_{k-1}) / D(q_k \| q_{k-1}) \geq 1\). When that step would decrease the mutual information, the classic step \(\mu = 1\) is taken instead. (The paper prints the reciprocal ratio, which is at most 1 by data processing and contradicts its own monotonicity bound.) method='ba' is the classic iteration.

  • eliminate=True iterates on a growing active set of inputs [Say00]. It adds inputs that violate the Kuhn-Tucker condition \(D_x \leq C\) and stops when none do. That condition is the upper bound above, taken over the full alphabet. This pays off when only a few of many inputs are used at capacity.

  • Weakly symmetric channels ([CT06], Theorem 7.2.1) are solved in closed form. So are square nonsingular channels whose optimal input uses every symbol: there \(D_x = C\) for all \(x\) is a linear system in \(\log q\). That closed form is used only when its own bound gap certifies it. No canonical reference for it is cited here.

Duplicate inputs and outputs that are never produced are removed before iterating.

channel_capacity_cost() adds an average cost constraint \(\mathbb{E}[c(X)] \leq B\) through Blahut’s Lagrangian form [Bla72], and sibson_capacity() computes Sibson’s capacity of order \(\alpha\). The latter is the maximum over inputs of sibson_mutual_information(), and it equals the Rényi radius \(\min_q \max_x D_\alpha(P_x \| q)\) [Csiszar95].

In [7]: from dit.algorithms import channel_capacity_cost, sibson_capacity

In [8]: channel_capacity_cost(bsc, cost=[0, 1], budget=0.2)[0]
Out[8]: 0.3577507789033368

In [9]: sibson_capacity(bsc, order=2)[0]
Out[9]: 0.713695814843359
channel_capacity(cdists, marginal=None, rtol=None, atol=None, maxiter=100000, method='accelerated', eliminate=False)[source]

Calculates the channel capacity from conditional distributions P(Y|X).

The capacity is computed with the Blahut-Arimoto algorithm [Ari72, Bla72], stopping once Blahut’s lower and upper bounds on the capacity agree to within tolerance, so the returned value is within max(atol, rtol * C) bits of the true capacity. Weakly symmetric [CT06] and square nonsingular channels are solved in closed form when the closed form certifies.

Parameters:
  • cdists (list, ndarray, Distribution) – A list of conditional distributions, a 2D array where rows are inputs and columns are outputs, or a conditional Distribution such as p(Y|X).

  • marginal (distribution | None) – The marginal distribution P(X). If provided, P*(X) is returned as a distribution object. If None, P*(X) is returned as a numpy array.

  • rtol (float or None) – Relative tolerance on the capacity. Defaults to ditParams[‘rtol’].

  • atol (float or None) – Absolute tolerance on the capacity, in bits. Defaults to ditParams[‘atol’].

  • maxiter (int) – Maximum number of Blahut-Arimoto iterations. A RuntimeWarning is raised if the bounds have not met by then.

  • method ({'accelerated', 'ba'}) – 'ba' is the classic iteration; 'accelerated' takes the step r <- r exp(mu D) of [MD04] with the adaptive mu = D(r_k || r_{k-1}) / D(q_k || q_{k-1}), falling back to the classic step whenever the mutual information would decrease.

  • eliminate (bool) – If True, iterate on a growing active set of inputs [Say00]. This helps when only a few of many inputs carry probability at capacity.

Returns:

  • cc (float) – The channel capacity.

  • marginal_opt (distribution or ndarray) – The optimal marginal P*(X).

channel_capacity_joint(dist, input_, output, marginal=False)[source]

Compute the channel capacity from input_ to output.

Parameters:
  • dist (Distribution) – The joint distribution.

  • input (iterable) – The random variables that are the input of the channel.

  • output (iterable) – The random variables that are the output of the channel.

  • marginal (bool) – Whether to return the marginal distribution. Defaults to False.

channel_capacity_cost(cdists, cost, budget, rtol=None, atol=None, maxiter=100000, method='accelerated')[source]

Capacity of a channel whose inputs carry a cost, under an average budget.

Computes C(B) = max { I[X : Y] : E[cost(X)] <= B } through Blahut’s Lagrangian form [Bla72]: for a multiplier s >= 0 the iteration maximizes I[X : Y] - s E[cost(X)] (the offset divergences D(P_x || q) - s cost(x) keep the bound-gap certificate), and C(B) = min_s max_p I - s (E[cost] - B). The multiplier is found by root-finding E[cost] = B.

Parameters:
  • cdists (list, ndarray, Distribution) – The channel, in any form accepted by channel_capacity().

  • cost (array_like) – The cost of each input, in the same order as the channel’s rows.

  • budget (float) – The maximum average cost.

  • rtol (float or None) – Relative tolerance. Defaults to ditParams[‘rtol’].

  • atol (float or None) – Absolute tolerance in bits. Defaults to ditParams[‘atol’].

  • maxiter (int) – Maximum Blahut-Arimoto iterations per value of the multiplier.

  • method ({'accelerated', 'ba'}) – As in channel_capacity().

Returns:

  • cc (float) – The cost-constrained capacity, in bits.

  • pmf (np.ndarray) – An input distribution achieving it. If the optimal input at the final multiplier is not unique its cost may differ slightly from budget.

  • s (float) – The Lagrange multiplier, in bits per unit cost; the slope of C(B). 0 when the budget does not bind and inf when only the cheapest inputs are affordable.

Raises:

ditException – If the budget is below the cheapest input’s cost.

sibson_capacity(cdists, order, rtol=None, atol=None, maxiter=100000)[source]

Sibson’s capacity of order alpha, C_alpha = max_p I_alpha(X : Y).

Sibson’s alpha-mutual information is

I_alpha(X : Y) = alpha / (alpha - 1) log2 sum_y (sum_x p(x) P(y|x)^alpha)^(1/alpha),

and its maximum over inputs equals the Rényi radius min_q max_x D_alpha(P_x || q) [Csiszar95]. Every input r therefore gives the bounds I_alpha(r) <= C_alpha <= max_x D_alpha(P_x || q_r), where q_r is Sibson’s optimal output for r. The iteration r <- r exp(mu D_alpha(P_x || q_r)) with mu = max(1, alpha) reduces to Blahut-Arimoto at alpha = 1; the step is halved whenever I_alpha would decrease, and the iteration stops once the bounds meet.

Parameters:
  • cdists (list, ndarray, Distribution) – The channel, in any form accepted by channel_capacity().

  • order (float) – The order alpha > 0. 1 gives the Shannon capacity and numpy.inf the maximal leakage capacity log2 sum_y max_x P(y|x).

  • rtol (float or None) – Relative tolerance. Defaults to ditParams[‘rtol’].

  • atol (float or None) – Absolute tolerance in bits. Defaults to ditParams[‘atol’].

  • maxiter (int) – Maximum number of iterations.

Returns:

  • cc (float) – The capacity of order alpha, in bits.

  • pmf (np.ndarray) – An optimal input distribution.

Commitment capacity

In bit commitment, Alice commits to a message by sending Bob information about it, and reveals the message later. The commitment must be concealing, so that Bob learns almost nothing about the message before it is revealed, and binding, so that Alice cannot reveal a different message. Without computational assumptions this is impossible over noiseless channels, but noisy channels allow it. The commitment capacity of a discrete memoryless channel is the largest rate at which messages can be committed to with vanishing errors, even with unlimited noiseless two-way communication on the side [WNI03].

An input is redundant when its output distribution \(P_x\) is a convex combination of the other inputs’ output distributions. Alice can then simulate it by randomizing over the others without Bob noticing, so it cannot bind her. Removing every redundant input leaves one input for each vertex of the convex hull of the \(P_x\), the non-redundant channel \(\tilde{P}\). Winter, Nascimento and Imai show that the commitment capacity is the maximal equivocation of the non-redundant channel ([WNI03], Theorem 2):

\[C_{\mathrm{com}} = \max_{p(x)} \H{X | Y}, \qquad p(y \mid x) = \tilde{P}(y \mid x).\]

It is zero exactly when the output of the non-redundant channel determines its input, so every other channel allows bit commitment. It is also a lower bound on the rate at which the channel allows coin tossing. nonredundant_inputs() finds the remaining inputs with one linear program per input, and maximal_equivocation() maximizes the equivocation of any channel. Without the reduction, the maximal equivocation is the secrecy capacity of a wiretap channel [Wyn75a] whose legitimate receiver sees the input noiselessly and whose eavesdropper sees it through the channel.

The equivocation is concave in the input. With \(q = rP\) and \(D_x = D(P_x \| q)\) as above, its gradient is \(g_x = -\log_2 r(x) - D_x\), and \(\sum_x r(x) g_x = \H{X | Y}\). So

\[\H{X | Y} \leq \max_{p(x)} \H{X | Y} \leq \max_x g_x,\]

the counterpart of Blahut’s bounds, and the iteration again stops once they meet. Its update \(r(x) \propto 2^{-D_x}\) maximizes \(\H{X} - \sum_x r(x) D(P_x \| q)\) with \(q\) held fixed. That function equals \(\H{X | Y} - D(rP \| q)\), so the equivocation never decreases. Inputs whose output supports overlap are solved together, and separately from the others: the output reveals which of these blocks was used, so all the probability goes to the best block.

The paper’s examples follow. The binary symmetric channel’s commitment capacity is \(H_b(p)\), so the noise that hurts transmission helps commitment:

In [10]: from dit.algorithms import commitment_capacity, maximal_equivocation, nonredundant_inputs

In [11]: commitment_capacity(bsc)
Out[11]: (0.46899559358928117, array([0.5, 0.5]))

In the next channel, the first input is the average of the other two, which form a noiseless channel. Mixing it in gives one bit of equivocation, but the commitment capacity is zero:

In [12]: import numpy as np

In [13]: P = np.array([[1 / 2, 1 / 2], [1, 0], [0, 1]])

In [14]: maximal_equivocation(P)
Out[14]: (1.0, array([0.5 , 0.25, 0.25]))

In [15]: nonredundant_inputs(P)
Out[15]: array([1, 2])

In [16]: commitment_capacity(P)
Out[16]: (0.0, array([0. , 0.5, 0.5]))

Transmission and commitment can favour very different inputs. On the Z-channel with p = 1/2 together they exceed the one bit of input entropy:

In [17]: from dit.example_channels import z_channel

In [18]: z = z_channel(0.5)

In [19]: channel_capacity(z)
Out[19]: (0.32192809488736235, array([0.6, 0.4]))

In [20]: commitment_capacity(z)
Out[20]: (0.6942419136306172, array([0.44721361, 0.55278639]))

Note

The commitment capacity is not continuous in the channel. As the crossover probability of the binary symmetric channel tends to one half, its commitment capacity tends to one bit, but at one half both inputs have the same output distribution, one of them is redundant, and the capacity is zero. Inputs whose output distributions lie within redundancy_tol (in \(\ell_1\)) of the convex hull of the others’ count as redundant.

commitment_capacity(cdists, rtol=None, atol=None, maxiter=100000, redundancy_tol=1e-09)[source]

The commitment capacity of a discrete memoryless channel [WNI03].

This is the largest rate at which Alice can commit to a message through the channel: Bob learns almost nothing about it until she reveals it, and she cannot reveal a different message, except with vanishing probability. Unlimited noiseless two-way communication is allowed on the side. Winter, Nascimento and Imai show that it is the maximal equivocation of the channel once its redundant inputs are removed:

C_com = max_p H[X | Y], with p ranging over inputs of the non-redundant channel.

See nonredundant_inputs() and maximal_equivocation(). The commitment capacity is zero exactly when the output of the non-redundant channel determines its input; every other channel allows bit commitment. It is also a lower bound on the rate at which the channel allows coin tossing.

Parameters:
  • cdists (list, ndarray, Distribution) – The channel, in any form accepted by channel_capacity().

  • rtol (float or None) – Relative tolerance. Defaults to ditParams[‘rtol’].

  • atol (float or None) – Absolute tolerance in bits. Defaults to ditParams[‘atol’].

  • maxiter (int) – Maximum number of iterations. A RuntimeWarning is raised if the bounds have not met by then.

  • redundancy_tol (float) – Passed to nonredundant_inputs() as atol. The commitment capacity is not continuous in the channel: inputs closer than this count as one. For instance, the binary symmetric channel’s commitment capacity H_b(p) tends to one bit as p tends to 1/2, but at p = 1/2 its two inputs coincide and it is zero.

Returns:

  • cc (float) – The commitment capacity, in bits.

  • pmf (np.ndarray) – An optimal input distribution. It is zero on redundant inputs.

maximal_equivocation(cdists, rtol=None, atol=None, maxiter=100000)[source]

The maximal equivocation of a channel, max_p H[X | Y].

The equivocation is concave in the input. For an input r with output q = rP, its gradient is g_x = -log2 r(x) - D(P_x || q), and the r-average of g is H[X | Y]. So every input certifies H[X | Y] <= max_p H[X | Y] <= max_x g_x, and the iteration r(x) ∝ 2^(-D(P_x || q)), an alternating maximization in the style of Blahut-Arimoto, stops once the two bounds agree to within max(atol, rtol * value) bits. It never decreases the equivocation. Inputs whose output supports overlap are solved together, and separately from the others.

This is the secrecy capacity of a wiretap channel [Wyn75a] whose legitimate receiver sees the input noiselessly and whose eavesdropper sees it through the channel. Unlike commitment_capacity(), it counts redundant inputs.

Parameters:
  • cdists (list, ndarray, Distribution) – The channel, in any form accepted by channel_capacity().

  • rtol (float or None) – Relative tolerance. Defaults to ditParams[‘rtol’].

  • atol (float or None) – Absolute tolerance in bits. Defaults to ditParams[‘atol’].

  • maxiter (int) – Maximum number of iterations. A RuntimeWarning is raised if the bounds have not met by then.

Returns:

  • value (float) – The maximal equivocation, in bits.

  • pmf (np.ndarray) – An input distribution attaining it.

nonredundant_inputs(cdists, atol=1e-09)[source]

The inputs of the non-redundant version of a channel [WNI03].

An input is redundant when its output distribution is a convex combination of the other inputs’ output distributions, since it can then be simulated by randomizing over them. Removing every redundant input leaves one input for each vertex of the convex hull of the output distributions. Of several inputs with the same output distribution, the first is kept.

Each input is tested with a linear program for the l1 distance from its output distribution to the convex hull of the others’, from the last input to the first and against the inputs still present.

Parameters:
  • cdists (list, ndarray, Distribution) – The channel, in any form accepted by channel_capacity().

  • atol (float) – An input counts as redundant when its output distribution is within this l1 distance of the convex hull of the others’.

Returns:

inputs – The indices of the remaining inputs, in increasing order.

Return type:

np.ndarray