Skip to content

Implement Distribution<u64> support for Zeta, Zipf #1516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rand_distr/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Move some of the computations in Binomial from `sample` to `new` (#1484)
- Add Kolmogorov Smirnov test for sampling of `Normal` and `Binomial` (#1494)
- Add Kolmogorov Smirnov test for more distributions (#1504)
- Add `Distribution<u64>` support for `Zeta`, `Zipf` (#1516)

### Added
- Add plots for `rand_distr` distributions to documentation (#1434)
Expand Down
12 changes: 10 additions & 2 deletions rand_distr/src/zeta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ where
}
}

impl Distribution<u64> for Zeta<f64> {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u64 {
// `as` from float to int saturates
<Zeta<f64> as Distribution<f64>>::sample(self, rng) as u64
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -163,7 +171,7 @@ mod tests {
let d = Zeta::new(a).unwrap();
let mut rng = crate::test::rng(1);
for _ in 0..1000 {
let r = d.sample(&mut rng);
let r: f64 = d.sample(&mut rng);
assert!(r >= 1.);
}
}
Expand All @@ -174,7 +182,7 @@ mod tests {
let d = Zeta::new(a).unwrap();
let mut rng = crate::test::rng(2);
for _ in 0..1000 {
let r = d.sample(&mut rng);
let r: f64 = d.sample(&mut rng);
assert!(r >= 1.);
}
}
Expand Down
16 changes: 12 additions & 4 deletions rand_distr/src/zipf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ where
}
}

impl Distribution<u64> for Zipf<f64> {
#[inline]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u64 {
// `as` from float to int saturates
<Zipf<f64> as Distribution<f64>>::sample(self, rng) as u64
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -186,7 +194,7 @@ mod tests {
let d = Zipf::new(10, 0.5).unwrap();
let mut rng = crate::test::rng(2);
for _ in 0..1000 {
let r = d.sample(&mut rng);
let r: f64 = d.sample(&mut rng);
assert!(r >= 1.);
}
}
Expand All @@ -196,7 +204,7 @@ mod tests {
let d = Zipf::new(10, 1.).unwrap();
let mut rng = crate::test::rng(2);
for _ in 0..1000 {
let r = d.sample(&mut rng);
let r: f64 = d.sample(&mut rng);
assert!(r >= 1.);
}
}
Expand All @@ -206,7 +214,7 @@ mod tests {
let d = Zipf::new(10, 0.).unwrap();
let mut rng = crate::test::rng(2);
for _ in 0..1000 {
let r = d.sample(&mut rng);
let r: f64 = d.sample(&mut rng);
assert!(r >= 1.);
}
// TODO: verify that this is a uniform distribution
Expand All @@ -217,7 +225,7 @@ mod tests {
let d = Zipf::new(u64::MAX, 1.5).unwrap();
let mut rng = crate::test::rng(2);
for _ in 0..1000 {
let r = d.sample(&mut rng);
let r: f64 = d.sample(&mut rng);
assert!(r >= 1.);
}
// TODO: verify that this is a zeta distribution
Expand Down
6 changes: 4 additions & 2 deletions rand_distr/tests/cdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ fn zeta() {

for (seed, s) in parameters.into_iter().enumerate() {
let dist = rand_distr::Zeta::new(s).unwrap();
test_discrete(seed as u64, dist, |k| cdf(k, s));
test_discrete::<f64, _, _>(seed as u64, dist, |k| cdf(k, s));
test_discrete::<u64, _, _>(seed as u64, dist, |k| cdf(k, s));
}
}

Expand All @@ -386,7 +387,8 @@ fn zipf() {

for (seed, (n, x)) in parameters.into_iter().enumerate() {
let dist = rand_distr::Zipf::new(n, x).unwrap();
test_discrete(seed as u64, dist, |k| cdf(k, n, x));
test_discrete::<f64, _, _>(seed as u64, dist, |k| cdf(k, n, x));
test_discrete::<u64, _, _>(seed as u64, dist, |k| cdf(k, n, x));
}
}

Expand Down