Skip to main content

Module sampling

Module sampling 

Source
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§

DiscreteAliasUrn
Walker’s alias method (SciPy’s DiscreteAliasUrn, Vose’s construction): after O(n) set-up, each variate costs one uniform and one comparison. Each of n equally likely columns holds its own outcome with some probability and an alias otherwise.
DiscreteGuideTable
Inversion by guide table (SciPy’s DiscreteGuideTable): the cumulative probabilities are searched from a table that says where to start for each of guide_factor × n equal slices of [0, 1), so the expected search length is a small constant.
FastGeneratorInversion
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 resolution 1e-12): X = loc + scale · Z, Z with the given density.
MultivariateRatioUniforms
The multivariate ratio-of-uniforms method (Wakefield, Gelfand and Smith 1991; the idea of UNU.RAN’s VNROU) for a unimodal density f on ℝᵈ known up to a constant, with its mode m: if (u, v) is uniform on {(u, v): 0 < u ≤ f(m + v/u)^{1/(d+1)}}, then m + v/u has density ∝ f. The region lies in the box [0, f(m)^{1/(d+1)}] × ∏ᵢ [vᵢ⁻, vᵢ⁺] with vᵢ⁺ = sup (xᵢ − mᵢ) f(x)^{1/(d+1)} and vᵢ⁻ = 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 with d (about 27 % for the normal density at d = 3, 7 % at d = 5).
NumericalInverseHermite
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 matches x and dx/du = 1/f(x) at both ends inverts F to within u_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.
NumericalInversePolynomial
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 degree order through order + 1 Chebyshev-Lobatto points of the piece, as a function of the probability mass, inverts the integral of the density to within u_resolution of 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.
SimpleRatioUniforms
The simple ratio-of-uniforms method (SciPy’s SimpleRatioUniforms; Leydold’s SROU) for a unimodal density with a known mode: the region 0 < u ≤ √f(x), v = (x − mode) u, lies inside the rectangle [0, √f(mode)] × [v₋, v₊] with v₊ = sup_{x>mode} (x − mode) √f(x) and v₋ = 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.
TransformedDensityRejection
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 density f on (a, b).

Functions§

rvs_ratio_uniforms
The ratio-of-uniforms method (SciPy’s rvs_ratio_uniforms): if (u, v) is uniform on the region 0 < u ≤ √f(v/u + c), then v/u + c has density proportional to f. Points are drawn from the rectangle [0, umax] × [vmin, vmax] that contains the region and kept if they fall inside it; c shifts the centre (SciPy’s c).