porco

Crates.ioporco
lib.rsporco
version0.1.4
sourcesrc
created_at2021-02-21 21:42:59.497769
updated_at2021-03-25 17:21:02.665824
descriptionComposable probability distributions
homepagehttps://github.com/mingyli/porco
repositoryhttps://github.com/mingyli/porco
max_upload_size
id358666
size17,318
ming li (mingyli)

documentation

https://docs.rs/porco

README

porco

docs.rs

Composable probability distributions.

Examples

Create simple probability distributions.

enum Coin {
    Heads,
    Tails,
}
 
impl Coin {
    fn flip() -> Distribution<Coin> {
        Distribution::uniform([Coin::Heads, Coin::Tails])
    }
}
 
let coin = Coin::flip();
assert_eq!(coin.pmf(&Coin::Heads), Probability(0.5));

Compose operations over distributions using combinators.

fn reflip_if_tails(coin: Coin) -> Distribution<Coin> {
    match coin {
        Coin::Heads => Distribution::always(Coin::Heads),
        Coin::Tails => Coin::flip(),
    }
}
 
let coin = Coin::flip().and_then(reflip_if_tails);
assert_eq!(coin.pmf(&Coin::Heads), Probability(0.75));

Compute summary statistics of random variables.

let die = Distribution::uniform([1, 2, 3, 4, 5, 6]);
let ev = die.given(|&v| v <= 4).expectation();
assert_eq!(ev, 2.5);
Commit count: 21

cargo fmt