dit.coding
Coding: building source and channel codes.
The source-coding half constructs lossless codes for a source
Distribution using a variety of classical algorithms, and exposes
the code-theoretic properties of the resulting codes (rate, redundancy,
efficiency, the Kraft sum, whether the code is prefix-free / uniquely decodable /
optimal, …).
The channel-coding half builds binary (GF(2)) channel codes – linear block codes
(repetition, parity-check, Hamming, Reed-Muller, Golay), LDPC, polar, and
convolutional codes – and evaluates them against an arbitrary channel (decoding,
block-error probability, and gap to capacity). A channel is a conditional
Distribution p(Y|X) or any object implementing the channel
protocol of as_channel(), such as a finite-state channel with memory.
Feedback codes (erasure ARQ, stop-and-wait ARQ, Horstein’s bisection scheme)
see each channel output before choosing the next input; see
dit.coding.feedback.
Classes
Abstract base class for channel codes. |
|
A rate- |
|
Send each bit until it arrives unerased. |
|
Abstract base class for channel codes with noiseless feedback. |
|
Horstein's bisection scheme over the |
|
A linear code decoded by belief propagation on a sparse parity-check matrix. |
|
A linear block code over GF(2). |
|
A discrete memoryless channel, applying |
|
A polar code of length |
|
An exact finite-block polar source code for a small binary source. |
|
Abstract base class for source codes. |
|
Stop-and-wait automatic repeat request over a linear block code. |
|
A source code that maps each outcome to a single codeword. |
|
A variable-to-fixed-length source code. |
Functions
Coerce channel to an object implementing the channel protocol. |
|
The binary erasure channel with erasure probability |
|
The binary symmetric channel with crossover probability |
|
Build a rate- |
|
The Elias delta codeword for |
|
The Elias gamma codeword for |
|
The Elias omega (recursive) codeword for |
|
An |
|
Build a Fano code (the Shannon-Fano top-down splitting method). |
|
The Fibonacci codeword for |
|
Build a regular Gallager LDPC code. |
|
The binary Golay code. |
|
Build a Golomb code over non-negative-integer outcomes. |
|
The |
|
A |
|
Build a Huffman code, the optimal symbol code for the source. |
|
Build an LDPC code from a parity-check matrix. |
|
Build an optimal prefix code whose codewords are at most max_length long. |
|
The |
|
Build a polar code. |
|
Build an exact finite-block polar source code. |
|
The Reed-Muller code |
|
The |
|
Build a Rice code, the Golomb code with parameter |
|
Build a Shannon code. |
|
Build a Shannon-Fano-Elias code. |
|
The source Bhattacharyya parameter of a binary variable. |
|
The high-entropy indices selected by a polar source code. |
|
The exact source-polarization profile of |
|
A |
|
Build a Tunstall code for a memoryless source. |
|
The unary codeword for |
|
Build a |
Reference
- class ChannelCoding(channel=None, radix=2)[source]
Bases:
ABCAbstract base class for channel codes.
A channel code adds redundancy to a message so that it can be recovered after transmission across a noisy channel. This class is scaffolding for future channel codes (repetition, polar, LDPC, …); no concrete subclass is provided yet.
Concrete subclasses are expected to implement
encode()(message -> codeword),decode()(received word -> message), andrate()(message symbols per channel use), and may additionally expose channel-code properties such as the minimum distance, the block error probability for a given channel, and the gap to capacity (viadit.algorithms.channel_capacity()).- Parameters:
channel (Distribution, object, None) – The channel, as a conditional distribution
p(output | input)or any object implementing the channel protocol (seeas_channel()).radix (int) – The size of the code alphabet. Default is 2 (binary).
- abstractmethod decode(received, channel=None)[source]
Decode a received word back into a message.
Soft-decision decoders use channel (a conditional
p(Y|X)distribution, or any object implementing the channel protocol) to form log-likelihoods; hard-decision decoders ignore it.
- capacity_gap(channel, **kwargs)[source]
The gap between the channel capacity and the code rate.
- Parameters:
channel (Distribution or object) – The channel: a conditional distribution
p(Y|X), or any object implementing the channel protocol (seeas_channel()) and providingcapacity().kwargs (dict) – Passed to the channel’s
capacity().
- Returns:
gap –
capacity(channel) - rate, in bits per channel use. Positive when the code operates below capacity. When the channel only brackets its capacity, a(lower, upper)bracket on the gap.- Return type:
- probability_of_error(channel, method='auto', samples=10000, rng=None)[source]
The block error probability of the code over a channel.
The code’s own
decode()is used (passing channel so soft-decision decoders engage). Withmethod='exact'every message and every received word is enumerated – feasible only for small codes – whilemethod='montecarlo'estimates the probability by sampling.- Parameters:
channel (Distribution or object) – The channel: a conditional distribution
p(Y|X), or any object implementing the channel protocol (seeas_channel()), such as a finite-state channel with memory.method (str) –
'exact','montecarlo', or'auto'(exact when small).samples (int) – The number of Monte Carlo samples.
rng (numpy.random.Generator, None) – The random number generator for Monte Carlo sampling.
- Returns:
pe – The probability that the decoded message differs from the sent one.
- Return type:
- class ConvolutionalCode(generators, message_length, channel=None)[source]
Bases:
ChannelCodingA rate-
1/nconvolutional code.- Parameters:
generators (sequence of int) – The generator polynomials, in octal (e.g.
(0o7, 0o5)).message_length (int) – The number of information bits per encoded block.
channel (Distribution, None) – A default channel.
- class ErasureARQ(k, erasure=2)[source]
Bases:
FeedbackCodeSend each bit until it arrives unerased.
On the binary erasure channel this is zero-error and uses
k / (1 - epsilon)channel uses on average, so it achieves the capacity1 - epsilonexactly [CT06]. Any output other than the erasure symbol is taken at face value, so on a channel that also flips bits it makes errors.
- class FeedbackCode(k)[source]
Bases:
ABCAbstract base class for channel codes with noiseless feedback.
Subclasses implement
start(),next_input(),update()anddecode(). States must be treated as immutable:update()returns a new state, so thatprobability_of_error()can branch on every channel output.- Parameters:
k (int) – The number of message bits.
- property message_length
The number of message bits
k.
- abstractmethod next_input(state, message)[source]
The next channel input for message, or
Noneto stop transmitting.
- abstractmethod update(state, y)[source]
The shared state after the channel output y is received (and fed back).
- transmit(message, channel, rng=None, max_uses=64)[source]
Send message across channel once.
- Parameters:
message (sequence of int) – The
kmessage bits.channel (Distribution or object) – The channel, implementing
feedback_initialandfeedback_step.rng (numpy.random.Generator, None) – The random number generator.
max_uses (int) – Stop (and decode) after this many channel uses regardless.
- Returns:
decoded (tuple) – The receiver’s estimate of the message.
uses (int) – The number of channel uses.
- performance(channel, method='auto', samples=10000, rng=None, max_uses=64)[source]
The block error probability and the expected number of channel uses.
- Parameters:
channel (Distribution or object) – The channel, implementing
feedback_initialandfeedback_step.method (str) –
'exact'enumerates every message and every output sequence;'montecarlo'samples;'auto'is exact unless the output tree exceeds about a million branches.samples (int) – The number of Monte Carlo samples.
rng (numpy.random.Generator, None) – The random number generator for Monte Carlo sampling.
max_uses (int) – Transmission is cut off (and decoded) after this many channel uses, so the exact values are those of the truncated code.
- Returns:
pe (float) – The probability that the decoded message differs from the sent one.
uses (float) – The expected number of channel uses.
- probability_of_error(channel, **kwargs)[source]
The block error probability over channel; keyword arguments as in
performance().
- expected_uses(channel, **kwargs)[source]
The expected number of channel uses; keyword arguments as in
performance().
- rate(channel, **kwargs)[source]
The rate
k / E[N]in bits per channel use, whereNis the number of uses.The rate of a variable-length code depends on the channel, so unlike
ChannelCoding.rateit takes one. Keyword arguments are as inperformance().
- class HorsteinCode(k, n=None, delta=None)[source]
Bases:
FeedbackCodeHorstein’s bisection scheme over the
2^kmessages.Both ends keep the receiver’s posterior over the messages, in a fixed order. At each use the transmitter sends
1if its message lies above the posterior median and0otherwise, so every output roughly halves the receiver’s uncertainty [Hor63]. Posterior matching [SF11] generalizes the idea; Schalkwijk and Kailath’s scheme is its Gaussian-channel counterpart [SK66].The posterior is updated through the channel’s
feedback_step, as a joint belief over the message and the channel’s hidden state, so the same code runs on finite-state channels. Inputs must be the bits{0, 1}.- Parameters:
- start(channel)[source]
The uniform prior over messages, with the channel’s initial belief for each.
- class LDPCCode(H, channel=None, max_iterations=50)[source]
Bases:
LinearCodeA linear code decoded by belief propagation on a sparse parity-check matrix.
- Parameters:
H (array-like) – An
m x nbinary parity-check matrix (typically sparse).channel (Distribution, None) – A default channel.
max_iterations (int) – The maximum number of belief-propagation iterations.
- class LinearCode(G, channel=None)[source]
Bases:
ChannelCodingA linear block code over GF(2).
- Parameters:
G (array-like) – A
k x nbinary generator matrix of full row rank.channel (Distribution, None) – A default channel, as a conditional distribution
p(Y|X).
- property length
The block length
n.
- property dimension
The number of information bits
k.
- property message_length
The number of message bits per codeword (
k).
- decode(received, channel=None)[source]
Decode a received word.
With no channel, hard-decision syndrome decoding is used. With a channel, maximum-likelihood decoding over the codebook is used.
- error_correcting_capability()[source]
The number of errors the code can correct,
floor((d - 1) / 2).
- property generator_matrix
The generator matrix
G.
- property parity_check_matrix
The parity-check matrix
H.
- class MemorylessChannel(dist)[source]
Bases:
objectA discrete memoryless channel, applying
p(Y | X)independently to each symbol.- Parameters:
dist (Distribution) – A conditional distribution
p(Y | X).
- class PolarCode(n, k, channel)[source]
Bases:
LinearCodeA polar code of length
n = 2^mwithkinformation bits.- Parameters:
n (int) – The block length; must be a power of two.
k (int) – The number of information bits.
channel (Distribution or object) – The channel used both to select the frozen set and to decode. It must provide a Bhattacharyya parameter; to decode over a channel that does not (e.g. one with memory), design on a memoryless channel and pass the other channel to
decode().
- property message_length
The number of message bits per codeword (
k).
- class PolarSourceCode(dist, block_length, rv=0, crvs=None, rank_by='entropy', rate=None, size=None, tol=1e-09, max_states=65536)[source]
Bases:
SourceCodingAn exact finite-block polar source code for a small binary source.
The code applies the Arikan transform
U^N = X^N G_Nto a block ofNsource bits and stores only the coordinates in the high-entropy set (those that stay nearly uniform given the past and any side information). Decoding fills in the remaining low-entropy coordinates by sequential maximum a posteriori (MAP) decisions against the exactp(U^N, Y^N)table and then inverts the transform.Because the code enumerates the joint distribution exactly (no density evolution or list decoding), it is intended for small blocks: the number of enumerated states is
|copy support|^N, guarded bymax_states.- Parameters:
dist (Distribution) – A single-copy source
(X)or source-with-side-information(X, Y, ...)distribution.X(indexed byrv) must be binary.block_length (int) – The block length
N; must be a power of two.rv (int) – The index of the binary source variable. Default is 0.
crvs (list, None) – Indices of the side-information variables available at the decoder. If None, the code is a plain (side-information-free) polar source code.
rank_by (str) – How to rank coordinates for the high-entropy set:
"entropy"(default) or"bhattacharyya".rate (float, None) – A fixed target rate in
[0, 1](keepround(rate * N)coordinates). Mutually exclusive withsize.size (int, None) – The exact high-entropy set size. By default the set is chosen losslessly – every coordinate whose conditional entropy exceeds
tolis kept, so the code reconstructs each block exactly. Fixingsize(orrate) below the lossless size yields a fixed-rate code that may not be exact.tol (float) – The conditional-entropy threshold used by the lossless default.
max_states (int) – A guard on the number of enumerated joint outcomes.
- property message_length
The number of stored (high-entropy) coordinates per block.
- decode(encoded, side_information=None)[source]
Decode a block from its high-entropy coordinates (and any side info).
The high-entropy coordinates are filled from
encoded; the low-entropy coordinates are recovered one at a time by an exact MAP decision againstp(U_i | U^{i-1}, Y^N). The inverse Arikan transform then returns the source bits.- Parameters:
encoded (sequence of int) – The stored high-entropy bits, in ascending index order.
side_information (sequence, None) – The decoder side information
Y^N: one symbol per copy when the code was built withcrvs(flattened copy-major when multiple side variables). Required iff the code has side information.
- Returns:
x_block – The recovered length-
Nblock of source bits.- Return type:
- class SourceCoding(dist=None, radix=2)[source]
Bases:
ABCAbstract base class for source codes.
A source code maps the outcomes of a
Distributionto codewords over a radix-ary code alphabet. Concrete subclasses define how outcomes are encoded and decoded, and what the code’s rate is; this base class provides the comparisons to the source entropy that are common to all source codes.- Parameters:
dist (Distribution, None) – The source distribution the code is built for. Required for any of the rate-based properties.
radix (int) – The size of the code alphabet. Default is 2 (binary).
- abstractmethod encode(source)[source]
Encode a sequence of source outcomes into a sequence of code symbols.
- abstractmethod decode(encoded)[source]
Decode a sequence of code symbols back into source outcomes.
- source_entropy()[source]
The entropy of the source, in units of radix-ary digits.
This is the fundamental lower bound on the rate of any uniquely decodable source code (the source coding theorem; Cover & Thomas, Ch. 5).
- Returns:
H – The source entropy,
H[X] / log2(radix).- Return type:
- class StopAndWaitARQ(code, max_rounds=None)[source]
Bases:
FeedbackCodeStop-and-wait automatic repeat request over a linear block code.
The codeword is sent; if the received word is not a codeword (a nonzero syndrome, or any non-binary output such as an erasure) the receiver asks for it again. An error goes undetected only when the error pattern is itself a nonzero codeword. After max_rounds failed attempts the last received word is syndrome-decoded.
- Parameters:
code (LinearCode) – The block code used for error detection.
max_rounds (int, None) – The maximum number of transmissions of the block; unlimited by default.
- class SymbolCode(codebook, dist=None, radix=2)[source]
Bases:
SourceCodingA source code that maps each outcome to a single codeword.
- Parameters:
codebook (dict) – A mapping from source outcomes to codeword strings (e.g.
'010').dist (Distribution, None) – The source distribution. Required for the rate-based properties (
rate(),average_length(),redundancy(), …).radix (int) – The size of the code alphabet. Default is 2 (binary).
- encode(source)[source]
Encode a sequence of source outcomes into a string of code symbols.
- Parameters:
source (iterable) – A sequence of source outcomes.
- Returns:
encoded – The concatenated codewords.
- Return type:
- average_length()[source]
The expected codeword length,
sum_x p(x) * len(codeword(x)).- Returns:
L
- Return type:
- rate()[source]
The expected number of code symbols per source symbol.
For a symbol code this is exactly the average codeword length.
- length_variance()[source]
The variance of the codeword length under the source distribution.
Among optimal codes (which share the minimal average length) one often prefers the one of least length variance.
- Returns:
var
- Return type:
- kraft_sum()[source]
The Kraft sum
sum_x radix ** -len(codeword(x)).By the Kraft-McMillan inequality this is
<= 1for any uniquely decodable code, with equality iff the code is complete.- Returns:
K
- Return type:
- is_complete()[source]
Whether the code is complete (its Kraft sum equals 1).
- Returns:
complete
- Return type:
- is_prefix_free()[source]
Whether no codeword is a prefix of another (the code is instantaneous).
- Returns:
prefix_free
- Return type:
- class TunstallCode(word_to_code, word_probs, code_length, dist=None, radix=2)[source]
Bases:
SourceCodingA variable-to-fixed-length source code.
- Parameters:
word_to_code (dict) – A mapping from source words (tuples of outcomes) to fixed-length codeword strings.
word_probs (dict) – A mapping from source words to their probabilities (used for the rate).
code_length (int) – The fixed codeword length, in code symbols.
dist (Distribution, None) – The source distribution.
radix (int) – The size of the code alphabet. Default is 2.
- expected_word_length()[source]
The expected number of source symbols per dictionary word.
- Returns:
L
- Return type:
- rate()[source]
The expected number of code symbols per source symbol.
Each word maps to
code_lengthcode symbols and spansexpected_word_lengthsource symbols on average.
- as_channel(channel)[source]
Coerce channel to an object implementing the channel protocol.
- Parameters:
channel (Distribution or object) – A conditional distribution
p(Y | X); an object with a__dit_channel__()method; or an object already implementing the channel protocol described indit.coding._channel.- Returns:
channel – An object implementing the channel protocol.
- Return type:
- binary_erasure_channel(epsilon)[source]
The binary erasure channel with erasure probability
epsilon.Each transmitted bit is independently erased with probability
epsilonand 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}, where2denotes an erasure.- Return type:
- 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:
- convolutional(generators, message_length, channel=None)[source]
Build a rate-
1/nconvolutional code.- Parameters:
generators (sequence of int) – The generator polynomials in octal (e.g.
(0o7, 0o5)).message_length (int) – The number of information bits per encoded block.
channel (Distribution, None) – A default channel.
- Returns:
code
- Return type:
- elias_delta(n)[source]
The Elias delta codeword for
n >= 1.The codeword is the gamma code of the bit-length of
nfollowed by the bits ofnbelow its leading one.
- elias_gamma(n)[source]
The Elias gamma codeword for
n >= 1.The codeword is
floor(log2(n))zeros followed by the binary representation ofn.
- erasure_arq(k, erasure=2)[source]
An
ErasureARQcode for k bits: resend each bit until it is not erased.
- fano(dist, radix=2)[source]
Build a Fano code (the Shannon-Fano top-down splitting method).
Outcomes are sorted by probability and recursively partitioned into two groups of as-equal-as-possible total probability; one bit distinguishes the groups at each level.
- Parameters:
dist (Distribution) – The source distribution.
radix (int) – The size of the code alphabet. Only
radix=2is currently supported.
- Returns:
code
- Return type:
- gallager(n, wc, wr, rng=None, channel=None)[source]
Build a regular Gallager LDPC code.
- Parameters:
n (int) – The block length; must be divisible by
wr.wc (int) – The column weight (variable degree).
wr (int) – The row weight (check degree);
wc < wr.rng (numpy.random.Generator, None) – The random number generator used to permute the sub-bands.
channel (Distribution, None) – A default channel.
- Returns:
code
- Return type:
- golay(extended=False, channel=None)[source]
The binary Golay code.
- Parameters:
extended (bool) – If True, return the extended
[24, 12, 8]code; otherwise the perfect[23, 12, 7]code.channel (Distribution, None) – A default channel.
- Returns:
code
- Return type:
- golomb(dist, m=None, radix=2)[source]
Build a Golomb code over non-negative-integer outcomes.
Each codeword is the unary-coded quotient
n // mfollowed by the truncated-binary remaindern % m. Golomb codes are optimal prefix codes for geometrically distributed sources.- Parameters:
dist (Distribution) – The source distribution; its outcomes must be non-negative integers.
m (int, None) – The Golomb parameter. If None, the parameter optimal for the (assumed geometric) source is chosen from its mean.
radix (int) – The size of the code alphabet. Only
radix=2is currently supported.
- Returns:
code
- Return type:
- hamming(r, channel=None)[source]
The
[2^r - 1, 2^r - 1 - r, 3]Hamming code.- Parameters:
r (int) – The number of parity bits (
r >= 2).channel (Distribution, None) – A default channel.
- Returns:
code
- Return type:
- horstein(k, n=None, delta=None)[source]
A
HorsteinCodefor k bits, stopping after n uses or at posterior1 - delta.
- huffman(dist, radix=2)[source]
Build a Huffman code, the optimal symbol code for the source.
For
radix > 2the alphabet is padded with zero-probability dummy symbols so that every internal node has exactly radix children.- Parameters:
dist (Distribution) – The source distribution.
radix (int) – The size of the code alphabet. Default is 2.
- Returns:
code
- Return type:
- ldpc(H, channel=None, max_iterations=50)[source]
Build an LDPC code from a parity-check matrix.
- Parameters:
H (array-like) – An
m x nbinary parity-check matrix.channel (Distribution, None) – A default channel.
max_iterations (int) – The maximum number of belief-propagation iterations.
- Returns:
code
- Return type:
- length_limited_huffman(dist, max_length, radix=2)[source]
Build an optimal prefix code whose codewords are at most max_length long.
Uses the package-merge algorithm (Larmore & Hirschberg, 1990).
- Parameters:
dist (Distribution) – The source distribution.
max_length (int) – The maximum allowed codeword length.
radix (int) – The size of the code alphabet. Only
radix=2is currently supported.
- Returns:
code
- Return type:
- parity_check(k, channel=None)[source]
The
[k+1, k, 2]single-parity-check code.- Parameters:
k (int) – The number of information bits.
channel (Distribution, None) – A default channel.
- Returns:
code
- Return type:
- polar(n, k, channel)[source]
Build a polar code.
- Parameters:
n (int) – The block length; must be a power of two.
k (int) – The number of information bits.
channel (Distribution) – The channel used to select the frozen set and to decode.
- Returns:
code
- Return type:
- polar_source(dist, block_length, rv=0, crvs=None, rank_by='entropy', rate=None, size=None, tol=1e-09, max_states=65536)[source]
Build an exact finite-block polar source code.
- Parameters:
dist (Distribution) – A single-copy binary source
(X)or(X, Y, ...)distribution.block_length (int) – The block length
N; must be a power of two.rv (int) – The index of the binary source variable. Default is 0.
crvs (list, None) – Indices of the side-information variables available at the decoder.
rank_by (str) –
"entropy"(default) or"bhattacharyya".rate (float, None) – A fixed target rate in
[0, 1]. Mutually exclusive withsize.size (int, None) – The high-entropy set size. Defaults to the lossless set (see
PolarSourceCode).tol (float) – The conditional-entropy threshold used by the lossless default.
max_states (int) – A guard on the number of enumerated joint outcomes.
- Returns:
code
- Return type:
- reed_muller(r, m, channel=None)[source]
The Reed-Muller code
RM(r, m).- Parameters:
r (int) – The order (
0 <= r <= m).m (int) – The number of variables; the block length is
2^m.channel (Distribution, None) – A default channel.
- Returns:
code
- Return type:
- repetition(n, channel=None)[source]
The
[n, 1, n]repetition code.- Parameters:
n (int) – The block length.
channel (Distribution, None) – A default channel.
- Returns:
code
- Return type:
- rice(dist, k=None)[source]
Build a Rice code, the Golomb code with parameter
m = 2 ** k.- Parameters:
dist (Distribution) – The source distribution; its outcomes must be non-negative integers.
k (int, None) – The Rice parameter. If None, it is chosen from the optimal Golomb parameter for the source.
- Returns:
code
- Return type:
- shannon(dist, radix=2)[source]
Build a Shannon code.
Codeword lengths are
ceil(log_radix(1/p))and codewords are read off the base-radix expansion of the cumulative distribution (outcomes sorted by decreasing probability).- Parameters:
dist (Distribution) – The source distribution.
radix (int) – The size of the code alphabet. Default is 2.
- Returns:
code
- Return type:
- shannon_fano_elias(dist, radix=2)[source]
Build a Shannon-Fano-Elias code.
Codeword lengths are
ceil(log_radix(1/p)) + 1and codewords are read off the base-radix expansion of the midpoint cumulative distributionF_bar(x) = sum_{y<x} p(y) + p(x)/2. The result is prefix-free.- Parameters:
dist (Distribution) – The source distribution.
radix (int) – The size of the code alphabet. Default is 2.
- Returns:
code
- Return type:
- source_bhattacharyya(dist, rv=0, crvs=None)[source]
The source Bhattacharyya parameter of a binary variable.
For a binary
Xwith side informationY,\[Z(X \mid Y) = 2 \sum_y \sqrt{p(0, y)\,p(1, y)},\]which reduces to
2 sqrt(p(0) p(1))when there is no side information. It satisfies0 <= Z <= 1, withZnear1whenXis nearly uniform givenYand near0whenXis nearly determined byY.- Parameters:
dist (Distribution) – The source distribution.
rv (int) – The index of the binary source variable. Default is 0.
crvs (list, None) – Indices of the side-information variables. If None,
Z(X)is computed.
- Returns:
Z
- Return type:
- source_high_entropy_set(dist, block_length, rate=None, size=None, rv=0, crvs=None, rank_by='entropy', tol=1e-09)[source]
The high-entropy indices selected by a polar source code.
Ranks the
Nsynthesized coordinates by their conditional metric (largest conditional entropy, or largest source Bhattacharyya, given the past and any side information) and returns the top indices – those a lossless polar source code must transmit. At most one ofrateorsizemay be given:size– keep exactly this many coordinates,rate– keepround(rate * N)coordinates,neither – keep every coordinate whose conditional entropy exceeds
tol, i.e. the lossless set (the dropped coordinates are deterministic given the past and side information, so the code reconstructs the block exactly).
- Parameters:
dist (Distribution) – The source distribution.
block_length (int) – The number of i.i.d. copies
N; must be a power of two.rate (float, None) – The target fraction of coordinates to keep, in
[0, 1]. The set size isround(rate * N).size (int, None) – The exact number of coordinates to keep.
rv (int) – The index of the binary source variable. Default is 0.
crvs (list, None) – Indices of the side-information variables.
rank_by (str) –
"entropy"(default) or"bhattacharyya".tol (float) – The conditional-entropy threshold for the lossless default; coordinates at or below
tolare treated as deterministic and dropped.
- Returns:
indices – The selected coordinate indices, sorted ascending.
- Return type:
- source_polarization_profile(dist, block_length, rv=0, crvs=None, metrics=('entropy', 'bhattacharyya'))[source]
The exact source-polarization profile of
N = block_lengthi.i.d. copies.Applies the Arikan transform
U^N = X^N G_Nand reports, for each synthesized coordinatei, the requested metrics conditioned on the pastU^{i-1}and the side informationY^N:"entropy"– the conditional entropyH(U_i | U^{i-1}, Y^N),"bhattacharyya"– the conditional source Bhattacharyya parameterZ(U_i | U^{i-1}, Y^N).
The conditional entropies sum to
N * H(X | Y)(entropy conservation), and asNgrows they polarize toward0or1.A third, optional Goela-style diagnostic reuses
dit.divergences.maximum_correlation():"max_correlation_with_past"– the maximal correlationrho_m(U_i ; U^{i-1})between each coordinate and its past (0.0ati = 0, and ignoring side information). Goela et al. (2014) show these also polarize.
- Parameters:
dist (Distribution) – The source distribution for a single copy
(X)or(X, Y, ...).block_length (int) – The number of i.i.d. copies
N; must be a power of two.rv (int) – The index of the binary source variable. Default is 0.
crvs (list, None) – Indices of the side-information variables. If None, no side information.
metrics (tuple of str) – Which metrics to compute per coordinate.
- Returns:
profile – One dict per coordinate
i(in[0, N)), keyed"index"plus the requested metric names.- Return type:
- stop_and_wait_arq(code, max_rounds=None)[source]
A
StopAndWaitARQcode: resend code’s block until it is received as a codeword.
- tunstall(dist, code_length, radix=2)[source]
Build a Tunstall code for a memoryless source.
- Parameters:
dist (Distribution) – The source distribution over single symbols (assumed i.i.d.).
code_length (int) – The fixed codeword length, in code symbols. The dictionary holds up to
radix ** code_lengthwords.radix (int) – The size of the code alphabet. Default is 2.
- Returns:
code
- Return type:
- universal_code(dist, kind='gamma')[source]
Build a
SymbolCodeover integer outcomes using a universal code.- Parameters:
dist (Distribution) – The source distribution; its outcomes must be positive integers.
kind (str) – One of
'unary','gamma','delta','omega', or'fibonacci'.
- Returns:
code
- Return type: