Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @brief Checks if given points are form a given distribution
- * @param x
- * @param dist distribution name
- * @return A squared
- */
- double StatUtils::andersonDarlingTest(Points1D x, const QString &d) {
- // Check point count
- int n = x.size();
- if (n < 25) {
- QString msg = QString("Anderson-Darling: '%1' is too few points for test.");
- logWarning(msg.arg(n));
- }
- if (n >= 1000) {
- QString msg = QString("Anderson-Darling: '%1' is too many points for test. "
- "Consider for example Kolmogorov-Smirnov test.");
- logWarning(msg.arg(n));
- }
- // Sort data
- qSort(x);
- // Choose distribution
- // NOTE: Adjustment coeficient has to be chosen. The test is most often used
- // in contexts where a family of distributions is being tested, in which case
- // the parameters of that family need to be estimated and account must be
- // taken of this in adjusting either the test-statistic or its critical values
- double adj;
- if (d.compare("normal") == 0 || d.compare("gauss")) {
- adj = 1.0 + (0.75 + 2.25 / n) / n;
- x = normcdf(zscore(x));
- } else {
- logError(QString("Anderson-Darling test for %1 not implemented").arg(d));
- return -1;
- }
- // Check data validity
- for (int i = 0; i < n; i++) {
- if (x[i] < 0.0 || x[i] > 1.0) {
- logError("Anderson-Darling test requires data in CDF form");
- return -1;
- }
- }
- // Calculate data for standard normal CDF
- Points1D data;
- for (int i = 0; i < n; i++) {
- double coef = 2 * (i + 1) - 1;
- double alog = log(x[i]);
- double blog = log(1 - x[n - (i + 1)]);
- data.append(coef * (alog + blog));
- }
- double asq = -n - MathUtils::sum(data) / n;
- return asq * adj;
- }
- //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement