#monadic #probability-distribution #probability

categorical

combining categorical random distributions and computing exact probabilities

4 releases

0.1.3 Sep 16, 2024
0.1.2 Sep 16, 2024
0.1.1 Sep 15, 2024
0.1.0 Sep 15, 2024

#384 in Simulation

Download history 402/week @ 2024-09-13 51/week @ 2024-09-20 4/week @ 2024-09-27 1/week @ 2024-10-04 7/week @ 2024-10-11

76 downloads per month

MIT/Apache

11KB
200 lines

categorical

This crate provides a type representing a categorical probability distribution: Categorical<T,P>. A Categorical is a collection of objects of type T, each associated with a probability of type P. You can combine two Categoricals and compute the probability of each combination (assuming the two distributions are sampled independently).

use categorical::{Categorical, CategoricalHash};
let die_roll = CategoricalHash::new_uniform(vec![1, 2, 3, 4, 5, 6].into_iter());
// roll two dice and pick the higher number
let max_of_two = CategoricalHash::combined(&die_roll, &die_roll, |&a, &b| a.max(b));
let double_wins: f64 =
    CategoricalHash::combined(&max_of_two, &die_roll, |double, single| double > single)
        .probability_of(&true);
println!("player rolling two dice rolls higher with probability of {double_wins}");

License: MIT OR Apache-2.0


lib.rs:

This crate provides a type representing a categorical probability distribution: Categorical<T,P>. A Categorical is a collection of objects of type T, each associated with a probability of type P. You can combine two Categoricals and compute the probability of each combination (assuming the two distributions are sampled independently).

use categorical::{Categorical, CategoricalHash};
let die_roll = CategoricalHash::new_uniform(vec![1, 2, 3, 4, 5, 6].into_iter());
// roll two dice and pick the higher number
let max_of_two = CategoricalHash::combined(&die_roll, &die_roll, |&a, &b| a.max(b));
let double_wins: f64 =
    CategoricalHash::combined(&max_of_two, &die_roll, |double, single| double > single)
        .probability_of(&true);
println!("player rolling two dice rolls higher with probability of {double_wins}");

Dependencies

~150KB