Crates.io | porco |
lib.rs | porco |
version | 0.1.4 |
source | src |
created_at | 2021-02-21 21:42:59.497769 |
updated_at | 2021-03-25 17:21:02.665824 |
description | Composable probability distributions |
homepage | https://github.com/mingyli/porco |
repository | https://github.com/mingyli/porco |
max_upload_size | |
id | 358666 |
size | 17,318 |
Composable probability distributions.
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);