Advertisement
Guest User

anderson-darling

a guest
May 7th, 2014
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. /**
  2.  * @brief Checks if given points are form a given distribution
  3.  * @param x
  4.  * @param dist distribution name
  5.  * @return A squared
  6.  */
  7. double StatUtils::andersonDarlingTest(Points1D x, const QString &d) {
  8.  
  9.   // Check point count
  10.   int n = x.size();
  11.   if (n < 25) {
  12.     QString msg = QString("Anderson-Darling: '%1' is too few points for test.");
  13.     logWarning(msg.arg(n));
  14.   }
  15.   if (n >= 1000) {
  16.     QString msg = QString("Anderson-Darling: '%1' is too many points for test. "
  17.                           "Consider for example Kolmogorov-Smirnov test.");
  18.     logWarning(msg.arg(n));
  19.   }
  20.  
  21.   // Sort data
  22.   qSort(x);
  23.  
  24.   // Choose distribution
  25.   // NOTE: Adjustment coeficient has to be chosen. The test is most often used
  26.   // in contexts where a family of distributions is being tested, in which case
  27.   // the parameters of that family need to be estimated and account must be
  28.   // taken of this in adjusting either the test-statistic or its critical values
  29.   double adj;
  30.   if (d.compare("normal") == 0 || d.compare("gauss")) {
  31.     adj = 1.0 + (0.75 + 2.25 / n) / n;
  32.     x = normcdf(zscore(x));
  33.   } else {
  34.     logError(QString("Anderson-Darling test for %1 not implemented").arg(d));
  35.     return -1;
  36.   }
  37.  
  38.   // Check data validity
  39.   for (int i = 0; i < n; i++) {
  40.     if (x[i] < 0.0 || x[i] > 1.0) {
  41.       logError("Anderson-Darling test requires data in CDF form");
  42.       return -1;
  43.     }
  44.   }
  45.  
  46.   // Calculate data for standard normal CDF
  47.   Points1D data;
  48.   for (int i = 0; i < n; i++) {
  49.     double coef =  2 * (i + 1) - 1;
  50.     double alog = log(x[i]);
  51.     double blog = log(1 - x[n - (i + 1)]);
  52.     data.append(coef * (alog + blog));
  53.   }
  54.   double asq = -n - MathUtils::sum(data) / n;
  55.   return asq * adj;
  56. }
  57. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement