<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The ryg blog]]></provider_name><provider_url><![CDATA[https://fgiesen.wordpress.com]]></provider_url><author_name><![CDATA[fgiesen]]></author_name><author_url><![CDATA[https://fgiesen.wordpress.com/author/fgiesen/]]></author_url><title><![CDATA[Mixing discrete probability&nbsp;distributions]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>Let&#8217;s talk a bit about probability distributions in data compression; more specifically, about a problem in dealing with multi-symbol alphabets during adaptation.</p>
<p>Distributions over a binary alphabet are easy to mix (in the &#8220;context mixing&#8221; sense): they can be represented by a single scalar (I&#8217;ll use the probability of the symbol being a &#8216;1&#8217;, probability of &#8216;0&#8217; works too) which are easily combined with other scalars. In practice, these probabilities are represented as fixed-size integers in a specific range. A typical choice is <img src="https://s0.wp.com/latex.php?latex=p%3Dk%2F4096%2C+0+%5Cle+k+%5Cle+4096&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=p%3Dk%2F4096%2C+0+%5Cle+k+%5Cle+4096&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=p%3Dk%2F4096%2C+0+%5Cle+k+%5Cle+4096&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="p=k/4096, 0 &#92;le k &#92;le 4096" class="latex" />. Often, p=0 (&#8220;it&#8217;s certainly a 0&#8221;) and p=1 (certain 1) are excluded, because they only allow us to &#8220;encode&#8221; (using zero bits!) one of the two symbols.</p>
<p>Multi-symbol alphabets are trickier. A binary alphabet can be described using one probability p; the probability of the other symbol must be 1-p. Larger alphabets involve multiple probabilities, and that makes things trickier. Say we have a n-symbol alphabet. A probability distribution for such an alphabet is, conventionally, a vector <img src="https://s0.wp.com/latex.php?latex=p+%5Cin+%5Cmathbb%7BR%7D%5En&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=p+%5Cin+%5Cmathbb%7BR%7D%5En&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=p+%5Cin+%5Cmathbb%7BR%7D%5En&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="p &#92;in &#92;mathbb{R}^n" class="latex" /> such that <img src="https://s0.wp.com/latex.php?latex=p_k+%5Cge+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=p_k+%5Cge+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=p_k+%5Cge+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="p_k &#92;ge 0" class="latex" /> for all <img src="https://s0.wp.com/latex.php?latex=1+%5Cle+k+%5Cle+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=1+%5Cle+k+%5Cle+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=1+%5Cle+k+%5Cle+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="1 &#92;le k &#92;le n" class="latex" /> and furthermore <img src="https://s0.wp.com/latex.php?latex=%5Csum_%7Bk%3D1%7D%5En+p_k+%3D+1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Csum_%7Bk%3D1%7D%5En+p_k+%3D+1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Csum_%7Bk%3D1%7D%5En+p_k+%3D+1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;sum_{k=1}^n p_k = 1" class="latex" />. In practice, we will again represent them using integers. Rather than dealing with the hassle of having fractions everywhere, let&#8217;s just define our &#8220;finite-precision distributions&#8221; as integer vectors <img src="https://s0.wp.com/latex.php?latex=p+%5Cin+%5Cmathbb%7BZ%7D%5En&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=p+%5Cin+%5Cmathbb%7BZ%7D%5En&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=p+%5Cin+%5Cmathbb%7BZ%7D%5En&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="p &#92;in &#92;mathbb{Z}^n" class="latex" />, again <img src="https://s0.wp.com/latex.php?latex=p_k+%5Cge+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=p_k+%5Cge+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=p_k+%5Cge+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="p_k &#92;ge 0" class="latex" /> for all k, and <img src="https://s0.wp.com/latex.php?latex=%5Csum_%7Bk%3D1%7D%5En+p_k+%3D+T&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Csum_%7Bk%3D1%7D%5En+p_k+%3D+T&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Csum_%7Bk%3D1%7D%5En+p_k+%3D+T&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;sum_{k=1}^n p_k = T" class="latex" /> where T is the total. Same as with the binary case, we would like T to be a constant power of 2. This lets us use cheaper variants of conventional arithmetic coders, as well as <a href="https://fgiesen.wordpress.com/2014/02/02/rans-notes/">rANS</a>. Unlike the binary case, maintaining this invariant takes explicit work, and it&#8217;s not cheap.</p>
<p>In practice, the common choice is to either drop the requirement that T be constant (most adaptive multi-symbol models for arithmetic coding take that route), or switch to a semi-adaptive model such as <a href="http://www.cbloom.com/papers/context.pdf">deferred summation</a> that performs model updates in batches, which can either pick batches such that the constant sum is automatically maintained, or at least amortize the work spent enforcing it. A last option is using a &#8220;sliding window&#8221; style model that just uses a history of character counts over the last N input symbols. This makes it easy to maintain the constant sum, but it&#8217;s pretty limited.</p>
<p>A second problem is mixing &#8211; both as an explicit operation for things like context mixing, and as a building block for model updates more sophisticated than simply incrementing a counter. With binary models, this is easy &#8211; just a weighted sum of probabilities. Multi-symbol models with a constant sum are harder: for example, say we want to average the two 3-symbol models <img src="https://s0.wp.com/latex.php?latex=p%3D%5Cbegin%7Bpmatrix%7D3+%26+3+%26+2%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=p%3D%5Cbegin%7Bpmatrix%7D3+%26+3+%26+2%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=p%3D%5Cbegin%7Bpmatrix%7D3+%26+3+%26+2%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="p=&#92;begin{pmatrix}3 &amp; 3 &amp; 2&#92;end{pmatrix}" class="latex" /> and <img src="https://s0.wp.com/latex.php?latex=q%3D%5Cbegin%7Bpmatrix%7D1+%26+6+%26+1%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=q%3D%5Cbegin%7Bpmatrix%7D1+%26+6+%26+1%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=q%3D%5Cbegin%7Bpmatrix%7D1+%26+6+%26+1%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="q=&#92;begin{pmatrix}1 &amp; 6 &amp; 1&#92;end{pmatrix}" class="latex" /> while maintaining a total of T=8. Computing <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D%28p%2Bq%29+%3D+%5Cbegin%7Bpmatrix%7D2+%26+4.5+%26+1.5%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D%28p%2Bq%29+%3D+%5Cbegin%7Bpmatrix%7D2+%26+4.5+%26+1.5%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D%28p%2Bq%29+%3D+%5Cbegin%7Bpmatrix%7D2+%26+4.5+%26+1.5%5Cend%7Bpmatrix%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{1}{2}(p+q) = &#92;begin{pmatrix}2 &amp; 4.5 &amp; 1.5&#92;end{pmatrix}" class="latex" /> illustrates the problem: the two non-integer counts have to get rounded back to integers <em>somehow</em>. But if we round both up, we end up at a total of 9; and if we round both down, we end up with a total of 7. To achieve our desired total of 8, we need to round one up, and one down &#8211; but it&#8217;s not exactly obvious how we should choose on any given symbol! In short, maintaining a constant total while mixing in this form proves to be tricky.</p>
<p>However, I recently realized that these problems all disappear when we work with the cumulative distribution function (CDF) instead of the raw symbol counts.</p>
<h3>Working with cumulative counts</h3>
<p>For a discrete probability distribution p with total T, define the corresponding cumulative probabilities:</p>
<p><img src="https://s0.wp.com/latex.php?latex=P_j+%3A%3D+%5Csum_%7Bk%3D1%7D%5Ej+p_k%2C+%5Cquad+0+%5Cle+j+%5Cle+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=P_j+%3A%3D+%5Csum_%7Bk%3D1%7D%5Ej+p_k%2C+%5Cquad+0+%5Cle+j+%5Cle+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=P_j+%3A%3D+%5Csum_%7Bk%3D1%7D%5Ej+p_k%2C+%5Cquad+0+%5Cle+j+%5Cle+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="P_j := &#92;sum_{k=1}^j p_k, &#92;quad 0 &#92;le j &#92;le n" class="latex" /></p>
<p>P<sub>0</sub> is an empty sum, so P<sub>0</sub>=0. On the other end, P<sub>n</sub> = T, since that&#8217;s just the sum over all values in p, and we know the total is T. For the elements in between, p<sub>k</sub> &ge; 0 implies that P<sub>k</sub> &ge; P<sub>k-1</sub>, i.e. P is monotonically non-decreasing. Conversely, given any P with these three properties, we can compute the differences between adjacent elements and determine the underlying distribution p.</p>
<p>And it turns out that while mixing quantized symbol probabilities is problematic, mixing cumulative distribution functions pretty much just works. To wit: suppose we are given two CDFs P and Q with the same total T, and a blending factor <img src="https://s0.wp.com/latex.php?latex=%5Clambda+%5Cin+%5B0%2C1%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Clambda+%5Cin+%5B0%2C1%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Clambda+%5Cin+%5B0%2C1%5D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;lambda &#92;in [0,1]" class="latex" />, then define:</p>
<p><img src="https://s0.wp.com/latex.php?latex=%5Ctilde%7BR%7D_j+%3A%3D+%281-%5Clambda%29+P_j+%2B+%5Clambda+Q_j%2C+%5Cquad+0+%5Cle+j+%5Cle+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctilde%7BR%7D_j+%3A%3D+%281-%5Clambda%29+P_j+%2B+%5Clambda+Q_j%2C+%5Cquad+0+%5Cle+j+%5Cle+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctilde%7BR%7D_j+%3A%3D+%281-%5Clambda%29+P_j+%2B+%5Clambda+Q_j%2C+%5Cquad+0+%5Cle+j+%5Cle+n&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;tilde{R}_j := (1-&#92;lambda) P_j + &#92;lambda Q_j, &#92;quad 0 &#92;le j &#92;le n" class="latex" /></p>
<p>Note that because summation is linear, we have</p>
<p><img src="https://s0.wp.com/latex.php?latex=%5Ctilde%7BR%7D_j+%3D+%281-%5Clambda%29+%5Cleft%28%5Csum_%7Bk%3D1%7D%5Ej+p_k%5Cright%29+%2B+%5Clambda+%5Cleft%28%5Csum_%7Bk%3D1%7D%5Ej+q_k%5Cright%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctilde%7BR%7D_j+%3D+%281-%5Clambda%29+%5Cleft%28%5Csum_%7Bk%3D1%7D%5Ej+p_k%5Cright%29+%2B+%5Clambda+%5Cleft%28%5Csum_%7Bk%3D1%7D%5Ej+q_k%5Cright%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctilde%7BR%7D_j+%3D+%281-%5Clambda%29+%5Cleft%28%5Csum_%7Bk%3D1%7D%5Ej+p_k%5Cright%29+%2B+%5Clambda+%5Cleft%28%5Csum_%7Bk%3D1%7D%5Ej+q_k%5Cright%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;tilde{R}_j = (1-&#92;lambda) &#92;left(&#92;sum_{k=1}^j p_k&#92;right) + &#92;lambda &#92;left(&#92;sum_{k=1}^j q_k&#92;right)" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=%3D+%5Csum_%7Bk%3D1%7D%5Ej+%28%281-%5Clambda%29+p_k+%2B+%5Clambda+q_k%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%3D+%5Csum_%7Bk%3D1%7D%5Ej+%28%281-%5Clambda%29+p_k+%2B+%5Clambda+q_k%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%3D+%5Csum_%7Bk%3D1%7D%5Ej+%28%281-%5Clambda%29+p_k+%2B+%5Clambda+q_k%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="= &#92;sum_{k=1}^j ((1-&#92;lambda) p_k + &#92;lambda q_k)" class="latex" /></p>
<p>so this is indeed the CDF corresponding to a blended model between p and q. However, we&#8217;re still dealing with real values here; we need integers, and the easiest way to get there is to just truncate, possibly with some rounding bias <img src="https://s0.wp.com/latex.php?latex=b+%5Cin+%5B0%2C1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=b+%5Cin+%5B0%2C1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=b+%5Cin+%5B0%2C1%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="b &#92;in [0,1)" class="latex" />:</p>
<p><img src="https://s0.wp.com/latex.php?latex=R_j+%3A%3D+%5Clfloor+%5Ctilde%7BR%7D_j+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=R_j+%3A%3D+%5Clfloor+%5Ctilde%7BR%7D_j+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=R_j+%3A%3D+%5Clfloor+%5Ctilde%7BR%7D_j+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="R_j := &#92;lfloor &#92;tilde{R}_j + b &#92;rfloor" class="latex" /></p>
<p>It turns out that this just works, but this requires proof, and to get there it helps to prove a little lemma first.</p>
<p><b>Spacing lemma</b>: Suppose that for some j, <img src="https://s0.wp.com/latex.php?latex=P_j+-+P_%7Bj-1%7D+%5Cge+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=P_j+-+P_%7Bj-1%7D+%5Cge+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=P_j+-+P_%7Bj-1%7D+%5Cge+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="P_j - P_{j-1} &#92;ge m" class="latex" /> and <img src="https://s0.wp.com/latex.php?latex=Q_j+-+Q_%7Bj-1%7D+%5Cge+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Q_j+-+Q_%7Bj-1%7D+%5Cge+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Q_j+-+Q_%7Bj-1%7D+%5Cge+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Q_j - Q_{j-1} &#92;ge m" class="latex" /> where m is an arbitrary integer. Then we also have <img src="https://s0.wp.com/latex.php?latex=R_j+-+R_%7Bj-1%7D+%5Cge+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=R_j+-+R_%7Bj-1%7D+%5Cge+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=R_j+-+R_%7Bj-1%7D+%5Cge+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="R_j - R_{j-1} &#92;ge m" class="latex" />.</p>
<p><b>Proof</b>: We start by noting that</p>
<p><img src="https://s0.wp.com/latex.php?latex=%5Ctilde%7BR%7D_j+-+%5Ctilde%7BR%7D_%7Bj-1%7D+%3D+%281-%5Clambda%29+%28P_j+-+P_%7Bj-1%7D%29+%2B+%5Clambda+%28Q_j+-+Q_%7Bj-1%7D%29+%5Cge+%281-%5Clambda%29+m+%2B+%5Clambda+m+%3D+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctilde%7BR%7D_j+-+%5Ctilde%7BR%7D_%7Bj-1%7D+%3D+%281-%5Clambda%29+%28P_j+-+P_%7Bj-1%7D%29+%2B+%5Clambda+%28Q_j+-+Q_%7Bj-1%7D%29+%5Cge+%281-%5Clambda%29+m+%2B+%5Clambda+m+%3D+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctilde%7BR%7D_j+-+%5Ctilde%7BR%7D_%7Bj-1%7D+%3D+%281-%5Clambda%29+%28P_j+-+P_%7Bj-1%7D%29+%2B+%5Clambda+%28Q_j+-+Q_%7Bj-1%7D%29+%5Cge+%281-%5Clambda%29+m+%2B+%5Clambda+m+%3D+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;tilde{R}_j - &#92;tilde{R}_{j-1} = (1-&#92;lambda) (P_j - P_{j-1}) + &#92;lambda (Q_j - Q_{j-1}) &#92;ge (1-&#92;lambda) m + &#92;lambda m = m" class="latex" /></p>
<p>and since m is an integer, we have</p>
<p><img src="https://s0.wp.com/latex.php?latex=R_j+-+R_%7Bj-1%7D+%3D+%5Clfloor+%5Ctilde%7BR%7D_j+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=R_j+-+R_%7Bj-1%7D+%3D+%5Clfloor+%5Ctilde%7BR%7D_j+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=R_j+-+R_%7Bj-1%7D+%3D+%5Clfloor+%5Ctilde%7BR%7D_j+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="R_j - R_{j-1} = &#92;lfloor &#92;tilde{R}_j + b &#92;rfloor - &#92;lfloor &#92;tilde{R}_{j-1} + b &#92;rfloor" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=%3D+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+%28%5Ctilde%7BR%7D_j+-+%5Ctilde%7BR%7D_%7Bj-1%7D%29+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%3D+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+%28%5Ctilde%7BR%7D_j+-+%5Ctilde%7BR%7D_%7Bj-1%7D%29+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%3D+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+%28%5Ctilde%7BR%7D_j+-+%5Ctilde%7BR%7D_%7Bj-1%7D%29+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="= &#92;lfloor &#92;tilde{R}_{j-1} + (&#92;tilde{R}_j - &#92;tilde{R}_{j-1}) + b &#92;rfloor - &#92;lfloor &#92;tilde{R}_{j-1} + b &#92;rfloor" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=%5Cge+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+m+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cge+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+m+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cge+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+m+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;ge &#92;lfloor &#92;tilde{R}_{j-1} + m + b &#92;rfloor - &#92;lfloor &#92;tilde{R}_{j-1} + b &#92;rfloor" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=%3D+m+%2B+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor+%3D+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%3D+m+%2B+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor+%3D+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%3D+m+%2B+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor+-+%5Clfloor+%5Ctilde%7BR%7D_%7Bj-1%7D+%2B+b+%5Crfloor+%3D+m&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="= m + &#92;lfloor &#92;tilde{R}_{j-1} + b &#92;rfloor - &#92;lfloor &#92;tilde{R}_{j-1} + b &#92;rfloor = m" class="latex" /></p>
<p>which was our claim.</p>
<p>Using the lemma, it&#8217;s now easy to show that R is a valid CDF with total T: we need to establish that R<sub>0</sub>=0, R<sub>n</sub>=T, and show that R is monotonic. The first two are easy, since the P and Q we&#8217;re mixing both agree at these points and 0&le;b&lt;1.</p>
<p><img src="https://s0.wp.com/latex.php?latex=R_0+%3D+%5Clfloor+%5Ctilde%7BR%7D_0+%2B+b+%5Crfloor+%3D+%5Clfloor+b+%5Crfloor+%3D+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=R_0+%3D+%5Clfloor+%5Ctilde%7BR%7D_0+%2B+b+%5Crfloor+%3D+%5Clfloor+b+%5Crfloor+%3D+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=R_0+%3D+%5Clfloor+%5Ctilde%7BR%7D_0+%2B+b+%5Crfloor+%3D+%5Clfloor+b+%5Crfloor+%3D+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="R_0 = &#92;lfloor &#92;tilde{R}_0 + b &#92;rfloor = &#92;lfloor b &#92;rfloor = 0" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=R_n+%3D+%5Clfloor+%5Ctilde%7BR%7D_n+%2B+b+%5Crfloor+%3D+%5Clfloor+T+%2B+b+%5Crfloor+%3D+T&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=R_n+%3D+%5Clfloor+%5Ctilde%7BR%7D_n+%2B+b+%5Crfloor+%3D+%5Clfloor+T+%2B+b+%5Crfloor+%3D+T&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=R_n+%3D+%5Clfloor+%5Ctilde%7BR%7D_n+%2B+b+%5Crfloor+%3D+%5Clfloor+T+%2B+b+%5Crfloor+%3D+T&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="R_n = &#92;lfloor &#92;tilde{R}_n + b &#92;rfloor = &#92;lfloor T + b &#92;rfloor = T" class="latex" /></p>
<p>As for monotonicity, note that <img src="https://s0.wp.com/latex.php?latex=R_j+%5Cge+R_%7Bj-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=R_j+%5Cge+R_%7Bj-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=R_j+%5Cge+R_%7Bj-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="R_j &#92;ge R_{j-1}" class="latex" /> is the same as <img src="https://s0.wp.com/latex.php?latex=R_j+-+R_%7Bj-1%7D+%5Cge+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=R_j+-+R_%7Bj-1%7D+%5Cge+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=R_j+-+R_%7Bj-1%7D+%5Cge+0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="R_j - R_{j-1} &#92;ge 0" class="latex" /> (and the same for P and Q). Therefore, we can apply the spacing lemma with m=0 for 1&le;j&le;n: monotonicity of P and Q implies that R will be monotonic too. And that&#8217;s it: this proves that R is valid CDF for a distribution with total T. If we want the individual symbol frequencies, we can recover them as <img src="https://s0.wp.com/latex.php?latex=r_k+%3D+R_k+-+R_%7Bk-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=r_k+%3D+R_k+-+R_%7Bk-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=r_k+%3D+R_k+-+R_%7Bk-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="r_k = R_k - R_{k-1}" class="latex" />.</p>
<p>Note that the spacing lemma is a fair bit stronger than just establishing monotonicity. Most importantly, if p<sub>k</sub>&ge;m and q<sub>k</sub>&ge;m, the spacing lemma tells us that r<sub>k</sub>&ge;m &#8211; these properties carry over to the blended distribution, as one would expect! I make note of this because this kind of invariant is often violated by approximate techniques that first round the probabilities, then fudge them to make the total come out right.</p>
<h3>Conclusion</h3>
<p>This gives us a way to blend between two probability distributions while maintaining a constant total, without having to deal with dodgy ad-hoc rounding decisions. This requires working in CDF form, but that&#8217;s pretty common for arithmetic coding models anyway. As long as the mixing computation is done exactly (which is straightforward when working in integers), the total will remain constant.</p>
<p>I only described linear blending, but the construction generalizes in the obvious way to arbitrary <a href="http://en.wikipedia.org/wiki/Convex_combination">convex combinations</a>. It is thus directly applicable to mixing more than two distributions while only rounding once. Furthermore, given the CDFs of the input models, the corresponding interval for a single symbol can be found using just two mixing operations to find the two interval end points; there&#8217;s no need to compute the entire CDF for the mixed model. This is in contrast to direct mixing of the symbol probabilities, which in general needs to look at all symbols to either determine the total (if a non-constant-T approach is used) or perform the adjustments to make the total come out to T.</p>
<p>Furthermore, the construction shows that probability distributions with sum T are closed under &#8220;rounded convex combinations&#8221; (as used in the proof). The spacing lemma implies that the same is true for a multitude of more restricted distributions: for example, the set of probability distributions where each symbol has a nonzero probability (corresponding to distributions with monotonically increasing, instead of merely non-decreasing, CDFs) is also closed under convex combinations in this sense. This is a non-obvious result, to me anyway.</p>
<p>One application for this (as frequently noted) is context mixing of multi-symbol distributions. Another is as a building block in adaptive model updates that&#8217;s a good deal more versatile than the obvious &#8220;steal the count from one symbol, add it to another&#8221; update step.</p>
<p>I have no idea whether this is new or not (probably not); I certainly hadn&#8217;t seen it before, and neither had anyone else at RAD. Nor do I know whether this will be useful to anyone else, but it seemed worth writing up!</p>
]]></html></oembed>