1+ //! Non-parametric statistical tests for single-cell data analysis.
2+ //!
3+ //! This module implements non-parametric statistical tests that make fewer assumptions about
4+ //! data distribution. These tests are particularly useful for single-cell data which often
5+ //! exhibits non-normal distributions, high sparsity, and outliers.
6+ //!
7+ //! The primary test implemented is the Mann-Whitney U test (also known as the Wilcoxon
8+ //! rank-sum test), which compares the distributions of two groups without assuming normality.
9+
110use std:: { cmp:: Ordering , f64} ;
211
312use nalgebra_sparse:: CsrMatrix ;
@@ -13,6 +22,34 @@ struct TieInfo {
1322 tie_correction : f64 ,
1423}
1524
25+ /// Perform Mann-Whitney U tests on all genes comparing two groups of cells.
26+ ///
27+ /// This function efficiently computes Mann-Whitney U statistics for all genes in a sparse
28+ /// matrix, comparing expression distributions between two groups of cells. The implementation
29+ /// uses parallel processing for improved performance on large datasets.
30+ ///
31+ /// # Arguments
32+ ///
33+ /// * `matrix` - Sparse expression matrix (genes × cells)
34+ /// * `group1_indices` - Column indices for the first group of cells
35+ /// * `group2_indices` - Column indices for the second group of cells
36+ /// * `alternative` - Type of alternative hypothesis (two-sided, less, greater)
37+ ///
38+ /// # Returns
39+ ///
40+ /// Vector of `TestResult` objects containing U statistics and p-values for each gene.
41+ /// let group1 = vec![0, 1, 2]; // First group of cells
42+ /// let group2 = vec![3, 4, 5]; // Second group of cells
43+ ///
44+ /// let results = mann_whitney_matrix_groups(
45+ /// &matrix,
46+ /// &group1,
47+ /// &group2,
48+ /// Alternative::TwoSided
49+ /// )?;
50+ /// # Ok(())
51+ /// # }
52+ /// ```
1653pub fn mann_whitney_matrix_groups < T > (
1754 matrix : & CsrMatrix < T > ,
1855 group1_indices : & [ usize ] ,
4885 Ok ( results)
4986}
5087
88+ /// Perform an optimized Mann-Whitney U test on two samples.
89+ ///
90+ /// This function computes the Mann-Whitney U statistic and p-value for comparing two
91+ /// independent samples. It handles ties correctly and supports different alternative
92+ /// hypotheses.
93+ ///
94+ /// # Arguments
95+ ///
96+ /// * `x` - First sample
97+ /// * `y` - Second sample
98+ /// * `alternative` - Type of alternative hypothesis
99+ ///
100+ /// # Returns
101+ ///
102+ /// `TestResult` containing the U statistic and p-value.
103+ ///
104+ /// # Example
105+ ///
106+ /// ```rust
107+ /// use single_statistics::testing::inference::nonparametric::mann_whitney_optimized;
108+ /// use single_statistics::testing::Alternative;
109+ ///
110+ /// let group1 = vec![1.0, 2.0, 3.0];
111+ /// let group2 = vec![4.0, 5.0, 6.0];
112+ /// let result = mann_whitney_optimized(&group1, &group2, Alternative::TwoSided);
113+ ///
114+ /// println!("U statistic: {}, p-value: {}", result.statistic, result.p_value);
115+ /// ```
51116pub fn mann_whitney_optimized ( x : & [ f64 ] , y : & [ f64 ] , alternative : Alternative ) -> TestResult < f64 > {
52117 let nx = x. len ( ) ;
53118 let ny = y. len ( ) ;
0 commit comments