rv
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:
use *;
// Prior over the unknown coin weight. Assume all weights are equally
// likely.
let prior = 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 data_fair: = Data;
let data_fixed: = Data;
// Let's compute the posterior predictive probability (pp) of a heads given
// the observations from each coin.
let postpred_fair = prior.pp;
let postpred_fixed = prior.pp;
// The probability of heads should be greater under the all heads data
assert!;
// We can also get the posteriors
let post_fair: Beta = prior.posterior;
let post_fixed: Beta = prior.posterior;
// And compare their means
let post_mean_fair: f64 = post_fair.mean.unwrap;
let post_mean_fixed: f64 = post_fixed.mean.unwrap;
assert!;
Design
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
use *;
// Beta(0.5, 0.5)
let beta = jeffreys;
let mut rng = thread_rng;
// 100 f64 weights in (0, 1)
let f64s: = beta.sample;
let pdf_x = beta.ln_pdf;
// 100 f32 weights in (0, 1)
let f32s: = beta.sample;
let pdf_y = beta.ln_pdf;
// 100 Bernoulli distributions -- Beta is a prior on the weight
let berns: = beta.sample;
let pdf_bern = beta.ln_pdf;
For more interesting examples, including use in machine learning, see
examples/
.
Contributing
- Please create an issue before starting any work. We're far from stable, so we might actually be working on what you want, or we might be working on something that will change the way you might implement it.
- If you plan on implementing a new distribution, implement at least
Rv
,Support
, and eitherContinuousDistr
orDiscreteDistr
. Of course, more is better! - Implement new distributions for the appropriate types. For example, don't
just implement
Rv<f64>
, also implementRv<f32>
. Check out other distributions to see how it can be done easily with macros. - Write tests, docs, and doc tests.
- Use
rustfmt
. We've included a.rustfmt.toml
in the project directory.