source: trunk/test/averager_test.cc @ 499

Last change on this file since 499 was 499, checked in by Peter, 17 years ago

set tolerance to round_error

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1// $Id: averager_test.cc 499 2006-01-18 16:26:09Z peter $
2
3#include <c++_tools/statistics/Averager.h>
4#include <c++_tools/statistics/AveragerPair.h>
5#include <c++_tools/statistics/AveragerPairWeighted.h>
6#include <c++_tools/statistics/AveragerWeighted.h>
7#include <c++_tools/gslapi/vector.h>
8
9#include <fstream>
10#include <limits>
11#include <iostream>
12
13using namespace theplu::statistics;
14
15//Forward declarations
16bool equal(const Averager&, const Averager&);
17bool equal(const AveragerWeighted&, const AveragerWeighted&, const double, 
18           std::ostream* error);
19bool equal(const Averager&, const AveragerWeighted&, const double, 
20           std::ostream* error);
21bool equal(const AveragerPair&, const AveragerPair&, 
22           const double, std::ostream* error);
23bool equal(const AveragerPair&, const AveragerPairWeighted&, 
24           const double, std::ostream* error);
25bool equal(const AveragerPairWeighted&, const AveragerPairWeighted&, 
26           const double, std::ostream* error);
27
28
29int main(const int argc,const char* argv[])
30{ 
31
32  std::ostream* error;
33  if (argc>1 && argv[1]==std::string("-v"))
34    error = &std::cerr;
35  else {
36    error = new std::ofstream("/dev/null");
37    if (argc>1)
38      std::cout << "averager_test -v : for printing extra information\n";
39  }
40  bool ok = true;
41
42  // Testing Averager
43  *error << "testing Averager" << std::endl;
44  Averager a;
45  a.add(1);
46  a.add(3);
47  a.add(5);
48  if (a.n()!=3 || a.mean()!=3 || a.sum_xx()!=35){
49    ok=false;
50    *error << "error: add\n";
51  }
52
53  Averager* a1 = new Averager(1+3+5,1+9+25,3);
54  if (!equal(a,*a1)){
55    ok=false;
56    *error << "error: Averager(const double x,const double xx,const long n)\n";
57  }
58  delete a1;
59
60  a1 = new Averager(a);
61  if (!equal(a,*a1)){
62    ok=false;
63    *error << "error: Copy constructor\n";
64  }
65  delete a1;
66
67  a.add(3,5);
68  if (! a.standard_error()==sqrt(a.variance()/a.n())){
69    ok=false;
70    *error << "error: standard_error\n";
71  }
72     
73
74  if ( fabs(a.variance() - a.std()*a.std())>
75       std::numeric_limits<double>().round_error() ){
76    ok=false;
77    *error << "error: std squared should be variance" << std::endl;
78    *error << "std2: " << a.std()*a.std() << std::endl;
79    *error << "variance: " << a.variance() << std::endl;
80    *error << "difference is: " << a.std()*a.std()-a.variance() << std::endl;
81  }
82 
83  if ( a.variance() != a.variance(a.mean()) ){
84    ok=false;
85    *error << "error: variance incorrect\n" << std::endl;
86    *error << "variance: " << a.variance() << std::endl;
87    *error << "mean: " << a.mean() << std::endl;
88    *error << "variance(mean) " << a.variance(a.mean()) << std::endl;
89  }
90
91  // Testing AveragerWeighted
92  *error << "testing AveragerWeighted" << std::endl;
93  theplu::gslapi::vector x(3,0);
94  x(0)=0;
95  x(1)=1;
96  x(2)=2;
97  theplu::gslapi::vector w(3,1);
98  theplu::statistics::AveragerWeighted aw;
99  aw.add(x,w);
100  a.reset();
101  a.add(x);
102  const double tol=std::numeric_limits<double>().round_error();
103  if (!equal(a,aw,tol,error)){
104    *error << "error: AveragerWeighted with unitary weights should " 
105           << "be equal to Averager" << std::endl;
106    ok=false;
107  }
108
109  AveragerWeighted* aw2 = new AveragerWeighted(aw);
110  if (!equal(aw,*aw2,tol,error)){
111    *error << "error: AveragerWeighted copy constructor " << std::endl;
112    ok=false;
113  }
114   
115  aw2->add(12,0);
116  if (!equal(aw,*aw2,tol,error)){
117    *error << "error: AveragerWeighted adding a data point with weight=0 " 
118           << "should make no change " << std::endl;
119    ok=false;
120  }
121     
122  aw2->reset();
123  w.scale(17);
124  aw2->add(x,w);
125  if (!equal(aw,*aw2,tol,error)){
126    *error << "error: AveragerWeighted rescaling weights " 
127           << "should make no change " << std::endl;
128    ok=false;
129  }
130  delete aw2;
131 
132
133  *error << "testing AveragerPair" << std::endl;
134  AveragerPair ap;
135  for (int i=0; i<10; i++)
136    ap.add(i,i);
137  if (fabs(ap.correlation()-1)>tol){
138    ok=false;
139    *error << "correlation: " << ap.correlation() << std::endl;
140    *error << "error: correlation between identical vectors should be unity" 
141           << std::endl;
142  }
143  if (ap.x_averager().variance()!=ap.covariance()){
144    ok=false;
145    *error << "error: covariance of identical vectors should equal to variance" 
146           << std::endl;
147  }
148  AveragerPair* ap2 = new AveragerPair(ap);
149  delete ap2;
150
151  *error << "testing AveragerPairWeighted" << std::endl;
152  AveragerPairWeighted apw;
153  x(0)=0; x(1)=1; x(2)=2;
154  theplu::gslapi::vector y(3,0);
155  x(0)=0; x(1)=0; x(2)=2;
156  apw.add(x,y,w,w);
157  ap.reset();
158  ap.add(x,y);
159  if (!equal(ap,apw,tol,error)){
160    *error << "error: AveragerPairWeighted with unitary weights should " 
161           << "be equal to AveragerPair" << std::endl;
162    ok=false;
163  }
164
165  AveragerPairWeighted* apw2 = new AveragerPairWeighted(apw);
166  if (!equal(apw,*apw2,tol,error)){
167    *error << "error: AveragerPairWeighted copy constructor " << std::endl;
168    ok=false;
169  }
170   
171  apw2->add(12,23222.03,32.3,0);
172  if (!equal(apw,*apw2,tol,error)){
173    *error << "error: AveragerWeighted adding a data point with weight=0 " 
174           << "should make no change " << std::endl;
175    ok=false;
176  }
177     
178  apw2->reset();
179  w.scale(17);
180  apw2->add(x,y,w,w);
181  if (!equal(apw,*apw2,tol,error)){
182    *error << "error: AveragerWeighted rescaling weights " 
183           << "should make no change " << std::endl;
184    ok=false;
185  }
186  delete apw2;
187
188  if (error!=&std::cerr)
189    delete error;
190
191  if (!ok)
192    return -1;
193  return 0;
194}
195
196bool equal(const Averager& a, const Averager& b)
197{
198  return (a.n()==b.n() && a.mean()==b.mean() && a.variance()==b.variance());
199}
200
201bool equal(const AveragerWeighted& a, const AveragerWeighted& b, 
202           const double tol, std::ostream* error)
203{
204  bool equal = true;
205  if  ( fabs(a.mean()-b.mean())>tol){
206    equal=false;
207    *error << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
208  }
209  if ( fabs(a.variance()-b.variance())>tol ) {
210    equal=false;
211    *error << "error for variance:\t" << a.variance() << " " << b.variance() 
212           << std::endl;
213  } 
214  if ( fabs(a.standard_error()-b.standard_error())>tol ) {
215    equal =false;
216    *error << "error for standard error:\t" << std::endl;
217  }
218  return equal;
219}
220
221bool equal(const Averager& a, const AveragerWeighted& b, const double tol, 
222           std::ostream* error)
223{
224  bool equal = true;
225  if  ( fabs(a.mean()-b.mean())>tol){
226    equal=false;
227    *error << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
228  }
229  if ( fabs(a.variance()-b.variance())>tol ) {
230    equal=false;
231    *error << "error for variance:\t" << a.variance() << " " << b.variance() 
232           << std::endl;
233  } 
234  if ( fabs(a.standard_error()-b.standard_error())>tol ) {
235    equal =false;
236    *error << "error for standard error:\t" << std::endl;
237  }
238  return equal;
239}
240
241bool equal(const AveragerPair& a, const AveragerPair& b, 
242           const double tol, std::ostream* error)
243{
244  bool ok = true;
245  if  ( fabs(a.covariance()-b.covariance())>tol){
246    ok=false;
247    *error << "error covariance: " << a.covariance() << "\t" 
248           << b.covariance() << std::endl;
249  }
250  if ( fabs(a.correlation()-b.correlation())>tol ) {
251    ok=false;
252    *error << "error correlation" << std::endl;
253  } 
254  return ok;
255}
256
257bool equal(const AveragerPair& a, const AveragerPairWeighted& b, 
258           const double tol, std::ostream* error)
259{
260  bool ok = true;
261  if  ( fabs(a.covariance()-b.covariance())>tol){
262    ok=false;
263    *error << "error covariance: " << a.covariance() << "\t" 
264           << b.covariance() << std::endl;
265  }
266  if ( fabs(a.correlation()-b.correlation())>tol ) {
267    ok=false;
268    *error << "error correlation" << std::endl;
269  } 
270  if ( !equal(a.x_averager(),b.x_averager(),tol,error)) {
271    ok =false;
272    *error << "error for x_averager():\t" << std::endl;
273  }
274  return ok;
275}
276bool equal(const AveragerPairWeighted& a, const AveragerPairWeighted& b, 
277           const double tol, std::ostream* error)
278{
279  bool ok = true;
280  if  ( fabs(a.covariance()-b.covariance())>tol){
281    ok=false;
282    *error << "error covariance: " << a.covariance() << "\t" 
283           << b.covariance() << std::endl;
284  }
285  if ( fabs(a.correlation()-b.correlation())>tol ) {
286    ok=false;
287    *error << "error correlation" << std::endl;
288  } 
289  if ( !equal(a.x_averager(),b.x_averager(),tol,error)) {
290    ok =false;
291    *error << "error for x_averager():\t" << std::endl;
292  }
293  return ok;
294}
295
296
297
Note: See TracBrowser for help on using the repository browser.