# Crate porco[−][src]

Porco is a library for working with and composing probability distributions.

The API is inspired by the contents of Probabilistic Functional Programming in Haskell but with naming conventions that match those of `Option` and `Result` (such as `Option::and_then`).

```enum Coin {
Tails,
}

impl Coin {
fn flip() -> Distribution<Coin> {
}
}

let coin = Coin::flip();

You can compose various operations over `Distribution`s using combinators like `Distribution::map`, `Distribution::and_then`, and `Distribution::given`.

```fn reflip_if_tails(coin: Coin) -> Distribution<Coin> {
match coin {
Coin::Tails => Coin::flip(),
}
}

let coin = Coin::flip().and_then(reflip_if_tails);

You can also manipulate random variables and compute summary statistics.

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

fn two_sided_die() -> Distribution<u8> {
Distribution::uniform(vec![1, 2])
}

let x = two_sided_die();
let y = two_sided_die();
let sum = x.convolve(y);
assert_eq!(sum.pmf(&2), Probability(0.25));
assert_eq!(sum.pmf(&3), Probability(0.5));```

## Structs

 Distribution `Distribution` is a discrete probability distribution over the set of outcomes `T`. Probability `Probability` is a light container for probabilities.