Random variables (RV) for rust. rv offers basic functionality for many probability distributions.
For example, if we wanted to perform a conjugate analysis of Bernoulli trials:
```rust use rv::prelude::*;
// Prior over the unknown coin weight. Assume all weights are equally // likely. let prior = Beta::uniform();
// observations generated by a fair coin
let obs_fair: Vec
// observations generated by a coin rigged to always show heads. Note that
// we're using bool
s here. Bernoulli supports mutliple types.
let obs_fixed: Vec
let datafair: BernoulliData<_> = DataOrSuffStat::Data(&obsfair); let datafixed: BernoulliData<_> = DataOrSuffStat::Data(&obsfixed);
// Let's compute the posterior predictive probability (pp) of a heads given // the observations from each coin. let postpredfair = prior.pp(&1u8, &datafair); let postpredfixed = prior.pp(&true, &datafixed);
// The probability of heads should be greater under the all heads data assert!(postpredfixed > postpredfair);
// We can also get the posteriors let postfair: Beta = prior.posterior(&datafair); let postfixed: Beta = prior.posterior(&datafixed);
// And compare their means let postmeanfair: f64 = postfair.mean().unwrap(); let postmeanfixed: f64 = postfixed.mean().unwrap();
assert!(postmeanfixed > postmeanfair); ```
Random variables are designed to be flexible. For example, we don't just want a
Beta
distribution that works with f64
; we want it to work with a bunch of
things like
```rust use rv::prelude::*;
// Beta(0.5, 0.5) let beta = Beta::jeffreys();
let mut rng = rand::thread_rng();
// 100 f64 weights in (0, 1)
let f64s: Vec
// 100 f32 weights in (0, 1)
let f32s: Vec
// 100 Bernoulli distributions -- Beta is a prior on the weight
let berns: Vec
For more interesting examples, including use in machine learning, see
examples/
.
Rv
,
Support
, and either ContinuousDistr
or DiscreteDistr
. Of course, more
is better!Rv<f64>
, also implement Rv<f32>
. Check out other
distributions to see how it can be done easily with macros.rustfmt
. We've included a .rustfmt.toml
in the project directory.