Crates.io | ferric-macros |
lib.rs | ferric-macros |
version | 0.1.2 |
source | src |
created_at | 2022-01-09 00:52:45.071912 |
updated_at | 2022-06-25 02:29:31.468654 |
description | A Probablistic Programming Language with a declarative syntax for random variables. |
homepage | https://ferric.ai |
repository | https://github.com/ferric-ai/ferric |
max_upload_size | |
id | 510613 |
size | 23,653 |
A Probabilistic Programming Language in Rust with a declarative syntax.
Add this to your Cargo.toml
:
[dependencies]
ferric = "0.1"
use std::time::Instant;
use ferric::make_model;
make_model! {
mod grass;
use ferric::distributions::Bernoulli;
let rain : bool ~ Bernoulli::new( 0.2 );
let sprinkler : bool ~
if rain {
Bernoulli::new( 0.01 )
} else {
Bernoulli::new( 0.4 )
};
let grass_wet : bool ~ Bernoulli::new(
if sprinkler && rain { 0.99 }
else if sprinkler && !rain { 0.9 }
else if !sprinkler && rain { 0.8 }
else { 0.0 }
);
observe grass_wet;
query rain;
query sprinkler;
}
fn main() {
let model = grass::Model {grass_wet: true};
let mut num_rain = 0;
let mut num_sprinkler = 0;
let num_samples = 100000;
let start = Instant::now();
for sample in model.sample_iter().take(num_samples) {
if sample.rain {
num_rain += 1;
}
if sample.sprinkler {
num_sprinkler += 1;
}
}
let num_samples = num_samples as f64;
println!(
"posterior: rain = {} sprinkler = {}. Elapsed {} millisec for {} samples",
(num_rain as f64) / num_samples,
(num_sprinkler as f64) / num_samples,
start.elapsed().as_millis(),
num_samples,
);
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.