source: trunk/test/averager4.cc @ 2799

Last change on this file since 2799 was 2799, checked in by Peter, 11 years ago

first version of class Averager4. refs #705

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1// $Id: averager4.cc 2799 2012-07-28 08:17:15Z peter $
2
3/*
4  Copyright (C) 2012 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/Averager4.h"
25#include "yat/statistics/utility.h"
26#include "yat/utility/Vector.h"
27
28using namespace theplu::yat;
29using namespace theplu::yat::statistics;
30
31using theplu::yat::test::Suite;
32
33int main(int argc, char* argv[])
34{
35  Suite suite(argc, argv);
36
37  Averager4 a;
38  Averager4 copy(a);
39  a = copy;
40  a += copy;
41
42  a.add(0, 9);
43  a.add(10);
44
45  if (!suite.equal(a.central_moment3(), (-9.0 + std::pow(9.0,3))/10.0)) {
46    suite.add(false);
47    suite.err() << "central moment3 failed\n";
48  }
49  if (!suite.equal(a.central_moment4(), (9 + 9*9*9*9)/10)) {
50    suite.add(false);
51    suite.err() << "central moment4 failed\n";
52  }
53  a.reset();
54
55  suite.out() << "=========================\n";
56  utility::Vector vec(10);
57  for (size_t i=0; i<vec.size(); ++i) {
58    vec(i) = i;
59    a.add(vec(i), 1);
60  }
61  double m = a.averager().mean();
62
63  double sum = 0;
64  for (size_t i=0; i<vec.size(); ++i)
65    sum += std::pow(vec(i)-m, 3);
66  sum = sum/vec.size();
67  if (!suite.equal(a.central_moment3(), sum)) {
68    suite.add(false);
69    suite.err() << "central moment3 failed: expected " << sum << "\n";
70  }
71  return suite.return_value();
72
73  sum = 0;
74  for (size_t i=0; i<vec.size(); ++i)
75    sum += std::pow(vec(i)-m, 4)/vec.size();
76  if (!suite.equal(a.central_moment4(), sum)) {
77    suite.add(false);
78    suite.err() << "central moment4 failed\n";
79  }
80
81  suite.out() << "comparing against GSL implementations\n";
82  double correct_factor = std::sqrt(static_cast<double>(vec.size())/
83                                    (vec.size()-1.0));
84  if (!suite.add(suite.equal(a.skewness(),
85                             std::pow(correct_factor,3)*skewness(vec),
86                             10))) {
87    suite.err() << "skewness failed\n";
88  }
89
90  if (!suite.add(suite.equal(a.kurtosis(),
91                             std::pow(correct_factor,4)*kurtosis(vec),
92                             10))) {
93    suite.err() << "kurtosis failed\n";
94  }
95
96  suite.out() << "test operator+=\n";
97  Averager4 a1;
98  a1.add(0);
99  a1.add(-2.5,4);
100
101  Averager4 a2;
102  a2.add(1,4);
103  a2.add(6,1);
104
105  // cm3 = (4*1 + 4^3)/5 = 68/5 = 13.6
106  if (!suite.add(suite.equal(a2.central_moment3(), 13.6)))
107    suite.err() << "central moment3 failed\n";
108
109  a1 += a2;
110  if (!suite.add(suite.equal(a1.averager().mean(), 0)))
111    suite.err() << "mean failed\n";
112  if (!suite.add(suite.equal(a1.averager().variance(),
113                             (4*2.5*2.5+4*1*1+1*6*6)/10.0)))
114    suite.err() << "variance failed\n";
115  if (!suite.add(suite.equal(a1.central_moment3(),
116                             (4*std::pow(2.5,3)+4+std::pow(6.0,3))/10)))
117    suite.err() << "central_moment3 failed\n";
118  if (!suite.add(suite.equal(a1.central_moment4(),
119                             (4*std::pow(2.5,4)+4+std::pow(6.0,4))/10)))
120    suite.err() << "central_moment4 failed\n";
121
122
123  Averager aver = a.averager();
124  aver.mean();
125
126  return suite.return_value();
127}
Note: See TracBrowser for help on using the repository browser.