Advertisement
Guest User

kolmogorov-smirnov

a guest
May 7th, 2014
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. /**
  2.  * @brief Performs one sample Kolmogorov-Smirnov test
  3.  * @param x points
  4.  * @param d distribution name ('normal',...)
  5.  * @return probability that points have given distribution
  6.  */
  7. double StatUtils::kolmogorovSmirnovTest(Points1D x, const QString &d) {
  8.  
  9.   // Calculate size of array and sort it
  10.   int n = x.size();
  11.   qSort(x);
  12.  
  13.   // Choose distribution
  14.   if (d.compare("normal") == 0 || d.compare("gauss")) {
  15.     x = normcdf(zscore(x));
  16.   } else {
  17.     QString msg = QString("Kolmogorov-Smirnov test for %1 not implemented");
  18.     logError(msg.arg(d));
  19.     return -1;
  20.   }
  21.  
  22.   // Calculate probability that given sequence of points belong to distribution
  23.   Points1D a1 = MathUtils::div(MathUtils::sequence(0.0, (double) n + 0), n);
  24.   Points1D a2 = MathUtils::div(MathUtils::sequence(1.0, (double) n + 1), n);
  25.   Points1D asub1 = MathUtils::abs(MathUtils::sub(x, a1));
  26.   Points1D asub2 = MathUtils::abs(MathUtils::sub(x, a2));
  27.   double ks = sqrt(n) * MathUtils::max(MathUtils::max(asub1, asub2));
  28.   return 1 - kolmogorovcdf(ks);
  29. }
  30. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement