# Inverse transform sampling

Inverse transform sampling (also known as inversion sampling, the inverse probability integral transform, the inverse transformation method, Smirnov transform, golden rule[1]) is a basic method for pseudo-random number sampling, i.e. for generating sample numbers at random from any probability distribution given its cumulative distribution function.

Inverse transformation sampling takes uniform samples of a number ${\displaystyle u}$ between 0 and 1, interpreted as a probability, and then return the largest number ${\displaystyle x}$ from the domain of the distribution ${\displaystyle P(X)}$ such that ${\displaystyle P(-\infty . For example, imagine that ${\displaystyle P(X)}$ is the standard normal distribution with mean zero and standard deviation one. The table below shows samples taken from the uniform distribution and their representation on the standard normal distribution.

Transformation from uniform sample to normal
${\displaystyle u}$ ${\displaystyle F^{-1}(u)}$
.5 0
.975 1.95996
0.995 2.5758
.999999 4.75342
1-2^{-52} 8.12589
Inverse transform sampling for normal distribution

We are randomly choosing a proportion of the area under the curve and returning the number in the domain such that exactly this proportion of the area occurs to the left of that number. Intuitively, we are unlikely to choose a number in the far end of tails because there is very little area in them which would require choosing a number very close to zero or one.

Computationally, this method involves computing the quantile function of the distribution — in other words, computing the cumulative distribution function (CDF) of the distribution (which maps a number in the domain to a probability between 0 and 1) and then inverting that function. This is the source of the term "inverse" or "inversion" in most of the names for this method. Note that for a discrete distribution, computing the CDF is not in general too difficult: we simply add up the individual probabilities for the various points of the distribution. For a continuous distribution, however, we need to integrate the probability density function (PDF) of the distribution, which is impossible to do analytically for most distributions (including the normal distribution). As a result, this method may be computationally inefficient for many distributions and other methods are preferred; however, it is a useful method for building more generally applicable samplers such as those based on rejection sampling.

For the normal distribution, the lack of an analytical expression for the corresponding quantile function means that other methods (e.g. the Box–Muller transform) may be preferred computationally. It is often the case that, even for simple distributions, the inverse transform sampling method can be improved on:[2] see, for example, the ziggurat algorithm and rejection sampling. On the other hand, it is possible to approximate the quantile function of the normal distribution extremely accurately using moderate-degree polynomials, and in fact the method of doing this is fast enough that inversion sampling is now the default method for sampling from a normal distribution in the statistical package R.[3]

## Definition

The probability integral transform states that if ${\displaystyle X}$ is a continuous random variable with cumulative distribution function ${\displaystyle F_{X}}$, then the random variable ${\displaystyle Y=F_{X}(X)}$ has a uniform distribution on [0, 1]. The inverse probability integral transform is just the inverse of this: specifically, if ${\displaystyle Y}$ has a uniform distribution on [0, 1] and if ${\displaystyle X}$ has a cumulative distribution ${\displaystyle F_{X}}$, then the random variable ${\displaystyle F_{X}^{-1}(Y)}$ has the same distribution as ${\displaystyle X}$ .

Graph of the inversion technique from ${\displaystyle x}$ to ${\displaystyle F(x)}$. On the bottom right we see the regular function and in the top left its inversion.

## The method

The problem that the inverse transform sampling method solves is as follows:

The inverse transform sampling method works as follows:

1. Generate a random number u from the standard uniform distribution in the interval [0,1].
2. Compute the value x such that F(x) = u.
3. Take x to be the random number drawn from the distribution described by F.

Expressed differently, given a continuous uniform variable U in [0, 1] and an invertible cumulative distribution function F, the random variable X = F −1(U) has distribution F (or, X is distributed F).

A treatment of such inverse functions as objects satisfying differential equations can be given.[4] Some such differential equations admit explicit power series solutions, despite their non-linearity.

## Examples

{\displaystyle {\begin{aligned}F(x)=1-\exp(-{\sqrt {x}})\end{aligned}}}

In order to perform an inversion we want to solve for ${\displaystyle F(F^{-1}(u))=u}$

{\displaystyle {\begin{aligned}F(F^{-1}(u))&=u\\1-\exp \left(-{\sqrt {F^{-1}(u)}}\right)&=u\\F^{-1}(u)&=(-\log(1-u))^{2}\\&=(\log(1-u))^{2}\end{aligned}}}
From here we would perform steps one, two and three.
• As another example, we use the exponential distribution with ${\displaystyle F(x)=1-e^{-\lambda x}}$ for x ≥ 0 (and 0 otherwise). By solving y=F(x) we obtain the inverse function
${\displaystyle x=F^{-1}(y)=-{\frac {1}{\lambda }}\ln(1-y).}$
The idea is illustrated in the following graph:
Random numbers yi are generated from a uniform distribution between 0 and 1, i.e. Y ~ U(0, 1). They are sketched as colored points on the y-axis. Each of the points is mapped according to x=F-1(y), which is shown with gray arrows for two example points. In this example, we have used an exponential distribution. Hence, for x ≥ 0, the probability density is ${\displaystyle \varrho _{X}(x)=\lambda e^{-\lambda \,x}}$ and the cumulated distribution function is ${\displaystyle F(x)=1-e^{-\lambda \,x}}$. Therefore, ${\displaystyle x=F^{-1}(y)=-{\frac {\ln(1-y)}{\lambda }}}$. We can see that using this method, many points end up close to 0 and only few points end up having high x-values - just as it is expected for an exponential distribution.
Note that the distribution does not change if we start with 1-y instead of y. For computational purposes, it therefore suffices to generate random numbers y in [0, 1] and then simply calculate
${\displaystyle x=F^{-1}(y)=-{\frac {1}{\lambda }}\ln(y).}$

## Proof of correctness

Let F be a continuous cumulative distribution function, and let F−1 be its inverse function (using the infimum because CDFs are weakly monotonic and right-continuous):[5]

${\displaystyle F^{-1}(u)=\inf \;\{x\mid F(x)\geq u\}\qquad (0

Claim: If U is a uniform random variable on (0, 1) then ${\displaystyle F^{-1}(U)}$ follows the distribution F.

Proof:

{\displaystyle {\begin{aligned}&\Pr(F^{-1}(U)\leq x)\\&{}=\Pr(U\leq F(x))\quad &({\text{applying }}F,{\text{ to both sides}})\\&{}=F(x)\quad &({\text{because }}\Pr(U\leq y)=y)\end{aligned}}}

## Reduction of the number of inversions

In order to obtain a large number (lets say M) of samples one needs to perform the same number of inversions ${\displaystyle F_{X}^{-1}(u)}$ of the distribution ${\displaystyle F_{X}}$. One possible way to reduce the number of inversions to only a few while obtaining a large number of samples is the application of the so-called the Stochastic Collocation Monte Carlo sampler (SCMC sampler), within a polynomial chaos expansion framework, allows us the generation of any number of Monte Carlo samples based on only a few inversions of the original distribution and independent samples from a variable for which the inversions are analytically available, like for example the standard normal variable.[6]