Skip to main content

MeanTest

Struct MeanTest 

Source
pub struct MeanTest<F> {
    pub null_mean: F,
    pub n_permutations: usize,
}
Expand description

Permutation test for the hypothesis about the population mean.

Tests the null hypothesis: H₀: μ = μ₀ without assuming normality of the distribution. Uses the sign-flipping (random sign inversion) method on centered data.

§Statistical assumptions

  • Assumes: i.i.d. sample, existence of the population mean
  • Does not require: normality, symmetry, or finite variance
  • Test type: two-sided (tests μ ≠ μ₀)

§Example

use your_crate::MeanTest;

let data = vec![0.5, -1.2, 0.8, 1.5, -0.3];
let test = MeanTest::<f64>::zero(0.01); // accuracy ±0.01
let result = test.compute(&data);

println!("p-value: {:.4}", result.p_value);

Fields§

§null_mean: F§n_permutations: usize

Implementations§

Source§

impl<F: Float + FromPrimitive> MeanTest<F>

Source

pub fn new(null_mean: F, n_permutations: usize) -> Self

Creates a test with an explicitly specified number of permutations.

§Arguments
  • null_mean — the hypothesized mean μ₀ under H₀
  • n_permutations — number of resamples to approximate the null distribution
§Panics

Panics if n_permutations == 0.

Source

pub fn from_absolute_accuracy( null_mean: F, accuracy: f64, confidence_level: f64, ) -> Self

Creates a test with a desired absolute accuracy for the p-value estimate.

§Statistical guarantee

Guarantees that the width of the (1−α) confidence interval for the estimated p-value does not exceed 2·accuracy in the worst case (when the true p-value ≈ 0.5), assuming:

  • The permutation distribution is well-approximated by independent sampling
  • The normal approximation to the binomial distribution is adequate (typically satisfied when n_permutations > 30)
§Important caveats
  1. This controls sampling error from approximating the permutation distribution, NOT the inherent discreteness of the exact permutation test. The exact test has only 2ⁿ unique sign-flipping configurations.
  2. For small samples (n < 15), the discreteness dominates — consider exhaustive enumeration of all 2ⁿ permutations instead of sampling.
  3. The guarantee is conservative: actual accuracy is substantially better when the true p-value is far from 0.5 (e.g., p < 0.1 or p > 0.9).
  4. “Accuracy ±0.01” refers to estimation error of p-value, NOT error in hypothesis decision (Type I/II error rates remain unaffected).
§Formula

Uses the conservative sample size formula for binomial proportion estimation:

n_permutations = ceil( (z_{1−α/2}² · 0.25) / accuracy² )

where 0.25 is the maximum variance of a Bernoulli variable (at p = 0.5).

§Arguments
  • null_mean — hypothesized mean under H₀
  • accuracy — half-width of the target confidence interval (e.g., 0.01)
  • confidence_level — confidence level for the interval (e.g., 0.95)
§Panics

Panics if accuracy ∉ (0, 0.5) or confidence_level ∉ (0.5, 1.0).

Source

pub fn zero(accuracy: f64) -> Self
where F: FromPrimitive,

Tests the hypothesis H₀: μ = 0 with a specified absolute accuracy.

Uses the standard 95% confidence level.

§Arguments
  • accuracy — absolute error tolerance for the p-value estimate
§Example accuracy levels
  • accuracy = 0.02 → 2,401 permutations (exploratory analysis)
  • accuracy = 0.01 → 9,604 permutations (standard testing)
  • accuracy = 0.005 → 38,416 permutations (publication-ready)
  • accuracy = 0.001 → 960,385 permutations (critical decisions)
§Example
let test = MeanTest::<f64>::zero(0.01); // ±1% accuracy

Trait Implementations§

Source§

impl<F: Clone> Clone for MeanTest<F>

Source§

fn clone(&self) -> MeanTest<F>

Returns a duplicate 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<F: Debug> Debug for MeanTest<F>

Source§

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

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

impl<D, F> Statistic<D, TestResult<F>> for MeanTest<F>

Source§

fn compute(&self, data: &D) -> TestResult<F>

Source§

impl<F: Copy> Copy for MeanTest<F>

Auto Trait Implementations§

§

impl<F> Freeze for MeanTest<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for MeanTest<F>
where F: RefUnwindSafe,

§

impl<F> Send for MeanTest<F>
where F: Send,

§

impl<F> Sync for MeanTest<F>
where F: Sync,

§

impl<F> Unpin for MeanTest<F>
where F: Unpin,

§

impl<F> UnwindSafe for MeanTest<F>
where F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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

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

fn is_in_subset(&self) -> bool

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

fn to_subset_unchecked(&self) -> SS

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

fn from_subset(element: &SS) -> SP

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

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

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

Source§

fn vzip(self) -> V