statrs-dev / statrs

Statistical computation library for Rust
https://docs.rs/statrs/latest/statrs/
MIT License
546 stars 78 forks source link

Support sampling integers from discrete distributions #155

Open SabrinaJewson opened 2 years ago

henryjac commented 4 months ago

Very reasonable PR, adding to milestone 0.17 and we'll probably get this merged! Preliminary checks looks good to me.

vks commented 3 months ago

I would be careful with returning integers for distributions that use floats internally. For large numbers, this is lossy.

SabrinaJewson commented 3 months ago

For most the distributions, their implementations effectively use integers anyway and this PR just exposes that functionality.

The only exception is the geometric distribution:

x.log(1.0 - self.p).ceil() as u64

The largest finite value this expression can be is if x = 1.4e-45 and 1.0 - self.p = 0.9999999999999999, in which case we get 930262250532780300, which when casted to a u64 is 930262250532780288. Importantly it does stay within the range of a u64, so I would say the lossiness is acceptable here.