Changeset 1600 for trunk/test/kolmogorov_smirnov_test.cc
 Timestamp:
 Oct 27, 2008, 8:10:49 PM (13 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test/kolmogorov_smirnov_test.cc
r1593 r1600 22 22 #include "Suite.h" 23 23 24 #include "yat/statistics/Averager.h" 24 25 #include "yat/statistics/KolmogorovSmirnov.h" 25 26 26 #include <ostream> 27 #include <cmath> 28 #include <iostream> 29 30 using namespace theplu::yat; 31 32 void test_one_sample(test::Suite&); 33 void test_p_value(test::Suite&); 27 34 28 35 int main(int argc, char* argv[]) 29 36 { 30 using namespace theplu::yat;31 32 37 test::Suite suite(argc, argv); 33 38 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); 46 41 47 42 return suite.return_value(); 48 43 } 49 44 45 void 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.0i/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_correctmargin) { 77 suite.err() << "Error: pvalue: " << p << "\n" 78 << "expected approximately: " << p_correct << "\n" 79 << "and at most " << margin << "deviation\n"; 80 suite.add(false); 81 } 82 } 83 84 void 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 pvalue: " << 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.