Changeset 484
 Timestamp:
 Jan 3, 2006, 1:27:07 PM (17 years ago)
 Location:
 trunk
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

trunk/lib/statistics/Averager.h
r483 r484 111 111 /// 112 112 inline double variance(const double m) const 113 { return n_ ? (xx_  2*m*x_ + m*m ) /n_ : 0; }113 { return n_ ? (xx_  2*m*x_ + m*m*n()) /n_ : 0; } 114 114 115 115 /// 
trunk/test/averager_test.cc
r401 r484 8 8 #include <iostream> 9 9 10 using namespace theplu::statistics; 11 12 bool equal(const Averager& a, const Averager& b) 13 { 14 return a.n()==b.n() && a.mean()==b.mean() && a.variance()==b.variance(); 15 } 16 10 17 int main(const int argc,const char* argv[]) 11 18 { 12 using namespace theplu; 19 13 20 std::ostream* error; 14 21 if (argc>1 && argv[1]==std::string("v")) … … 19 26 std::cout << "averager_test v : for printing extra information\n"; 20 27 } 21 *error << "testing averagers" << std::endl;22 28 bool ok = true; 23 29 24 theplu::statistics::Averager a; 30 *error << "testing Averager" << std::endl; 31 Averager a; 32 a.add(1); 33 a.add(3); 34 a.add(5); 35 if (a.n()!=3  a.mean()!=3  a.sum_xx()!=35){ 36 ok=false; 37 *error << "error: add\n"; 38 } 39 40 Averager* a1 = new Averager(1+3+5,1+9+25,3); 41 if (!equal(a,*a1)){ 42 ok=false; 43 *error << "error: Averager(const double x,const double xx,const long n)\n"; 44 } 45 delete a1; 46 47 a1 = new Averager(a); 48 if (!equal(a,*a1)){ 49 ok=false; 50 *error << "error: Copy constructor\n"; 51 } 52 delete a1; 53 54 a.add(3,5); 55 if (! a.standard_error()==sqrt(a.variance()/a.n())){ 56 ok=false; 57 *error << "error: standard_error\n"; 58 } 59 60 61 if ( a.variance() != a.std()*a.std() ){ 62 ok=false; 63 *error << "error: std squared should be variance" << std::endl; 64 *error << "std: " << a.std() << std::endl; 65 *error << "variance: " << a.variance() << std::endl; 66 } 67 68 if ( a.variance() != a.variance(a.mean()) ){ 69 ok=false; 70 *error << "error: variance incorrect\n" << std::endl; 71 *error << "variance: " << a.variance() << std::endl; 72 *error << "mean: " << a.mean() << std::endl; 73 *error << "variance(mean) " << a.variance(a.mean()) << std::endl; 74 } 75 25 76 theplu::statistics::AveragerWeighted aw; 26 77 theplu::statistics::AveragerPair ap;
Note: See TracChangeset
for help on using the changeset viewer.