diff --git a/src/interval.rs b/src/interval.rs index ba0eed6..1cce356 100644 --- a/src/interval.rs +++ b/src/interval.rs @@ -1,10 +1,10 @@ //! Interval trait and structures. -pub mod lower_upper; -pub mod mean_margin; +pub mod bounded; +pub mod centered; -pub use self::lower_upper::*; -pub use self::mean_margin::*; +pub use self::bounded::*; +pub use self::centered::*; pub trait Interval { /// Returns the interval's mean. diff --git a/src/interval/lower_upper.rs b/src/interval/bounded.rs similarity index 76% rename from src/interval/lower_upper.rs rename to src/interval/bounded.rs index 8f2ea7d..a1252a3 100644 --- a/src/interval/lower_upper.rs +++ b/src/interval/bounded.rs @@ -3,18 +3,19 @@ use std::ops::{Add, Div, Mul, Sub}; use num_traits::One; -use crate::{Interval, MeanMarginInterval}; +use crate::{CenteredInterval, Interval}; /// Interval represented by its lower and upper bounds. #[must_use] #[derive(Clone)] -pub struct LowerUpperInterval { +pub struct BoundedInterval { pub(crate) lower: T, pub(crate) upper: T, } -impl + Sub + Div> Interval - for LowerUpperInterval +impl Interval for BoundedInterval +where + T: Copy + One + Add + Sub + Div, { #[inline] fn mean(&self) -> T { @@ -42,11 +43,11 @@ impl + Sub + Div> Interv } } -impl From> for LowerUpperInterval +impl From> for BoundedInterval where T: Copy + Add + Sub, { - fn from(interval: MeanMarginInterval) -> Self { + fn from(interval: CenteredInterval) -> Self { Self { lower: interval.lower(), upper: interval.upper(), @@ -54,7 +55,7 @@ where } } -impl Add for LowerUpperInterval +impl Add for BoundedInterval where T: Copy + Add, { @@ -68,7 +69,7 @@ where } } -impl Mul for LowerUpperInterval +impl Mul for BoundedInterval where T: Copy + Mul, { @@ -82,14 +83,14 @@ where } } -impl PartialEq for LowerUpperInterval { +impl PartialEq for BoundedInterval { #[must_use] fn eq(&self, _other: &Self) -> bool { false } } -impl PartialOrd for LowerUpperInterval { +impl PartialOrd for BoundedInterval { #[must_use] fn partial_cmp(&self, other: &Self) -> Option { if self.upper < other.lower { diff --git a/src/interval/mean_margin.rs b/src/interval/centered.rs similarity index 81% rename from src/interval/mean_margin.rs rename to src/interval/centered.rs index 7e587dc..1c2b7a2 100644 --- a/src/interval/mean_margin.rs +++ b/src/interval/centered.rs @@ -3,17 +3,17 @@ use std::ops::{Add, Div, Mul, Sub}; use num_traits::One; -use crate::{Interval, LowerUpperInterval}; +use crate::{BoundedInterval, Interval}; /// Represents an interval with its mean and margin. #[must_use] #[derive(Clone)] -pub struct MeanMarginInterval { +pub struct CenteredInterval { pub mean: T, pub margin: T, } -impl + Sub + Copy> Interval for MeanMarginInterval { +impl + Sub + Copy> Interval for CenteredInterval { #[inline] fn mean(&self) -> T { self.mean @@ -40,11 +40,11 @@ impl + Sub + Copy> Interval for MeanMarginInte } } -impl From> for MeanMarginInterval +impl From> for CenteredInterval where T: Copy + Add + Div + Sub + One, { - fn from(interval: LowerUpperInterval) -> Self { + fn from(interval: BoundedInterval) -> Self { Self { mean: interval.mean(), margin: interval.margin(), @@ -52,7 +52,7 @@ where } } -impl Add for MeanMarginInterval +impl Add for CenteredInterval where T: Copy + Add, { @@ -66,7 +66,7 @@ where } } -impl Mul for MeanMarginInterval +impl Mul for CenteredInterval where T: Copy + Mul, { @@ -80,14 +80,14 @@ where } } -impl PartialEq for MeanMarginInterval { +impl PartialEq for CenteredInterval { #[must_use] fn eq(&self, _other: &Self) -> bool { false } } -impl PartialOrd for MeanMarginInterval +impl PartialOrd for CenteredInterval where T: Copy + Sub + Add + PartialOrd, { diff --git a/src/math/agresti_coull.rs b/src/math/agresti_coull.rs index fb0102d..22228c3 100644 --- a/src/math/agresti_coull.rs +++ b/src/math/agresti_coull.rs @@ -2,11 +2,11 @@ use num_traits::Float; -use crate::{MeanMarginInterval, PHatSample, Sample, Wald, WilsonScore}; +use crate::{CenteredInterval, PHatSample, Sample, Wald, WilsonScore}; pub trait AgrestiCoull { /// [Agresti–Coull interval](https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Agresti%E2%80%93Coull_interval). - fn agresti_coull(&self, z_score: F) -> MeanMarginInterval; + fn agresti_coull(&self, z_score: F) -> CenteredInterval; } impl AgrestiCoull for S @@ -15,7 +15,7 @@ where N: Into, F: Float, { - fn agresti_coull(&self, z_score: F) -> MeanMarginInterval { + fn agresti_coull(&self, z_score: F) -> CenteredInterval { let p_tilde = self.wilson_score(z_score).mean; let n_tilde = self.size().into() + z_score * z_score; let corrected_sample = PHatSample { diff --git a/src/math/wald.rs b/src/math/wald.rs index 5413136..c4c70b5 100644 --- a/src/math/wald.rs +++ b/src/math/wald.rs @@ -2,11 +2,11 @@ use num_traits::Float; -use crate::{MeanMarginInterval, Sample}; +use crate::{CenteredInterval, Sample}; pub trait Wald { /// [Normal approximation interval or Wald interval](https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Normal_approximation_interval_or_Wald_interval). - fn wald(&self, z_score: F) -> MeanMarginInterval; + fn wald(&self, z_score: F) -> CenteredInterval; } impl Wald for S @@ -15,9 +15,9 @@ where N: Into, F: Float, { - fn wald(&self, z_score: F) -> MeanMarginInterval { + fn wald(&self, z_score: F) -> CenteredInterval { let p_hat = self.p_hat(); - MeanMarginInterval { + CenteredInterval { mean: p_hat, margin: z_score * (p_hat * (F::one() - p_hat) / self.size().into()).sqrt(), } diff --git a/src/math/wilson_score.rs b/src/math/wilson_score.rs index bb4062a..8e2c074 100644 --- a/src/math/wilson_score.rs +++ b/src/math/wilson_score.rs @@ -4,16 +4,16 @@ use std::ops::{Add, Div}; use num_traits::{Float, One}; -use crate::interval::{LowerUpperInterval, MeanMarginInterval}; +use crate::interval::{BoundedInterval, CenteredInterval}; use crate::Sample; pub trait WilsonScore { /// [Wilson score interval](https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval). - fn wilson_score(&self, z_level: F) -> MeanMarginInterval; + fn wilson_score(&self, z_level: F) -> CenteredInterval; /// [Wilson score interval with continuity correction](https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval_with_continuity_correction) /// as per [Wallis (2021)](https://www.routledge.com/Statistics-in-Corpus-Linguistics-Research-A-New-Approach/Wallis/p/book/9781138589384). - fn wilson_score_with_cc(&self, z_level: F) -> LowerUpperInterval; + fn wilson_score_with_cc(&self, z_level: F) -> BoundedInterval; } impl WilsonScore for S @@ -22,14 +22,14 @@ where F: Float, N: Into, { - fn wilson_score(&self, z_level: F) -> MeanMarginInterval { + fn wilson_score(&self, z_level: F) -> CenteredInterval { let (one, two, four) = one_two_four(); let sample_size = self.size().into(); let p_hat = self.p_hat(); let (a, b) = a_b(sample_size, z_level); - MeanMarginInterval { + CenteredInterval { mean: b * (p_hat + a / two), margin: z_level * b @@ -37,7 +37,7 @@ where } } - fn wilson_score_with_cc(&self, z_level: F) -> LowerUpperInterval { + fn wilson_score_with_cc(&self, z_level: F) -> BoundedInterval { let (one, two, four) = one_two_four(); let sample_size = self.size().into(); @@ -50,7 +50,7 @@ where let half_a = a / two; let quarter_sized_a = a / sample_size / four; - LowerUpperInterval { + BoundedInterval { lower: b * (lower_p_hat + half_a) - z_level * b