# On the Optimal Calculation of the Rice Coding Parameter
## 3 General Selection of the Golomb Parameter for Rice Coding  
We now focus on the problem of how to find the value of the Rice parameter \(r\) that yields the minimum bit-length of the output stream for any raw data sequence \(\mathbf{n}\). The variation in the bit-length due to the choice of this parameter was illustrated above with an example in Section 1.1.  
To simplify notation, we assume that we deal only with the Rice encoding of values with their sign. In such a case, to apply Rice coding to a number \(n_{i}\), a total of \(r+\left\lfloor\frac{n_{i}}{2^{r}}\right\rfloor+2\) bits are required. Hereinafter, since the sign is encoded in a separate bit, we assume that the samples in the input sequence \(\mathbf{n}\) have been already stripped off its sign; they are all non-negative.  
In this section, we are interested in finding the value of \(r\) for which the value of the following function:  
\[f(r,\mathbf{n}) =\sum_{1\leq i\leq N}\left(r+\left\lfloor\frac{n_{i}}{2^{r}} \right\rfloor+2\right)\] \[=(r+2)\cdot N+\underbrace{\sum_{1\leq i\leq N}\left\lfloor\frac{n _{i}}{2^{r}}\right\rfloor}_{Q(r)}, \tag{9}\]  
is minimum. Note that \(f(r,\mathbf{n})\) is a discontinuous function since its values are only valid for integer values of \(r\). In this section, an analytical solution is provided to this problem.  
Let us define the function \(L(n_{i})\) as the number of bits used for representing \(n_{i}\):\[L(n_{i})=\begin{cases}\lfloor\log_{2}n_{i}\rfloor+1,&\text{ if }n_{i}\
eq 0\\ 0,&\text{ otherwise}.\end{cases} \tag{10}\]  
The values of \(L(n_{i})\) can be observed for our example sequence in Table 1.  
It is clear that the optimal value for \(r\), \(r^{*}\), for \(f(r,\mathbf{n})\) is an integer value bounded by:  
\[\min_{i}L(n_{i})\leq r^{*}\leq\max_{i}L(n_{i}). \tag{11}\]  
Hereinafter, we name \(L_{\text{min}}\) and \(L_{\text{max}}\) the left- and right-hand sides expressions of (11), respectively.  
Moreover, note that for each positive number to encode, \(n_{i}\), the integer division expression can be bounded as follows:  
\[\frac{n_{i}}{2^{r}}-\phi_{i}\leq\left\lfloor\frac{n_{i}}{2^{r}}\right\rfloor \leq\frac{n_{i}}{2^{r}}-\theta_{i}, \tag{12}\]  
for certain values of \(\phi_{i}\) and \(\theta_{i}\).  
We create two derivable continuous functions that will bound the value of \(f(r,\mathbf{n})\) as follows:  
\[g(r,\mathbf{n})\leq f(r,\mathbf{n})\leq h(r,\mathbf{n}),\text{ where} \tag{13}\]  
\[\begin{split} g(r,\mathbf{n})&=(r+2)\cdot N+\sum\left( \frac{n_{i}}{2^{r}}-\phi_{i}\right)\\ &=(r+2)\cdot N+\frac{\sum n_{i}}{2^{r}}-\phi,\end{split} \tag{14}\]  
and:  
\[\begin{split} h(r,\mathbf{n})&=(r+2)\cdot N+\sum\left( \frac{n_{i}}{2^{r}}-\theta_{i}\right)\\ &=(r+2)\cdot N+\frac{\sum n_{i}}{2^{r}}-\theta,\end{split} \tag{15}\]  
for certain values of \(\phi\) and \(\theta\).  
To simplify notation, we drop the parameter \(\mathbf{n}\) (the input data sequence) from the functions \(f,g\), and \(h\) since it remains constant in what remains of our analysis.  
It is not difficult to prove that \(g(r)\) and \(h(r)\) are both convex functions in \(r\). As an example, we illustrate in Figure 2 the two bounding functions for the Rice parameter for the example given in Section 1.1, together with the optimal values of each function (indicated using a larger squared region).  
Figure 2: Function \(f(r)\) bounded by \(g(r)\) and \(h(r)\) for the input sequence of Section 1.1. The minimum value of each function is marked with a squared red dot. The total bit-length function \(f(r)\) is only defined for integer values of \(r\). A continuous line was added between the points to improve visibility.  
Therefore, in order to find the solution, the evaluation of the functions for consecutive values of \(r\) until the output value starts increasing is not a very time-consuming task. Nonetheless, in further subsections, we will present a faster method for finding the optimal Rice parameter.