Fixed-to-Variable Length Distribution Matching

Fixed-to-variable length (f2v) matchers are used to reversibly transform an input sequence of independent and uniformly distributed bits into an output sequence of bits that are (approximately) independent and distributed according to a target distribution. The degree of approximation is measured by the informational divergence between the output distribution and the target distribution. An algorithm is developed that efficiently finds optimal f2v codes. It is shown that by encoding the input bits blockwise, the informational divergence per bit approaches zero as the block length approaches infinity. A relation to data compression by Tunstall coding is established.


I. INTRODUCTION
Distribution matching considers the problem of mapping uniformly distributed bits to symbols that are approximately distributed according to a target distribution. In difference to the simulation of random processes [1] or the exact generation of distributions [2], distribution matching requires that the original bit sequence can be recovered from the generated symbol sequence. We measure the degree of approximation by the normalized informational divergence (Idivergence), which is an appropriate measure when we want to achieve channel capacity of noisy and noiseless channels [3,Sec. 3.4.3 & Chap. 6] by using a matcher. A related work is [4], [3,Chap. 3], where it is shown that variable-to-fixed length (v2f) matching is optimally done by geometric Huffman coding and the relation to fixed-to-variable length (f2v) source encoders is discussed. In the present work, we consider binary distribution matching by prefix-free f2v codes.

A. Rooted Trees With Probabilities
We use the framework of rooted trees with probabilities [5], [6]. Let T be the set of all binary trees with 2 m leaves and consider some tree T ∈ T . Index all nodes by the numbers N := {1, 2, 3, . . . } where 1 is the root. Note that there are at least |N | ≥ 2 m+1 − 1 nodes in the tree, with equality if the tree is complete. A tree is complete if any right-infinite binary sequence starts with a path from the root to a leaf. Let L ⊂ N be the set of leaf nodes and let B := N \ L be the set of branching nodes. Probabilities can be assigned to the tree by defining a distribution over the 2 m paths through the tree. For each i ∈ N , denote by P T (i) the probability that a path is chosen that passes through node i. Since each path ends at a different leaf node, P T defines a leaf distribution, i.e., This work was supported by the German Ministry of Education and Research in the framework of an Alexander von Humboldt Professorship. In (a), a uniform leaf distribution U T is chosen, i.e., for each i ∈ L, U T (i) = 1 4 . The leaf distribution determines the node probabilities and branching distributions. In (b), identical branching distributions are chosen, i.e., for each i ∈ B, Q i T = Q, where Q(0) =: q 0 = 2 3 and Q(1) =: q 1 = 1 3 . The branching distributions determine the resulting node probabilities, which we denote by Q T (i). Since the tree is complete, i∈L Q T (i) = 1 and Q T defines a leaf distribution.
i∈L P T (i) = 1. For each branching node i ∈ B, denote by P i T the branching distribution, i.e., the probabilities of branch 0 and branch 1 after passing through node i. The probabilities on the tree are completely defined either by defining the branching distributions {P i T , i ∈ B} or by defining the leaf distribution {P T (i), i ∈ L}. See Fig. 1 for an example.

B. v2f Source Encoding and f2v Distribution Matching
Consider a binary distribution Q with q 1 = Q(0), q 1 = Q(1), 0 < q 0 < 1, and a binary tree T with 2 m leaves. Let Q T (i), i ∈ N , be the node probabilities that result from having all branching distributions equal to Q, i.e. Q i T = Q for each i ∈ B. See Fig. 1(b) for an example. Let U T be a uniform leaf distribution, i.e., U T (i) = 2 −m for each i ∈ L, see Fig. 1(a) for an example. We use the tree as a v2f source code for a discrete memoryless source (DMS) Q. To guarantee lossless compression, the tree for a v2f source encoder has to be complete. Consequently, Q T defines a leaf distribution, i.e., i∈L Q T (i) = 1. We denote the set of complete binary trees with 2 m leaves by C. Each code word consists of log 2 2 m = m bits and the resulting entropy rate at the encoder output is where H(Q T ) is the entropy of the leaf distribution defined by Q T and where D(Q T U T ) is defined accordingly. From (1), we conclude that the objective is to solve The solution is known to be attained by Tunstall coding [7]. The tree in Fig. 1 is a Tunstall code for Q : q 0 = 2 3 , q 1 = 1 3 and m = 2 and the corresponding v2f source encoder is 000 → 00, 001 → 01, 01 → 10, 1 → 11.
The dual problem is f2v distribution matching. Q is now a binary target distribution and we generate the codewords defined by the paths through a (not necessarily complete) binary tree uniformly according to U T . For example, the f2v distribution matcher defined by the tree in Fig. 1 is Denote by i , i ∈ L the path lengths and let L be a random variable that is uniformly distributed over the path lengths according to U T . We want the I-divergence per output bit of U T and Q T to be small, i.e., we want to solve In contrast to (2), the minimization is now over the set of all (not necessarily complete) binary trees with 2 m leaves. Note that although for a non-complete tree we have i∈L Q T (i) < 1, the problem (5) is well-defined, since there is always a complete tree with leaves L ⊇ L and i∈L Q T (i) = 1. The sum in (5) is over the support of U T , which is L. Solving (5) is the problem that we consider in this work.

C. Outline
In Sec. II and Sec. III, we restrict attention to complete trees. We show that Tunstall coding applied to Q minimizes D(U T Q T ) and that iteratively applying Tunstall coding to weighted versions of Q minimizes D(U T Q T )/ E U T (L). In Sec. IV we derive conditions for the optimality of complete trees and show that the I-divergence per bit can be made arbitrarily small by letting the blocklength m approach infinity. Finally, in Sec. V, we illustrate by an example that source decoders are sub-optimal distribution matchers and vice-versa, distribution dematchers are sub-optimal source encoders.

II. MINIMIZING I-DIVERGENCE
Let R be the set of real numbers. For a finite set S, we say that W : S → R is a weighted distribution if for each i ∈ S, W (i) > 0. We allow for i∈S W (i) = 1. The I-divergence of a distribution P and a weighted distribution W is where supp denotes the support of P . The reason why we need this generalization of the notion of distributions and Idivergence will become clear in the next section.
Proposition 1. Let Q be a weighted binary target distribution, and let be an optimal complete tree. Then we find that i. An optimal complete tree T * can be constructed by applying Tunstall coding to Q. ii. If 0 ≤ q 0 ≤ 1 and 0 ≤ q 1 ≤ 1, then T * also minimizes D(U T Q T ) among all possibly non-complete binary trees T , i.e., the optimal tree is complete.
Proof: Part i. We write and hence Consider now an arbitrary complete tree T ∈ C. Since the tree is complete, there exist (at least) two leaves that are siblings, say j and j + 1. Denote by k the corresponding branching node. The contribution of these two leaves to the objective function on the right-hand side of (9) can be written as Now consider the tree T that results from removing the nodes j and j +1. The new set of leaf nodes is L = k ∪L\{j, j +1} and the new set of branching nodes is B = B \ k. Also Q T defines a weighted leaf distribution on L . The same procedure can be applied repeatedly by defining T = T , until T consists only of the root node. We use this idea to re-write the objective function of the right-hand side of (9) as follows. i∈L The right-hand side of (12) is clearly maximized by the complete tree with the branching nodes with the greatest weighted probabilities. According to [8, p. 47], this is exactly the tree that is constructed when Tunstall coding is applied to the weighted distribution Q. Part ii. We now consider q 0 ≤ 1 and q 1 ≤ 1. Assume we have constructed a non-complete binary tree. Because of noncompleteness, we can remove a branch from the tree. Without loss of generality, assume that this branch is labeled by a zero. Denote by S the leaves on the subtree of the branch. Denote the tree after removing the branch by T . Now, where the inequality follows because by assumption q 0 ≤ 1. Thus, for the new tree T , the objective function (11) is bounded as In summary, under the assumption q 0 ≤ 1 and q 1 ≤ 1, the objective function (11) that we want to maximize does not decrease when removing branches, which shows that there is an optimal complete tree. This proves the statement ii. of the proposition.

III. MINIMIZING I-DIVERGENCE PER BIT
The following two propositions relate the problem of minimizing the I-divergence per bit to the problem of minimizing the un-normalized I-divergence.
Let T ⊆ T be some set of binary trees with 2 m leaves and define Proposition 2. We have where Q ∆ T is the weighted distribution induced by Q • 2 ∆ := [q 0 2 ∆ , q 1 2 ∆ ].
Proof: By (15), for any tree T ∈ T , we have We write the left-hand side of (18) as Consider the path through the tree that ends at leaf i. Denote by 0 i and 1 i the number of times the labels 0 and 1 occur, respectively. The length of the path can be expressed as i = 0 i + 1 i . The term Q T (i)2 ∆ i can now be written as Using (20) and (19) in (18) shows that for any binary tree which is the statement of the proposition.

(22)
Then the optimal complete tree is constructed by applying Tunstall coding to Q ∆ T . Proof: The proposition is a consequence of Prop. 2 and Prop. 1.i.

2.T = argmin
By Prop. 3, if we know the I-divergence ∆, then we can find T * by Tunstall coding. However, ∆ is not known a priori. We solve this problem by iteratively applying Tunstall coding to Q • 2∆, where∆ is an estimate of ∆ and by updating our estimate. This procedure is stated in Alg. 1. We first show that ∆ is strictly monotonically decreasing. Let∆ i be the value that is assigned to∆ in step 1. of the ith iteration and denote byT i the value that is assigned toT in step 2. of the ith iteration. Suppose that the algorithm does not terminate in the ith iteration. We havê By step 2, we havê and since by our assumption the algorithm does not terminate in the ith iteration, we have Now assume the algorithm terminated, and letT be the tree after termination. Because of the assignments in steps 1. and 2., the terminating condition implies that for any tree T ∈ C, we have Consequently, we have We conclude that after termination, (∆,T ) is equal to the optimal tuple (∆, T * ) in Prop. 3. Finally, we have shown that ∆ is strictly monotonically decreasing so thatT i =T j for all i < j. But there is only a finite number of complete binary trees with 2 m leaves. Thus, the algorithm terminates after finitely many steps.

IV. OPTIMALITY OF COMPLETE TREES
Complete trees are not optimal in general: Consider m = 1 and Q : q 0 = 5 6 , q 1 = 1 6 . For m = 1, Tunstall coding constructs the (unique) complete binary tree T with 2 leaves, independent of which target vector we pass to it. The path lengths are 1 = 2 = 1. The I-divergence per bit achieved by this is Now, we could instead use a non-complete tree T with the paths 0 and 10. In this case, I-divergence per bit is In summary, for the considered example, using a complete tree is sub-optimal. We will in the following derive simple conditions on the target vector Q that guarantee that the optimal tree is complete.

A. Sufficient Conditions for Optimality
Proposition 5. Let Q be a distribution. If max{q 0 , q 1 } ≤ 4 min{q 0 , q 1 }, then the optimal tree is complete for any m ≥ 1 and it is constructed by Alg. 1.
Proof: According to Prop. 1.ii, the tree that minimizes D(U T Q ∆ T ) is complete if the entries of the weighted distribution Q • 2 ∆ are both less than or equal to one. Without loss of generality, assume that q 0 ≥ q 1 . Thus, we only need to check this condition for q 0 . We have We calculate the value of ∆ that is achieved by the (unique) complete tree with 2 leaves, namely For each m ≥ 1, this ∆ is achieved by the complete tree with all path lengths equal to m. Substituting the right-hand side of (32) for ∆ in (31), we obtain which is the condition stated in the proposition.
B. Asymptotic Achievability for Complete Trees Proposition 6. Denote by T (m) the complete tree with 2 m leaves that is constructed by applying Alg. 1 to a target distribution Q. Then we have and in particular, the I-divergence per bit approaches zero as m → ∞.
Proof: The expected length can be bounded by the converse of the Coding Theorem for DMS [8, p. 45] as Thus, we have The tree T (m) that minimizes the right-hand side is found by applying Tunstall coding to Q. Without loss of generality, assume that q 0 ≥ q 1 . According to the Tunstall Lemma [8, p. 47], the induced leaf probability of a tree constructed by Tunstall coding is lower bounded as We can therefore bound the I-divergence as We can now bound the I-divergence per bit as This proves the proposition.
C. Optimality of Complete Trees for Large Enough m Proposition 7. For any target distribution Q with q 0 < 1 and q 1 = 1 − q 0 , there is an m 0 such that for all m > m 0 , the tree that minimizes is complete.
Proof: Without loss of generality, assume that q 0 ≥ q 1 .
By Prop. 6, we have ∆ m ≤  V. SOURCE CODING VERSUS DISTRIBUTION MATCHING An ideal source encoder transforms the output of a DMS Q into a sequence of bits that are independent and uniformly distributed. Reversely, applying the corresponding decoder to a sequence of uniformly distributed bits generates a sequence of symbols that are iid according to Q. This suggests to design a f2v distribution matcher by first calculating the optimal v2f source encoder. The inverse mapping is f2v and can be used as a distribution matcher.
We illustrate by an example that this approach is suboptimal in general. Consider the DMS Q with Q : q 0 = 0.615, q 1 = 0.385. We calculate the optimal binary v2f source encoder with blocklength m = 2 by applying Tunstall coding to Q. The resulting encoder is displayed in the 1st column of Table I. Using the source decoder as a distribution matcher results in an I-divergence per bit of 0.039206 bits. Next, we use Alg. 1 to calculate the optimal f2v matcher for Q. The resulting mapping is displayed in the 2nd column of Table I. The achieved I-divergence per bit is 0.037695 bits, which is smaller than the value obtained by using the source decoder.
In general, the decoder of an optimal v2f source encoder is a sub-optimal f2v distribution matcher and the dematcher of an optimal v2f distribution matcher is a sub-optimal v2f source encoder.