Introduction
Random quantity turbines typically provide many predefined sampling distributions. Nevertheless, for extremely particular distributions, you’ll doubtless must create your personal. This entails combining and reworking primary distributions to attain the specified consequence.
As an example, to uniformly pattern the interval between a
and b
, you may apply an affine remodel on the usual uniform sampling from [0,1]
.
On this article, we’ll discover the best way to uniformly pattern factors inside the 2D unit disk by constructing on the fundamental [0,1] uniform sampling.
For readability, I’ve deliberately used the adjective “unit” in two totally different contexts on this article. The “unit sq.” refers back to the [0,1]² area, reflecting the vary of the fundamental random generator. Conversely, the “unit disk” is described inside [-1,1]² for comfort in polar coordinates. In apply, we will simply map between these utilizing an affine transformation. We’ll denote
u
andv
as samples drawn from the usual [0,1] or [-1,1] uniform distribution.
Rejection Sampling
Using the [0,1] uniform sampling twice permits us to uniformly pattern the unit sq. [0,1]².
A quite simple method generally known as “Rejection Sampling” consists in sampling the unit sq. and rejecting any pattern falling exterior the disk.
This leads to factors that comply with a uniform 2D distribution inside the disk contained within the unit sq., as illustrated within the determine beneath.
The density maps on this article are generated by sampling many factors from the required distribution after which making use of a Kernel Density Estimator. Strategies for addressing boundary bias within the density are detailed within the earlier article “Unit Disk and 2D Bounded KDE”.
A serious disadvantage is that rejection sampling can require many factors to get the specified variety of legitimate samples, with no higher restrict on the overall quantity, resulting in inefficiencies and better computational prices.
Intuitive Polar Sampling
An intuitive method is to make use of polar coordinates for uniform sampling: draw a radius inside [0,1] and an angle inside [0, 2π].
Each the radius and angle are uniform, what might probably go mistaken?Nevertheless, this technique results in an infinite density singularity on the origin, as illustrated by the empirical density map beneath.
To make sure the linear colormap stays readable, the density map has been capped at an arbitrary most worth of 10. With out this cover, the map would show a single purple dot on the middle of a blue disk.
The determine beneath makes use of coloration to indicate how the unit sq., sampled with (u,v), maps to the unit disk with (x,y) utilizing the polar remodel outlined above. The coloured areas within the sq. are equal, however this equality doesn’t maintain as soon as mapped to the disk. This visually illustrates the density map: giant radii exhibit a lot decrease density, as they’re distributed throughout a wider ring farther from the origin.
Let’s discover the mathematical particulars. When making use of a remodel T to a multi-dimensional random variable A, the ensuing density is discovered by dividing by absolutely the worth of the determinant of the Jacobian of T.
The polar remodel is given by the equation beneath.
We will compute the determinant of its jacobian.
Thus, the polar transformation leads to a density that’s inversely proportional to the radius, explaining the noticed singularity.
The 1D density profile alongside the diagonal proven above corresponds to absolutely the worth of the inverse operate, which is then set to zero exterior [-1, 1]. At first look, this may appear counterintuitive, on condition that the 1D inverse operate isn’t integrable over [-1, 1]! Nevertheless, it’s important to do not forget that the mixing is carried out in 2D utilizing polar coordinates, not in 1D.
Uniform Polar Sampling — Differential Equation
Tlisted here are two methods to search out the proper polar remodel that leads to an uniform distribution. Both by fixing a differential equation or by utilizing the inversion technique. Let’s discover each approaches.
To deal with the heterogeneity within the radius, we introduce a operate f
to regulate it: r=f(u)
. The angle, nevertheless, is stored uniform as a result of symmetry of the disk. We will then resolve the differential equation that ensures the determinant of the corresponding Jacobian stays fixed, to maintain the identical density.
We get ff'=c
, which has a singular answer given the boundary circumstances f(0)=0
and f(1)=1
. We find yourself with the next remodel.
Uniform Polar Sampling — Inversion Technique
The inversion technique is less complicated to understand within the discrete case. Think about three doable values A, B and C with possibilities of 60%, 10%, and 30%, respectively. As proven within the determine beneath, we will stack these possibilities to achieve a complete peak of 100%. By uniformly drawing a share U between 0% and 100% and mapping it to the corresponding worth A, B or C on the stack, we will remodel a uniform sampling into our discrete non‑uniform distribution.
That is very comparable within the steady case. The inversion technique begins by integrating the likelihood distribution operate (PDF) p
of a 1D variable X
to acquire the cumulative distribution operate (CDF) P(X<x)
, which will increase from 0 to 1. Then, by sampling a uniform variable U
from [0,1] and making use of the inverse CDF to U
, we will receive a pattern x
that follows the specified distribution p
.
Assuming a uniform disk distribution p(x,y), we will deduce the corresponding polar distribution p(r,θ) to implement.
It seems that this density p(r,θ) is separable, and we will independently pattern r and θ from their respective anticipated 1D marginal densities utilizing the inversion technique.
When the joint density isn’t separable, we first pattern a variable from its marginal density after which draw the second variable from its conditional density given the primary variable.
We combine these marginal densities into CDFs.
Sampling uniform (u,v) inside [0,1] and making use of the inverse CDFs offers us the next remodel, which is similar because the one obtained above utilizing the differential equation.
The ensuing distribution is successfully uniform, as confirmed by the empirical density map proven beneath.
The sq. root operate successfully adjusts the coloured columns to protect their relative areas when mapped to the disk.