unuran_sampler
UNU.RAN Default Sampler
This module provides the default UNU.RAN sampler implementation that uses the UNU.RAN library for efficient random variate generation from probability distributions. The sampler automatically selects appropriate sampling methods based on available distribution characteristics.
- class pysatl_core.sampling.unuran.core.unuran_sampler.DefaultUnuranSampler(distr, config=None)[source]
Bases:
objectDefault UNU.RAN sampler implementation.
This sampler provides a default implementation for generating random variates from probability distributions using the UNU.RAN library. It automatically selects appropriate sampling methods based on available distribution characteristics (PDF, CDF, PPF, PMF).
The sampler supports both continuous and discrete univariate distributions and uses various UNU.RAN methods (PINV, HINV, NINV, DGT, etc.) depending on the available characteristics and configuration.
- Parameters:
distr (Distribution)
config (UnuranMethodConfig | None)
- distr
The probability distribution to sample from.
- Type:
Distribution
- config
Configuration for method selection and parameters.
- Type:
UnuranMethodConfig
- method
The sampling method currently used by this sampler.
- Type:
UnuranMethod
Notes
Only univariate Euclidean distributions are supported
The sampler automatically selects the best method if
method=AUTOCallbacks are kept alive to prevent garbage collection issues with CFFI
Resources are automatically cleaned up when the object is deleted
- __init__(distr, config=None)[source]
Initialize the UNU.RAN sampler.
- Parameters:
distr (
Distribution) – The probability distribution to sample from. Must be a univariate Euclidean distribution (dimension=1).config (
UnuranMethodConfig, optional) – Configuration for method selection and parameters. If None, uses default configuration.
- Raises:
RuntimeError – If the distribution type is not supported (not Euclidean or dimension != 1), or if initialization fails.
Notes
The initialization process: 1. Validates distribution type and dimension 2. Determines available characteristics 3. Selects appropriate sampling method (if AUTO) 4. Creates UNURAN distribution object 5. Sets up callbacks for available characteristics 6. Initializes the UNURAN generator
- __del__()[source]
Cleanup on object deletion.
Automatically calls
_cleanup()when the object is garbage collected. All exceptions during finalization are silently ignored.- Return type:
- sample(n)[source]
Generate random variates from the distribution.
- Parameters:
n (
int) – Number of samples to generate. Must be non-negative.- Returns:
1D array of shape
(n,)containing the generated samples.- Return type:
npt.NDArray[np.float64]- Raises:
RuntimeError – If the sampler is not initialized.
ValueError – If
n < 0.
Notes
Uses UNURAN’s sampling functions: -
unur_sample_cont()for continuous distributions -unur_sample_discr()for discrete distributionsSamples are generated sequentially in a loop. For large
n, this may be slower than vectorized operations, but it’s necessary due to UNURAN’s C API design.
- property method: UnuranMethod
The sampling method used by this sampler.