Expand description
Random variate generation from a given density or mass function, after the methods of UNU.RAN
that scipy.stats.sampling wraps: the alias method and guide-table inversion for discrete
distributions, transformed density rejection for log-concave densities, Hermite-interpolated
numerical inversion of a distribution function, and the ratio-of-uniforms method.
Distributions are given by closures (the density, its derivative, the distribution function).
The generators draw from any UniformGenerator. They are exact (rejection methods) or exact
to a stated resolution (inversion), and do not reproduce SciPy’s numbers for the same seed:
their uniform streams differ.
See the book entry Random variate generation.
Structs§
- Discrete
Alias Urn - Walker’s alias method (SciPy’s
DiscreteAliasUrn, Vose’s construction): afterO(n)set-up, each variate costs one uniform and one comparison. Each ofnequally likely columns holds its own outcome with some probability and an alias otherwise. - Discrete
Guide Table - Inversion by guide table (SciPy’s
DiscreteGuideTable): the cumulative probabilities are searched from a table that says where to start for each ofguide_factor × nequal slices of[0, 1), so the expected search length is a small constant. - Fast
Generator Inversion - Fast numerical inversion of a standard density with a location and a scale (SciPy’s
FastGeneratorInversion, which wraps the library’s distributions and picks the method that suits each; here the density is given, and the method is polynomial inversion of order 5 at resolution1e-12):X = loc + scale · Z,Zwith the given density. - Multivariate
Ratio Uniforms - The multivariate ratio-of-uniforms method (Wakefield, Gelfand and Smith 1991; the idea of UNU.RAN’s
VNROU) for a unimodal densityfonℝᵈknown up to a constant, with its modem: if(u, v)is uniform on{(u, v): 0 < u ≤ f(m + v/u)^{1/(d+1)}}, thenm + v/uhas density∝ f. The region lies in the box[0, f(m)^{1/(d+1)}] × ∏ᵢ [vᵢ⁻, vᵢ⁺]withvᵢ⁺ = sup (xᵢ − mᵢ) f(x)^{1/(d+1)}andvᵢ⁻ = inf, found at construction by a derivative-free search from several starting points, with a 1 % margin, or given. Points of the box are drawn and kept if they fall in the region. The method is exact when the box contains the region, and its acceptance rate falls withd(about 27 % for the normal density atd = 3, 7 % atd = 5). - Numerical
Inverse Hermite - Numerical inversion of a distribution function by cubic Hermite interpolation of its inverse
(SciPy’s
NumericalInverseHermite; Hörmann and Leydold’s HINV). The interval(a, b)is split until, on every piece, the cubic that matchesxanddx/du = 1/f(x)at both ends invertsFto withinu_resolution:|F(x̂(u)) − u| ≤ εat the middle of the piece. The pieces are then a table, and a variate or a quantile costs a search and a cubic. - Numerical
Inverse Polynomial - Numerical inversion of a distribution function from the density alone, by polynomial
interpolation of its inverse (SciPy’s
NumericalInversePolynomial; Derflinger, Hörmann and Leydold’s PINV). The interval is split until, on every piece, the polynomial of degreeorderthroughorder + 1Chebyshev-Lobatto points of the piece, as a function of the probability mass, inverts the integral of the density to withinu_resolutionof the whole mass at three interior test points. The masses are integrals of the density (adaptive Gauss-Legendre), so the density needs neither normalising nor a distribution function. - Simple
Ratio Uniforms - The simple ratio-of-uniforms method (SciPy’s
SimpleRatioUniforms; Leydold’s SROU) for a unimodal density with a known mode: the region0 < u ≤ √f(x),v = (x − mode) u, lies inside the rectangle[0, √f(mode)] × [v₋, v₊]withv₊ = sup_{x>mode} (x − mode) √f(x)andv₋ = inf_{x<mode} (x − mode) √f(x). The two suprema are found numerically at construction (a scan over geometrically spaced points, then golden-section refinement), so the density need not be normalised and no derivative is needed. - Transformed
Density Rejection - Transformed density rejection with the logarithm as the transformation (SciPy’s
TransformedDensityRejection; Gilks and Wild’s adaptive rejection sampling, Hörmann’s formulation) for a log-concave densityfon(a, b).
Functions§
- rvs_
ratio_ uniforms - The ratio-of-uniforms method (SciPy’s
rvs_ratio_uniforms): if(u, v)is uniform on the region0 < u ≤ √f(v/u + c), thenv/u + chas density proportional tof. Points are drawn from the rectangle[0, umax] × [vmin, vmax]that contains the region and kept if they fall inside it;cshifts the centre (SciPy’sc).