1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
//!
//! Confidence intervals over the mean (arithmetic, geometric, harmonic) of a given sample.
//!
//! The premise on which confidence intervals are computed is that the sample data is a random
//! sample from a population following some (unknown) distribution. The confidence interval
//! is computed from the sample data, and is an estimate of the true mean of the population.
//!
//! Unlike what is sometimes stated, the population does not need to be normally distributed.
//! However, it is assumed that the __standard error__ of the sample mean is normally distributed
//! (or close to it).
//! This is true for most distributions (especially symmetrical ones), and is guaranteed by
//! the central limit theorem as the size of the sample data grows large.
//!
//! The calculations use Student's t distribution almost regardless of sample size (until
//! a size of 100'000). This provides more conservative (and accurate intervals) than the
//! normal distribution when the number of samples is small, and asymptotically approaches
//! the normal distribution as the number of samples increases. This compensates for the
//! fact that the central limit theorem applies only asymptotically.
//!
//! # Assumptions
//!
//! The following assumptions are made:
//!
//! * The sample data is a random sample from a population following some (unknown) distribution.
//! * The sample data is independent and identically distributed (iid).
//! * The standard approaches a normal distribution.
//! * For geometric / harmonic means, the sample data is strictly positive.
//!
//! # Examples
//!
//! Confidence intervals on the arithmetic mean of a sample:
//! ```
//! use stats_ci::*;
//! let data = [
//! 82., 94., 68., 6., 39., 80., 10., 97., 34., 66., 62., 7., 39., 68., 93., 64., 10., 74.,
//! 15., 34., 4., 48., 88., 94., 17., 99., 81., 37., 68., 66., 40., 23., 67., 72., 63.,
//! 71., 18., 51., 65., 87., 12., 44., 89., 67., 28., 86., 62., 22., 90., 18., 50., 25.,
//! 98., 24., 61., 62., 86., 100., 96., 27., 36., 82., 90., 55., 26., 38., 97., 73., 16.,
//! 49., 23., 26., 55., 26., 3., 23., 47., 27., 58., 27., 97., 32., 29., 56., 28., 23.,
//! 37., 72., 62., 77., 63., 100., 40., 84., 77., 39., 71., 61., 17., 77.,
//! ];
//! let confidence = Confidence::new_two_sided(0.95);
//! let stats = mean::Arithmetic::from_iter(data)?;
//! // reference values computed in python / numpy
//! use approx::*;
//! assert_abs_diff_eq!(stats.sample_mean(), 53.67, epsilon = 1e-6);
//! assert_abs_diff_eq!(stats.sample_std_dev(), 28.097613040716798, epsilon = 1e-6);
//! assert_abs_diff_eq!(stats.ci_mean(confidence)?, Interval::new(48.094823990767836, 59.24517600923217)?, epsilon = 1e-6);
//! # Ok::<(),error::CIError>(())
//! ```
//!
//! Confidence intervals on the geometric mean of a sample:
//! ```
//! # use stats_ci::*;
//! # let data = [
//! # 82., 94., 68., 6., 39., 80., 10., 97., 34., 66., 62., 7., 39., 68., 93., 64., 10., 74.,
//! # 15., 34., 4., 48., 88., 94., 17., 99., 81., 37., 68., 66., 40., 23., 67., 72., 63.,
//! # 71., 18., 51., 65., 87., 12., 44., 89., 67., 28., 86., 62., 22., 90., 18., 50., 25.,
//! # 98., 24., 61., 62., 86., 100., 96., 27., 36., 82., 90., 55., 26., 38., 97., 73., 16.,
//! # 49., 23., 26., 55., 26., 3., 23., 47., 27., 58., 27., 97., 32., 29., 56., 28., 23.,
//! # 37., 72., 62., 77., 63., 100., 40., 84., 77., 39., 71., 61., 17., 77.,
//! # ];
//! # let confidence = Confidence::new_two_sided(0.95);
//! let stats = mean::Geometric::from_iter(data)?;
//! // reference values computed in python / numpy
//! use approx::*;
//! assert_abs_diff_eq!(stats.sample_mean(), 43.7268032829256, epsilon = 1e-6);
//! assert_abs_diff_eq!(stats.ci_mean(confidence)?, Interval::new(37.731050052224354, 50.67532768627392)?, epsilon = 1e-6);
//! # Ok::<(),error::CIError>(())
//! ```
//!
//! Confidence intervals on the harmonic mean of a sample:
//! ```
//! # use stats_ci::*;
//! # let data = [
//! # 82., 94., 68., 6., 39., 80., 10., 97., 34., 66., 62., 7., 39., 68., 93., 64., 10., 74.,
//! # 15., 34., 4., 48., 88., 94., 17., 99., 81., 37., 68., 66., 40., 23., 67., 72., 63.,
//! # 71., 18., 51., 65., 87., 12., 44., 89., 67., 28., 86., 62., 22., 90., 18., 50., 25.,
//! # 98., 24., 61., 62., 86., 100., 96., 27., 36., 82., 90., 55., 26., 38., 97., 73., 16.,
//! # 49., 23., 26., 55., 26., 3., 23., 47., 27., 58., 27., 97., 32., 29., 56., 28., 23.,
//! # 37., 72., 62., 77., 63., 100., 40., 84., 77., 39., 71., 61., 17., 77.,
//! # ];
//! # let confidence = Confidence::new_two_sided(0.95);
//! let stats = mean::Harmonic::from_iter(data)?;
//! // reference values computed in python / numpy
//! use approx::*;
//! assert_abs_diff_eq!(stats.sample_mean(), 30.031313156339586, epsilon = 1e-6);
//! assert_abs_diff_eq!(stats.ci_mean(confidence)?, Interval::new(23.614092539657168, 41.237860649168255)?, epsilon = 1e-6);
//! # Ok::<(),error::CIError>(())
//! ```
//!
use super::*;
use crate::utils;
use error::*;
use num_traits::Float;
///
/// Trait for incremental statistics.
/// This trait is implemented for the following statistics:
/// - [`mean::Arithmetic`] for arithmetic calculations
/// - [`mean::Geometric`] for geometric calculations (logarithmic space)
/// - [`mean::Harmonic`] for 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)?;
/// # use approx::*;
/// assert_abs_diff_eq!(ci, Interval::new(3.3341, 7.6659)?, epsilon = 1e-4);
/// # Ok::<(),error::CIError>(())
/// ```
pub trait StatisticsOps<F: Float>: Default {
///
/// 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 approx::*;
/// 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);
/// # Ok::<(),error::CIError>(())
/// ```
///
/// # Note
///
/// This is simply a shortcut for [`Default::default`] and [`Self::extend`]:
/// ```
/// # use stats_ci::*;
/// # let data = [1., 2., 3., 4., 5., 6., 7., 8., 9., 10.];
/// let stats = mean::Arithmetic::new().extend(data)?;
/// # Ok::<(),error::CIError>(())
/// ```
///
fn from_iter<I: IntoIterator<Item = F>>(data: I) -> CIResult<Self> {
let mut state = Self::default();
state.extend(data)?;
Ok(state)
}
///
/// Mean of the sample
///
/// Complexity: \\( O(1) \\)
///
fn sample_mean(&self) -> F;
///
/// Standard error of the sample mean
///
/// Complexity: \\( O(1) \\)
///
fn sample_sem(&self) -> F;
///
/// Number of samples
///
/// Complexity: \\( O(1) \\)
///
fn sample_count(&self) -> usize;
///
/// Confidence interval of the sample mean
///
/// Complexity: \\( O(1) \\)
///
fn ci_mean(&self, confidence: Confidence) -> CIResult<Interval<F>>;
///
/// Append a new sample to the data
///
/// Complexity: \\( O(1) \\)
///
fn append(&mut self, x: F) -> 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).
///
fn extend<I: IntoIterator<Item = F>>(&mut self, data: I) -> CIResult<()> {
for x_i in data {
self.append(x_i)?;
}
Ok(())
}
}
///
/// Represents the state of the computation of the arithmetic mean.
/// This is a simple implementation that accumulates information about the samples, such as sum and sum of squares.
///
/// It is best used through the [`StatisticsOps`] trait.
///
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Arithmetic<F: Float> {
sum: utils::KahanSum<F>,
sum_sq: utils::KahanSum<F>,
count: usize,
}
impl<F: Float> Default for Arithmetic<F> {
fn default() -> Self {
Self {
sum: utils::KahanSum::default(),
sum_sq: utils::KahanSum::default(),
count: 0,
}
}
}
impl<F: Float> Arithmetic<F> {
///
/// Create a new empty state
///
/// # Example
/// ```
/// use stats_ci::*;
/// let mut stats = mean::Arithmetic::new();
/// stats.append(10.);
/// assert_eq!(stats.sample_count(), 1);
/// assert_eq!(stats.sample_mean(), 10.);
/// ```
///
pub fn new() -> Self {
Default::default()
}
///
/// Variance of the sample
/// \\( \frac{1}{n-1}\left(\sum_{i=1}^n x_i^2 - \frac{1}{n} \left(\sum_{i=1}^n x_i\right)^2 \right) \\)
///
/// Complexity: \\( O(1) \\)
///
pub fn sample_variance(&self) -> F {
let mean = self.sample_mean();
(self.sum_sq.value() - mean * self.sum.value()) / F::from(self.count - 1).unwrap()
}
///
/// Standard deviation of the sample
///
/// Complexity: \\( O(1) \\)
///
pub fn sample_std_dev(&self) -> F {
self.sample_variance().sqrt()
}
}
impl<F: Float> StatisticsOps<F> for Arithmetic<F> {
fn append(&mut self, x: F) -> CIResult<()> {
self.sum += x;
self.sum_sq += x * x;
self.count += 1;
Ok(())
}
fn sample_mean(&self) -> F {
self.sum.value() / F::from(self.count).unwrap()
}
fn sample_sem(&self) -> F {
self.sample_std_dev() / F::from(self.count - 1).unwrap().sqrt()
}
fn ci_mean(&self, confidence: Confidence) -> CIResult<Interval<F>> {
let n = self.count as f64;
let mean = self.sample_mean().try_f64("stats.mean")?;
let std_dev = self.sample_std_dev().try_f64("stats.std_dev")?;
let std_err_mean = std_dev / n.sqrt();
let degrees_of_freedom = n - 1.;
let (lo, hi) = stats::interval_bounds(confidence, mean, std_err_mean, degrees_of_freedom);
let (lo, hi) = (F::from(lo).convert("lo")?, F::from(hi).convert("hi")?);
match confidence {
Confidence::TwoSided(_) => Interval::new(lo, hi).map_err(|e| e.into()),
Confidence::UpperOneSided(_) => Ok(Interval::new_upper(lo)),
Confidence::LowerOneSided(_) => Ok(Interval::new_lower(hi)),
}
}
fn sample_count(&self) -> usize {
self.count
}
}
impl<F: Float> std::ops::Add<Self> for Arithmetic<F> {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let mut sum = self.sum;
let mut sum_sq = self.sum_sq;
sum += rhs.sum;
sum_sq += rhs.sum_sq;
let count = self.count + rhs.count;
Self { sum, sum_sq, count }
}
}
///
/// Represents the state of the computation related to the harmonic mean.
/// This is a simple implementation that accumulates information about the samples, such as sum and sum of squares.
/// It is implemented as a wrapper around [`Arithmetic`] to compute the arithmetic mean of the reciprocals of the samples.
///
/// It is best used through the [`StatisticsOps`] trait.
///
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Harmonic<F: Float> {
recip_space: Arithmetic<F>,
}
impl<F: Float> Harmonic<F> {
///
/// Create a new empty state
///
/// # Example
/// ```
/// use stats_ci::*;
/// let mut stats = mean::Harmonic::new();
/// stats.append(10.);
/// assert_eq!(stats.sample_count(), 1);
/// assert_eq!(stats.sample_mean(), 10.);
/// ```
///
pub fn new() -> Self {
Default::default()
}
}
impl<F: Float> Default for Harmonic<F> {
fn default() -> Self {
Self {
recip_space: Arithmetic::default(),
}
}
}
impl<F: Float> StatisticsOps<F> for Harmonic<F> {
fn append(&mut self, x: F) -> CIResult<()> {
if x <= F::zero() {
return Err(error::CIError::NonPositiveValue(
x.to_f64().unwrap_or(f64::NAN),
));
}
self.recip_space.append(F::one() / x)?;
Ok(())
}
///
/// Harmonic mean of the sample
/// \\( H = \left( \frac{1}{n} \sum_i \frac{1}{x_i} \right)^{-1} \\)
///
/// Complexity: \\( O(1) \\)
///
fn sample_mean(&self) -> F {
F::one() / self.recip_space.sample_mean()
}
///
/// Standard error of the harmonic mean
/// \\( s_H = \frac{1}{\alpha^2} \frac{s_{1/x_i}}{\sqrt{n-1}} \\)
///
/// where
/// * the estimate of \\( \alpha \\) is given by \\( \alpha = \frac{1}{n} \sum_i 1/x_i \\);
/// * \\( s_{1/x_i} \\) is the estimate of the standard deviation of the reciprocals of the samples;
/// * and \\( n-1 \\) is the degree of freedom of the sample data.
///
/// # Reference
///
/// * Nilan Noris. "The standard errors of the geometric and harmonic means and their application to index numbers." Ann. Math. Statist. 11(4): 445-448 (December, 1940). DOI: [10.1214/aoms/1177731830](https://doi.org/10.1214/aoms/1177731830) [JSTOR](https://www.jstor.org/stable/2235727)
///
fn sample_sem(&self) -> F {
let harm_mean = self.sample_mean();
let recip_std_dev = self.recip_space.sample_std_dev();
harm_mean * harm_mean * recip_std_dev
/ F::from(self.recip_space.sample_count() - 1).unwrap().sqrt()
}
fn sample_count(&self) -> usize {
self.recip_space.sample_count()
}
///
/// Confidence interval for the harmonic mean
///
fn ci_mean(&self, confidence: Confidence) -> CIResult<Interval<F>> {
let arith_ci = self.recip_space.ci_mean(confidence.flipped())?;
let (lo, hi) = (F::one() / arith_ci.high_f(), F::one() / arith_ci.low_f());
match confidence {
Confidence::TwoSided(_) => Interval::new(lo, hi).map_err(|e| e.into()),
Confidence::UpperOneSided(_) => Ok(Interval::new_upper(lo)),
Confidence::LowerOneSided(_) => Ok(Interval::new_lower(hi)),
}
}
}
impl<F: Float> std::ops::Add<Self> for Harmonic<F> {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
recip_space: self.recip_space + rhs.recip_space,
}
}
}
///
/// Represents the state of the computation of the geometric mean.
/// This is a simple implementation that accumulates information about the samples, such as sum and sum of squares.
/// It is implemented as a wrapper around [`Arithmetic`] to compute the arithmetic mean of the logarithms of the samples.
///
/// It is best used through the [`StatisticsOps`] trait.
///
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Geometric<F: Float> {
log_space: Arithmetic<F>,
}
impl<F: Float> Geometric<F> {
///
/// Create a new empty state
///
/// # Example
/// ```
/// # use stats_ci::*;
/// # use approx::*;
/// let mut stats = mean::Geometric::new();
/// stats.append(10.)?;
/// assert_eq!(stats.sample_count(), 1);
/// assert_abs_diff_eq!(stats.sample_mean(), 10., epsilon = 1e-10);
/// # Ok::<(),error::CIError>(())
/// ```
///
pub fn new() -> Self {
Default::default()
}
}
impl<F: Float> Default for Geometric<F> {
fn default() -> Self {
Self {
log_space: Arithmetic::default(),
}
}
}
impl<F: Float> StatisticsOps<F> for Geometric<F> {
fn append(&mut self, x: F) -> CIResult<()> {
if x <= F::zero() {
return Err(error::CIError::NonPositiveValue(
x.to_f64().unwrap_or(f64::NAN),
));
}
self.log_space.append(x.ln())?;
Ok(())
}
///
/// Geometric mean of the sample
///
fn sample_mean(&self) -> F {
self.log_space.sample_mean().exp()
}
///
/// Standard error of the geometric mean
///
/// Computed as: \\( G \frac{s_{\log x_i}}{\sqrt{n-1}} \\)
/// where \\( G \\) is the geometric mean of the sample;
/// \\( s_{\log x_i} \\) is the estimate of the standard deviation of the logarithms of the samples;
/// and \\( n-1 \\) is the degree of freedom of the sample data.
///
/// # Reference
///
/// * Nilan Noris. "The standard errors of the geometric and harmonic means and their application to index numbers." Ann. Math. Statist. 11(4): 445-448 (December, 1940). DOI: [10.1214/aoms/1177731830](https://doi.org/10.1214/aoms/1177731830) [JSTOR](https://www.jstor.org/stable/2235727)
///
fn sample_sem(&self) -> F {
let geom_mean = self.sample_mean();
let log_std_dev = self.log_space.sample_std_dev();
geom_mean * log_std_dev / F::from(self.log_space.sample_count() - 1).unwrap().sqrt()
}
fn sample_count(&self) -> usize {
self.log_space.sample_count()
}
///
/// Confidence interval for the geometric mean
///
fn ci_mean(&self, confidence: Confidence) -> CIResult<Interval<F>> {
let arith_ci = self.log_space.ci_mean(confidence)?;
let (lo, hi) = (arith_ci.low_f().exp(), arith_ci.high_f().exp());
match confidence {
Confidence::TwoSided(_) => Interval::new(lo, hi).map_err(|e| e.into()),
Confidence::UpperOneSided(_) => Ok(Interval::new_upper(lo)),
Confidence::LowerOneSided(_) => Ok(Interval::new_lower(hi)),
}
}
}
impl<F: Float> std::ops::Add<Self> for Geometric<F> {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self {
log_space: self.log_space + rhs.log_space,
}
}
}
///
/// Trait for computing confidence intervals on the mean of a sample.
///
/// It is superceded by the [`StatisticsOps`] trait which allows incremental statistics.
/// It is retained for backwards compatibility and will be deprecated in the future, as
/// it brings no advantage over [`StatisticsOps`] and is less flexible.
///
/// # Examples
///
/// ```
/// use stats_ci::*;
/// let data = [
/// 82., 94., 68., 6., 39., 80., 10., 97., 34., 66., 62., 7., 39., 68., 93., 64., 10., 74.,
/// 15., 34., 4., 48., 88., 94., 17., 99., 81., 37., 68., 66., 40., 23., 67., 72., 63.,
/// 71., 18., 51., 65., 87., 12., 44., 89., 67., 28., 86., 62., 22., 90., 18., 50., 25.,
/// 98., 24., 61., 62., 86., 100., 96., 27., 36., 82., 90., 55., 26., 38., 97., 73., 16.,
/// 49., 23., 26., 55., 26., 3., 23., 47., 27., 58., 27., 97., 32., 29., 56., 28., 23.,
/// 37., 72., 62., 77., 63., 100., 40., 84., 77., 39., 71., 61., 17., 77.,
/// ];
/// let confidence = Confidence::new_two_sided(0.95);
/// let ci = mean::Arithmetic::ci(confidence, data)?;
/// // arithmetic mean: 52.5
///
/// use num_traits::Float;
/// use approx::*;
/// assert_abs_diff_eq!(ci, Interval::new(48.094823990767836, 59.24517600923217)?, epsilon = 1e-3);
/// # Ok::<(),error::CIError>(())
/// ```
///
pub trait MeanCI<T: PartialOrd> {
///
/// Compute the confidence interval on the mean of a sample
///
/// # Arguments
///
/// * `confidence` - The confidence level of the interval
/// * `data` - 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 interval
/// * [`CIError::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
///
fn ci<I>(confidence: Confidence, data: I) -> CIResult<Interval<T>>
where
I: IntoIterator<Item = T>;
}
impl<F: Float, T: StatisticsOps<F>> MeanCI<F> for T {
fn ci<I>(confidence: Confidence, data: I) -> CIResult<Interval<F>>
where
I: IntoIterator<Item = F>,
{
Self::from_iter(data)?.ci_mean(confidence)
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::*;
#[test]
fn test_mean_ci() -> CIResult<()> {
let data = [
82., 94., 68., 6., 39., 80., 10., 97., 34., 66., 62., 7., 39., 68., 93., 64., 10., 74.,
15., 34., 4., 48., 88., 94., 17., 99., 81., 37., 68., 66., 40., 23., 67., 72., 63.,
71., 18., 51., 65., 87., 12., 44., 89., 67., 28., 86., 62., 22., 90., 18., 50., 25.,
98., 24., 61., 62., 86., 100., 96., 27., 36., 82., 90., 55., 26., 38., 97., 73., 16.,
49., 23., 26., 55., 26., 3., 23., 47., 27., 58., 27., 97., 32., 29., 56., 28., 23.,
37., 72., 62., 77., 63., 100., 40., 84., 77., 39., 71., 61., 17., 77.,
];
let confidence = Confidence::new_two_sided(0.95);
let ci = Arithmetic::ci(confidence, data)?;
// mean: 53.67
// stddev: 28.097613040716798
// reference values computed in python
// [48.094823990767836, 59.24517600923217]
// ```python
// import numpy as np
// import scipy.stats as st
// st.t.interval(confidence=0.95, df=len(data)-1, loc=np.mean(data), scale=st.sem(data))
// ```
assert_abs_diff_eq!(ci.low_f(), 48.094823990767836, epsilon = 1e-8);
assert_abs_diff_eq!(ci.high_f(), 59.24517600923217, epsilon = 1e-8);
assert_abs_diff_eq!(ci.low_f() + ci.high_f(), 2. * 53.67);
let one_sided_ci = Arithmetic::ci(Confidence::UpperOneSided(0.975), data)?;
assert_abs_diff_eq!(one_sided_ci.low_f(), ci.low_f());
assert_eq!(one_sided_ci.high_f(), f64::INFINITY);
let one_sided_ci = Arithmetic::ci(Confidence::LowerOneSided(0.975), data)?;
assert_abs_diff_eq!(one_sided_ci.high_f(), ci.high_f());
assert_eq!(one_sided_ci.low_f(), f64::NEG_INFINITY);
let mut state = Arithmetic::default();
state.extend(data.iter().copied())?;
let ci = state.ci_mean(confidence)?;
assert_abs_diff_eq!(ci.low_f(), 48.094823990767836, epsilon = 1e-8);
assert_abs_diff_eq!(ci.high_f(), 59.24517600923217, epsilon = 1e-8);
assert_abs_diff_eq!(ci.low_f() + ci.high_f(), 2. * 53.67, epsilon = 1e-8);
let one_sided_ci = state.ci_mean(Confidence::UpperOneSided(0.975))?;
assert_abs_diff_eq!(one_sided_ci.low_f(), ci.low_f());
assert_eq!(one_sided_ci.high_f(), f64::INFINITY);
let one_sided_ci = state.ci_mean(Confidence::LowerOneSided(0.975))?;
assert_abs_diff_eq!(one_sided_ci.high_f(), ci.high_f());
assert_eq!(one_sided_ci.low_f(), f64::NEG_INFINITY);
Ok(())
}
#[test]
fn test_geometric_ci() -> CIResult<()> {
let data = [
82., 94., 68., 6., 39., 80., 10., 97., 34., 66., 62., 7., 39., 68., 93., 64., 10., 74.,
15., 34., 4., 48., 88., 94., 17., 99., 81., 37., 68., 66., 40., 23., 67., 72., 63.,
71., 18., 51., 65., 87., 12., 44., 89., 67., 28., 86., 62., 22., 90., 18., 50., 25.,
98., 24., 61., 62., 86., 100., 96., 27., 36., 82., 90., 55., 26., 38., 97., 73., 16.,
49., 23., 26., 55., 26., 3., 23., 47., 27., 58., 27., 97., 32., 29., 56., 28., 23.,
37., 72., 62., 77., 63., 100., 40., 84., 77., 39., 71., 61., 17., 77.,
];
let confidence = Confidence::new_two_sided(0.95);
let ci = Geometric::ci(confidence, data)?;
// geometric mean: 43.7268032829256
//
// reference values computed in python:
// in log space: (3.630483364286656, 3.9254391587458475)
// [37.731050052224354, 50.67532768627392]
assert_abs_diff_eq!(ci.low_f(), 37.731050052224354, epsilon = 1e-8);
assert_abs_diff_eq!(ci.high_f(), 50.67532768627392, epsilon = 1e-8);
let one_sided_ci = Geometric::ci(Confidence::UpperOneSided(0.975), data)?;
assert_abs_diff_eq!(one_sided_ci.low_f(), ci.low_f());
assert_eq!(one_sided_ci.high_f(), f64::INFINITY);
let one_sided_ci = Geometric::ci(Confidence::LowerOneSided(0.975), data)?;
assert_abs_diff_eq!(one_sided_ci.high_f(), ci.high_f());
assert_eq!(one_sided_ci.low_f(), f64::NEG_INFINITY);
let mut state = Geometric::default();
state.extend(data.iter().copied())?;
let ci = state.ci_mean(confidence)?;
assert_abs_diff_eq!(state.sample_mean(), 43.7268032829256, epsilon = 1e-8);
assert_abs_diff_eq!(ci.low_f(), 37.731050052224354, epsilon = 1e-8);
assert_abs_diff_eq!(ci.high_f(), 50.67532768627392, epsilon = 1e-8);
let one_sided_ci = state.ci_mean(Confidence::UpperOneSided(0.975))?;
assert_abs_diff_eq!(one_sided_ci.low_f(), ci.low_f());
assert_eq!(one_sided_ci.high_f(), f64::INFINITY);
let one_sided_ci = state.ci_mean(Confidence::LowerOneSided(0.975))?;
assert_abs_diff_eq!(one_sided_ci.high_f(), ci.high_f());
assert_eq!(one_sided_ci.low_f(), f64::NEG_INFINITY);
Ok(())
}
#[test]
fn test_harmonic_ci() -> CIResult<()> {
let data = [
82., 94., 68., 6., 39., 80., 10., 97., 34., 66., 62., 7., 39., 68., 93., 64., 10., 74.,
15., 34., 4., 48., 88., 94., 17., 99., 81., 37., 68., 66., 40., 23., 67., 72., 63.,
71., 18., 51., 65., 87., 12., 44., 89., 67., 28., 86., 62., 22., 90., 18., 50., 25.,
98., 24., 61., 62., 86., 100., 96., 27., 36., 82., 90., 55., 26., 38., 97., 73., 16.,
49., 23., 26., 55., 26., 3., 23., 47., 27., 58., 27., 97., 32., 29., 56., 28., 23.,
37., 72., 62., 77., 63., 100., 40., 84., 77., 39., 71., 61., 17., 77.,
];
let confidence = Confidence::new_two_sided(0.95);
let ci = Harmonic::ci(confidence, data)?;
// harmonic mean: 30.031313156339586
//
// reference values computed in python:
// in reciprocal space: (0.02424956057996111, 0.042347593849757906)
// [41.237860649168255, 23.614092539657168] (reversed by conversion from reciprocal space)
assert_abs_diff_eq!(ci.low_f(), 23.614092539657168, epsilon = 1e-8);
assert_abs_diff_eq!(ci.high_f(), 41.237860649168255, epsilon = 1e-8);
let one_sided_ci = Harmonic::ci(Confidence::UpperOneSided(0.975), data)?;
assert_abs_diff_eq!(one_sided_ci.low_f(), ci.low_f());
assert_eq!(one_sided_ci.high_f(), f64::INFINITY);
let one_sided_ci = Harmonic::ci(Confidence::LowerOneSided(0.975), data)?;
assert_abs_diff_eq!(one_sided_ci.high_f(), ci.high_f());
assert_eq!(one_sided_ci.low_f(), f64::NEG_INFINITY);
let confidence = Confidence::new_two_sided(0.95);
let data = [
1.81600583, 0.07498389, 1.29092744, 0.62023863, 0.09345327, 1.94670997, 2.27687339,
0.9251231, 1.78173864, 0.4391542, 1.36948099, 1.5191194, 0.42286756, 1.48463176,
0.17621009, 2.31810064, 0.15633061, 2.55137878, 1.11043948, 1.35923319, 1.58385561,
0.63431437, 0.49993148, 0.49168534, 0.11533354,
];
let ci = Harmonic::ci(confidence, data)?;
// harmonic mean: 0.38041820166550844
//
// reference values computed in python:
// in reciprocal space: (1.1735238063066096, 4.083848080632111)
// [0.8521343961033607, 0.2448670911003175]
assert_abs_diff_eq!(ci.low_f(), 0.2448670911003175, epsilon = 1e-6);
assert_abs_diff_eq!(ci.high_f(), 0.8521343961033607, epsilon = 1e-6);
let mut state = Harmonic::default();
state.extend(data.iter().copied())?;
let ci = state.ci_mean(confidence)?;
assert_abs_diff_eq!(state.sample_mean(), 0.38041820166550844, epsilon = 1e-8);
assert_abs_diff_eq!(ci.low_f(), 0.2448670911003175, epsilon = 1e-6);
assert_abs_diff_eq!(ci.high_f(), 0.8521343961033607, epsilon = 1e-6);
Ok(())
}
#[test]
fn test_stats_sum() {
const VALUE: f32 = 0.1;
let size = 1_000_000;
let mut stats_ref = Arithmetic::default();
let mut stats_summed = Arithmetic::default();
for _ in 0..size {
stats_ref.append(VALUE).unwrap();
let mut new_stat = Arithmetic::default();
new_stat.append(VALUE).unwrap();
stats_summed = stats_summed + new_stat;
}
assert_eq!(stats_ref.sample_count(), size);
assert_eq!(stats_summed.sample_count(), size);
assert_eq!(stats_ref.sample_mean(), stats_summed.sample_mean());
assert_eq!(stats_ref.sample_variance(), stats_summed.sample_variance());
assert_eq!(stats_ref.sample_std_dev(), stats_summed.sample_std_dev());
assert_eq!(stats_ref.sample_sem(), stats_summed.sample_sem());
}
#[test]
fn test_blah() -> CIResult<()> {
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);
Ok(())
}
}