anofox-statistics 0.4.2

A comprehensive statistical hypothesis testing library validated against R
Documentation
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
//! TOST equivalence tests for means using t-tests.
//!
//! Provides one-sample, two-sample (independent), and paired t-test based TOST.

use crate::equivalence::{EquivalenceBounds, OneSidedTestResult, TostResult};
use crate::error::{Result, StatError};
use crate::utils::math::{mean, variance};
use statrs::distribution::{ContinuousCDF, StudentsT};

/// Perform one-sample TOST to test if a mean is equivalent to a specified value.
///
/// Tests whether the population mean is practically equivalent to `mu` within
/// the specified equivalence bounds.
///
/// # Arguments
/// * `x` - Sample data
/// * `mu` - Value to test equivalence against (usually 0)
/// * `bounds` - Equivalence bounds specification
/// * `alpha` - Significance level (default: 0.05)
///
/// # Returns
/// * `TostResult` containing test statistics, p-values, and equivalence conclusion
///
/// # Example
/// ```
/// use anofox_statistics::equivalence::{tost_t_test_one_sample, EquivalenceBounds};
///
/// let x = vec![0.1, -0.2, 0.3, 0.0, -0.1, 0.2, -0.15, 0.05];
/// let bounds = EquivalenceBounds::Symmetric { delta: 0.5 };
///
/// let result = tost_t_test_one_sample(&x, 0.0, &bounds, 0.05).unwrap();
/// println!("Equivalent: {}", result.equivalent);
/// ```
///
/// # R equivalent
/// `TOSTER::TOSTone(m = mean(x), mu = mu, sd = sd(x), n = length(x), low_eqbound = -delta, high_eqbound = delta)`
pub fn tost_t_test_one_sample(
    x: &[f64],
    mu: f64,
    bounds: &EquivalenceBounds,
    alpha: f64,
) -> Result<TostResult> {
    validate_alpha(alpha)?;

    let n = x.len();
    if n < 2 {
        return Err(StatError::InsufficientData { needed: 2, got: n });
    }

    let mean_x = mean(x)?;
    let var_x = variance(x)?;
    let sd_x = var_x.sqrt();
    let se = sd_x / (n as f64).sqrt();
    let df = (n - 1) as f64;

    // Convert bounds to raw units
    let (lower_bound, upper_bound) = bounds.to_raw(Some(sd_x))?;

    // Estimate: difference from mu
    let estimate = mean_x - mu;

    // t-distribution for inference
    let t_dist = StudentsT::new(0.0, 1.0, df).map_err(|e| {
        StatError::InvalidParameter(format!("Failed to create t-distribution: {}", e))
    })?;

    // Lower test: H0: estimate <= lower_bound (effect too negative)
    // Reject if estimate significantly greater than lower_bound
    let t_lower = (estimate - lower_bound) / se;
    let p_lower = t_dist.sf(t_lower);

    // Upper test: H0: estimate >= upper_bound (effect too positive)
    // Reject if estimate significantly less than upper_bound
    let t_upper = (estimate - upper_bound) / se;
    let p_upper = t_dist.cdf(t_upper);

    // TOST p-value is the maximum of the two one-sided p-values
    let tost_p = p_lower.max(p_upper);

    // Confidence interval at (1 - 2*alpha) level for TOST
    let t_crit = t_dist.inverse_cdf(1.0 - alpha);
    let margin = t_crit * se;
    let ci = (estimate - margin, estimate + margin);

    // Equivalence is established if CI falls entirely within bounds
    let equivalent = ci.0 >= lower_bound && ci.1 <= upper_bound;

    Ok(TostResult {
        estimate,
        ci,
        bounds: (lower_bound, upper_bound),
        lower_test: OneSidedTestResult {
            hypothesis: format!("H0: effect <= {:.4}", lower_bound),
            statistic: t_lower,
            p_value: p_lower,
            rejected: p_lower < alpha,
        },
        upper_test: OneSidedTestResult {
            hypothesis: format!("H0: effect >= {:.4}", upper_bound),
            statistic: t_upper,
            p_value: p_upper,
            rejected: p_upper < alpha,
        },
        tost_p_value: tost_p,
        equivalent,
        alpha,
        n,
        df: Some(df),
        method: "One-sample TOST".to_string(),
    })
}

/// Perform two-sample TOST to test if two means are equivalent.
///
/// Uses Welch's t-test (unequal variances) to test whether the difference
/// in population means is practically equivalent to zero.
///
/// # Arguments
/// * `x` - First sample
/// * `y` - Second sample
/// * `bounds` - Equivalence bounds specification
/// * `alpha` - Significance level (default: 0.05)
/// * `pooled` - If true, use pooled variance (Student's t); if false, use Welch's t (default)
///
/// # Returns
/// * `TostResult` containing test statistics, p-values, and equivalence conclusion
///
/// # Example
/// ```
/// use anofox_statistics::equivalence::{tost_t_test_two_sample, EquivalenceBounds};
///
/// let x = vec![10.1, 9.8, 10.2, 10.0, 9.9];
/// let y = vec![10.0, 10.1, 9.9, 10.2, 10.0];
/// let bounds = EquivalenceBounds::Symmetric { delta: 0.5 };
///
/// let result = tost_t_test_two_sample(&x, &y, &bounds, 0.05, false).unwrap();
/// println!("Groups equivalent: {}", result.equivalent);
/// ```
///
/// # R equivalent
/// `TOSTER::TOSTtwo(m1, m2, sd1, sd2, n1, n2, low_eqbound_d, high_eqbound_d)`
pub fn tost_t_test_two_sample(
    x: &[f64],
    y: &[f64],
    bounds: &EquivalenceBounds,
    alpha: f64,
    pooled: bool,
) -> Result<TostResult> {
    validate_alpha(alpha)?;

    let nx = x.len();
    let ny = y.len();

    if nx < 2 {
        return Err(StatError::InsufficientData { needed: 2, got: nx });
    }
    if ny < 2 {
        return Err(StatError::InsufficientData { needed: 2, got: ny });
    }

    let mean_x = mean(x)?;
    let mean_y = mean(y)?;
    let var_x = variance(x)?;
    let var_y = variance(y)?;

    let nx_f = nx as f64;
    let ny_f = ny as f64;

    // Compute pooled standard deviation for Cohen's d conversion
    let pooled_var = ((nx_f - 1.0) * var_x + (ny_f - 1.0) * var_y) / (nx_f + ny_f - 2.0);
    let pooled_sd = pooled_var.sqrt();

    // Convert bounds to raw units using pooled SD
    let (lower_bound, upper_bound) = bounds.to_raw(Some(pooled_sd))?;

    // Estimate: difference in means
    let estimate = mean_x - mean_y;

    // Compute SE and df based on whether we use pooled or Welch
    let (se, df) = if pooled {
        // Student's t-test (pooled variance)
        let se = (pooled_var * (1.0 / nx_f + 1.0 / ny_f)).sqrt();
        let df = nx_f + ny_f - 2.0;
        (se, df)
    } else {
        // Welch's t-test (unequal variances)
        let se_x = var_x / nx_f;
        let se_y = var_y / ny_f;
        let se = (se_x + se_y).sqrt();

        // Welch-Satterthwaite degrees of freedom
        let num = (se_x + se_y).powi(2);
        let denom = (se_x.powi(2) / (nx_f - 1.0)) + (se_y.powi(2) / (ny_f - 1.0));
        let df = num / denom;
        (se, df)
    };

    // t-distribution for inference
    let t_dist = StudentsT::new(0.0, 1.0, df).map_err(|e| {
        StatError::InvalidParameter(format!("Failed to create t-distribution: {}", e))
    })?;

    // Lower test: H0: estimate <= lower_bound
    let t_lower = (estimate - lower_bound) / se;
    let p_lower = t_dist.sf(t_lower);

    // Upper test: H0: estimate >= upper_bound
    let t_upper = (estimate - upper_bound) / se;
    let p_upper = t_dist.cdf(t_upper);

    // TOST p-value
    let tost_p = p_lower.max(p_upper);

    // (1 - 2*alpha) confidence interval
    let t_crit = t_dist.inverse_cdf(1.0 - alpha);
    let margin = t_crit * se;
    let ci = (estimate - margin, estimate + margin);

    // Equivalence established if CI within bounds
    let equivalent = ci.0 >= lower_bound && ci.1 <= upper_bound;

    let method = if pooled {
        "Two-sample TOST (Student's t)"
    } else {
        "Two-sample TOST (Welch's t)"
    };

    Ok(TostResult {
        estimate,
        ci,
        bounds: (lower_bound, upper_bound),
        lower_test: OneSidedTestResult {
            hypothesis: format!("H0: effect <= {:.4}", lower_bound),
            statistic: t_lower,
            p_value: p_lower,
            rejected: p_lower < alpha,
        },
        upper_test: OneSidedTestResult {
            hypothesis: format!("H0: effect >= {:.4}", upper_bound),
            statistic: t_upper,
            p_value: p_upper,
            rejected: p_upper < alpha,
        },
        tost_p_value: tost_p,
        equivalent,
        alpha,
        n: nx + ny,
        df: Some(df),
        method: method.to_string(),
    })
}

/// Perform paired-samples TOST to test if paired differences are equivalent to zero.
///
/// # Arguments
/// * `x` - First sample
/// * `y` - Second sample (must be same length as x)
/// * `bounds` - Equivalence bounds specification
/// * `alpha` - Significance level (default: 0.05)
///
/// # Returns
/// * `TostResult` containing test statistics, p-values, and equivalence conclusion
///
/// # Example
/// ```
/// use anofox_statistics::equivalence::{tost_t_test_paired, EquivalenceBounds};
///
/// let before = vec![10.0, 12.0, 11.0, 13.0, 10.5];
/// let after = vec![10.2, 11.8, 11.1, 13.1, 10.4];
/// let bounds = EquivalenceBounds::Symmetric { delta: 0.5 };
///
/// let result = tost_t_test_paired(&before, &after, &bounds, 0.05).unwrap();
/// println!("Treatment effect equivalent to zero: {}", result.equivalent);
/// ```
///
/// # R equivalent
/// `TOSTER::TOSTpaired(m_diff, sd_diff, n, low_eqbound, high_eqbound)`
pub fn tost_t_test_paired(
    x: &[f64],
    y: &[f64],
    bounds: &EquivalenceBounds,
    alpha: f64,
) -> Result<TostResult> {
    validate_alpha(alpha)?;

    let n = x.len();
    if n != y.len() {
        return Err(StatError::InvalidParameter(format!(
            "Paired samples must have equal length: {} vs {}",
            n,
            y.len()
        )));
    }

    if n < 2 {
        return Err(StatError::InsufficientData { needed: 2, got: n });
    }

    // Compute differences
    let diffs: Vec<f64> = x.iter().zip(y.iter()).map(|(xi, yi)| xi - yi).collect();

    let mean_diff = mean(&diffs)?;
    let var_diff = variance(&diffs)?;
    let sd_diff = var_diff.sqrt();
    let se = sd_diff / (n as f64).sqrt();
    let df = (n - 1) as f64;

    // Convert bounds to raw units using SD of differences
    let (lower_bound, upper_bound) = bounds.to_raw(Some(sd_diff))?;

    // Estimate: mean difference
    let estimate = mean_diff;

    // t-distribution for inference
    let t_dist = StudentsT::new(0.0, 1.0, df).map_err(|e| {
        StatError::InvalidParameter(format!("Failed to create t-distribution: {}", e))
    })?;

    // Lower test: H0: estimate <= lower_bound
    let t_lower = (estimate - lower_bound) / se;
    let p_lower = t_dist.sf(t_lower);

    // Upper test: H0: estimate >= upper_bound
    let t_upper = (estimate - upper_bound) / se;
    let p_upper = t_dist.cdf(t_upper);

    // TOST p-value
    let tost_p = p_lower.max(p_upper);

    // (1 - 2*alpha) confidence interval
    let t_crit = t_dist.inverse_cdf(1.0 - alpha);
    let margin = t_crit * se;
    let ci = (estimate - margin, estimate + margin);

    // Equivalence established if CI within bounds
    let equivalent = ci.0 >= lower_bound && ci.1 <= upper_bound;

    Ok(TostResult {
        estimate,
        ci,
        bounds: (lower_bound, upper_bound),
        lower_test: OneSidedTestResult {
            hypothesis: format!("H0: effect <= {:.4}", lower_bound),
            statistic: t_lower,
            p_value: p_lower,
            rejected: p_lower < alpha,
        },
        upper_test: OneSidedTestResult {
            hypothesis: format!("H0: effect >= {:.4}", upper_bound),
            statistic: t_upper,
            p_value: p_upper,
            rejected: p_upper < alpha,
        },
        tost_p_value: tost_p,
        equivalent,
        alpha,
        n,
        df: Some(df),
        method: "Paired-samples TOST".to_string(),
    })
}

/// Validate alpha parameter.
fn validate_alpha(alpha: f64) -> Result<()> {
    if !(0.0 < alpha && alpha < 1.0) {
        return Err(StatError::InvalidParameter(format!(
            "alpha must be between 0 and 1, got {}",
            alpha
        )));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_one_sample_tost_equivalent() {
        // Data with small mean close to 0
        let x = vec![0.1, -0.1, 0.05, -0.05, 0.0, 0.08, -0.03, 0.02];
        let bounds = EquivalenceBounds::Symmetric { delta: 0.5 };

        let result = tost_t_test_one_sample(&x, 0.0, &bounds, 0.05).unwrap();

        // Should be equivalent (mean is very close to 0, well within ±0.5)
        assert!(result.equivalent);
        assert!(result.tost_p_value < 0.05);
    }

    #[test]
    fn test_one_sample_tost_not_equivalent() {
        // Data with mean clearly outside bounds
        let x = vec![2.0, 2.1, 1.9, 2.2, 1.8, 2.0];
        let bounds = EquivalenceBounds::Symmetric { delta: 0.5 };

        let result = tost_t_test_one_sample(&x, 0.0, &bounds, 0.05).unwrap();

        // Should NOT be equivalent (mean ~2.0 is outside ±0.5)
        assert!(!result.equivalent);
    }

    #[test]
    fn test_two_sample_tost_equivalent() {
        // Two groups with very similar means
        let x = vec![10.1, 10.0, 9.9, 10.2, 10.0, 9.8, 10.1, 10.0];
        let y = vec![10.0, 10.1, 9.9, 10.0, 10.2, 9.9, 10.0, 10.1];
        let bounds = EquivalenceBounds::Symmetric { delta: 0.5 };

        let result = tost_t_test_two_sample(&x, &y, &bounds, 0.05, false).unwrap();

        // Should be equivalent
        assert!(result.equivalent);
    }

    #[test]
    fn test_two_sample_tost_not_equivalent() {
        // Two groups with different means
        let x = vec![10.0, 10.1, 9.9, 10.2, 10.0];
        let y = vec![12.0, 12.1, 11.9, 12.2, 12.0];
        let bounds = EquivalenceBounds::Symmetric { delta: 0.5 };

        let result = tost_t_test_two_sample(&x, &y, &bounds, 0.05, false).unwrap();

        // Should NOT be equivalent (difference ~2)
        assert!(!result.equivalent);
    }

    #[test]
    fn test_paired_tost_equivalent() {
        // Paired data with small differences
        let before = vec![10.0, 12.0, 11.0, 13.0, 10.5, 11.5, 12.5, 10.0];
        let after = vec![10.1, 11.9, 11.05, 13.1, 10.45, 11.55, 12.45, 10.05];
        let bounds = EquivalenceBounds::Symmetric { delta: 0.5 };

        let result = tost_t_test_paired(&before, &after, &bounds, 0.05).unwrap();

        // Should be equivalent (differences are tiny)
        assert!(result.equivalent);
    }

    #[test]
    fn test_cohen_d_bounds() {
        // Test with Cohen's d bounds
        let x = vec![0.1, 0.2, 0.0, -0.1, 0.15, -0.05, 0.1, 0.0];
        let bounds = EquivalenceBounds::CohenD { d: 0.5 };

        let result = tost_t_test_one_sample(&x, 0.0, &bounds, 0.05).unwrap();

        // Bounds should be converted based on sample SD
        assert!(result.bounds.0 < 0.0);
        assert!(result.bounds.1 > 0.0);
    }

    #[test]
    fn test_invalid_alpha() {
        let x = vec![1.0, 2.0, 3.0];
        let bounds = EquivalenceBounds::Symmetric { delta: 0.5 };

        assert!(tost_t_test_one_sample(&x, 0.0, &bounds, 0.0).is_err());
        assert!(tost_t_test_one_sample(&x, 0.0, &bounds, 1.0).is_err());
        assert!(tost_t_test_one_sample(&x, 0.0, &bounds, -0.1).is_err());
    }

    #[test]
    fn test_insufficient_data() {
        let x = vec![1.0];
        let bounds = EquivalenceBounds::Symmetric { delta: 0.5 };

        assert!(tost_t_test_one_sample(&x, 0.0, &bounds, 0.05).is_err());
    }
}