1#![no_std]
2extern crate alloc;
44
45#[cfg(feature = "std")]
46extern crate std;
47
48pub use tpt_math_prob_core::{Distribution, Rng, Sampler, SplitMix64, Standard};
50
51use core::fmt;
52
53#[cfg(any(feature = "std", feature = "alloc"))]
54use alloc::vec::Vec;
55
56#[inline]
58fn fabs(x: f64) -> f64 {
59 if x < 0.0 {
60 -x
61 } else {
62 x
63 }
64}
65
66#[inline]
68fn uniform_f64<R: Rng + ?Sized>(rng: &mut R) -> f64 {
69 Distribution::<f64>::sample(&Standard, rng)
70}
71
72#[derive(Clone, Copy, Debug, PartialEq, Eq)]
74#[non_exhaustive]
75pub enum SamplerError {
76 Empty,
78 NegativeWeight,
80 NonFiniteWeight,
82 ZeroTotal,
84 InvalidCdf,
86 InvalidProbability,
88 InvalidBound,
90 InvalidInterval,
92 TooManySamples,
94}
95
96impl fmt::Display for SamplerError {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 let s = match self {
99 SamplerError::Empty => "empty distribution (no weights)",
100 SamplerError::NegativeWeight => "negative weight encountered",
101 SamplerError::NonFiniteWeight => "non-finite (NaN/inf) weight encountered",
102 SamplerError::ZeroTotal => "total weight is zero",
103 SamplerError::InvalidCdf => "CDF is not monotone non-decreasing ending in 1",
104 SamplerError::InvalidProbability => "probability not in [0, 1]",
105 SamplerError::InvalidBound => "envelope constant m must be strictly positive",
106 SamplerError::InvalidInterval => "uniform interval lo >= hi",
107 SamplerError::TooManySamples => "requested sample count exceeds the population",
108 };
109 f.write_str(s)
110 }
111}
112
113#[cfg(feature = "std")]
114impl std::error::Error for SamplerError {}
115
116fn validate_weights(weights: &[f64]) -> Result<f64, SamplerError> {
118 if weights.is_empty() {
119 return Err(SamplerError::Empty);
120 }
121 let mut total = 0.0;
122 for &w in weights {
123 if !w.is_finite() {
124 return Err(SamplerError::NonFiniteWeight);
125 }
126 if w < 0.0 {
127 return Err(SamplerError::NegativeWeight);
128 }
129 total += w;
130 }
131 if total <= 0.0 || !total.is_finite() {
132 return Err(SamplerError::ZeroTotal);
133 }
134 Ok(total)
135}
136
137#[inline]
142fn inverse_cdf_index(cdf: &[f64], u: f64) -> usize {
143 let mut idx = cdf.partition_point(|&c| c <= u);
144 if idx >= cdf.len() {
145 idx = cdf.len() - 1;
148 while idx > 0 && cdf[idx] == cdf[idx - 1] {
149 idx -= 1;
150 }
151 }
152 idx
153}
154
155pub struct InverseCdfSampler<S> {
180 cdf: S,
181}
182
183impl<S: AsRef<[f64]>> Clone for InverseCdfSampler<S>
184where
185 S: Clone,
186{
187 fn clone(&self) -> Self {
188 InverseCdfSampler {
189 cdf: self.cdf.clone(),
190 }
191 }
192}
193impl<S: AsRef<[f64]>> Copy for InverseCdfSampler<S> where S: Copy {}
194impl<S: AsRef<[f64]>> fmt::Debug for InverseCdfSampler<S>
195where
196 S: fmt::Debug,
197{
198 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199 f.debug_struct("InverseCdfSampler")
200 .field("cdf", &self.cdf)
201 .finish()
202 }
203}
204impl<S: AsRef<[f64]>> PartialEq for InverseCdfSampler<S>
205where
206 S: PartialEq,
207{
208 fn eq(&self, other: &Self) -> bool {
209 self.cdf == other.cdf
210 }
211}
212
213#[cfg(any(feature = "std", feature = "alloc"))]
215pub type WeightedIndex = InverseCdfSampler<Vec<f64>>;
216
217impl<S: AsRef<[f64]>> InverseCdfSampler<S> {
218 pub fn from_cdf(cdf: S) -> Result<Self, SamplerError> {
225 let slice = cdf.as_ref();
226 if slice.is_empty() {
227 return Err(SamplerError::Empty);
228 }
229 let mut prev = 0.0;
230 for &c in slice {
231 if !c.is_finite() || !(0.0..=1.0).contains(&c) {
232 return Err(SamplerError::InvalidCdf);
233 }
234 if c < prev {
235 return Err(SamplerError::InvalidCdf);
236 }
237 prev = c;
238 }
239 if fabs(prev - 1.0) > 1e-12 {
240 return Err(SamplerError::InvalidCdf);
241 }
242 Ok(InverseCdfSampler { cdf })
243 }
244
245 pub fn len(&self) -> usize {
247 self.cdf.as_ref().len()
248 }
249
250 pub fn is_empty(&self) -> bool {
252 self.cdf.as_ref().is_empty()
253 }
254
255 pub fn cdf(&self) -> &[f64] {
257 self.cdf.as_ref()
258 }
259
260 pub fn probability(&self, i: usize) -> f64 {
264 let cdf = self.cdf.as_ref();
265 if i >= cdf.len() {
266 return 0.0;
267 }
268 if i == 0 {
269 cdf[0]
270 } else {
271 cdf[i] - cdf[i - 1]
272 }
273 }
274
275 pub fn sample_index_from_uniform(&self, u: f64) -> usize {
279 inverse_cdf_index(self.cdf.as_ref(), u)
280 }
281}
282
283#[cfg(any(feature = "std", feature = "alloc"))]
284impl InverseCdfSampler<Vec<f64>> {
285 pub fn from_weights(weights: &[f64]) -> Result<Self, SamplerError> {
291 let total = validate_weights(weights)?;
292 let mut cdf = Vec::with_capacity(weights.len());
293 let mut acc = 0.0;
294 for &w in weights {
295 acc += w / total;
296 cdf.push(acc);
297 }
298 if let Some(last) = cdf.last_mut() {
300 *last = 1.0;
301 }
302 Ok(InverseCdfSampler { cdf })
303 }
304}
305
306impl<S: AsRef<[f64]>> Distribution<usize> for InverseCdfSampler<S> {
307 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> usize {
308 let u: f64 = Standard.sample(rng);
309 self.sample_index_from_uniform(u)
310 }
311}
312
313#[cfg(any(feature = "std", feature = "alloc"))]
324pub fn categorical(weights: &[f64]) -> Result<WeightedIndex, SamplerError> {
325 WeightedIndex::from_weights(weights)
326}
327
328pub fn sample_categorical<R: Rng + ?Sized>(weights: &[f64], rng: &mut R) -> Option<usize> {
344 let total = validate_weights(weights).ok()?;
345 let mut u = uniform_f64(rng) * total;
346 for (i, &w) in weights.iter().enumerate() {
347 if u < w {
348 return Some(i);
349 }
350 u -= w;
351 }
352 Some(weights.len() - 1)
354}
355
356#[derive(Clone, Copy, Debug, PartialEq, Eq)]
362pub enum ResampleScheme {
363 Multinomial,
365 Systematic,
368 Stratified,
370}
371
372pub struct SystematicResampler<S> {
393 sampler: InverseCdfSampler<S>,
394}
395
396impl<S: AsRef<[f64]>> Clone for SystematicResampler<S>
397where
398 S: Clone,
399{
400 fn clone(&self) -> Self {
401 SystematicResampler {
402 sampler: self.sampler.clone(),
403 }
404 }
405}
406impl<S: AsRef<[f64]>> Copy for SystematicResampler<S> where S: Copy {}
407impl<S: AsRef<[f64]>> fmt::Debug for SystematicResampler<S>
408where
409 S: fmt::Debug,
410{
411 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
412 f.debug_struct("SystematicResampler")
413 .field("sampler", &self.sampler)
414 .finish()
415 }
416}
417impl<S: AsRef<[f64]>> PartialEq for SystematicResampler<S>
418where
419 S: PartialEq,
420{
421 fn eq(&self, other: &Self) -> bool {
422 self.sampler == other.sampler
423 }
424}
425
426#[cfg(any(feature = "std", feature = "alloc"))]
427impl SystematicResampler<Vec<f64>> {
428 pub fn from_weights(weights: &[f64]) -> Result<Self, SamplerError> {
430 Ok(SystematicResampler {
431 sampler: InverseCdfSampler::from_weights(weights)?,
432 })
433 }
434}
435
436impl<S: AsRef<[f64]>> SystematicResampler<S> {
437 pub fn new(cdf: S) -> Result<Self, SamplerError> {
439 Ok(SystematicResampler {
440 sampler: InverseCdfSampler::from_cdf(cdf)?,
441 })
442 }
443
444 pub fn len(&self) -> usize {
446 self.sampler.len()
447 }
448
449 pub fn is_empty(&self) -> bool {
451 self.sampler.is_empty()
452 }
453
454 pub fn cdf(&self) -> &[f64] {
456 self.sampler.cdf()
457 }
458
459 pub fn sample_indices<R: Rng + ?Sized>(
463 &self,
464 rng: &mut R,
465 scheme: ResampleScheme,
466 out: &mut [usize],
467 ) {
468 let cdf = self.sampler.cdf();
469 let n = out.len();
470 if n == 0 {
471 return;
472 }
473 match scheme {
474 ResampleScheme::Multinomial => {
475 for slot in out.iter_mut() {
476 let u = uniform_f64(rng);
477 *slot = inverse_cdf_index(cdf, u);
478 }
479 }
480 ResampleScheme::Systematic => {
481 let u0: f64 = uniform_f64(rng) / (n as f64);
482 let mut cursor = 0usize;
483 for (j, slot) in out.iter_mut().enumerate() {
484 let u = u0 + (j as f64) / (n as f64);
485 while cursor + 1 < cdf.len() && cdf[cursor] <= u {
486 cursor += 1;
487 }
488 *slot = cursor;
489 }
490 }
491 ResampleScheme::Stratified => {
492 for (j, slot) in out.iter_mut().enumerate() {
493 let u: f64 = (uniform_f64(rng) + j as f64) / (n as f64);
494 *slot = inverse_cdf_index(cdf, u);
495 }
496 }
497 }
498 }
499}
500
501pub struct ReservoirSampler<'a, T> {
529 buf: &'a mut [T],
530 seen: u64,
531}
532
533impl<'a, T> ReservoirSampler<'a, T> {
534 pub fn new(buf: &'a mut [T]) -> Self {
536 ReservoirSampler { buf, seen: 0 }
537 }
538
539 pub fn capacity(&self) -> usize {
541 self.buf.len()
542 }
543
544 pub fn seen(&self) -> u64 {
546 self.seen
547 }
548
549 pub fn sample(&self) -> &[T] {
551 let live = self.seen as usize;
552 let n = if live < self.buf.len() {
553 live
554 } else {
555 self.buf.len()
556 };
557 &self.buf[..n]
558 }
559
560 pub fn len(&self) -> usize {
562 self.sample().len()
563 }
564
565 pub fn is_empty(&self) -> bool {
567 self.seen == 0
568 }
569
570 pub fn is_full(&self) -> bool {
572 self.seen as usize >= self.buf.len()
573 }
574
575 pub fn reset(&mut self) {
577 self.seen = 0;
578 }
579
580 pub fn offer<R: Rng + ?Sized>(&mut self, item: T, rng: &mut R) -> Option<T> {
586 let cap = self.buf.len();
587 let idx = self.seen as usize;
588 self.seen = self.seen.wrapping_add(1);
589 if idx < cap {
590 let _ = core::mem::replace(&mut self.buf[idx], item);
592 return None;
593 }
594 let j = uniform_index(rng, self.seen as usize).expect("seen > 0");
597 if j < cap {
598 Some(core::mem::replace(&mut self.buf[j], item))
599 } else {
600 Some(item)
601 }
602 }
603}
604
605pub fn uniform_index<R: Rng + ?Sized>(rng: &mut R, n: usize) -> Option<usize> {
621 if n == 0 {
622 return None;
623 }
624 if n == 1 {
625 return Some(0);
626 }
627 let span = n as u64;
628 let limit = if span.is_power_of_two() {
629 u64::MAX
630 } else {
631 (u64::MAX / span) * span
633 };
634 loop {
635 let x = rng.next_u64();
636 if x < limit {
637 return Some((x % span) as usize);
638 }
639 }
640}
641
642pub fn shuffle<R: Rng + ?Sized, T>(slice: &mut [T], rng: &mut R) {
656 let n = slice.len();
657 for i in (1..n).rev() {
658 let j = uniform_index(rng, i + 1).expect("i + 1 > 0");
659 slice.swap(i, j);
660 }
661}
662
663pub trait Density {
673 fn density(&self, x: f64) -> f64;
675}
676
677pub struct DensityFn<F>(pub F);
687
688impl<F: Fn(f64) -> f64> Density for DensityFn<F> {
689 fn density(&self, x: f64) -> f64 {
690 (self.0)(x)
691 }
692}
693
694pub struct Uniform {
699 lo: f64,
700 hi: f64,
701 width: f64,
702}
703
704impl Uniform {
705 pub fn new(lo: f64, hi: f64) -> Result<Self, SamplerError> {
710 if !lo.is_finite() || !hi.is_finite() || lo >= hi {
711 return Err(SamplerError::InvalidInterval);
712 }
713 Ok(Uniform {
714 lo,
715 hi,
716 width: hi - lo,
717 })
718 }
719
720 pub fn lo(&self) -> f64 {
722 self.lo
723 }
724
725 pub fn hi(&self) -> f64 {
727 self.hi
728 }
729}
730
731impl Distribution<f64> for Uniform {
732 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
733 let u: f64 = Standard.sample(rng);
734 self.lo + u * self.width
735 }
736}
737
738impl Density for Uniform {
739 fn density(&self, x: f64) -> f64 {
740 if x >= self.lo && x < self.hi {
741 1.0 / self.width
742 } else {
743 0.0
744 }
745 }
746}
747
748pub struct Bernoulli {
750 p: f64,
751}
752
753impl Bernoulli {
754 pub fn new(p: f64) -> Result<Self, SamplerError> {
758 if !p.is_finite() || !(0.0..=1.0).contains(&p) {
759 return Err(SamplerError::InvalidProbability);
760 }
761 Ok(Bernoulli { p })
762 }
763
764 pub fn p(&self) -> f64 {
766 self.p
767 }
768}
769
770impl Distribution<bool> for Bernoulli {
771 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> bool {
772 let u: f64 = Standard.sample(rng);
773 u < self.p
774 }
775}
776
777pub struct RejectionSampler<T, P> {
805 target: T,
806 proposal: P,
807 m: f64,
808 max_iterations: usize,
809}
810
811impl<T, P> RejectionSampler<T, P>
812where
813 T: Density,
814 P: Distribution<f64> + Density,
815{
816 pub fn new(target: T, proposal: P, m: f64) -> Result<Self, SamplerError> {
821 if !m.is_finite() || m <= 0.0 {
822 return Err(SamplerError::InvalidBound);
823 }
824 Ok(RejectionSampler {
825 target,
826 proposal,
827 m,
828 max_iterations: 1024,
829 })
830 }
831
832 pub fn target(&self) -> &T {
834 &self.target
835 }
836
837 pub fn proposal(&self) -> &P {
839 &self.proposal
840 }
841
842 pub fn m(&self) -> f64 {
844 self.m
845 }
846
847 pub fn max_iterations(mut self, n: usize) -> Self {
849 self.max_iterations = n;
850 self
851 }
852
853 pub fn acceptance_probability(&self) -> f64 {
856 1.0 / self.m
857 }
858
859 pub fn try_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Option<f64> {
862 for _ in 0..self.max_iterations {
863 let x = self.proposal.sample(rng);
864 let pd = self.proposal.density(x);
865 if pd <= 0.0 {
866 continue;
867 }
868 let td = self.target.density(x);
869 let u: f64 = Standard.sample(rng);
870 if u * self.m * pd <= td {
871 return Some(x);
872 }
873 }
874 None
875 }
876}
877
878impl<T, P> Distribution<f64> for RejectionSampler<T, P>
879where
880 T: Density,
881 P: Distribution<f64> + Density,
882{
883 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
884 self.try_sample(rng)
885 .unwrap_or_else(|| self.proposal.sample(rng))
886 }
887}
888
889pub mod prelude {
895 pub use crate::{
896 Bernoulli, Density, DensityFn, Distribution, InverseCdfSampler, RejectionSampler,
897 ReservoirSampler, Rng, Sampler, SamplerError, SystematicResampler, Uniform,
898 };
899 pub use tpt_math_prob_core::{SplitMix64, Standard};
900
901 #[cfg(any(feature = "std", feature = "alloc"))]
902 pub use crate::{categorical, WeightedIndex};
903}
904
905#[cfg(test)]
906mod tests {
907 use super::*;
908
909 #[cfg(any(feature = "std", feature = "alloc"))]
910 use alloc::vec;
911 #[cfg(any(feature = "std", feature = "alloc"))]
912 use alloc::vec::Vec;
913
914 const SEED: u64 = 0x1234_5678;
915
916 #[test]
917 fn cdf_from_weights_is_normalised() {
918 #[cfg(any(feature = "std", feature = "alloc"))]
919 {
920 let s = InverseCdfSampler::from_weights(&[1.0, 2.0, 3.0]).unwrap();
921 let cdf = s.cdf();
922 assert!((cdf.last().unwrap() - 1.0).abs() < 1e-12);
923 assert!((s.probability(0) - 1.0 / 6.0).abs() < 1e-12);
924 assert!((s.probability(1) - 2.0 / 6.0).abs() < 1e-12);
925 assert!((s.probability(2) - 3.0 / 6.0).abs() < 1e-12);
926 }
927 }
928
929 #[test]
930 fn from_cdf_rejects_bad_input() {
931 #[cfg(any(feature = "std", feature = "alloc"))]
932 {
933 assert_eq!(
934 InverseCdfSampler::from_cdf(vec![0.5, 0.4, 1.0]),
935 Err(SamplerError::InvalidCdf)
936 );
937 assert_eq!(
938 InverseCdfSampler::from_cdf(vec![0.5, 0.9]),
939 Err(SamplerError::InvalidCdf)
940 );
941 }
942 let _ = SEED;
943 }
944
945 #[test]
946 fn weights_validation_errors() {
947 #[cfg(any(feature = "std", feature = "alloc"))]
948 {
949 assert_eq!(
950 InverseCdfSampler::from_weights(&[]),
951 Err(SamplerError::Empty)
952 );
953 assert_eq!(
954 InverseCdfSampler::from_weights(&[-1.0]),
955 Err(SamplerError::NegativeWeight)
956 );
957 assert_eq!(
958 InverseCdfSampler::from_weights(&[f64::NAN]),
959 Err(SamplerError::NonFiniteWeight)
960 );
961 assert_eq!(
962 InverseCdfSampler::from_weights(&[0.0, 0.0]),
963 Err(SamplerError::ZeroTotal)
964 );
965 }
966 }
967
968 #[test]
969 fn zero_weight_never_selected() {
970 #[cfg(any(feature = "std", feature = "alloc"))]
971 {
972 let s = InverseCdfSampler::from_weights(&[1.0, 0.0, 0.0, 1.0]).unwrap();
973 let mut rng = SplitMix64::seed_from_u64(SEED);
974 for _ in 0..5000 {
975 let i = s.sample(&mut rng);
976 assert!(i == 0 || i == 3);
977 }
978 }
979 }
980
981 #[test]
982 fn sample_index_from_uniform_boundaries() {
983 #[cfg(any(feature = "std", feature = "alloc"))]
984 {
985 let s = InverseCdfSampler::from_weights(&[0.5, 0.5]).unwrap();
986 assert_eq!(s.sample_index_from_uniform(0.0), 0);
987 assert_eq!(s.sample_index_from_uniform(0.499999), 0);
988 assert_eq!(s.sample_index_from_uniform(0.5), 1);
989 assert_eq!(s.sample_index_from_uniform(0.999999), 1);
990 }
991 }
992
993 #[test]
994 fn linear_categorical_matches_alias() {
995 #[cfg(any(feature = "std", feature = "alloc"))]
996 {
997 let weights = [1.0, 3.0, 2.0, 4.0];
998 let alias = categorical(&weights).unwrap();
999 let mut rng = SplitMix64::seed_from_u64(SEED);
1000 let mut acc = [0usize; 4];
1001 for _ in 0..40000 {
1002 let a = alias.sample(&mut rng);
1003 let b = sample_categorical(&weights, &mut rng).unwrap();
1004 acc[a] += 1;
1005 acc[b] += 1;
1006 }
1007 let total = acc.iter().sum::<usize>() as f64;
1008 let sum = weights.iter().sum::<f64>();
1009 for i in 0..4 {
1010 let expected = weights[i] / sum;
1011 assert!((acc[i] as f64 / total - expected).abs() < 0.02, "bin {i}");
1012 }
1013 }
1014 }
1015
1016 #[test]
1017 fn systematic_resample_counts_bounded() {
1018 #[cfg(any(feature = "std", feature = "alloc"))]
1019 {
1020 let weights = [1.0, 1.0, 2.0];
1021 let r = SystematicResampler::from_weights(&weights).unwrap();
1022 let mut rng = SplitMix64::seed_from_u64(SEED);
1023 let n = 1000usize;
1024 let mut out = vec![0usize; n];
1025 for &scheme in &[
1027 ResampleScheme::Multinomial,
1028 ResampleScheme::Systematic,
1029 ResampleScheme::Stratified,
1030 ] {
1031 r.sample_indices(&mut rng, scheme, &mut out);
1032 assert!(out.iter().all(|&i| i < 3));
1033 let mut counts = [0usize; 3];
1035 for &i in &out {
1036 counts[i] += 1;
1037 }
1038 let total = weights.iter().sum::<f64>();
1039 let n_f = n as f64;
1040 for (i, &c) in counts.iter().enumerate() {
1041 let p = weights[i] / total;
1042 if scheme == ResampleScheme::Systematic {
1043 let lo = (n_f * p).floor() as usize;
1046 let hi = (n_f * p).ceil() as usize;
1047 assert!(
1048 c >= lo && c <= hi,
1049 "scheme {scheme:?} bin {i}: {c} not in [{lo},{hi}]"
1050 );
1051 } else {
1052 let mean = c as f64 / n_f;
1055 assert!(
1056 (mean - p).abs() < 0.05,
1057 "scheme {scheme:?} bin {i}: mean {mean} vs {p}"
1058 );
1059 }
1060 }
1061 }
1062 }
1063 }
1064
1065 #[test]
1066 fn reservoir_uniform_among_stream() {
1067 let mut rng = SplitMix64::seed_from_u64(SEED);
1068 let mut slots = [0u32; 10];
1069 let mut rs = ReservoirSampler::new(&mut slots);
1070 for x in 0u32..100 {
1071 rs.offer(x, &mut rng);
1072 }
1073 assert_eq!(rs.capacity(), 10);
1074 assert!(slots.iter().all(|&v| v < 100));
1075 assert!(slots.iter().any(|&v| v >= 90));
1076
1077 let mut small = [0u32; 5];
1079 let mut rs2 = ReservoirSampler::new(&mut small);
1080 for x in 0u32..3 {
1081 rs2.offer(x, &mut rng);
1082 }
1083 let mut collected: Vec<u32> = rs2.sample().to_vec();
1084 collected.sort_unstable();
1085 assert_eq!(collected, vec![0, 1, 2]);
1086 }
1087
1088 #[test]
1089 fn reservoir_is_fair_over_trials() {
1090 let mut rng = SplitMix64::seed_from_u64(SEED);
1091 let mut hits = [0u32; 100];
1092 for _ in 0..20000u32 {
1093 let mut slots = [0u32; 10];
1094 let mut rs = ReservoirSampler::new(&mut slots);
1095 for x in 0u32..100 {
1096 rs.offer(x, &mut rng);
1097 }
1098 for &v in rs.sample() {
1099 hits[v as usize] += 1;
1100 }
1101 }
1102 for &h in &hits {
1103 assert!((h as f64 - 2000.0).abs() < 350.0, "hit count {h}");
1105 }
1106 }
1107
1108 #[test]
1109 fn uniform_index_is_unbiased() {
1110 let mut rng = SplitMix64::seed_from_u64(SEED);
1111 let n = 6usize;
1112 let mut counts = [0u32; 6];
1113 for _ in 0..60000 {
1114 let i = uniform_index(&mut rng, n).unwrap();
1115 counts[i] += 1;
1116 }
1117 for &c in &counts {
1118 assert!((c as f64 - 10000.0).abs() < 600.0, "count {c}");
1119 }
1120 assert_eq!(uniform_index(&mut rng, 0), None);
1121 assert_eq!(uniform_index(&mut rng, 1), Some(0));
1122 }
1123
1124 #[test]
1125 fn shuffle_is_a_permutation() {
1126 let mut rng = SplitMix64::seed_from_u64(SEED);
1127 let mut v = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
1128 shuffle(&mut v, &mut rng);
1129 let mut sorted = v;
1130 sorted.sort_unstable();
1131 assert_eq!(sorted, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
1132 }
1133
1134 #[test]
1135 fn rejection_triangular_mean() {
1136 let target = DensityFn(|x: f64| {
1138 if (0.0..=1.0).contains(&x) {
1139 2.0 * x
1140 } else {
1141 0.0
1142 }
1143 });
1144 let proposal = Uniform::new(0.0, 1.0).unwrap();
1145 let rs = RejectionSampler::new(target, proposal, 2.0).unwrap();
1146 let mut rng = SplitMix64::seed_from_u64(SEED);
1147 let mut sum = 0.0;
1148 let trials = 50000u32;
1149 for _ in 0..trials {
1150 let x = rs.sample(&mut rng);
1151 assert!((0.0..=1.0).contains(&x));
1152 sum += x;
1153 }
1154 let mean = sum / trials as f64;
1155 assert!((mean - 2.0 / 3.0).abs() < 0.02, "mean {mean}");
1156 let _ = rs.acceptance_probability();
1158 }
1159
1160 #[test]
1161 fn rejection_invalid_bound() {
1162 let target = DensityFn(|_x: f64| 1.0);
1163 let proposal = Uniform::new(0.0, 1.0).unwrap();
1164 assert!(matches!(
1165 RejectionSampler::new(target, proposal, 0.0),
1166 Err(SamplerError::InvalidBound)
1167 ));
1168 }
1169
1170 #[test]
1171 fn bernoulli_mean() {
1172 let b = Bernoulli::new(0.3).unwrap();
1173 let mut rng = SplitMix64::seed_from_u64(SEED);
1174 let trials = 50000u32;
1175 let mut ones = 0u32;
1176 for _ in 0..trials {
1177 if b.sample(&mut rng) {
1178 ones += 1;
1179 }
1180 }
1181 let p = ones as f64 / trials as f64;
1182 assert!((p - 0.3).abs() < 0.02, "p {p}");
1183 }
1184
1185 #[test]
1186 fn sampler_slice_fills() {
1187 #[cfg(any(feature = "std", feature = "alloc"))]
1188 {
1189 let s = categorical(&[1.0, 1.0, 1.0, 1.0]).unwrap();
1190 let mut rng = SplitMix64::seed_from_u64(SEED);
1191 let mut buf = [0usize; 16];
1192 s.sample_slice(&mut rng, &mut buf);
1193 assert!(buf.iter().all(|&i| i < 4));
1194 }
1195 }
1196
1197 #[test]
1198 fn deterministic_with_seed() {
1199 let mut a = SplitMix64::seed_from_u64(SEED);
1200 let mut b = SplitMix64::seed_from_u64(SEED);
1201 #[cfg(any(feature = "std", feature = "alloc"))]
1202 {
1203 let s = categorical(&[1.0, 2.0, 3.0, 4.0]).unwrap();
1204 let x1 = s.sample(&mut a);
1205 let x2 = s.sample(&mut b);
1206 assert_eq!(x1, x2);
1207 }
1208 let _ = (&mut a, &mut b);
1209 }
1210}