Changeset 1600
- Timestamp:
- Oct 27, 2008, 8:10:49 PM (14 years ago)
- Location:
- trunk
- Files:
-
- 3 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.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 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 p-value: " << a.mean() << "\n" 101 << "analytical approximation: " << p_approx << "\n" 102 << "expected deviation to be smaller than " << margin << "\n"; 103 } 104 105 } -
trunk/yat/statistics/KolmogorovSmirnov.cc
r1595 r1600 61 61 double res=0; 62 62 double res2=0; 63 double s2 = score() * sqrt(sum_w1_*sum_w2_/(sum_w1_+sum_w2_)); 64 s2 *= s2; 63 double s2 = score() * score() * sum_w1_*sum_w2_/(sum_w1_+sum_w2_); 65 64 int sign = 1; 66 65 for (size_t k = 1; k<100; ++k) { 67 res += sign * exp(-2.0 * k * k * s2);66 res += sign * 2 * exp(-2.0 * k * k * s2); 68 67 sign *= -1; 68 // ignore remaining terms as they are small 69 69 if (res==res2) 70 70 return res; -
trunk/yat/statistics/KolmogorovSmirnov.h
r1595 r1600 99 99 mutable double score_; 100 100 typedef std::pair<std::pair<double,double>, bool> trip; 101 typedef std:: set<trip, std::greater<trip>> data_w;101 typedef std::multiset<trip> data_w; 102 102 data_w data_; 103 103 double sum_w1_;
Note: See TracChangeset
for help on using the changeset viewer.