use ferric::make_model;
#[test]
fn congestion() {
make_model! {
name congestion;
use ferric::distributions::Bernoulli;
use ferric::distributions::Poisson;
let num_packets : u64 ~ Poisson::new( 5.0 );
let congested : bool ~ Bernoulli::new(
if num_packets > 8 { 0.9 } else { 0.1 }
);
observe congested;
query num_packets;
};
let model = congestion::Model { congested: true };
let num_samples = 200000;
let mut packet_vals = Vec::with_capacity(num_samples);
let mut log_weights = Vec::with_capacity(num_samples);
for ws in model.weighted_sample_iter().take(num_samples) {
packet_vals.push(ws.sample.num_packets as f64);
log_weights.push(ws.log_weight);
}
let post_mean = ferric::weighted_mean(&packet_vals, &log_weights);
println!("posterior num_packets mean = {}", post_mean);
assert!(
post_mean > 5.5 && post_mean < 10.0,
"posterior mean {} outside expected range (5.5, 10)",
post_mean
);
}