pub mod core;
pub mod seq;
pub mod slice_ops;
pub mod arrays;
pub mod distributions;
pub mod distributions_unified;
pub mod qmc;
pub mod variance_reduction;
pub mod parallel;
pub mod secure;
pub mod ml;
pub mod prelude;
pub mod quick;
pub mod scientific;
pub mod advanced_numerical;
pub mod cutting_edge_mcmc;
pub mod ecosystem_integration;
pub mod neural_sampling;
pub mod quantum_inspired;
pub use self::core::{seeded_rng, thread_rng, DistributionExt};
pub use rand_chacha::{ChaCha12Rng, ChaCha20Rng, ChaCha8Rng};
pub use self::core::Random as CoreRandom;
pub use slice_ops::{ScientificSliceRandom, SliceRandomExt};
pub use distributions::{
Beta, Categorical, Dirichlet, GammaDist, MultivariateNormal, VonMises, WeightedChoice,
};
pub use distributions_unified::{
UnifiedBeta, UnifiedBinomial, UnifiedCauchy, UnifiedChiSquared, UnifiedDirichlet,
UnifiedDistribution, UnifiedDistributionError, UnifiedExp, UnifiedFisherF, UnifiedGamma,
UnifiedLogNormal, UnifiedNormal, UnifiedPoisson, UnifiedStudentT, UnifiedWeibull,
};
pub use arrays::{
random_exponential_array, random_gamma_array, random_he_weights, random_normal_array,
random_sparse_array, random_uniform_array, random_xavier_weights, OptimizedArrayRandom,
};
pub use variance_reduction::{
AntitheticSampling, CommonRatio, ControlVariate, ImportanceSplitting,
};
pub use qmc::{
HaltonGenerator, LatinHypercubeSampler, LowDiscrepancySequence, QmcError, SobolGenerator,
};
pub use secure::{utils as secure_utils, SecureRandom, SecureRngPool};
pub use parallel::{BatchRng, DistributedRngPool, ParallelRng, ThreadLocalRngPool};
pub use advanced_numerical::{
AdaptiveResult, AdaptiveSampler, ImportanceResult, ImportanceSampler, MLMCResult,
MultiLevelMonteCarlo, SequentialMonteCarlo,
};
pub use cutting_edge_mcmc::{
EllipticalSliceSampler, HamiltonianMonteCarlo, NoUTurnSampler, ParallelTempering,
SteinVariationalGradientDescent,
};
pub use neural_sampling::{
DiffusionConfig, EnergyBasedModel, NeuralPosteriorEstimation, NormalizingFlow,
ScoreBasedDiffusion,
};
pub use quantum_inspired::{
CoinParameters, QuantumAmplitudeAmplification, QuantumInspiredAnnealing,
QuantumInspiredEvolutionary, QuantumWalk,
};
pub use ecosystem_integration::{
AugmentationConfig, ExperimentalDesign, LinalgBridge, NeuralBridge, OptimizationBridge,
StatsBridge, SyntheticDataset,
};
pub use ::ndarray::Dimension;
pub use rand::prelude as rand_prelude;
#[allow(deprecated)]
pub use rand::rand_core::RngCore;
pub use rand::rngs;
pub use rand::seq::SliceRandom;
pub use rand::{Rng, RngExt, SeedableRng, TryRng};
pub use rand_distr as rand_distributions;
pub use rand_distr::uniform;
pub fn random<T>() -> T
where
rand::distr::StandardUniform: rand::distr::Distribution<T>,
{
rand::random()
}
pub fn rng() -> rand::rngs::ThreadRng {
rand::rng()
}
pub use rand_distr::{
Alphanumeric,
Bernoulli as RandBernoulli,
Beta as RandBeta,
Binomial,
Cauchy,
ChiSquared,
Distribution,
Exp,
FisherF,
Gamma as RandGamma,
Geometric,
Hypergeometric,
InverseGaussian,
LogNormal,
Normal as RandNormal,
Open01,
OpenClosed01,
Pareto,
Pert,
Poisson,
StandardNormal,
StudentT,
Triangular,
Uniform as RandUniform,
UnitBall,
UnitCircle,
UnitDisc,
UnitSphere,
Weibull,
Zeta,
Zipf,
};
pub use rand_distr::weighted::WeightedIndex;
pub use rand_distr::multi::Dirichlet as RandDirichlet;
pub use rand_distr::Bernoulli;
pub use rand_distr::Exp as Exponential; pub use rand_distr::Gamma;
pub use rand_distr::Normal;
pub use rand_distr::Uniform;
#[cfg(feature = "random")]
pub use ndarray_rand::RandomExt;
#[cfg(not(feature = "random"))]
pub trait RandomExt<T, D> {
fn random_using<R: rand::Rng>(
shape: D,
distribution: impl rand_distr::Distribution<T>,
rng: &mut R,
) -> Self;
}
#[cfg(not(feature = "random"))]
impl<T, D> RandomExt<T, D> for crate::ndarray::ArrayBase<crate::ndarray::OwnedRepr<T>, D>
where
D: crate::ndarray::Dimension,
{
fn random_using<R: rand::Rng>(
shape: D,
distribution: impl rand_distr::Distribution<T>,
rng: &mut R,
) -> Self {
let size = shape.size();
let mut data = Vec::with_capacity(size);
for _ in 0..size {
data.push(distribution.sample(rng));
}
Self::from_shape_vec(shape, data).expect("Operation failed")
}
}
pub mod legacy {
use super::*;
use rand_distr::Uniform;
pub fn rng() -> Random<rand::rngs::ThreadRng> {
Random { rng: rand::rng() }
}
pub fn f64() -> f64 {
rand::random::<f64>()
}
pub fn f32() -> f32 {
rand::random::<f32>()
}
pub fn usize(range: std::ops::Range<usize>) -> usize {
rand::rng().random_range(range)
}
}
pub mod convenience {
use super::*;
use ::ndarray::{Array, Dimension, IxDyn};
use rand_distr::{Distribution, Normal, Uniform};
pub fn uniform() -> f64 {
let mut rng = thread_rng();
rng.sample(Uniform::new(0.0, 1.0).expect("Operation failed"))
}
pub fn normal() -> f64 {
let mut rng = thread_rng();
rng.sample(Normal::new(0.0, 1.0).expect("Operation failed"))
}
pub fn integer(min: i64, max: i64) -> i64 {
let mut rng = thread_rng();
rng.sample(Uniform::new_inclusive(min, max).expect("Operation failed"))
}
pub fn boolean() -> bool {
let mut rng = thread_rng();
rng.random_bool(0.5)
}
pub fn uniform_array<Sh: Into<IxDyn>>(shape: Sh) -> Array<f64, IxDyn> {
let mut rng = thread_rng();
let shape = shape.into();
let size = shape.size();
let values: Vec<f64> = (0..size)
.map(|_| rng.sample(Uniform::new(0.0, 1.0).expect("Operation failed")))
.collect();
Array::from_shape_vec(shape, values).expect("Operation failed")
}
pub fn normal_array<Sh: Into<IxDyn>>(shape: Sh, mean: f64, std: f64) -> Array<f64, IxDyn> {
let mut rng = thread_rng();
let shape = shape.into();
let size = shape.size();
let values: Vec<f64> = (0..size)
.map(|_| rng.sample(Normal::new(mean, std).expect("Operation failed")))
.collect();
Array::from_shape_vec(shape, values).expect("Operation failed")
}
}
pub mod sampling {
use super::*;
use ::ndarray::{Array, Dimension, IxDyn};
use rand_distr::{Distribution, Exp, LogNormal, Normal, Uniform};
pub fn random_uniform01<R: rand::Rng>(rng: &mut Random<R>) -> f64 {
rng.sample(Uniform::new(0.0, 1.0).expect("Operation failed"))
}
pub fn random_standard_normal<R: rand::Rng>(rng: &mut Random<R>) -> f64 {
rng.sample(Normal::new(0.0, 1.0).expect("Operation failed"))
}
pub fn random_normal<R: rand::Rng>(rng: &mut Random<R>, mean: f64, stddev: f64) -> f64 {
rng.sample(Normal::new(mean, stddev).expect("Operation failed"))
}
pub fn random_lognormal<R: rand::Rng>(rng: &mut Random<R>, mean: f64, stddev: f64) -> f64 {
rng.sample(LogNormal::new(mean, stddev).expect("Operation failed"))
}
pub fn random_exponential<R: rand::Rng>(rng: &mut Random<R>, lambda: f64) -> f64 {
rng.sample(Exp::new(lambda).expect("Operation failed"))
}
pub fn random_integers<R: rand::Rng, Sh>(
rng: &mut Random<R>,
min: i64,
max: i64,
shape: Sh,
) -> Array<i64, IxDyn>
where
Sh: Into<IxDyn>,
{
rng.sample_array(
Uniform::new_inclusive(min, max).expect("Operation failed"),
shape,
)
}
pub fn random_floats<R: rand::Rng, Sh>(
rng: &mut Random<R>,
min: f64,
max: f64,
shape: Sh,
) -> Array<f64, IxDyn>
where
Sh: Into<IxDyn>,
{
rng.sample_array(Uniform::new(min, max).expect("Operation failed"), shape)
}
pub fn bootstrap_indices<R: rand::Rng>(
rng: &mut Random<R>,
data_size: usize,
sample_size: usize,
) -> Vec<usize> {
let dist = Uniform::new(0, data_size).expect("Operation failed");
rng.sample_vec(dist, sample_size)
}
pub fn sample_without_replacement<R: rand::Rng>(
rng: &mut Random<R>,
data_size: usize,
sample_size: usize,
) -> Vec<usize> {
use rand::seq::SliceRandom;
let mut indices: Vec<usize> = (0..data_size).collect();
indices.shuffle(&mut rng.rng);
indices.truncate(sample_size);
indices
}
}
pub mod importance_sampling {
use super::*;
use rand_distr::{Normal, Uniform};
#[derive(Debug)]
pub struct ImportanceSampler<R: rand::Rng> {
rng: Random<R>,
}
impl<R: rand::Rng> ImportanceSampler<R> {
pub fn new(rng: Random<R>) -> Self {
Self { rng }
}
pub fn sample_with_weights<F, G>(
&mut self,
target_pdf: F,
proposal_pdf: G,
proposal_sampler: impl Fn(&mut Random<R>) -> f64,
n_samples: usize,
) -> (Vec<f64>, Vec<f64>)
where
F: Fn(f64) -> f64,
G: Fn(f64) -> f64,
{
let mut samples = Vec::with_capacity(n_samples);
let mut weights = Vec::with_capacity(n_samples);
for _ in 0..n_samples {
let sample = proposal_sampler(&mut self.rng);
let weight = target_pdf(sample) / proposal_pdf(sample);
samples.push(sample);
weights.push(weight);
}
(samples, weights)
}
pub fn estimate_expectation<F, G, H>(
&mut self,
function: F,
target_pdf: G,
proposal_pdf: H,
proposal_sampler: impl Fn(&mut Random<R>) -> f64,
n_samples: usize,
) -> f64
where
F: Fn(f64) -> f64,
G: Fn(f64) -> f64,
H: Fn(f64) -> f64,
{
let (samples, weights) =
self.sample_with_weights(target_pdf, proposal_pdf, proposal_sampler, n_samples);
let weighted_sum: f64 = samples
.iter()
.zip(weights.iter())
.map(|(&x, &w)| function(x) * w)
.sum();
let weight_sum: f64 = weights.iter().sum();
weighted_sum / weight_sum
}
pub fn adaptive_sampling<F>(
&mut self,
target_log_pdf: F,
initial_samples: usize,
adaptation_rounds: usize,
) -> Vec<f64>
where
F: Fn(f64) -> f64,
{
let mut samples = Vec::new();
let mut proposal_mean: f64 = 0.0;
let mut proposal_std: f64 = 1.0;
for round in 0..adaptation_rounds {
let round_samples = if round == 0 {
initial_samples
} else {
initial_samples / 2
};
let normal_dist =
Normal::new(proposal_mean, proposal_std).expect("Operation failed");
let mut round_sample_vec = Vec::new();
let mut weights = Vec::new();
for _ in 0..round_samples {
let sample = self.rng.sample(normal_dist);
let normal_log_pdf = -0.5 * ((sample - proposal_mean) / proposal_std).powi(2)
- 0.5 * (2.0 * std::f64::consts::PI).ln()
- proposal_std.ln();
let log_weight = target_log_pdf(sample) - normal_log_pdf;
round_sample_vec.push(sample);
weights.push(log_weight.exp());
}
let weight_sum: f64 = weights.iter().sum();
if weight_sum > 0.0 {
let normalized_weights: Vec<f64> =
weights.iter().map(|w| w / weight_sum).collect();
proposal_mean = round_sample_vec
.iter()
.zip(normalized_weights.iter())
.map(|(&x, &w)| x * w)
.sum();
let variance = round_sample_vec
.iter()
.zip(normalized_weights.iter())
.map(|(&x, &w)| w * (x - proposal_mean).powi(2))
.sum::<f64>();
proposal_std = variance.sqrt().max(0.1); }
samples.extend(round_sample_vec);
}
samples
}
}
impl ImportanceSampler<rand::rngs::ThreadRng> {
pub fn with_default_rng() -> Self {
Self::new(Random::default())
}
}
}
#[cfg(feature = "gpu")]
pub mod gpu {
pub struct GpuRng;
impl Default for GpuRng {
fn default() -> Self {
Self::new()
}
}
impl GpuRng {
pub fn new() -> Self {
Self
}
}
}
#[derive(Debug)]
pub struct Random<R: rand::Rng + ?Sized = rand::rngs::ThreadRng> {
pub(crate) rng: R,
}
impl Default for Random<rand::rngs::ThreadRng> {
fn default() -> Self {
Self { rng: rand::rng() }
}
}
impl<R: rand::Rng + Clone> Clone for Random<R> {
fn clone(&self) -> Self {
Self {
rng: self.rng.clone(),
}
}
}
impl<R: rand::Rng> Random<R> {
pub fn sample<D, T>(&mut self, distribution: D) -> T
where
D: rand_distr::Distribution<T>,
{
use rand_distr::Distribution;
distribution.sample(&mut self.rng)
}
pub fn random_range_bounds<T: rand_distr::uniform::SampleUniform + PartialOrd + Copy>(
&mut self,
min: T,
max: T,
) -> T {
self.sample(rand_distr::Uniform::new(min, max).expect("Operation failed"))
}
pub fn gen_range<T, RNG>(&mut self, range: RNG) -> T
where
T: rand_distr::uniform::SampleUniform,
RNG: rand_distr::uniform::SampleRange<T>,
{
rand::RngExt::random_range(&mut self.rng, range)
}
pub fn random_range<T, RNG>(&mut self, range: RNG) -> T
where
T: rand_distr::uniform::SampleUniform,
RNG: rand_distr::uniform::SampleRange<T>,
{
rand::RngExt::random_range(&mut self.rng, range)
}
pub fn random_f64(&mut self) -> f64 {
self.sample(rand_distr::Uniform::new(0.0, 1.0).expect("Operation failed"))
}
pub fn random_f64_raw(&mut self) -> f64 {
rand::RngExt::random(&mut self.rng)
}
pub fn random_bool(&mut self) -> bool {
use rand_distr::Distribution;
let dist = rand_distr::Bernoulli::new(0.5).expect("Operation failed");
dist.sample(&mut self.rng)
}
pub fn random_bool_with_chance(&mut self, prob: f64) -> bool {
use rand_distr::Distribution;
let dist = rand_distr::Bernoulli::new(prob).expect("Operation failed");
dist.sample(&mut self.rng)
}
pub fn shuffle<T>(&mut self, slice: &mut [T]) {
use rand::seq::SliceRandom;
slice.shuffle(&mut self.rng);
}
pub fn sample_vec<D, T>(&mut self, distribution: D, size: usize) -> Vec<T>
where
D: rand_distr::Distribution<T> + Copy,
{
(0..size)
.map(|_| distribution.sample(&mut self.rng))
.collect()
}
pub fn sample_array<D, T, Sh>(
&mut self,
distribution: D,
shape: Sh,
) -> crate::ndarray::Array<T, crate::ndarray::IxDyn>
where
D: rand_distr::Distribution<T> + Copy,
Sh: Into<crate::ndarray::IxDyn>,
{
let shape = shape.into();
let size = shape.size();
let values = self.sample_vec(distribution, size);
crate::ndarray::Array::from_shape_vec(shape, values).expect("Operation failed")
}
}
impl Random<rand::rngs::ThreadRng> {
pub fn seed(seed: u64) -> Random<rand::rngs::StdRng> {
Random {
rng: rand::SeedableRng::seed_from_u64(seed),
}
}
}
impl<R: rand::Rng> rand::TryRng for Random<R> {
type Error = std::convert::Infallible;
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
Ok(self.rng.next_u32())
}
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
Ok(self.rng.next_u64())
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> {
self.rng.fill_bytes(dest);
Ok(())
}
}
impl rand::SeedableRng for Random<rand::rngs::StdRng> {
type Seed = <rand::rngs::StdRng as rand::SeedableRng>::Seed;
fn from_seed(seed: Self::Seed) -> Self {
Random {
rng: rand::rngs::StdRng::from_seed(seed),
}
}
fn seed_from_u64(state: u64) -> Self {
Random {
rng: rand::rngs::StdRng::seed_from_u64(state),
}
}
}
use std::cell::RefCell;
thread_local! {
static THREAD_RNG: RefCell<Random> = RefCell::new(Random::default());
}
#[allow(dead_code)]
pub fn get_rng<F, R>(f: F) -> R
where
F: FnOnce(&mut Random) -> R,
{
THREAD_RNG.with(|rng| f(&mut rng.borrow_mut()))
}
pub struct DeterministicSequence {
seed: u64,
counter: u64,
}
impl DeterministicSequence {
pub fn seed(seed: u64) -> Self {
Self { seed, counter: 0 }
}
pub fn next_f64(&mut self) -> f64 {
let mut x = self.counter.wrapping_add(self.seed);
x = ((x >> 16) ^ x).wrapping_mul(0x45d9f3b);
x = ((x >> 16) ^ x).wrapping_mul(0x45d9f3b);
x = (x >> 16) ^ x;
self.counter = self.counter.wrapping_add(1);
(x as f64) / (u64::MAX as f64)
}
pub fn reset(&mut self) {
self.counter = 0;
}
pub fn get_vec(&mut self, size: usize) -> Vec<f64> {
(0..size).map(|_| self.next_f64()).collect()
}
pub fn get_array<Sh>(&mut self, shape: Sh) -> crate::ndarray::Array<f64, crate::ndarray::IxDyn>
where
Sh: Into<crate::ndarray::IxDyn>,
{
let shape = shape.into();
let size = shape.size();
let values = self.get_vec(size);
crate::ndarray::Array::from_shape_vec(shape, values).expect("Operation failed")
}
}
pub type ThreadRng = Random<rand::rngs::ThreadRng>;
pub type StdRng = Random<rand::rngs::StdRng>;
pub type UniformDist = rand_distributions::Uniform<f64>;
pub type NormalDist = rand_distributions::Normal<f64>;
pub type ExponentialDist = rand_distributions::Exp<f64>;
pub type Array1D<T> = crate::ndarray::Array1<T>;
pub type Array2D<T> = crate::ndarray::Array2<T>;
pub type Array3D<T> = crate::ndarray::Array3<T>;
pub use quick as rapid;
pub use scientific as research;
pub use ml as machine_learning;
pub use secure as crypto;
pub mod quasi_monte_carlo {
pub use crate::random::qmc::*;
pub type SobolSequence = crate::random::qmc::SobolGenerator;
pub type HaltonSequence = crate::random::qmc::HaltonGenerator;
pub type LatinHypercubeSampling = crate::random::qmc::LatinHypercubeSampler;
}
pub mod specialized_distributions {
pub use crate::random::distributions::*;
}
pub mod optimized_arrays {
pub use crate::random::arrays::*;
}
pub mod slice_random {
pub use crate::random::slice_ops::convenience::*;
}
pub mod essentials {
pub use crate::random::rand_distributions::{Normal, Uniform};
pub use crate::random::{
random_normal_array, random_uniform_array, seeded_rng, thread_rng, Beta, Categorical,
Random, Rng, SeedableRng, WeightedChoice,
};
}
pub mod statistics {
pub use crate::random::{
AntitheticSampling, Beta, Categorical, CommonRatio, ControlVariate, Dirichlet,
ExponentialDist, GammaDist, HaltonGenerator, LatinHypercubeSampler, MultivariateNormal,
SobolGenerator, VonMises, WeightedChoice,
};
}
pub mod hpc {
pub use crate::random::{
random_he_weights, random_normal_array, random_uniform_array, random_xavier_weights,
BatchRng, DistributedRngPool, OptimizedArrayRandom, ParallelRng, ThreadLocalRngPool,
};
}
pub mod cutting_edge {
pub use crate::random::{
advanced_numerical::*, cutting_edge_mcmc::*, ecosystem_integration::*, neural_sampling::*,
quantum_inspired::*,
};
}
pub mod bayesian {
pub use crate::random::{
EllipticalSliceSampler, HamiltonianMonteCarlo, ImportanceSampler, NoUTurnSampler,
ParallelTempering, SteinVariationalGradientDescent,
};
}
pub mod ai_sampling {
pub use crate::random::{
DiffusionConfig, EnergyBasedModel, NeuralBridge, NeuralPosteriorEstimation,
NormalizingFlow, ScoreBasedDiffusion,
};
}
pub mod quantum {
pub use crate::random::{
CoinParameters, QuantumAmplitudeAmplification, QuantumInspiredAnnealing,
QuantumInspiredEvolutionary, QuantumWalk,
};
}
pub mod numerical_methods {
pub use crate::random::{
AdaptiveResult, AdaptiveSampler, ImportanceResult, MLMCResult, MultiLevelMonteCarlo,
SequentialMonteCarlo,
};
}
pub mod bridges {
pub use crate::random::{
AugmentationConfig, ExperimentalDesign, LinalgBridge, NeuralBridge, OptimizationBridge,
StatsBridge, SyntheticDataset,
};
}