source: trunk/test/averager_test.cc @ 2119

Last change on this file since 2119 was 2119, checked in by Peter, 14 years ago

converted files to utf-8. fixes #577

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