This is a unofficial rewrite of Allen B. Downey's ThinkBayes in to Rust.
This project has two goals:
- To be a clean port of ThinkBayes.py; so that the book can easily be followed in Rust code.
- To be Rusty, demonstrating and using good Rust practise.
Issues, Pull Requests, comments, spelling corrections, and questions all welcome.
These goals do not always align, but we will jump off that bridge when we come to it. So here goes:
Code from this section ended up as tests in src/pmf.rs
Code from this section (cookie.py) ended up in tests/cookie.rs
Rust dose not (yet 1.9.0) have class inheritance. Rust has traits or composition for code reuse. tests/cookie2.rs demonstrates using composition to extend Pmf.
test/monty.rs also uses composition.
The "Template method pattern" and "abstract type" are perfect for trait inheritance. So we have a trait in std/suite.rs that need:
fn likelihood(&self, data: &D, hypo: &V) -> f64;
fn get_mut_pmf(&mut self) -> &mut Pmf<V>;
And uses them to provide default implementation of:
fn update(&mut self, data: &D);
Code from this section (monty2.py) ended up in tests/monty2.rs
Code from this section (m_and_m.py) ended up in tests/m_and_m.rs
Code from this section (dice.py) ended up in tests/dice.rs
Code from this section (train.py) ended up in tests/train.rs
Code from this section is in tests/train2.rs. This is the code from the book, not from train3.py.
Code from this section is in tests/euro.rs. This is the code from the book, not from euro.py.
This will wrap a beta distribution from probability. But for now it is on the to do list.
Code from this section is in tests/dungeons.rs.
random_max
, sample_max
that are in the book are so slow that Prf. Downey
dose not include them in thinkbayes.py. So I do not as well. The rest of this section is in tests/dungeons.rs.
Rust float types do not impl Eq and Hash because floating point is hard. This intern means that Cdf and Pmf can not impl Eq and Hash. If you think this a technicality having to do with Nan, note that 0.3 + 0.1 + 0.2 <> 0.3 + 0.2 + 0.1
. So if we have a s: pdf = {0.1: 1, 0.2: 1, 0.3:1}
than s + s + s
will have key a for 0.3 + 0.1 + 0.2
and a separate key 0.3 + 0.2 + 0.1
. That is just asking for trouble.
This means that make_mixture
can not take Pmf<Pmf<V>>
as MakeMixture
dose in python. So the rust version takes an Iterator or (&Pmf<V>, f64)
Code from this section is in tests/dungeons.rs.
- Go through the book.
- Wrap distributions from probability.
- Be more consistent on use of &V vs. V us as_ref where possible.
- Clean up formatting of doc strings.