57 const int n =
static_cast<int>(data.rows());
58 const int nVars =
static_cast<int>(data.cols());
62 RowVectorXd means = data.colwise().mean();
65 MatrixXd centered = data.rowwise() - means;
66 RowVectorXd stddev = (centered.colwise().squaredNorm() /
static_cast<double>(df)).array().sqrt();
69 double sqrtN = std::sqrt(
static_cast<double>(n));
70 RowVectorXd tstat = (means.array() - mu) / (stddev.array() / sqrtN);
73 MatrixXd matPval(1, nVars);
74 for (
int j = 0; j < nVars; ++j) {
75 matPval(0, j) = tToPval(tstat(j), df, tail);
89 return oneSample(dataA - dataB, 0.0, tail);
96 const int nA =
static_cast<int>(dataA.rows());
97 const int nB =
static_cast<int>(dataB.rows());
98 const int nVars =
static_cast<int>(dataA.cols());
99 const int df = nA + nB - 2;
101 RowVectorXd meanA = dataA.colwise().mean();
102 RowVectorXd meanB = dataB.colwise().mean();
105 MatrixXd centA = dataA.rowwise() - meanA;
106 MatrixXd centB = dataB.rowwise() - meanB;
107 RowVectorXd ssA = centA.colwise().squaredNorm();
108 RowVectorXd ssB = centB.colwise().squaredNorm();
109 RowVectorXd pooledVar = (ssA + ssB) /
static_cast<double>(df);
112 double invN = 1.0 /
static_cast<double>(nA) + 1.0 /
static_cast<double>(nB);
113 RowVectorXd tstat = (meanA - meanB).array() / (pooledVar.array() * invN).sqrt();
116 MatrixXd matPval(1, nVars);
117 for (
int j = 0; j < nVars; ++j) {
118 matPval(0, j) = tToPval(tstat(j), df, tail);
135 double x =
static_cast<double>(df) / (
static_cast<double>(df) + t * t);
138 return 1.0 - 0.5 * iBeta;
146double StatsTtest::tToPval(
double t,
int df,
StatsTailType tail)
148 double cdf =
tCdf(t, df);
156 return 2.0 * std::min(cdf, 1.0 - cdf);
167 if (x <= 0.0)
return 0.0;
168 if (x >= 1.0)
return 1.0;
170 if (x > (a + 1.0) / (a + b + 2.0)) {
174 double lnPre = a * std::log(x) + b * std::log(1.0 - x) - logBeta(a, b) - std::log(a);
175 return std::exp(lnPre) * betaCf(x, a, b);
180double StatsTtest::betaCf(
double x,
double a,
double b)
183 const int maxIter = 200;
184 const double eps = 1.0e-12;
185 const double tiny = 1.0e-30;
188 double qap = a + 1.0;
189 double qam = a - 1.0;
192 double d = 1.0 - qab * x / qap;
193 if (std::fabs(d) < tiny) d = tiny;
197 for (
int m = 1; m <= maxIter; ++m) {
201 double aa = m * (b - m) * x / ((qam + m2) * (a + m2));
203 if (std::fabs(d) < tiny) d = tiny;
205 if (std::fabs(c) < tiny) c = tiny;
210 aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2));
212 if (std::fabs(d) < tiny) d = tiny;
214 if (std::fabs(c) < tiny) c = tiny;
219 if (std::fabs(del - 1.0) < eps)
break;
226double StatsTtest::logBeta(
double a,
double b)
228 return std::lgamma(a) + std::lgamma(b) - std::lgamma(a + b);
StatsTtest class declaration.
Statistical testing (t-tests, F-tests, cluster permutation, multiple comparison correction).
static double regularizedBeta(double x, double a, double b)
static StatsTtestResult paired(const Eigen::MatrixXd &dataA, const Eigen::MatrixXd &dataB, StatsTailType tail=StatsTailType::Both)
static double tCdf(double t, int df)
static StatsTtestResult oneSample(const Eigen::MatrixXd &data, double mu=0.0, StatsTailType tail=StatsTailType::Both)
static StatsTtestResult independent(const Eigen::MatrixXd &dataA, const Eigen::MatrixXd &dataB, StatsTailType tail=StatsTailType::Both)