10 releases

0.1.0 Jul 31, 2022
0.1.0-beta.7 Jul 29, 2022
0.1.0-alpha.1 Jul 28, 2022

#1938 in Algorithms

MIT license

20KB
502 lines

bpci

Binomial proportion confidence intervals.

Crates.io Last commit GitHub Workflow Status License: MIT

Samples

By size and number of successes

use bpci::NSuccessesSample;

fn main() {
    // 10 successes out of 20 trials:
    let sample = NSuccessesSample::new(20, 10).unwrap();
}

By size and proportion

use bpci::ProportionSample;

fn main() {
    // 20 trials with 0.5 success rate:
    let sample = ProportionSample::new(20, 0.5).unwrap();
}

Intervals

Wilson score interval

use bpci::*;
use approx::assert_relative_eq;

fn main() {
    let sample = ProportionSample::new(100, 0.25).unwrap();
    let interval = sample.wilson_score(1.960); // 95%
    assert_relative_eq!(interval.lower(), 0.1754509400372426);
    assert_relative_eq!(interval.upper(), 0.3430464637007583);
}

Wilson score interval with continuity correction

use bpci::*;
use approx::assert_relative_eq;

fn main() {
    let sample = ProportionSample::new(100, 0.25).unwrap();
    let interval = sample.wilson_score_with_cc(1.960); // 95%
    assert_relative_eq!(interval.lower(), 0.17117438961361867);
    assert_relative_eq!(interval.upper(), 0.34838596518606424);
}

Agresti–Coull interval

use bpci::*;
use approx::assert_relative_eq;

fn main() {
    let sample = ProportionSample::new(40, 0.25).unwrap();
    let interval = sample.agresti_coull(1.960); // 95%
    assert_relative_eq!(interval.mean, 0.2719061348125981);
    assert_relative_eq!(interval.margin, 0.1317091851034039);
}
use bpci::*;
use approx::assert_relative_eq;

fn main() {
    let sample = NSuccessesSample::new(40, 10).unwrap();
    let interval = sample.agresti_coull(1.960); // 95%
    assert_relative_eq!(interval.mean, 0.2719061348125981);
    assert_relative_eq!(interval.margin, 0.1317091851034039);
}

Normal approximation interval or Wald interval

use bpci::*;
use approx::assert_relative_eq;

fn main() {
    let sample = ProportionSample::new(100, 0.3).unwrap();
    let interval = sample.wald(1.960); // 95%
    assert_relative_eq!(interval.lower(), 0.2101815163788655);
    assert_relative_eq!(interval.upper(), 0.38981848362113447);
}

Dependencies

~0.4–0.9MB
~19K SLoC