# Ratio of uniforms

The ratio of uniforms is a method initially proposed by Kinderman and Monahan in 1977 for pseudo-random number sampling, that is, for drawing random samples from a statistical distribution. Like rejection sampling and inverse transform sampling, it is an exact simulation method. The basic idea of the method is to use a change of variables to create a bounded set, which can then be sampled uniformly to generate random variables following the original distribution. One feature of this method is that the distribution to sample is only required to be known up to an unknown multiplicative factor, a common situation in computational statistics and statistical physics.

## Motivation

A convenient technique to sample a statistical distribution is rejection sampling. When the probability density function of the distribution is bounded and has finite support, one can define a bounding box around it (a uniform proposal distribution), draw uniform samples in the box and return only the x coordinates of the points that fall below the function (see graph). As a direct consequence of the fundamental theorem of simulation, the returned samples are distributed according to the original distribution.
When the support of the distribution is infinite, it is impossible to draw a rectangular bounding box containing the graph of the function. One can still use rejection sampling, but with a non-uniform proposal distribution. It can be delicate to choose an appropriate proposal distribution, and one also has to know how to efficiently sample this proposal distribution.
The method of the ratio of uniforms offers a solution to this problem, by essentially using as proposal distribution the distribution created by the ratio of two uniform random variables.

## Statement

The statement and the proof are adapted from the presentation by Gobet
Theorem — Let $X$ be a multidimensional random variable with probability density function $p(x_{1},x_{2},\ldots ,x_{d})$ on $\mathbb {R} ^{d}$. The function $p$ is only required to be known up to a constant, so we can assume that we only know $f$ where $p(x_{1},x_{2},\ldots ,x_{d})=cf(x_{1},x_{2},\ldots ,x_{d})$, with $c$ a constant unknown or difficult to compute. Let $r>0$, a parameter that can be adjusted as we choose to improve the properties of the method. We can define the set $A_{f,r}$:$A_{f,r}=\left\{(u,v_{1},v_{2},\ldots ,v_{d})\in \mathbb {R} ^{d+1}:0\leq u\leq f\left({\frac {v_{1}}{u^{r}}},{\frac {v_{2}}{u^{r}}},\ldots ,{\frac {v_{d}}{u^{r}}}\right)^{\frac {1}{1+rd}}\right\}$The Lebesgue measure of the set $A_{f,r}$ is finite and equal to ${\frac {1}{c\,(1+rd)}}$.
Furthermore, let $(U,V_{1},V_{2},\ldots ,V_{d})$ be a random variable uniformly distributed on the set $A_{f,r}$. Then, $\left({\frac {V_{1}}{U^{r}}},{\frac {V_{2}}{U^{r}}},\ldots ,{\frac {V_{d}}{U^{r}}}\right)$ is a random variable on $\mathbb {R} ^{d}$ distributed like $X$.
We will first assume that the first statement is correct, i.e. $|A_{f,r}|={\frac {1}{c\,(1+rd)}}$.
Let $\varphi$ be a measurable function on $\mathbb {R} ^{d}$. Let's consider the expectation of $\varphi \left({\frac {V_{1}}{U^{r}}},\ldots ,{\frac {V_{d}}{U^{r}}}\right)$ on the set $A_{f,r}$:
    $E\left[\varphi \left({\frac {V_{1}}{U^{r}}},\ldots ,{\frac {V_{d}}{U^{r}}}\right)\right]={\frac {1}{|A_{f,r}|}}\int _{-\infty }^{\infty }\int _{-\infty }^{\infty }\cdots \int _{-\infty }^{\infty }\varphi \left({\frac {v_{1}}{u^{r}}},\ldots ,{\frac {v_{d}}{u^{r}}}\right)\mathbf {1} _{(u,v_{1},\ldots ,v_{d})\in A_{f,r}}\mathrm {d} u\,\mathrm {d} v_{1}\ldots \mathrm {d} v_{d}$
With the change of variables $x_{i}={\frac {v_{i}}{u^{r}}}$, we have
    ${\begin{aligned}E\left[\varphi \left({\frac {V_{1}}{U^{r}}},\ldots ,{\frac {V_{d}}{U^{r}}}\right)\right]&={\frac {1}{|A_{f,r}|}}\int _{-\infty }^{\infty }\int _{-\infty }^{\infty }\cdots \int _{-\infty }^{\infty }\varphi (x_{1},\ldots ,x_{d})\mathbf {1} _{0\leq u\leq f(x_{1},\ldots ,x_{d})^{\frac {1}{1+rd}}}u^{rd}\mathrm {d} u\,\mathrm {d} x_{1}\cdots \mathrm {d} x_{d}\\&={\frac {1}{|A_{f,r}|}}\int _{-\infty }^{\infty }\cdots \int _{-\infty }^{\infty }\varphi \left(x_{1},\ldots ,x_{d}\right){\frac {1}{1+rd}}f\left(x_{1},\ldots ,x_{d}\right)\mathrm {d} x_{1}\cdots \mathrm {d} x_{d}\\&=\int _{-\infty }^{\infty }\ldots \int _{-\infty }^{\infty }\varphi \left(x_{1},\ldots ,x_{d}\right)p\left(x_{1},\ldots ,x_{d}\right)\mathrm {d} x_{1}\cdots \mathrm {d} x_{d}\end{aligned}}$
where we can see that $\left({\frac {V_{1}}{U^{r}}},\ldots ,{\frac {V_{d}}{U^{r}}}\right)$ has indeed the density $p$.
Coming back to the first statement, a similar argument shows that $|A_{f,r}|={\frac {1}{c\,(1+rd)}}$.

## Complements
### Rejection sampling in $A_{f,r}$

The above statement does not specify how one should perform the uniform sampling in $A_{f,r}$. However, the interest of this method is that under mild conditions on $f$ (namely that $f(x_{1},x_{2},\ldots ,x_{d})^{\frac {1}{1+rd}}$ and $x_{i}f(x_{1},x_{2},\ldots ,x_{d})^{\frac {r}{1+rd}}$ for all $i$ are bounded), $A_{f,r}$ is bounded. One can define the rectangular bounding box ${\tilde {A}}_{f,r}$ such that$A_{f,r}\subset {\tilde {A}}_{f,r}=\left[0,\sup _{x_{1},\ldots ,x_{d}}{f(x_{1},\ldots ,x_{d})^{\frac {1}{1+rd}}}\right]\times \prod _{i}\left[\inf _{x_{1},\ldots ,x_{d}}{x_{i}f(x_{1},\ldots ,x_{d})^{\frac {r}{1+rd}}},\sup _{x_{1},\ldots ,x_{d}}{x_{i}f(x_{1},\ldots ,x_{d})^{\frac {r}{1+rd}}}\right]$This allows to sample uniformly the set $A_{f,r}$ by rejection sampling inside ${\tilde {A}}_{f,r}$. The parameter $r$ can be adjusted to change the shape of $A_{f,r}$ and maximize the acceptance ratio of this sampling.

### Parametric description of the boundary of $A_{f,r}$

The definition of $A_{f,r}$ is already convenient for the rejection sampling step. For illustration purposes, it can be interesting to draw the set, in which case it can be useful to know the parametric description of its boundary:$u=f\left(x_{1},x_{2},\ldots ,x_{d}\right)^{\frac {1}{1+rd}}\quad {\text{and}}\quad \forall i\in [|1,n|],v_{i}=x_{i}u^{r}$or for the common case where $X$ is a 1-dimensional variable, $(u,v)=\left(f(x)^{\frac {1}{1+r}},x\,f(x)^{\frac {r}{1+r}}\right)$.

### Generalized ratio of uniforms

Above parameterized only with $r$, the ratio of uniforms can be described with a more general class of transformations in terms of a transformation g. In the 1-dimensional case, if $g:\mathbb {R} ^{+}\rightarrow \mathbb {R} ^{+}$ is a strictly increasing and differentiable function such that $g(0)=0$, then we can define $A_{f,g}$ such that
    $A_{f,g}=\left\{(u,v)\in \mathbb {R} ^{2}:0\leq u\leq g^{-1}\left[f\left({\frac {v}{g'(u)}}\right)\right]\right\}$
If $(U,V)$ is a random variable uniformly distributed in $A_{f,g}$, then ${\frac {V}{g'(U)}}$ is distributed with the density $p$.

## Examples
### The exponential distribution

Assume that we want to sample the exponential distribution, $p(x)=\lambda \mathrm {e} ^{-\lambda x}$ with the ratio of uniforms method. We will take here $r=1$.
We can start constructing the set $A_{f,1}$:
    $A_{f,1}=\left\{(u,v)\in \mathbb {R} ^{2}:0\leq u\leq {\sqrt {p\left({\frac {v}{u}}\right)}}\right\}$
The condition $0\leq u\leq {\sqrt {p\left({\frac {v}{u}}\right)}}$ is equivalent, after computation, to $0\leq v\leq -{\frac {u}{\lambda }}\ln {\frac {u^{2}}{\lambda }}$, which allows us to plot the shape of the set (see graph).
This inequality also allows us to determine the rectangular bounding box ${\tilde {A}}_{f,1}$ where $A_{f,1}$ is included. Indeed, with $g(u)=-{\frac {u}{\lambda }}\ln {\frac {u^{2}}{\lambda }}$, we have $g\left({\sqrt {\lambda }}\right)=0$ and $g'\left({\frac {\sqrt {\lambda }}{\mathrm {e} }}\right)=0$, from where ${\tilde {A}}_{f,1}=\left[0,{\sqrt {\lambda }}\right]\times \left[0,{\frac {2}{\mathrm {e} {\sqrt {\lambda }}}}\right]$.
From here, we can draw pairs of uniform random variables $U\sim \mathrm {Unif} \left(0,{\sqrt {\lambda }}\right)$ and $V\sim \mathrm {Unif} \left(0,{\frac {2}{\mathrm {e} {\sqrt {\lambda }}}}\right)$ until $u\leq {\sqrt {\lambda \,\mathrm {e} ^{-\lambda {\frac {v}{u}}}}}$, and when that happens, we return ${\frac {v}{u}}$, which is exponentially distributed.

### A mixture of normal distributions

Consider the mixture of two normal distributions ${\mathcal {D}}=0.6\,N(\mu =-1,\sigma =2)+0.4\,N(\mu =3,\sigma =1)$. To apply the method of the ratio of uniforms, with a given $r$, one should first determine the boundaries of the rectangular bounding box ${\tilde {A}}_{f,r}$ enclosing the set $A_{f,r}$. This can be done numerically, by computing the minimum and maximum of $u(x)=f(x)^{\frac {1}{1+r}}$ and $v(x)=x\,f(x)^{\frac {r}{1+r}}$ on a grid of values of $x$. Then, one can draw uniform samples $(u,v)\in {\tilde {A}}_{f,r}$, only keep those that fall inside the set $A_{f,r}$ and return them as ${\frac {v}{u^{r}}}$.
It is possible to optimize the acceptance ratio by adjusting the value of $r$, as seen on the graphs.

## Software

* The rust and Runuran contributed packages in R.