source: branches/0.4-stable/test/averager_test.cc @ 1294

Last change on this file since 1294 was 1294, checked in by Jari Häkkinen, 15 years ago

Reverting changes that break the interface of Averager and AveragerPair? classes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1// $Id: averager_test.cc 1294 2008-05-12 08:21:19Z jari $
2
3/*
4  Copyright (C) 2005 Peter Johansson
5  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
6  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
7
8  This file is part of the yat library, http://trac.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "Suite.h"
27
28#include "yat/statistics/Averager.h"
29#include "yat/statistics/AveragerPair.h"
30#include "yat/statistics/AveragerPairWeighted.h"
31#include "yat/statistics/AveragerWeighted.h"
32#include "yat/utility/Vector.h"
33
34#include <cmath>
35#include <fstream>
36#include <limits>
37#include <iostream>
38#include <set>
39
40using namespace theplu::yat;
41using namespace theplu::yat::statistics;
42
43using theplu::yat::test::Suite;
44
45//Forward declarations
46bool equal(const Averager&, const Averager&, unsigned int, Suite&);
47bool equal(const AveragerWeighted&, const AveragerWeighted&, unsigned int, 
48           Suite& suite);
49bool equal(const Averager&, const AveragerWeighted&, unsigned int, Suite& suite);
50bool equal(const AveragerPair&, const AveragerPair&, unsigned int, Suite& suite);
51bool equal(const AveragerPair&, const AveragerPairWeighted&, unsigned int, 
52           Suite& suite);
53bool equal(const AveragerPairWeighted&, const AveragerPairWeighted&,
54           unsigned int, Suite& suite);
55
56
57int main(int argc, char* argv[])
58{ 
59
60  Suite suite(argc, argv);
61
62  // Testing Averager
63  suite.out() << "testing Averager" << std::endl;
64  Averager a;
65  a.add(1);
66  a.add(3);
67  a.add(5);
68  if (a.n()!=3 || a.mean()!=3 || a.sum_xx()!=35){
69    suite.add(false);
70    suite.err() << "error: add\n";
71  }
72
73
74  Averager* a1 = new Averager(1.0+3+5,1.0+9+25,3);
75  unsigned int tol = 10;
76  if (!equal(a,*a1, tol, suite)){
77    suite.add(false);
78    suite.err().precision(25);
79    suite.err() << a.sum_x() << '\t' << a1->sum_x() << std::endl;
80    suite.err() << a.sum_xx() << '\t' << a1->sum_xx() << std::endl;
81    suite.err() << a.n() << '\t' << a1->n() << std::endl;
82    suite.err() << a.variance() << '\t' << a1->variance() << std::endl;
83    suite.err() << a.mean() << '\t' << a1->mean() << std::endl;
84    suite.err() << a.mean() - a1->mean() << std::endl;
85    suite.err() << a.variance() - a1->variance() << std::endl;
86    suite.err() << "error: Averager(double x, double xx, unsigned long n)\n";
87  }
88  delete a1;
89
90  a1 = new Averager(a);
91  if (!equal(a,*a1, tol, suite)){
92    suite.add(false);
93    suite.err() << "error: Copy constructor\n";
94  }
95  delete a1;
96
97  a.add(3,5);
98  if (std::abs(a.standard_error()-sqrt(a.variance()/a.n()))>
99      std::numeric_limits<double>().round_error() ){
100    suite.add(false);
101    suite.err() << "error: standard_error\n";
102  }
103     
104
105  if ( std::abs(a.variance() - a.std()*a.std())>
106       std::numeric_limits<double>().round_error() ){
107    suite.add(false);
108    suite.err() << "error: std squared should be variance" << std::endl;
109    suite.err() << "std2: " << a.std()*a.std() << std::endl;
110    suite.err() << "variance: " << a.variance() << std::endl;
111    suite.err() << "difference is: " << a.std()*a.std()-a.variance() << std::endl;
112  }
113 
114  if ( a.variance() != a.variance(a.mean()) ){
115    suite.add(false);
116    suite.err() << "error: variance incorrect\n" << std::endl;
117    suite.err() << "variance: " << a.variance() << std::endl;
118    suite.err() << "mean: " << a.mean() << std::endl;
119    suite.err() << "variance(mean) " << a.variance(a.mean()) << std::endl;
120  }
121  theplu::yat::utility::Vector* tmp_vec = new theplu::yat::utility::Vector(10);
122  add(a, tmp_vec->begin(), tmp_vec->end());
123  delete tmp_vec;
124  std::set<double>* tmp_set = new std::set<double>;
125  tmp_set->insert(1.0);
126  tmp_set->insert(2.0);
127  add(a, tmp_set->begin(), tmp_set->end());
128  delete tmp_set;
129
130
131  // Testing AveragerWeighted
132  suite.err() << "testing AveragerWeighted" << std::endl;
133  theplu::yat::utility::Vector x(3,0);
134  x(0)=0;
135  x(1)=1;
136  x(2)=2;
137  theplu::yat::utility::Vector w(3,1);
138  theplu::yat::statistics::AveragerWeighted aw;
139  add(aw, x.begin(), x.end(), w.begin());
140  a.reset();
141  add(a, x.begin(), x.end());
142  if (!equal(a,aw,tol,suite)){
143    suite.err() << "error: AveragerWeighted with unitary weights should " 
144           << "be equal to Averager" << std::endl;
145    suite.add(false);
146  }
147
148  AveragerWeighted* aw2 = new AveragerWeighted(aw);
149  if (!equal(aw,*aw2,tol,suite)){
150    suite.err() << "error: AveragerWeighted copy constructor " << std::endl;
151    suite.add(false);
152  }
153   
154  aw2->add(12,0);
155  if (!equal(aw,*aw2,tol,suite)){
156    suite.err() << "error: AveragerWeighted adding a data point with weight=0 " 
157           << "should make no change " << std::endl;
158    suite.add(false);
159  }
160     
161  aw2->reset();
162  w*=17;
163  add(*aw2, x.begin(), x.end(), w.begin());
164  if (!equal(aw,*aw2,tol,suite)){
165    suite.err() << "error: AveragerWeighted rescaling weights " 
166           << "should make no change " << std::endl;
167    suite.add(false);
168  }
169  delete aw2;
170  {
171    theplu::yat::utility::Vector tmp(10);
172    add(aw, tmp.begin(), tmp.end());
173  }
174  {
175    std::set<double> tmp;
176    tmp.insert(1.0);
177    tmp.insert(2.0);
178    add(aw, tmp.begin(), tmp.end());
179  }
180 
181
182  suite.out() << "testing AveragerPair" << std::endl;
183  AveragerPair ap;
184  for (int i=0; i<10; i++)
185    ap.add(static_cast<double>(i),i);
186  if (std::abs(ap.correlation()-1)>tol){
187    suite.add(false);
188    suite.err() << "correlation: " << ap.correlation() << std::endl;
189    suite.err() << "error: correlation between identical vectors should be unity" 
190           << std::endl;
191  }
192  if (ap.x_averager().variance()!=ap.covariance()){
193    suite.add(false);
194    suite.err() << "error: covariance of identical vectors should equal to variance" 
195           << std::endl;
196  }
197  AveragerPair* ap2 = new AveragerPair(ap);
198  delete ap2;
199
200  suite.out() << "testing AveragerPairWeighted" << std::endl;
201  AveragerPairWeighted apw;
202  x(0)=0; x(1)=1; x(2)=2;
203  theplu::yat::utility::Vector y(3,0);
204  y(0)=0; y(1)=0; y(2)=2;
205  add(apw, x.begin(), x.end(), y.begin(), w.begin(), w.begin());
206  ap.reset();
207  add(ap, x.begin(), x.end(), y.begin());
208  if (!equal(ap,apw,tol,suite)){
209    suite.err() << "error: AveragerPairWeighted with unitary weights should " 
210           << "be equal to AveragerPair" << std::endl;
211    suite.add(false);
212  }
213
214  AveragerPairWeighted* apw2 = new AveragerPairWeighted(apw);
215  if (!equal(apw,*apw2,tol,suite)){
216    suite.err() << "error: AveragerPairWeighted copy constructor " << std::endl;
217    suite.add(false);
218  }
219   
220  apw2->add(12,23222.03,32.3,0);
221  if (!equal(apw,*apw2,tol,suite)){
222    suite.err() << "error: AveragerWeighted adding a data point with weight=0 " 
223           << "should make no change " << std::endl;
224    suite.add(false);
225  }
226     
227  apw2->reset();
228  w*=17;
229  add(*apw2, x.begin(), x.end(), y.begin(), w.begin(), w.begin());
230  if (!equal(apw,*apw2,tol,suite)){
231    suite.err() << "error: AveragerWeighted rescaling weights " 
232           << "should make no change " << std::endl;
233    suite.add(false);
234  }
235  delete apw2;
236
237  return suite.return_value();
238}
239
240bool equal(const Averager& a, const Averager& b, unsigned int tol, 
241           Suite& suite)
242{
243//  std::cout << (a.n()==b.n()) << std::endl;
244//  std::cout << (a.mean()==b.mean()) << std::endl;
245//  std::cout << (std::abs(a.variance()-b.variance()<1e-15)) << std::endl;
246  return (a.n()==b.n() && suite.equal(a.mean(),b.mean(),tol) &&
247          suite.equal(a.variance(),b.variance(),tol));
248}
249
250bool equal(const AveragerWeighted& a, const AveragerWeighted& b, 
251           const unsigned int tol, Suite& suite)
252{
253  bool equal = true;
254  if  ( !suite.equal(a.mean(),b.mean(),tol)){
255    equal=false;
256    suite.err() << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
257    suite.err() << "difference:\t" << a.mean()-b.mean() << std::endl;
258  }
259  if ( !suite.equal(a.variance(),b.variance(),tol) ) {
260    equal=false;
261    suite.err() << "error for variance:\t" << a.variance() << " " 
262                << b.variance() << std::endl;
263  } 
264  if ( !suite.equal(a.standard_error(),b.standard_error(),tol) ) {
265    equal =false;
266    suite.err() << "error for standard error:\t" << std::endl;
267  }
268  return equal;
269}
270
271bool equal(const Averager& a, const AveragerWeighted& b, const unsigned int tol, 
272           Suite& suite)
273{
274  bool equal = true;
275  if  ( !suite.equal(a.mean(),b.mean(),tol)){
276    equal=false;
277    suite.err() << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
278  }
279  if ( !suite.equal(a.variance(),b.variance(),tol) ) {
280    equal=false;
281    suite.err() << "error for variance:\t" << a.variance() << " " 
282                << b.variance() << std::endl;
283  } 
284  if ( !suite.equal(a.standard_error(), b.standard_error(),tol) ) {
285    equal =false;
286    suite.err() << "error for standard error:\t" << std::endl;
287  }
288  return equal;
289}
290
291bool equal(const AveragerPair& a, const AveragerPair& b, 
292           const unsigned int tol, Suite& suite)
293{
294  bool ok = true;
295  if  ( std::abs(a.covariance()-b.covariance())>tol){
296    suite.add(false);
297    suite.err() << "error covariance: " << a.covariance() << "\t" 
298           << b.covariance() << std::endl;
299  }
300  if ( std::abs(a.correlation()-b.correlation())>tol ) {
301    suite.add(false);
302    suite.err() << "error correlation" << std::endl;
303  } 
304  return ok;
305}
306
307bool equal(const AveragerPair& a, const AveragerPairWeighted& b, 
308           const unsigned int tol, Suite& suite)
309{
310  bool ok = true;
311  if  ( std::abs(a.covariance()-b.covariance())>tol){
312    suite.add(false);
313    suite.err() << "error covariance: " << a.covariance() << "\t" 
314           << b.covariance() << std::endl;
315  }
316  if ( std::abs(a.correlation()-b.correlation())>tol ) {
317    suite.add(false);
318    suite.err() << "error correlation" << std::endl;
319    suite.err() << "unweighted:" << a.correlation() << std::endl;
320    suite.err() << "weighted:" << b.correlation() << std::endl;
321    suite.err() << "difference:" << a.correlation()-b.correlation() 
322                << std::endl;
323  } 
324  if ( !equal(a.x_averager(),b.x_averager(),tol,suite)) {
325    ok =false;
326    suite.err() << "error for x_averager():\t" << std::endl;
327  }
328  return ok;
329}
330bool equal(const AveragerPairWeighted& a, const AveragerPairWeighted& b, 
331           const unsigned int tol, Suite& suite)
332{
333  bool ok = true;
334  if  ( !suite.equal(a.covariance(),b.covariance(),tol) ){
335    suite.add(false);
336    suite.err() << "error covariance: " << a.covariance() << "\t" 
337           << b.covariance() << std::endl;
338  }
339  if ( !suite.equal(a.correlation(), b.correlation(), tol) ) {
340    suite.add(false);
341    suite.err() << "error correlation" << std::endl;
342    suite.err() << "a:" << a.correlation() << std::endl;
343    suite.err() << "b:" << b.correlation() << std::endl;
344    suite.err() << "difference:" << a.correlation()-b.correlation() 
345                << std::endl;
346    suite.err() << "tol:" << tol << std::endl;
347  } 
348  if ( !equal(a.x_averager(),b.x_averager(),tol,suite)) {
349    ok =false;
350    suite.err() << "error for x_averager():\t" << std::endl;
351  }
352  return ok;
353}
354
355
356
Note: See TracBrowser for help on using the repository browser.