Trait stats_ci::mean::StatisticsOps
source · pub trait StatisticsOps<F: Float>: Default {
// Required methods
fn sample_mean(&self) -> F;
fn sample_sem(&self) -> F;
fn sample_count(&self) -> usize;
fn ci_mean(&self, confidence: Confidence) -> CIResult<Interval<F>>;
fn append(&mut self, x: F) -> CIResult<()>;
fn ci<I>(confidence: Confidence, data: I) -> CIResult<Interval<F>>
where I: IntoIterator<Item = F>;
// Provided methods
fn from_iter<I: IntoIterator<Item = F>>(data: I) -> CIResult<Self> { ... }
fn extend<I: IntoIterator<Item = F>>(&mut self, data: I) -> CIResult<()> { ... }
}Expand description
Trait for incremental statistics. This trait is implemented for the following statistics:
mean::Arithmeticfor arithmetic calculationsmean::Geometricfor geometric calculations (logarithmic space)mean::Harmonicfor harmonic calculations (reciprocal space)
Example
use stats_ci::*;
let data = [1., 2., 3., 4., 5., 6., 7., 8., 9., 10.];
let stats = mean::Arithmetic::from_iter(data)?;
assert_eq!(stats.sample_count(), 10);
assert_eq!(stats.sample_mean(), 5.5);
assert_abs_diff_eq!(stats.sample_sem(), 1.0092, epsilon = 1e-4);
let confidence = Confidence::new_two_sided(0.95);
let ci = stats.ci_mean(confidence)?;
assert_abs_diff_eq!(ci, Interval::new(3.3341, 7.6659)?, epsilon = 1e-4);Required Methods§
sourcefn sample_mean(&self) -> F
fn sample_mean(&self) -> F
Mean of the sample
Complexity: \( O(1) \)
sourcefn sample_sem(&self) -> F
fn sample_sem(&self) -> F
Standard error of the sample mean
Complexity: \( O(1) \)
sourcefn sample_count(&self) -> usize
fn sample_count(&self) -> usize
Number of samples
Complexity: \( O(1) \)
sourcefn ci_mean(&self, confidence: Confidence) -> CIResult<Interval<F>>
fn ci_mean(&self, confidence: Confidence) -> CIResult<Interval<F>>
Confidence interval of the sample mean
Complexity: \( O(1) \)
sourcefn append(&mut self, x: F) -> CIResult<()>
fn append(&mut self, x: F) -> CIResult<()>
Append a new sample to the data
Complexity: \( O(1) \)
sourcefn ci<I>(confidence: Confidence, data: I) -> CIResult<Interval<F>>where
I: IntoIterator<Item = F>,
fn ci<I>(confidence: Confidence, data: I) -> CIResult<Interval<F>>where I: IntoIterator<Item = F>,
Compute the confidence interval on the mean of a sample
Arguments
confidence- The confidence level of the intervaldata- The data to compute the confidence interval on
Output
Ok(interval)- The confidence interval on the mean of the sample
Errors
CIError::TooFewSamples- If the input data has too few samples to compute the confidence intervalCIError::NonPositiveValue- If the input data contains non-positive values when computing harmonic/geometric means.CIError::InvalidInputData- If the input data contains invalid values (e.g. NaN)CIError::FloatConversionError- If some data cannot be converted to a float
Provided Methods§
sourcefn from_iter<I: IntoIterator<Item = F>>(data: I) -> CIResult<Self>
fn from_iter<I: IntoIterator<Item = F>>(data: I) -> CIResult<Self>
Create a new state and “populates” it with data from an iterator
Complexity: \( O(n) \), where \( n \) is the number of elements in data
Arguments
data- The data to populate the state with
Errors
CIError::NonPositiveValue- If the input data contains non-positive values when computing harmonic/geometric means.
Example
use stats_ci::*;
let data = [1., 2., 3., 4., 5., 6., 7., 8., 9., 10.];
let stats = mean::Arithmetic::from_iter(data)?;
assert_eq!(stats.sample_count(), 10);
assert_eq!(stats.sample_mean(), 5.5);
assert_abs_diff_eq!(stats.sample_sem(), 1.0092, epsilon = 1e-4);Note
This is simply a shortcut for Default::default and Self::extend:
let stats = mean::Arithmetic::new().extend(data)?;sourcefn extend<I: IntoIterator<Item = F>>(&mut self, data: I) -> CIResult<()>
fn extend<I: IntoIterator<Item = F>>(&mut self, data: I) -> CIResult<()>
Extend the data with additional sample data.
This is equivalent to calling Self::append for each value in data.
Complexity: \( O(n) \), where \( n \) is the number of elements in data
Arguments
data- The data to append as an array or an iterator
Output
Ok(())- If the data was successfully appended
Errors
CIError::NonPositiveValue- If the input data is invalid (for harmonic/geometric means).