-
-
Notifications
You must be signed in to change notification settings - Fork 437
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
Added Multinomial distribution #1478
Open
benjamin-lieser
wants to merge
7
commits into
rust-random:master
Choose a base branch
from
benjamin-lieser:multinomial_distr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a69eac4
Added Multinomial distribution
benjamin-lieser b2a85b6
make clippy happy
benjamin-lieser f69986f
make clippy happy
benjamin-lieser 79416b4
non std issues
benjamin-lieser f1c4d96
Remove Multinomial stub and remove lifetimes from Multinomial distrib…
benjamin-lieser 0cb47ce
fixed clippy warning in test
benjamin-lieser 6c4319f
doc and nostd problem
benjamin-lieser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,253 @@ | ||
// Copyright 2018 Developers of the Rand project. | ||
// Copyright 2013 The Rust Project Developers. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! The multinomial distribution. | ||
|
||
use core::borrow::Borrow; | ||
|
||
use crate::{Binomial, Distribution}; | ||
use num_traits::AsPrimitive; | ||
use rand::Rng; | ||
|
||
/// Error type returned from `Multinomial::new`. | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
pub enum Error { | ||
/// There is a negative weight or Nan | ||
ProbabilityNegative, | ||
/// Sum overflows to inf | ||
SumOverflow, | ||
/// Sum is zero | ||
SumZero, | ||
} | ||
|
||
impl core::fmt::Display for Error { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
f.write_str(match self { | ||
Error::ProbabilityNegative => "One of the weights is negative or Nan", | ||
Error::SumOverflow => "Sum of weights overflows to inf", | ||
Error::SumZero => "Sum of weights is zero", | ||
}) | ||
} | ||
} | ||
|
||
/// The [Multinomial](https://en.wikipedia.org/wiki/Multinomial_distribution) distribution `Multinomial(n, w)` with compile time number of categories. | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct MultinomialConst<const K: usize, I> { | ||
/// number of draws | ||
n: I, | ||
/// weights for the multinomial distribution | ||
weights: [f64; K], | ||
/// sum of the weights | ||
sum: f64, | ||
} | ||
|
||
impl<const K: usize, I> MultinomialConst<K, I> { | ||
/// Constructs a new `MultinomialConst` distribution which samples from `K` categories. | ||
/// | ||
/// `n` is the number of draws. | ||
/// | ||
/// `weights` have to be non negative and will be normalized to 1. | ||
/// | ||
/// `K` has to be known at compile time | ||
pub fn new(n: I, weights: [f64; K]) -> Result<MultinomialConst<K, I>, Error> | ||
where | ||
I: num_traits::PrimInt, | ||
u64: num_traits::AsPrimitive<I>, | ||
I: num_traits::AsPrimitive<u64>, | ||
{ | ||
let all_pos = weights.iter().all(|&x| x >= 0.0); | ||
|
||
if !all_pos { | ||
return Err(Error::ProbabilityNegative); | ||
} | ||
|
||
let sum: f64 = weights.iter().sum(); | ||
|
||
if !sum.is_finite() { | ||
return Err(Error::SumOverflow); | ||
} | ||
|
||
if sum == 0.0 { | ||
return Err(Error::SumZero); | ||
} | ||
|
||
Ok(MultinomialConst::<K, I> { n, weights, sum }) | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
/// The [Multinomial](https://en.wikipedia.org/wiki/Multinomial_distribution) distribution `Multinomial(n, w)` with runtime determined number of categories. | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct MultinomialDyn<I> { | ||
/// number of draws | ||
n: I, | ||
/// weights for the multinomial distribution | ||
weights: alloc::boxed::Box<[f64]>, | ||
/// sum of the weights | ||
sum: f64, | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
impl<I> MultinomialDyn<I> { | ||
/// Constructs a new `MultinomialDyn` distribution which samples from `K` different categories. | ||
/// | ||
/// `n` is the number of draws. | ||
/// | ||
/// `weights` have to be not negative and will be normalized to 1. | ||
/// | ||
/// `K` can be specified at runtime | ||
pub fn new<F: Borrow<f64>>( | ||
n: I, | ||
weights: impl IntoIterator<Item = F>, | ||
) -> Result<MultinomialDyn<I>, Error> { | ||
let weights: alloc::boxed::Box<[f64]> = weights.into_iter().map(|x| *x.borrow()).collect(); | ||
|
||
let all_pos = weights.iter().all(|&x| x >= 0.0); | ||
|
||
if !all_pos { | ||
return Err(Error::ProbabilityNegative); | ||
} | ||
|
||
let sum: f64 = weights.iter().sum(); | ||
|
||
if !sum.is_finite() { | ||
return Err(Error::SumOverflow); | ||
} | ||
|
||
if sum == 0.0 { | ||
return Err(Error::SumZero); | ||
} | ||
|
||
Ok(MultinomialDyn::<I> { n, weights, sum }) | ||
} | ||
} | ||
|
||
/// sum has to be the sum of the weights, this is a performance optimization | ||
fn sample<R: Rng + ?Sized, I>(rng: &mut R, n: I, weights: &[f64], sum: f64, result: &mut [I]) | ||
where | ||
I: num_traits::PrimInt, | ||
u64: num_traits::AsPrimitive<I>, | ||
I: num_traits::AsPrimitive<u64>, | ||
{ | ||
// This follows the binomial approach in "The computer generation of multinomial random variates" by Charles S. Davis | ||
|
||
let mut sum_p = 0.0; | ||
let mut sum_n: I = 0.as_(); | ||
|
||
for k in 0..weights.len() { | ||
if sum - sum_p <= 0.0 { | ||
result[k] = 0.as_(); | ||
continue; | ||
} | ||
|
||
let prob = (weights[k] / (sum - sum_p)).min(1.0); | ||
let binomial = Binomial::new((n - sum_n).as_(), prob) | ||
.expect("We know that prob is between 0.0 and 1.0"); | ||
result[k] = binomial.sample(rng).as_(); | ||
sum_n = sum_n + result[k]; | ||
sum_p += weights[k]; | ||
} | ||
} | ||
|
||
impl<const K: usize, I> Distribution<[I; K]> for MultinomialConst<K, I> | ||
where | ||
I: num_traits::PrimInt, | ||
u64: num_traits::AsPrimitive<I>, | ||
I: num_traits::AsPrimitive<u64>, | ||
{ | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> [I; K] { | ||
let mut result = [0.as_(); K]; | ||
sample(rng, self.n, &self.weights, self.sum, &mut result); | ||
result | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
impl<I> Distribution<alloc::vec::Vec<I>> for MultinomialDyn<I> | ||
where | ||
I: num_traits::PrimInt, | ||
u64: num_traits::AsPrimitive<I>, | ||
I: num_traits::AsPrimitive<u64>, | ||
{ | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> alloc::vec::Vec<I> { | ||
let mut result = alloc::vec![0.as_(); self.weights.len()]; | ||
sample(rng, self.n, &self.weights, self.sum, &mut result); | ||
result | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
|
||
#[test] | ||
fn test_multinomial_const() { | ||
use super::*; | ||
|
||
let n: i32 = 1000; | ||
let weights = [0.1, 0.2, 0.3, 0.4]; | ||
let mut rng = crate::test::rng(123); | ||
let multinomial = MultinomialConst::new(n, weights).unwrap(); | ||
let sample = multinomial.sample(&mut rng); | ||
assert_eq!(sample.iter().sum::<i32>(), n); | ||
} | ||
|
||
#[test] | ||
fn test_almost_zero_dist() { | ||
use super::*; | ||
|
||
let n: i32 = 1000; | ||
let weights = [0.0, 0.0, 0.0, 0.000000001]; | ||
let multinomial = MultinomialConst::new(n, weights).unwrap(); | ||
let sample = multinomial.sample(&mut crate::test::rng(123)); | ||
assert!(sample[3] == n); | ||
} | ||
|
||
#[test] | ||
fn test_zero_dist() { | ||
use super::*; | ||
|
||
let n: i32 = 1000; | ||
let weights = [0.0, 0.0, 0.0, 0.0]; | ||
let multinomial = MultinomialConst::new(n, weights); | ||
assert_eq!(multinomial, Err(Error::SumZero)); | ||
} | ||
|
||
#[test] | ||
fn test_negative_dist() { | ||
use super::*; | ||
|
||
let n: i32 = 1000; | ||
let weights = [0.1, 0.2, 0.3, -0.6]; | ||
let multinomial = MultinomialConst::new(n, weights); | ||
assert_eq!(multinomial, Err(Error::ProbabilityNegative)); | ||
} | ||
|
||
#[test] | ||
fn test_overflow() { | ||
use super::*; | ||
|
||
let n: i32 = 1000; | ||
let weights = [f64::MAX, f64::MAX, f64::MAX, f64::MAX]; | ||
let multinomial = MultinomialConst::new(n, weights); | ||
assert_eq!(multinomial, Err(Error::SumOverflow)); | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
#[test] | ||
fn test_multinomial_dyn() { | ||
use super::*; | ||
|
||
let n = 1000; | ||
let weights = alloc::vec![0.1, 0.2, 0.3, 0.4]; | ||
let mut rng = crate::test::rng(123); | ||
let multinomial = MultinomialDyn::new(n, weights).unwrap(); | ||
let sample = multinomial.sample(&mut rng); | ||
assert_eq!(sample.iter().sum::<u64>(), n); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo here "ail" vs "ial"