Inverse transform sampling

From HandWiki
Short description: Basic method for pseudo-random number sampling

Inverse transform sampling (also known as inversion sampling, the inverse probability integral transform, the inverse transformation method, Smirnov transform, or the 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 [math]\displaystyle{ u }[/math] between 0 and 1, interpreted as a probability, and then returns the smallest number [math]\displaystyle{ x\in\mathbb R }[/math] such that [math]\displaystyle{ F(x)\ge u }[/math] for the cumulative distribution function [math]\displaystyle{ F }[/math] of a random variable. For example, imagine that [math]\displaystyle{ F }[/math] 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
[math]\displaystyle{ u }[/math] [math]\displaystyle{ F^{-1}(u) }[/math]
.5 0
.975 1.95996
.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]

Formal statement

For any random variable [math]\displaystyle{ X\in\mathbb R }[/math], the random variable [math]\displaystyle{ F_X^{-1}(U) }[/math] has the same distribution as [math]\displaystyle{ X }[/math], where [math]\displaystyle{ F_X^{-1} }[/math] is the generalized inverse of the cumulative distribution function [math]\displaystyle{ F_X }[/math] of [math]\displaystyle{ X }[/math] and [math]\displaystyle{ U }[/math] is uniform on [math]\displaystyle{ [0,1] }[/math].[4]

For continuous random variables, the inverse probability integral transform is indeed the inverse of the probability integral transform, which states that for a continuous random variable [math]\displaystyle{ X }[/math] with cumulative distribution function [math]\displaystyle{ F_X }[/math], the random variable [math]\displaystyle{ U=F_X(X) }[/math] is uniform on [math]\displaystyle{ [0,1] }[/math].

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

Intuition

From [math]\displaystyle{ U \sim \mathrm{Unif}[0,1] }[/math], we want to generate [math]\displaystyle{ X }[/math] with CDF [math]\displaystyle{ F_X(x). }[/math] We assume [math]\displaystyle{ F_X(x) }[/math] to be a continuous, strictly increasing function, which provides good intuition.

We want to see if we can find some strictly monotone transformation [math]\displaystyle{ T:[0,1]\mapsto \mathbb{R} }[/math], such that [math]\displaystyle{ T(U)\overset{d}{=}X }[/math]. We will have

[math]\displaystyle{ F_X(x)=\Pr(X\leq x)=\Pr(T(U)\leq x) = \Pr(U\leq T^{-1}(x))=T^{-1}(x), \text{ for } x\in \mathbb{R}, }[/math]

where the last step used that [math]\displaystyle{ \Pr(U \leq y) = y }[/math] when [math]\displaystyle{ U }[/math] is uniform on [math]\displaystyle{ [0,1] }[/math].

So we got [math]\displaystyle{ F_X }[/math] to be the inverse function of [math]\displaystyle{ T }[/math], or, equivalently [math]\displaystyle{ T(u)=F_X^{-1}(u), u\in [0,1]. }[/math]

Therefore, we can generate [math]\displaystyle{ X }[/math] from [math]\displaystyle{ F_X^{-1}(U). }[/math]

The method

Schematic of the inverse transform sampling. The inverse function of [math]\displaystyle{ y=F_X(x) }[/math] can be defined by [math]\displaystyle{ F_X^{-1}(y)=\mathrm{inf}\{x| F_X(x)\geq y\} }[/math].
An animation of how inverse transform sampling generates normally distributed random values from uniformly distributed random values

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

  • Let [math]\displaystyle{ X }[/math] be a random variable whose distribution can be described by the cumulative distribution function [math]\displaystyle{ F_X }[/math].
  • We want to generate values of [math]\displaystyle{ X }[/math] which are distributed according to this distribution.

The inverse transform sampling method works as follows:

  1. Generate a random number [math]\displaystyle{ u }[/math] from the standard uniform distribution in the interval [math]\displaystyle{ [0,1] }[/math], i.e. from [math]\displaystyle{ U \sim \mathrm{Unif}[0,1]. }[/math]
  2. Find the generalized inverse of the desired CDF, i.e. [math]\displaystyle{ F_X^{-1}(u) }[/math].
  3. Compute [math]\displaystyle{ X'(u)=F_X^{-1}(u) }[/math]. The computed random variable [math]\displaystyle{ X'(U) }[/math] has distribution [math]\displaystyle{ F_X }[/math] and thereby the same law as [math]\displaystyle{ X }[/math].

Expressed differently, given a cumulative distribution function [math]\displaystyle{ F_X }[/math] and a uniform variable [math]\displaystyle{ U\in[0,1] }[/math], the random variable [math]\displaystyle{ X = F_X^{-1}(U) }[/math] has the distribution [math]\displaystyle{ F_X }[/math].[4]

In the continuous case, a treatment of such inverse functions as objects satisfying differential equations can be given.[5] Some such differential equations admit explicit power series solutions, despite their non-linearity.[6]

Examples

[math]\displaystyle{ \begin{align} F(x)=1-\exp(-\sqrt{x}) \end{align} }[/math]
In order to perform an inversion we want to solve for [math]\displaystyle{ F(F^{-1}(u))=u }[/math]
[math]\displaystyle{ \begin{align} 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{align} }[/math]
From here we would perform steps one, two and three.
  • As another example, we use the exponential distribution with [math]\displaystyle{ F_X(x)=1-e^{-\lambda x} }[/math] for x ≥ 0 (and 0 otherwise). By solving y=F(x) we obtain the inverse function
[math]\displaystyle{ x = F^{-1}(y) = -\frac{1}{\lambda}\ln(1-y). }[/math]
It means that if we draw some [math]\displaystyle{ y_0 }[/math]from a [math]\displaystyle{ U \sim \mathrm{Unif}(0,1) }[/math] and compute [math]\displaystyle{ x_0 = F_X^{-1}(y_0) = -\frac{1}{\lambda}\ln(1-y_0), }[/math] This [math]\displaystyle{ x_0 }[/math] has exponential distribution.
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 [math]\displaystyle{ \varrho_X(x) = \lambda e^{-\lambda \, x} }[/math] and the cumulative distribution function is [math]\displaystyle{ F(x) = 1 - e^{-\lambda \, x} }[/math]. Therefore, [math]\displaystyle{ x = F^{-1}(y) = - \frac{\ln(1-y)}{\lambda} }[/math]. 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
[math]\displaystyle{ x = F^{-1}(y) = -\frac{1}{\lambda}\ln(y). }[/math]

Proof of correctness

Let [math]\displaystyle{ F }[/math] be a cumulative distribution function, and let [math]\displaystyle{ F^{-1} }[/math] be its generalized inverse function (using the infimum because CDFs are weakly monotonic and right-continuous):[7]

[math]\displaystyle{ F^{-1}(u) = \inf\;\{x \mid F(x)\geq u\} \qquad (0\lt u\lt 1). }[/math]

Claim: If [math]\displaystyle{ U }[/math] is a uniform random variable on [math]\displaystyle{ [0,1] }[/math] then [math]\displaystyle{ F^{-1}(U) }[/math] has [math]\displaystyle{ F }[/math] as its CDF.

Proof:

[math]\displaystyle{ \begin{align} & \Pr(F^{-1}(U) \leq x) \\ & {} = \Pr(U \leq F(x)) \quad &(F\text{ is right-continuous, so }\{u:F^{-1}(u)\le x\}=\{u:u\le F(x)\}) \\ & {} = F(x) \quad &(\text{because }\Pr(U \leq u) = u,\text{ when } U \text{ is uniform on }[0,1]) \\ \end{align} }[/math]

Truncated distribution

Inverse transform sampling can be simply extended to cases of truncated distributions on the interval [math]\displaystyle{ (a,b] }[/math] without the cost of rejection sampling: the same algorithm can be followed, but instead of generating a random number [math]\displaystyle{ u }[/math] uniformly distributed between 0 and 1, generate [math]\displaystyle{ u }[/math] uniformly distributed between [math]\displaystyle{ F(a) }[/math] and [math]\displaystyle{ F(b) }[/math], and then again take [math]\displaystyle{ F^{-1}(u) }[/math].

Reduction of the number of inversions

In order to obtain a large number of samples, one needs to perform the same number of inversions of the distribution. One possible way to reduce the number of inversions while obtaining a large number of samples is the application of the so-called Stochastic Collocation Monte Carlo sampler (SCMC sampler) within a polynomial chaos expansion framework. This allows us to generate any number of Monte Carlo samples with only a few inversions of the original distribution with independent samples of a variable for which the inversions are analytically available, for example the standard normal variable.[8]

See also

References

  1. Aalto University, N. Hyvönen, Computational methods in inverse problems. Twelfth lecture https://noppa.tkk.fi/noppa/kurssi/mat-1.3626/luennot/Mat-1_3626_lecture12.pdf[yes|permanent dead link|dead link}}]
  2. Luc Devroye (1986). Non-Uniform Random Variate Generation. New York: Springer-Verlag. http://www.eirene.de/Devroye.pdf. Retrieved 2012-04-12. 
  3. "R: Random Number Generation". https://stat.ethz.ch/R-manual/R-devel/library/base/html/Random.html. 
  4. 4.0 4.1 McNeil, Alexander J.; Frey, Rüdiger; Embrechts, Paul (2005). Quantitative risk management. Princeton Series in Finance. Princeton University Press, Princeton, NJ. p. 186. ISBN 0-691-12255-5. 
  5. Steinbrecher, György; Shaw, William T. (19 March 2008). "Quantile mechanics". European Journal of Applied Mathematics 19 (2). doi:10.1017/S0956792508007341. 
  6. Arridge, Simon; Maass, Peter; Öktem, Ozan; Schönlieb, Carola-Bibiane (2019). "Solving inverse problems using data-driven models" (in en). Acta Numerica 28: 1–174. doi:10.1017/S0962492919000059. ISSN 0962-4929. 
  7. Luc Devroye (1986). "Section 2.2. Inversion by numerical solution of F(X) = U". Non-Uniform Random Variate Generation. New York: Springer-Verlag. http://luc.devroye.org/chapter_two.pdf. 
  8. L.A. Grzelak, J.A.S. Witteveen, M. Suarez, and C.W. Oosterlee. The stochastic collocation Monte Carlo sampler: Highly efficient sampling from “expensive” distributions. https://ssrn.com/abstract=2529691