10个版本

0.1.0 2022年7月31日
0.1.0-beta.72022年7月29日
0.1.0-alpha.12022年7月28日

#1319 in 算法

每月23次下载

MIT许可证

20KB
502

bpci

二项比例置信区间。

Crates.io Last commit GitHub Workflow Status License: MIT

示例

按大小和成功次数

use bpci::NSuccessesSample;

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

按大小和比例

use bpci::ProportionSample;

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

区间

威尔逊得分区间

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);
}

带有连续性校正的威尔逊得分区间

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区间

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);
}

正态近似区间或Wald区间

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);
}

依赖项

~0.4–1MB
~21K SLoC