The ratio of uniforms distribution is a useful distribution for rejection sampling. It gives a simple and fast way to sample from discrete distributions like the hypergeometric distribution1. To use the ratio of uniforms distribution in rejection sampling, we need to know the distributions density. This post summarizes some properties of the ratio of uniforms distribution and computes its density.
The ratio of uniforms distribution is the distribution of the ratio of two independent uniform random variables. Specifically, suppose and
are independent and uniformly distributed. Then
has the ratio of uniforms distribution. The plot below shows a histogram based on 10,000 samples from the ratio of uniforms distribution2.

The histogram has a flat section in the middle and then curves down on either side. This distinctive shape is called a “table mountain”. The density of also has a table mountain shape.

And here is the density plotted on top of the histogram.

A formula for the density of is
The first case in the definition of corresponds to the flat part of the table mountain. The second case corresponds to the sloping curves. The rest of this post use geometry to derive the above formula for
.
Calculating the density
The point is uniformly distributed in the box
. The image below shows an example of a point
inside the box
.

We can compute the ratio geometrically. First we draw a straight line that starts at
and goes through
. This line will hit the horizontal line
. The
coordinate at this point is exactly
.

In the above picture, all of the points on the dashed line map to the same value of . We can compute the density of
by computing an area. The probability that
is in a small interval
is
If we can compute the above area, then we will know the density of because by definition
We will first work on the case when is between
and
. In this case, the set
is a triangle. This triangle is drawn in blue below.

The horizontal edge of this triangle has length . The perpendicular height of the triangle from the horizontal edge is
. This means that
And so, when we have
Now let’s work on the case when is bigger than
or less than
. In this case, the set
is again triangle. But now the triangle has a vertical edge and is much skinnier. Below the triangle is drawn in red. Note that only points inside the box
are coloured in.

The vertical edge of the triangle has length . The perpendicular height of the triangle from the vertical edge is
. Putting this together
And so
And so putting everything together
Footnotes and references
- https://ieeexplore.ieee.org/document/718718 ↩︎
- For visual purposes, I restricted the sample to values of
between
and
. This is because the ratio of uniform distribution has heavy tails. This meant that there were some very large values of
that made the plot hard to see. ↩︎