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: usizeImplementations§
Source§impl<F: Float + FromPrimitive> MeanTest<F>
impl<F: Float + FromPrimitive> MeanTest<F>
Sourcepub fn from_absolute_accuracy(
null_mean: F,
accuracy: f64,
confidence_level: f64,
) -> Self
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
- 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.
- For small samples (n < 15), the discreteness dominates — consider exhaustive enumeration of all 2ⁿ permutations instead of sampling.
- 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).
- “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).
Sourcepub fn zero(accuracy: f64) -> Selfwhere
F: FromPrimitive,
pub fn zero(accuracy: f64) -> Selfwhere
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% accuracyTrait Implementations§
Source§impl<D, F> Statistic<D, TestResult<F>> for MeanTest<F>
impl<D, F> Statistic<D, TestResult<F>> for MeanTest<F>
fn compute(&self, data: &D) -> TestResult<F>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.