source: trunk/test/tukey.cc @ 2558

Last change on this file since 2558 was 2558, checked in by Peter, 10 years ago

refs #671. Follow Knuth's algorithm to calculate average and variance online

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1// $Id: tukey.cc 2558 2011-09-24 20:53:04Z peter $
2
3/*
4  Copyright (C) 2011 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
24#include "yat/statistics/TukeyBiweightEstimator.h"
25
26#include <vector>
27
28using namespace theplu::yat;
29using namespace statistics;
30
31template<typename T>
32void do_test(const T& data, double, test::Suite& suite);
33
34int main(int argc,char* argv[])
35{ 
36  test::Suite suite(argc, argv);
37
38  std::vector<double> x;
39  x.push_back(100);
40  x.push_back(-10);
41  x.push_back(-10);
42  x.push_back(0);
43  x.push_back(1);
44  x.push_back(2);
45  x.push_back(3);
46  x.push_back(4);
47  do_test(x, 2.0, suite);
48  std::vector<utility::DataWeight> xw;
49  xw.push_back(utility::DataWeight(2,1.1));
50  do_test(xw, 2, suite);
51  xw.push_back(utility::DataWeight(0,1.1));
52  do_test(xw, 1, suite);
53  xw.push_back(utility::DataWeight(2,0));
54  do_test(xw, 1, suite);
55  xw.push_back(utility::DataWeight(2000,1));
56  do_test(xw, 1, suite);
57  xw.push_back(utility::DataWeight(1,10));
58  do_test(xw, 1, suite);
59
60  return suite.return_value();
61}
62
63template<typename T>
64void do_test(const T& data, double correct, test::Suite& suite)
65{
66  TukeyBiweightEstimator estimator(5, false);
67  double x = estimator(data.begin(), data.end());
68  suite.add(suite.equal(x, correct, 2));
69  double y = estimator(data.begin(), data.end());
70  TukeyBiweightEstimator estimator2(5, true);
71  double z = estimator2(data.begin(), data.end());
72  z += z; // avoid compiler warning
73  suite.add(suite.equal(x, y));
74}
Note: See TracBrowser for help on using the repository browser.