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

source

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
source

pub fn population(&self) -> usize

Returns the population size (total number of samples).

Complexity: \( O(1) \)

source

pub fn successes(&self) -> usize

Returns the number of successes (number of true values found in the sample).

Complexity: \( O(1) \)

source

pub fn add_success(&mut self)

Add a success to the statistics and updates the population accordingly.

Complexity: \( O(1) \)

source

pub fn add_failure(&mut self)

Add a failure to the statistics and updates the population accordingly.

Complexity: \( O(1) \)

source

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

  1. The sample size is large enough to ensure that the sampling distribution of the sample proportion is approximately normal (N > 30)
  2. 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)
source

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 interval
  • TooFewFailures - if the number of failures is too small to compute a confidence interval
  • InvalidSuccesses - if the number of successes is larger than the population size
  • InvalidConfidenceLevel - 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).

source

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

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 slice
  • is_success - a function that returns true if 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

source§

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

type Output = Stats

The resulting type after applying the + operator.
source§

impl AddAssign<Stats> for Stats

source§

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 Clone for Stats

source§

fn clone(&self) -> Stats

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Stats

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Stats

source§

fn default() -> Stats

Returns the “default value” for a type. Read more
source§

impl FromIterator<bool> for Stats

source§

fn from_iter<I>(iter: I) -> Selfwhere I: IntoIterator<Item = bool>,

Creates a new statistics object with initial values from a Boolean iterator counting the number of successes.

Complexity: \( O(n) \) where \( n \) is the number of samples in iter.

Arguments
  • iter - a Boolean iterator or slice
source§

impl PartialEq<Stats> for Stats

source§

fn eq(&self, other: &Stats) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Stats

source§

impl Eq for Stats

source§

impl StructuralEq for Stats

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T, Right> ClosedAdd<Right> for Twhere T: Add<Right, Output = T> + AddAssign<Right>,

source§

impl<T> Scalar for Twhere T: 'static + Clone + PartialEq<T> + Debug,