source: trunk/test/kolmogorov_smirnov_test.cc @ 1608

Last change on this file since 1608 was 1608, checked in by Peter, 13 years ago

refs #457 - correcting error margin (not fixing the problem though)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
RevLine 
[1593]1// $Id: kolmogorov_smirnov_test.cc 1608 2008-11-01 14:34:43Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Suite.h"
23
[1600]24#include "yat/statistics/Averager.h"
[1593]25#include "yat/statistics/KolmogorovSmirnov.h"
26
[1600]27#include <cmath>
28#include <iostream>
[1593]29
[1600]30using namespace theplu::yat;
31
32void test_one_sample(test::Suite&);
33void test_p_value(test::Suite&);
34
[1593]35int main(int argc, char* argv[])
36{ 
37  test::Suite suite(argc, argv);
38
[1600]39  test_one_sample(suite);
40  test_p_value(suite);
[1593]41
[1600]42  return suite.return_value();
43}
[1593]44
[1600]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  }
[1593]56
[1600]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;
[1608]75  double margin = 5*std::sqrt(n*p_correct)/n;
[1600]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  } 
[1593]82}
83
[1600]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 TracBrowser for help on using the repository browser.