|
| 1 | +// Copyright 2024 Developers of the Rand project. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | +// option. This file may not be copied, modified, or distributed |
| 7 | +// except according to those terms. |
| 8 | + |
| 9 | +mod ks; |
| 10 | +use ks::test_discrete; |
| 11 | +use rand::distr::{Distribution, WeightedIndex}; |
| 12 | +use rand::seq::{IndexedRandom, IteratorRandom}; |
| 13 | +use rand_distr::{WeightedAliasIndex, WeightedTreeIndex}; |
| 14 | + |
| 15 | +/// Takes the unnormalized pdf and creates the cdf of a discrete distribution |
| 16 | +fn make_cdf(num: usize, f: impl Fn(i64) -> f64) -> impl Fn(i64) -> f64 { |
| 17 | + let mut cdf = Vec::with_capacity(num); |
| 18 | + let mut ac = 0.0; |
| 19 | + for i in 0..num { |
| 20 | + ac += f(i as i64); |
| 21 | + cdf.push(ac); |
| 22 | + } |
| 23 | + |
| 24 | + let frac = 1.0 / ac; |
| 25 | + for x in &mut cdf { |
| 26 | + *x *= frac; |
| 27 | + } |
| 28 | + |
| 29 | + move |i| { |
| 30 | + if i < 0 { |
| 31 | + 0.0 |
| 32 | + } else { |
| 33 | + cdf[i as usize] |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn weighted_index() { |
| 40 | + fn test_weights(num: usize, weight: impl Fn(i64) -> f64) { |
| 41 | + let distr = WeightedIndex::new((0..num).map(|i| weight(i as i64))).unwrap(); |
| 42 | + test_discrete(0, distr, make_cdf(num, weight)); |
| 43 | + } |
| 44 | + |
| 45 | + test_weights(100, |_| 1.0); |
| 46 | + test_weights(100, |i| ((i + 1) as f64).ln()); |
| 47 | + test_weights(100, |i| i as f64); |
| 48 | + test_weights(100, |i| (i as f64).powi(3)); |
| 49 | + test_weights(100, |i| 1.0 / ((i + 1) as f64)); |
| 50 | +} |
| 51 | + |
| 52 | +#[test] |
| 53 | +fn weighted_alias_index() { |
| 54 | + fn test_weights(num: usize, weight: impl Fn(i64) -> f64) { |
| 55 | + let weights = (0..num).map(|i| weight(i as i64)).collect(); |
| 56 | + let distr = WeightedAliasIndex::new(weights).unwrap(); |
| 57 | + test_discrete(0, distr, make_cdf(num, weight)); |
| 58 | + } |
| 59 | + |
| 60 | + test_weights(100, |_| 1.0); |
| 61 | + test_weights(100, |i| ((i + 1) as f64).ln()); |
| 62 | + test_weights(100, |i| i as f64); |
| 63 | + test_weights(100, |i| (i as f64).powi(3)); |
| 64 | + test_weights(100, |i| 1.0 / ((i + 1) as f64)); |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn weighted_tree_index() { |
| 69 | + fn test_weights(num: usize, weight: impl Fn(i64) -> f64) { |
| 70 | + let distr = WeightedTreeIndex::new((0..num).map(|i| weight(i as i64))).unwrap(); |
| 71 | + test_discrete(0, distr, make_cdf(num, weight)); |
| 72 | + } |
| 73 | + |
| 74 | + test_weights(100, |_| 1.0); |
| 75 | + test_weights(100, |i| ((i + 1) as f64).ln()); |
| 76 | + test_weights(100, |i| i as f64); |
| 77 | + test_weights(100, |i| (i as f64).powi(3)); |
| 78 | + test_weights(100, |i| 1.0 / ((i + 1) as f64)); |
| 79 | +} |
| 80 | + |
| 81 | +#[test] |
| 82 | +fn choose_weighted_indexed() { |
| 83 | + struct Adapter<F: Fn(i64) -> f64>(Vec<i64>, F); |
| 84 | + impl<F: Fn(i64) -> f64> Distribution<i64> for Adapter<F> { |
| 85 | + fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> i64 { |
| 86 | + *IndexedRandom::choose_weighted(&self.0[..], rng, |i| (self.1)(*i)).unwrap() |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fn test_weights(num: usize, weight: impl Fn(i64) -> f64) { |
| 91 | + let distr = Adapter((0..num).map(|i| i as i64).collect(), &weight); |
| 92 | + test_discrete(0, distr, make_cdf(num, &weight)); |
| 93 | + } |
| 94 | + |
| 95 | + test_weights(100, |_| 1.0); |
| 96 | + test_weights(100, |i| ((i + 1) as f64).ln()); |
| 97 | + test_weights(100, |i| i as f64); |
| 98 | + test_weights(100, |i| (i as f64).powi(3)); |
| 99 | + test_weights(100, |i| 1.0 / ((i + 1) as f64)); |
| 100 | +} |
| 101 | + |
| 102 | +#[test] |
| 103 | +fn choose_one_weighted_indexed() { |
| 104 | + struct Adapter<F: Fn(i64) -> f64>(Vec<i64>, F); |
| 105 | + impl<F: Fn(i64) -> f64> Distribution<i64> for Adapter<F> { |
| 106 | + fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> i64 { |
| 107 | + *IndexedRandom::choose_multiple_weighted(&self.0[..], rng, 1, |i| (self.1)(*i)) |
| 108 | + .unwrap() |
| 109 | + .next() |
| 110 | + .unwrap() |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + fn test_weights(num: usize, weight: impl Fn(i64) -> f64) { |
| 115 | + let distr = Adapter((0..num).map(|i| i as i64).collect(), &weight); |
| 116 | + test_discrete(0, distr, make_cdf(num, &weight)); |
| 117 | + } |
| 118 | + |
| 119 | + test_weights(100, |_| 1.0); |
| 120 | + test_weights(100, |i| ((i + 1) as f64).ln()); |
| 121 | + test_weights(100, |i| i as f64); |
| 122 | + test_weights(100, |i| (i as f64).powi(3)); |
| 123 | + test_weights(100, |i| 1.0 / ((i + 1) as f64)); |
| 124 | +} |
| 125 | + |
| 126 | +#[test] |
| 127 | +fn choose_two_weighted_indexed() { |
| 128 | + struct Adapter<F: Fn(i64) -> f64>(Vec<i64>, F); |
| 129 | + impl<F: Fn(i64) -> f64> Distribution<i64> for Adapter<F> { |
| 130 | + fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> i64 { |
| 131 | + let mut iter = |
| 132 | + IndexedRandom::choose_multiple_weighted(&self.0[..], rng, 2, |i| (self.1)(*i)) |
| 133 | + .unwrap(); |
| 134 | + let mut a = *iter.next().unwrap(); |
| 135 | + let mut b = *iter.next().unwrap(); |
| 136 | + assert!(iter.next().is_none()); |
| 137 | + if b < a { |
| 138 | + std::mem::swap(&mut a, &mut b); |
| 139 | + } |
| 140 | + a * self.0.len() as i64 + b |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + fn test_weights(num: usize, weight: impl Fn(i64) -> f64) { |
| 145 | + let distr = Adapter((0..num).map(|i| i as i64).collect(), &weight); |
| 146 | + |
| 147 | + let pmf1 = (0..num).map(|i| weight(i as i64)).collect::<Vec<f64>>(); |
| 148 | + let sum: f64 = pmf1.iter().sum(); |
| 149 | + let frac = 1.0 / sum; |
| 150 | + |
| 151 | + let mut ac = 0.0; |
| 152 | + let mut cdf = Vec::with_capacity(num * num); |
| 153 | + for a in 0..num { |
| 154 | + for b in 0..num { |
| 155 | + if a < b { |
| 156 | + let pa = pmf1[a] * frac; |
| 157 | + let pab = pa * pmf1[b] / (sum - pmf1[a]); |
| 158 | + |
| 159 | + let pb = pmf1[b] * frac; |
| 160 | + let pba = pb * pmf1[a] / (sum - pmf1[b]); |
| 161 | + |
| 162 | + ac += pab + pba; |
| 163 | + } |
| 164 | + cdf.push(ac); |
| 165 | + } |
| 166 | + } |
| 167 | + assert!((cdf.last().unwrap() - 1.0).abs() < 1e-9); |
| 168 | + |
| 169 | + let cdf = |i| { |
| 170 | + if i < 0 { |
| 171 | + 0.0 |
| 172 | + } else { |
| 173 | + cdf[i as usize] |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + test_discrete(0, distr, cdf); |
| 178 | + } |
| 179 | + |
| 180 | + test_weights(100, |_| 1.0); |
| 181 | + test_weights(100, |i| ((i + 1) as f64).ln()); |
| 182 | + test_weights(100, |i| i as f64); |
| 183 | + test_weights(100, |i| (i as f64).powi(3)); |
| 184 | + test_weights(100, |i| 1.0 / ((i + 1) as f64)); |
| 185 | + test_weights(10, |i| ((i + 1) as f64).powi(-8)); |
| 186 | +} |
| 187 | + |
| 188 | +#[test] |
| 189 | +fn choose_iterator() { |
| 190 | + struct Adapter<I>(I); |
| 191 | + impl<I: Clone + Iterator<Item = i64>> Distribution<i64> for Adapter<I> { |
| 192 | + fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> i64 { |
| 193 | + IteratorRandom::choose(self.0.clone(), rng).unwrap() |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + let distr = Adapter((0..100).map(|i| i as i64)); |
| 198 | + test_discrete(0, distr, make_cdf(100, |_| 1.0)); |
| 199 | +} |
| 200 | + |
| 201 | +#[test] |
| 202 | +fn choose_stable_iterator() { |
| 203 | + struct Adapter<I>(I); |
| 204 | + impl<I: Clone + Iterator<Item = i64>> Distribution<i64> for Adapter<I> { |
| 205 | + fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> i64 { |
| 206 | + IteratorRandom::choose_stable(self.0.clone(), rng).unwrap() |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + let distr = Adapter((0..100).map(|i| i as i64)); |
| 211 | + test_discrete(0, distr, make_cdf(100, |_| 1.0)); |
| 212 | +} |
| 213 | + |
| 214 | +#[test] |
| 215 | +fn choose_two_iterator() { |
| 216 | + struct Adapter<I>(I); |
| 217 | + impl<I: Clone + Iterator<Item = i64>> Distribution<i64> for Adapter<I> { |
| 218 | + fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> i64 { |
| 219 | + let mut buf = [0; 2]; |
| 220 | + IteratorRandom::choose_multiple_fill(self.0.clone(), rng, &mut buf); |
| 221 | + buf.sort_unstable(); |
| 222 | + assert!(buf[0] < 99 && buf[1] >= 1); |
| 223 | + let a = buf[0]; |
| 224 | + 4950 - (99 - a) * (100 - a) / 2 + buf[1] - a - 1 |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + let distr = Adapter((0..100).map(|i| i as i64)); |
| 229 | + |
| 230 | + test_discrete( |
| 231 | + 0, |
| 232 | + distr, |
| 233 | + |i| if i < 0 { 0.0 } else { (i + 1) as f64 / 4950.0 }, |
| 234 | + ); |
| 235 | +} |
0 commit comments