Changeset 484


Ignore:
Timestamp:
Jan 3, 2006, 1:27:07 PM (16 years ago)
Author:
Peter
Message:

added tests to Averager and fixed bug

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/statistics/Averager.h

    r483 r484  
    111111    ///
    112112    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; }
    114114
    115115    ///
  • trunk/test/averager_test.cc

    r401 r484  
    88#include <iostream>
    99
     10using namespace theplu::statistics;
     11
     12bool equal(const Averager& a, const Averager& b)
     13{
     14  return a.n()==b.n() && a.mean()==b.mean() && a.variance()==b.variance();
     15}
     16
    1017int main(const int argc,const char* argv[])
    1118
    12   using namespace theplu;
     19
    1320  std::ostream* error;
    1421  if (argc>1 && argv[1]==std::string("-v"))
     
    1926      std::cout << "averager_test -v : for printing extra information\n";
    2027  }
    21   *error << "testing averagers" << std::endl;
    2228  bool ok = true;
    2329
    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
    2576  theplu::statistics::AveragerWeighted aw;
    2677  theplu::statistics::AveragerPair ap;
Note: See TracChangeset for help on using the changeset viewer.