Skip to content

Commit

Permalink
💥 Rename the interval structs
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Jul 29, 2022
1 parent 9758eff commit db663bd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/interval.rs
Original file line number Diff line number Diff line change
@@ -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<F> {
/// Returns the interval's mean.
Expand Down
21 changes: 11 additions & 10 deletions src/interval/lower_upper.rs → src/interval/bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
pub struct BoundedInterval<T> {
pub(crate) lower: T,
pub(crate) upper: T,
}

impl<T: Copy + One + Add<Output = T> + Sub<Output = T> + Div<Output = T>> Interval<T>
for LowerUpperInterval<T>
impl<T> Interval<T> for BoundedInterval<T>
where
T: Copy + One + Add<Output = T> + Sub<Output = T> + Div<Output = T>,
{
#[inline]
fn mean(&self) -> T {
Expand Down Expand Up @@ -42,19 +43,19 @@ impl<T: Copy + One + Add<Output = T> + Sub<Output = T> + Div<Output = T>> Interv
}
}

impl<T> From<MeanMarginInterval<T>> for LowerUpperInterval<T>
impl<T> From<CenteredInterval<T>> for BoundedInterval<T>
where
T: Copy + Add<Output = T> + Sub<Output = T>,
{
fn from(interval: MeanMarginInterval<T>) -> Self {
fn from(interval: CenteredInterval<T>) -> Self {
Self {
lower: interval.lower(),
upper: interval.upper(),
}
}
}

impl<T> Add<T> for LowerUpperInterval<T>
impl<T> Add<T> for BoundedInterval<T>
where
T: Copy + Add<Output = T>,
{
Expand All @@ -68,7 +69,7 @@ where
}
}

impl<T> Mul<T> for LowerUpperInterval<T>
impl<T> Mul<T> for BoundedInterval<T>
where
T: Copy + Mul<Output = T>,
{
Expand All @@ -82,14 +83,14 @@ where
}
}

impl<T> PartialEq for LowerUpperInterval<T> {
impl<T> PartialEq for BoundedInterval<T> {
#[must_use]
fn eq(&self, _other: &Self) -> bool {
false
}
}

impl<T: PartialOrd> PartialOrd for LowerUpperInterval<T> {
impl<T: PartialOrd> PartialOrd for BoundedInterval<T> {
#[must_use]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.upper < other.lower {
Expand Down
18 changes: 9 additions & 9 deletions src/interval/mean_margin.rs → src/interval/centered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
pub struct CenteredInterval<T> {
pub mean: T,
pub margin: T,
}

impl<T: Add<Output = T> + Sub<Output = T> + Copy> Interval<T> for MeanMarginInterval<T> {
impl<T: Add<Output = T> + Sub<Output = T> + Copy> Interval<T> for CenteredInterval<T> {
#[inline]
fn mean(&self) -> T {
self.mean
Expand All @@ -40,19 +40,19 @@ impl<T: Add<Output = T> + Sub<Output = T> + Copy> Interval<T> for MeanMarginInte
}
}

impl<T> From<LowerUpperInterval<T>> for MeanMarginInterval<T>
impl<T> From<BoundedInterval<T>> for CenteredInterval<T>
where
T: Copy + Add<Output = T> + Div<Output = T> + Sub<Output = T> + One,
{
fn from(interval: LowerUpperInterval<T>) -> Self {
fn from(interval: BoundedInterval<T>) -> Self {
Self {
mean: interval.mean(),
margin: interval.margin(),
}
}
}

impl<T> Add<T> for MeanMarginInterval<T>
impl<T> Add<T> for CenteredInterval<T>
where
T: Copy + Add<Output = T>,
{
Expand All @@ -66,7 +66,7 @@ where
}
}

impl<T> Mul<T> for MeanMarginInterval<T>
impl<T> Mul<T> for CenteredInterval<T>
where
T: Copy + Mul<Output = T>,
{
Expand All @@ -80,14 +80,14 @@ where
}
}

impl<T> PartialEq for MeanMarginInterval<T> {
impl<T> PartialEq for CenteredInterval<T> {
#[must_use]
fn eq(&self, _other: &Self) -> bool {
false
}
}

impl<T> PartialOrd for MeanMarginInterval<T>
impl<T> PartialOrd for CenteredInterval<T>
where
T: Copy + Sub<Output = T> + Add<Output = T> + PartialOrd,
{
Expand Down
6 changes: 3 additions & 3 deletions src/math/agresti_coull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, N> {
/// [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<F>;
fn agresti_coull(&self, z_score: F) -> CenteredInterval<F>;
}

impl<S, F, N> AgrestiCoull<F, N> for S
Expand All @@ -15,7 +15,7 @@ where
N: Into<F>,
F: Float,
{
fn agresti_coull(&self, z_score: F) -> MeanMarginInterval<F> {
fn agresti_coull(&self, z_score: F) -> CenteredInterval<F> {
let p_tilde = self.wilson_score(z_score).mean;
let n_tilde = self.size().into() + z_score * z_score;
let corrected_sample = PHatSample {
Expand Down
8 changes: 4 additions & 4 deletions src/math/wald.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
use num_traits::Float;

use crate::{MeanMarginInterval, Sample};
use crate::{CenteredInterval, Sample};

pub trait Wald<F, N> {
/// [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<F>;
fn wald(&self, z_score: F) -> CenteredInterval<F>;
}

impl<S, F, N> Wald<F, N> for S
Expand All @@ -15,9 +15,9 @@ where
N: Into<F>,
F: Float,
{
fn wald(&self, z_score: F) -> MeanMarginInterval<F> {
fn wald(&self, z_score: F) -> CenteredInterval<F> {
let p_hat = self.p_hat();
MeanMarginInterval {
CenteredInterval {
mean: p_hat,
margin: z_score * (p_hat * (F::one() - p_hat) / self.size().into()).sqrt(),
}
Expand Down
14 changes: 7 additions & 7 deletions src/math/wilson_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, N> {
/// [Wilson score interval](https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval).
fn wilson_score(&self, z_level: F) -> MeanMarginInterval<F>;
fn wilson_score(&self, z_level: F) -> CenteredInterval<F>;

/// [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<F>;
fn wilson_score_with_cc(&self, z_level: F) -> BoundedInterval<F>;
}

impl<S, F, N> WilsonScore<F, N> for S
Expand All @@ -22,22 +22,22 @@ where
F: Float,
N: Into<F>,
{
fn wilson_score(&self, z_level: F) -> MeanMarginInterval<F> {
fn wilson_score(&self, z_level: F) -> CenteredInterval<F> {
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
* (p_hat * (one - p_hat) / sample_size + a / sample_size / four).sqrt(),
}
}

fn wilson_score_with_cc(&self, z_level: F) -> LowerUpperInterval<F> {
fn wilson_score_with_cc(&self, z_level: F) -> BoundedInterval<F> {
let (one, two, four) = one_two_four();

let sample_size = self.size().into();
Expand All @@ -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
Expand Down

0 comments on commit db663bd

Please sign in to comment.