Gaussian Generator Contract
- Gaussian Generator Contract is a finite-precision specification that guarantees generated samples exactly match the cumulative probabilities of a user-supplied Gaussian CDF.
- It employs the Knuth-Yao scheme for optimal, unbiased coin-flip mapping, significantly reducing entropy usage compared to traditional methods.
- The contract provides a robust API with formal guarantees on precision, overflow-avoidance, and reproducibility, making it suitable for high-performance sampling.
A Gaussian Generator Contract, as realized in the universal framework described by "Random Variate Generation with Formal Guarantees" (Saad et al., 17 Jul 2025), is an explicit, finite-precision specification and implementation of a random variate generator targeting the Gaussian distribution. It enforces that generated outputs strictly realize the exact cumulative probabilities prescribed by a user-supplied Gaussian CDF, with guarantees on precision, overflow-avoidance, and entropy-optimality. The contract encapsulates both mathematical formalism and a low-level software API, yielding a reproducible and auditable method for exact random sampling in floating-point, fixed-point, or posits-based systems.
1. Formal Specification of the Target Distribution
The contract is constructed upon a finite-precision CDF program defined over a binary number format , where bit-strings of length encode real values in . Monotonicity and strict endpoint requirements (, ) guarantee well-formedness. For the Gaussian case, is realized as
with all operations implemented in IEEE-754 binary64, including correct clipping and NaN-handling. This discrete mapping defines a probability mass function on the set of representable floats, fully characterizing the desired generator's output law in terms of CDF differences for each bit-string .
2. Construction of Entropy-Optimal Generator
Given a finite-precision CDF , the generator algorithm (denoted "Opt") operates using unbiased coin-flips (RandBit()), mapping entropy directly onto output via the information-theoretically optimal Knuth-Yao scheme:
- For each prefix of length , the binary-coded distribution is computed as
where and decomposes as (see Prop. 4.6).
- The algorithm iteratively refines the output prefix, querying the leading bits of the relevant probability differences; the search proceeds down an implicit DDG (Discrete Distribution Generation) tree with expected coin-flip cost bounded by entropy plus two bits (Theorem 2.1).
- Fast integer operations extract bitfields corresponding to floating-point subtractions, entirely avoiding arbitrary-precision arithmetic (Prop. 4.8).
The procedure realizes exact sampling for any distribution specified by a numerical CDF, restricted to the precision used in its definition, maintaining strict resource bounds per output sample.
3. Instantiation for the Gaussian Distribution
Within double precision (), the contract is instantiated by providing:
- A CDF Program:
1 2 3 4
double gaussian_cdf(double x, double sigma) { double z = x / (sigma * sqrt(2)); return 0.5 * erfc(-z); }
- Generator Invocation:
The macro
GENERATE_FROM_CDF(gaussian_cdf, sigma)synthesizes the optimal generator via the above algorithm. Each sample requires only the precision and arithmetic already present in the CDF code. - Accuracy Guarantee: The output distribution matches the induced discrete law exactly; there is no added error from the generator itself. The output float fulfills
even for subnormal and boundary values.
4. Contractual API and Operational Semantics
The contract exposes a consistent API—typically in C—enabling direct use in high-performance contexts:
1 |
double rng_gaussian_from_cdf(struct rng_state *R, double sigma); |
Rserves as a source of unbiased random bits,sigmais required to be positive,- The output in binary64 satisfies the exact cumulative probability constraint as per the provided CDF,
- Entropy utilization is theoretically bounded ( bits per sample for double, Cor. 4.15); buffer and alignment strategies can be tuned further.
Preconditions:
- Input CDF must be strictly monotonic, return values only in , and be robust to NaN inputs.
Postconditions:
- Each output is distributed precisely according to the finite-precision CDF, with no error introduced in the sampling process.
5. Performance and Entropy Efficiency
Empirical evaluations (Table 5 in (Saad et al., 17 Jul 2025)) demonstrate:
- Entropy Utilization:
- The OPT (optimal) sampler uses bits per Gaussian sample, with the minimum possible bound bits for mantissa bits in binary64.
- Standard routines such as Box-Muller or Ziggurat typically consume two uniform samples (each bits), totaling $106$ bits—thus the contract achieves more than double the entropy efficiency.
- Throughput:
- The C prototype generator achieves samples/sec on 3 GHz x86_64; with optimized CDF and inlined bit-extraction, rates approach one million samples/sec while retaining exactness.
- For comparison, GSL's Ziggurat implementation reaches samples/sec but does not enforce strict CDF fidelity.
- Theoretical Bounds:
- With , the expected coin-flips per sample satisfy (Cor. 4.15), matching information-theoretic lower bounds.
6. Theoretical Guarantees and Correctness Arguments
The generator's soundness and optimality are rigorously established:
- Correctness:
- By explicit carry analysis (Props. 3.9, 3.10), the generated samples realize exactly the target probability mass function (Thm 3.14).
- Optimality:
- Each generated path matches the DDG tree predicted by Knuth-Yao (Thm 3.17); resource consumption is bounded as per sample in both time and space, with all operations confined to machine word sizes.
- Overflow and Precision:
- Bit-extraction and arithmetic for CDF differences never exceed the width of an input word (Prop. 4.8), precluding overflows or loss of fidelity.
- Automation and Universality:
- The contract and methods are universal; any finite-precision CDF (floating-point, fixed-point, posit) can be used as input, enabling fully automated generator synthesis.
The contract for a Gaussian generator thereby encapsulates a precise, entropy-optimal, and formally auditable mechanism for sampling, distinguished by both its exactness (w.r.t. user-specified CDF) and its efficient resource profile (Saad et al., 17 Jul 2025).