v2.0.0
Loading...
Searching...
No Matches
sts_correction.cpp
Go to the documentation of this file.
1//=============================================================================================================
34
35//=============================================================================================================
36// INCLUDES
37//=============================================================================================================
38
39#include "sts_correction.h"
40
41#include <algorithm>
42#include <numeric>
43#include <vector>
44#include <cmath>
45
46//=============================================================================================================
47// USED NAMESPACES
48//=============================================================================================================
49
50using namespace STSLIB;
51using namespace Eigen;
52
53//=============================================================================================================
54// DEFINE METHODS
55//=============================================================================================================
56
57MatrixXd StatsMcCorrection::bonferroni(const MatrixXd& pValues)
58{
59 const int n = static_cast<int>(pValues.size());
60 MatrixXd corrected = pValues * static_cast<double>(n);
61 corrected = corrected.cwiseMin(1.0);
62 return corrected;
63}
64
65//=============================================================================================================
66
67MatrixXd StatsMcCorrection::holmBonferroni(const MatrixXd& pValues)
68{
69 const int n = static_cast<int>(pValues.size());
70
71 // Flatten to a vector with indices
72 std::vector<std::pair<double, int>> indexed(n);
73 for (int i = 0; i < n; ++i) {
74 indexed[i] = {pValues.data()[i], i};
75 }
76
77 // Sort ascending by p-value
78 std::sort(indexed.begin(), indexed.end());
79
80 // Compute adjusted p-values: adjusted_p[i] = p * (n - rank_i + 1), rank is 1-based
81 std::vector<double> adjusted(n);
82 for (int i = 0; i < n; ++i) {
83 adjusted[i] = indexed[i].first * static_cast<double>(n - i);
84 }
85
86 // Enforce monotonicity: from first to last, corrected[i] = max(corrected[i], corrected[i-1])
87 for (int i = 1; i < n; ++i) {
88 adjusted[i] = std::max(adjusted[i], adjusted[i - 1]);
89 }
90
91 // Cap at 1.0 and place back in original order
92 MatrixXd corrected(pValues.rows(), pValues.cols());
93 for (int i = 0; i < n; ++i) {
94 corrected.data()[indexed[i].second] = std::min(adjusted[i], 1.0);
95 }
96
97 return corrected;
98}
99
100//=============================================================================================================
101
102MatrixXd StatsMcCorrection::fdr(const MatrixXd& pValues, double alpha)
103{
104 Q_UNUSED(alpha);
105
106 const int n = static_cast<int>(pValues.size());
107
108 // Flatten to a vector with indices
109 std::vector<std::pair<double, int>> indexed(n);
110 for (int i = 0; i < n; ++i) {
111 indexed[i] = {pValues.data()[i], i};
112 }
113
114 // Sort ascending by p-value
115 std::sort(indexed.begin(), indexed.end());
116
117 // Compute adjusted p-values: adjusted_p = p * n / rank (rank is 1-based)
118 std::vector<double> adjusted(n);
119 for (int i = 0; i < n; ++i) {
120 int rank = i + 1;
121 adjusted[i] = indexed[i].first * static_cast<double>(n) / static_cast<double>(rank);
122 }
123
124 // Enforce monotonicity: from last to first, corrected[i] = min(corrected[i], corrected[i+1])
125 for (int i = n - 2; i >= 0; --i) {
126 adjusted[i] = std::min(adjusted[i], adjusted[i + 1]);
127 }
128
129 // Cap at 1.0 and place back in original order
130 MatrixXd corrected(pValues.rows(), pValues.cols());
131 for (int i = 0; i < n; ++i) {
132 corrected.data()[indexed[i].second] = std::min(adjusted[i], 1.0);
133 }
134
135 return corrected;
136}
StatsMcCorrection class declaration.
Statistical testing (t-tests, F-tests, cluster permutation, multiple comparison correction).
static Eigen::MatrixXd holmBonferroni(const Eigen::MatrixXd &pValues)
static Eigen::MatrixXd fdr(const Eigen::MatrixXd &pValues, double alpha=0.05)
static Eigen::MatrixXd bonferroni(const Eigen::MatrixXd &pValues)