Struct stats_ci::proportion::Stats
source · pub struct Stats { /* private fields */ }Expand description
Represents the state of the computation of a confidence interval for a proportion.
Examples
let grades = [40, 59, 73, 44, 82, 44, 58, 74, 94, 79, 40, 52, 100, 57, 76, 93, 68, 96, 92, 98, 58, 64, 76, 40, 89, 65, 63, 90, 66, 89];
let stats = proportion::Stats::from_iter(grades.iter().map(|&x| x >= 60));
let confidence = Confidence::new_two_sided(0.95);
let pass_rate_ci = stats.ci(confidence)?;
println!("Pass rate: {}", pass_rate_ci);
assert_abs_diff_eq!(pass_rate_ci, Interval::new(0.4878, 0.8077)?, epsilon = 1e-3);Panics
- if the number of successes is larger than the population size
Implementations§
source§impl Stats
impl Stats
sourcepub const fn new(population: usize, successes: usize) -> Self
pub const fn new(population: usize, successes: usize) -> Self
Creates a new statistics object with initial values for the population size and the number of successes. The number of successes must not be larger than the population size.
Complexity: \( O(1) \)
Panics
- if the number of successes is larger than the population size
sourcepub fn population(&self) -> usize
pub fn population(&self) -> usize
Returns the population size (total number of samples).
Complexity: \( O(1) \)
sourcepub fn successes(&self) -> usize
pub fn successes(&self) -> usize
Returns the number of successes (number of true values found in the sample).
Complexity: \( O(1) \)
sourcepub fn add_success(&mut self)
pub fn add_success(&mut self)
Add a success to the statistics and updates the population accordingly.
Complexity: \( O(1) \)
sourcepub fn add_failure(&mut self)
pub fn add_failure(&mut self)
Add a failure to the statistics and updates the population accordingly.
Complexity: \( O(1) \)
sourcepub fn is_significant(&self) -> bool
pub fn is_significant(&self) -> bool
Tests if the conditions for the validity of the Wilson score interval are met. The conditions for the validity of the Wilson score interval are stated as follows: https://www.itl.nist.gov/div898/handbook/prc/section2/prc24.htm
- The sample size is large enough to ensure that the sampling distribution of the sample proportion is approximately normal (N > 30)
- The number of successes and failures are large enough to ensure that the sampling distribution of the sample proportion is approximately normal (x > 5 and n - x > 5)
sourcepub fn ci(&self, confidence: Confidence) -> CIResult<Interval<f64>>
pub fn ci(&self, confidence: Confidence) -> CIResult<Interval<f64>>
Computes the confidence interval over the proportion of true values in a given sample.
Complexity: \( O(1) \)
Arguments
confidence- the confidence level (must be in (0, 1))
Errors
TooFewSuccesses- if the number of successes is too small to compute a confidence intervalTooFewFailures- if the number of failures is too small to compute a confidence intervalInvalidSuccesses- if the number of successes is larger than the population sizeInvalidConfidenceLevel- if the confidence level is not in (0, 1)
Examples
let data = [
true, false, true, true, false, true, true, false, true, true,
false, false, false, true, false, true, false, false, true, false
];
let confidence = Confidence::new_two_sided(0.95);
let stats = proportion::Stats::from_iter(data);
let interval = stats.ci(confidence)?;
assert_abs_diff_eq!(interval, Interval::new(0.299, 0.701)?, epsilon = 1e-2);Notes
The confidence interval is computed using the function ci_wilson (Wilson score interval).
sourcepub fn extend<I: IntoIterator<Item = bool>>(&mut self, data: I)
pub fn extend<I: IntoIterator<Item = bool>>(&mut self, data: I)
Extend the data with additional sample data.
Complexity: \( O(n) \) where \( n \) is the number of samples in data.
Arguments
data- the sample given as a boolean iterator or slice
Examples
let data = [true, false, true, true, false, true, true, false, true, true];
let mut stats = proportion::Stats::default();
stats.extend(data);
assert_eq!(stats, proportion::Stats::new(10, 7));sourcepub fn extend_if<T, I, F>(&mut self, data: I, is_success: F)where
I: IntoIterator<Item = T>,
F: Fn(T) -> bool,
pub fn extend_if<T, I, F>(&mut self, data: I, is_success: F)where I: IntoIterator<Item = T>, F: Fn(T) -> bool,
Extend the data with additional sample data and a condition that must be satisfied to be counted as a success.
Complexity: \( O(n) \) where \( n \) is the number of samples in data.
Arguments
data- the sample given as an iterator or sliceis_success- a function that returnstrueif a sample value is a success
Examples
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let mut stats = proportion::Stats::default();
stats.extend_if(data.iter(), |&x| x <= 5);
assert_eq!(stats, proportion::Stats::new(10, 5));Trait Implementations§
source§impl Add<Stats> for Stats
impl Add<Stats> for Stats
source§fn add(self, rhs: Self) -> Self::Output
fn add(self, rhs: Self) -> Self::Output
Combines two statistics objects by adding the number of samples and the number of successes.
Complexity: \( O(1) \)
Examples
let stats1 = proportion::Stats::new(100, 50);
let stats2 = proportion::Stats::new(200, 100);
let stats = stats1 + stats2;
assert_eq!(stats, proportion::Stats::new(300, 150));source§impl AddAssign<Stats> for Stats
impl AddAssign<Stats> for Stats
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Combines two statistics objects by adding the number of samples and the number of successes.
Complexity: \( O(1) \)
Examples
let mut stats1 = proportion::Stats::new(100, 50);
let stats2 = proportion::Stats::new(200, 100);
stats1 += stats2;
assert_eq!(stats1, proportion::Stats::new(300, 150));source§impl FromIterator<bool> for Stats
impl FromIterator<bool> for Stats
source§impl PartialEq<Stats> for Stats
impl PartialEq<Stats> for Stats
impl Copy for Stats
impl Eq for Stats
impl StructuralEq for Stats
impl StructuralPartialEq for Stats
Auto Trait Implementations§
impl RefUnwindSafe for Stats
impl Send for Stats
impl Sync for Stats
impl Unpin for Stats
impl UnwindSafe for Stats
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.