Misplaced Pages

Inverse transform sampling

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
(Redirected from Inverse transform sampling method) Basic method for pseudo-random number sampling

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

Formal statement

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

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 X {\displaystyle X} with cumulative distribution function F X {\displaystyle F_{X}} , the random variable U = F X ( X ) {\displaystyle U=F_{X}(X)} is uniform on [ 0 , 1 ] {\displaystyle } .

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

Intuition

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

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

F X ( x ) = Pr ( X x ) = Pr ( T ( U ) x ) = Pr ( U T 1 ( x ) ) = T 1 ( x ) ,  for  x R , {\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} ,}

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

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

Therefore, we can generate X {\displaystyle X} from F X 1 ( U ) . {\displaystyle F_{X}^{-1}(U).}

The method

Schematic of the inverse transform sampling. The inverse function of y = F X ( x ) {\displaystyle y=F_{X}(x)} can be defined by F X 1 ( y ) = i n f { x | F X ( x ) y } {\displaystyle F_{X}^{-1}(y)=\mathrm {inf} \{x|F_{X}(x)\geq y\}} .
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 X {\displaystyle X} be a random variable whose distribution can be described by the cumulative distribution function F X {\displaystyle F_{X}} .
  • We want to generate values of X {\displaystyle X} which are distributed according to this distribution.

The inverse transform sampling method works as follows:

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

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

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

Examples

F ( x ) = 1 exp ( x ) {\displaystyle {\begin{aligned}F(x)=1-\exp(-{\sqrt {x}})\end{aligned}}}
In order to perform an inversion we want to solve for F ( F 1 ( u ) ) = u {\displaystyle F(F^{-1}(u))=u}
F ( F 1 ( u ) ) = u 1 exp ( F 1 ( u ) ) = u F 1 ( u ) = ( log ( 1 u ) ) 2 = ( log ( 1 u ) ) 2 {\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 F X ( x ) = 1 e λ x {\displaystyle F_{X}(x)=1-e^{-\lambda x}} for x ≥ 0 (and 0 otherwise). By solving y=F(x) we obtain the inverse function
x = F 1 ( y ) = 1 λ ln ( 1 y ) . {\displaystyle x=F^{-1}(y)=-{\frac {1}{\lambda }}\ln(1-y).}
It means that if we draw some y 0 {\displaystyle y_{0}} from a U U n i f ( 0 , 1 ) {\displaystyle U\sim \mathrm {Unif} (0,1)} and compute x 0 = F X 1 ( y 0 ) = 1 λ ln ( 1 y 0 ) , {\displaystyle x_{0}=F_{X}^{-1}(y_{0})=-{\frac {1}{\lambda }}\ln(1-y_{0}),} This x 0 {\displaystyle x_{0}} 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(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 ϱ X ( x ) = λ e λ x {\displaystyle \varrho _{X}(x)=\lambda e^{-\lambda \,x}} and the cumulative distribution function is F ( x ) = 1 e λ x {\displaystyle F(x)=1-e^{-\lambda \,x}} . Therefore, x = F 1 ( y ) = ln ( 1 y ) λ {\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 and then simply calculate
x = F 1 ( y ) = 1 λ ln ( y ) . {\displaystyle x=F^{-1}(y)=-{\frac {1}{\lambda }}\ln(y).}

Proof of correctness

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

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

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

Proof:

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

Truncated distribution

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

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.

Software implementations

There are software implementations available for applying the inverse sampling method by using numerical approximations of the inverse in the case that it is not available in closed form. For example, an approximation of the inverse can be computed if the user provides some information about the distributions such as the PDF or the CDF.

  • C library UNU.RAN
  • R library Runuran
  • Python subpackage sampling in scipy.stats

See also

References

  1. Luc Devroye (1986). Non-Uniform Random Variate Generation (PDF). New York: Springer-Verlag. Archived from the original (PDF) on 2014-08-18. Retrieved 2012-04-12.
  2. "R: Random Number Generation".
  3. ^ 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.
  4. Steinbrecher, György; Shaw, William T. (19 March 2008). "Quantile mechanics". European Journal of Applied Mathematics. 19 (2). doi:10.1017/S0956792508007341. S2CID 6899308.
  5. Arridge, Simon; Maass, Peter; Öktem, Ozan; Schönlieb, Carola-Bibiane (2019). "Solving inverse problems using data-driven models". Acta Numerica. 28: 1–174. doi:10.1017/S0962492919000059. ISSN 0962-4929. S2CID 197480023.
  6. Luc Devroye (1986). "Section 2.2. Inversion by numerical solution of F(X) = U" (PDF). Non-Uniform Random Variate Generation. New York: Springer-Verlag.
  7. 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
  8. Derflinger, Gerhard; Hörmann, Wolfgang; Leydold, Josef (2010). "Random variate generation by numerical inversion when only the density is known" (PDF). ACM Transactions on Modeling and Computer Simulation. 20 (4). doi:10.1145/945511.945517.
  9. "UNU.RAN - Universal Non-Uniform RANdom number generators".
  10. "Runuran: R Interface to the 'UNU.RAN' Random Variate Generators". 17 January 2023.
  11. "Random Number Generators (Scipy.stats.sampling) — SciPy v1.12.0 Manual".
  12. Baumgarten, Christoph; Patel, Tirth (2022). "Automatic random variate generation in Python". Proceedings of the 21st Python in Science Conference. pp. 46–51. doi:10.25080/majora-212e5952-007.
Categories: