Changeset 1600 for trunk/test


Ignore:
Timestamp:
Oct 27, 2008, 8:10:49 PM (13 years ago)
Author:
Peter
Message:
  • Fixed bug that data disappeared because data internally was stored in a set (is now a multiset).
  • Changed ordering of data, which implies that sign of signed_score is flipped.
  • Added some tests for KolmogorovSmirnov?
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/kolmogorov_smirnov_test.cc

    r1593 r1600  
    2222#include "Suite.h"
    2323
     24#include "yat/statistics/Averager.h"
    2425#include "yat/statistics/KolmogorovSmirnov.h"
    2526
    26 #include <ostream>
     27#include <cmath>
     28#include <iostream>
     29
     30using namespace theplu::yat;
     31
     32void test_one_sample(test::Suite&);
     33void test_p_value(test::Suite&);
    2734
    2835int main(int argc, char* argv[])
    2936
    30   using namespace theplu::yat;
    31 
    3237  test::Suite suite(argc, argv);
    3338
    34   statistics::KolmogorovSmirnov ks;
    35   for (size_t i=0; i<10; ++i)
    36     ks.add(i,true);
    37   for (size_t i=0; i<1000; ++i)
    38     ks.add(100+i,false);
    39   suite.add(suite.equal(ks.score(), 1.0));
    40   for (size_t i=0; i<10; ++i)
    41     ks.add(100+10*i,true);
    42 
    43   suite.err() << ks.score() << std::endl;
    44 
    45   ks.p_value();
     39  test_one_sample(suite);
     40  test_p_value(suite);
    4641
    4742  return suite.return_value();
    4843}
    4944
     45void test_one_sample(test::Suite& suite)
     46{
     47  std::vector<double> correct(11);
     48  for (size_t i=0; i<correct.size(); ++i) {
     49    double s1 = 1.0 - i/10.0;
     50    double s2 = 0.0-i/10.0;
     51    if (std::abs(s1)>std::abs(s2))
     52      correct[i] = s1;
     53    else
     54      correct[i] = s2;
     55  }
     56
     57  for (size_t i=0; i<11; ++i) {
     58    statistics::KolmogorovSmirnov ks;
     59    for (size_t j=0; j<11; ++j) {
     60      ks.add(j, i==j);
     61    }     
     62    double score = ks.signed_score();
     63    if (!suite.add(suite.equal(score, correct[i]))) {
     64      suite.err() << "signed_score(void) failed\n";
     65    }
     66  }
     67
     68  statistics::KolmogorovSmirnov ks;
     69  for (size_t i=0; i<11; ++i) {
     70    ks.add(i, i==0);
     71  }     
     72  size_t n=110000;
     73  double p = ks.p_value(n);
     74  double p_correct = 2.0/11.0;
     75  double margin = 5*std::sqrt(n*p_correct);
     76  if (p>p_correct+margin || p<p_correct-margin) {
     77    suite.err() << "Error: p-value: " << p << "\n"
     78                << "expected approximately: " << p_correct << "\n"
     79                << "and at most " << margin << "deviation\n";
     80    suite.add(false);
     81  }
     82}
     83
     84void test_p_value(test::Suite& suite)
     85{
     86  statistics::KolmogorovSmirnov ks;
     87  for (size_t i=0; i<100; ++i) {
     88    ks.add(i, true);
     89    ks.add(i+14.5, false);
     90  }
     91  statistics::Averager a;
     92  for (size_t n=0; n<100; ++n) {
     93    a.add(ks.p_value(100));
     94  }
     95  double margin = 5 * a.standard_error();
     96  double p_approx = ks.p_value();
     97  if (std::abs(a.mean()-p_approx)>margin) {
     98    suite.add(false);
     99    suite.err() << "Error: unexpected large deviation between p_values\n"
     100                << "permutation p-value: " << a.mean() << "\n"
     101                << "analytical approximation: " << p_approx << "\n"
     102                << "expected deviation to be smaller than " << margin << "\n";
     103  }
     104 
     105}
Note: See TracChangeset for help on using the changeset viewer.