Changeset 490


Ignore:
Timestamp:
Jan 4, 2006, 9:32:05 PM (16 years ago)
Author:
Peter
Message:

added tests for AveragerPairWeighted?, corrected docs, and added an
add(vector) for AvergaerPair?

Location:
trunk
Files:
5 edited

Legend:

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

    r489 r490  
    1616  /// Class to calculate simple (first and second moments) averages.
    1717  ///
    18   /// @see Averager AveragerPair AveragerPairWeighted
     18  /// @see AveragerWeighted AveragerPair AveragerPairWeighted
    1919  ///
    2020  class Averager
  • trunk/lib/statistics/AveragerPair.cc

    r420 r490  
    44#include <c++_tools/statistics/Averager.h>
    55
     6#include <cassert>
    67#include <utility>
    78
     
    1718    return *this;
    1819  }
    19 
     20 
     21  void AveragerPair::add(const gslapi::vector& x, const gslapi::vector& y)
     22  {
     23    assert(x.size()==y.size());
     24    for (size_t i=0; i<x.size(); i++)
     25      add(x(i),y(i));
     26  }
    2027
    2128}} // of namespace statistics and namespace theplu
  • trunk/lib/statistics/AveragerPair.h

    r489 r490  
    1616  /// Class for taking care of mean and covariance of two variables.
    1717  ///
    18   /// @see Averager AveragerPair AveragerPairWeighted
     18  /// @see Averager AveragerWeighted AveragerPairWeighted
    1919  ///
    2020  class AveragerPair
     
    4646    inline void add(const double x, const double y, const unsigned long n=1)
    4747      { x_.add(x,n); y_.add(y,n), xy_ += n*x*y; }
     48
     49    ///
     50    /// Adding pairs of data points with value \a x and \a y.
     51    ///
     52    void add(const gslapi::vector& x, const gslapi::vector& y);
    4853
    4954    ///
  • trunk/lib/statistics/AveragerPairWeighted.h

    r489 r490  
    2626  /// does not change the performance of the object.</li></ul>
    2727  ///
    28   /// @see Averager AveragerPair AveragerPairWeighted
     28  /// @see Averager AveragerWeighted AveragerPair
    2929  ///
    3030  class AveragerPairWeighted
  • trunk/test/averager_test.cc

    r487 r490  
    33#include <c++_tools/statistics/Averager.h>
    44#include <c++_tools/statistics/AveragerPair.h>
     5#include <c++_tools/statistics/AveragerPairWeighted.h>
    56#include <c++_tools/statistics/AveragerWeighted.h>
    67#include <c++_tools/gslapi/vector.h>
     
    1112using namespace theplu::statistics;
    1213
    13 bool equal(const Averager& a, const Averager& b)
    14 {
    15   return (a.n()==b.n() && a.mean()==b.mean() && a.variance()==b.variance());
    16 }
    17 
    18 bool equal(const AveragerWeighted& a, const AveragerWeighted& b, double tol,
    19            std::ostream* error)
    20 {
    21   bool equal = true;
    22   if  ( fabs(a.mean()-b.mean())>tol){
    23     equal=false;
    24     *error << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
    25   }
    26   if ( fabs(a.variance()-b.variance())>tol ) {
    27     equal=false;
    28     *error << "error for variance:\t" << a.variance() << " " << b.variance()
    29            << std::endl;
    30   }
    31   if ( fabs(a.standard_error()-b.standard_error())>tol ) {
    32     equal =false;
    33     *error << "error for standard error:\t" << std::endl;
    34   }
    35   return equal;
    36 }
     14//Forward declarations
     15bool equal(const Averager&, const Averager&);
     16bool equal(const AveragerWeighted&, const AveragerWeighted&, const double,
     17           std::ostream* error);
     18bool equal(const Averager&, const AveragerWeighted&, const double,
     19           std::ostream* error);
     20bool equal(const AveragerPair&, const AveragerPair&,
     21           const double, std::ostream* error);
     22bool equal(const AveragerPair&, const AveragerPairWeighted&,
     23           const double, std::ostream* error);
     24bool equal(const AveragerPairWeighted&, const AveragerPairWeighted&,
     25           const double, std::ostream* error);
     26
    3727
    3828int main(const int argc,const char* argv[])
     
    10797  a.reset();
    10898  a.add(x);
    109   double tol=0.0001;
    110   if (fabs(a.mean()-aw.mean())>tol ||
    111       fabs(a.variance()-aw.variance())>tol ||
    112       fabs(a.standard_error()-aw.standard_error())>tol ||
    113       fabs(a.std()-aw.std())>tol){
     99  const double tol=0.0001;
     100  if (!equal(a,aw,tol,error)){
    114101    *error << "error: AveragerWeighted with unitary weights should "
    115102           << "be equal to Averager" << std::endl;
    116     *error << "\tAverager\tAveragerWeighted" << std::endl;
    117     *error << "mean:\t" << a.mean() << "\t" << aw.mean() << std::endl;
    118     *error << "var:\t" << a.variance() << "\t" << aw.variance() << std::endl;
    119     *error << "ste:\t" << a.standard_error() << "\t" << aw.standard_error()
    120            << std::endl;
    121     *error << "std:\t" << a.std() << "\t" << aw.std() << std::endl;
    122 
    123103    ok=false;
    124104  }
     
    148128 
    149129
     130  *error << "testing AveragerPair" << std::endl;
    150131  AveragerPair ap;
    151132  for (int i=0; i<10; i++)
     
    165146  delete ap2;
    166147
     148  *error << "testing AveragerPairWeighted" << std::endl;
     149  AveragerPairWeighted apw;
     150  x(0)=0; x(1)=1; x(2)=2;
     151  theplu::gslapi::vector y(3,0);
     152  x(0)=0; x(1)=0; x(2)=2;
     153  apw.add(x,y,w,w);
     154  ap.reset();
     155  ap.add(x,y);
     156  if (!equal(ap,apw,tol,error)){
     157    *error << "error: AveragerPairWeighted with unitary weights should "
     158           << "be equal to AveragerPair" << std::endl;
     159    ok=false;
     160  }
     161
     162  AveragerPairWeighted* apw2 = new AveragerPairWeighted(apw);
     163  if (!equal(apw,*apw2,tol,error)){
     164    *error << "error: AveragerPairWeighted copy constructor " << std::endl;
     165    ok=false;
     166  }
     167   
     168  apw2->add(12,23222.03,32.3,0);
     169  if (!equal(apw,*apw2,tol,error)){
     170    *error << "error: AveragerWeighted adding a data point with weight=0 "
     171           << "should make no change " << std::endl;
     172    ok=false;
     173  }
     174     
     175  apw2->reset();
     176  w.scale(17);
     177  apw2->add(x,y,w,w);
     178  if (!equal(apw,*apw2,tol,error)){
     179    *error << "error: AveragerWeighted rescaling weights "
     180           << "should make no change " << std::endl;
     181    ok=false;
     182  }
     183  delete apw2;
     184
    167185  if (error!=&std::cerr)
    168186    delete error;
     
    173191}
    174192
    175 
    176 
     193bool equal(const Averager& a, const Averager& b)
     194{
     195  return (a.n()==b.n() && a.mean()==b.mean() && a.variance()==b.variance());
     196}
     197
     198bool equal(const AveragerWeighted& a, const AveragerWeighted& b,
     199           const double tol, std::ostream* error)
     200{
     201  bool equal = true;
     202  if  ( fabs(a.mean()-b.mean())>tol){
     203    equal=false;
     204    *error << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
     205  }
     206  if ( fabs(a.variance()-b.variance())>tol ) {
     207    equal=false;
     208    *error << "error for variance:\t" << a.variance() << " " << b.variance()
     209           << std::endl;
     210  }
     211  if ( fabs(a.standard_error()-b.standard_error())>tol ) {
     212    equal =false;
     213    *error << "error for standard error:\t" << std::endl;
     214  }
     215  return equal;
     216}
     217
     218bool equal(const Averager& a, const AveragerWeighted& b, const double tol,
     219           std::ostream* error)
     220{
     221  bool equal = true;
     222  if  ( fabs(a.mean()-b.mean())>tol){
     223    equal=false;
     224    *error << "mean:\t" << a.mean() << "\t" << b.mean() << std::endl;
     225  }
     226  if ( fabs(a.variance()-b.variance())>tol ) {
     227    equal=false;
     228    *error << "error for variance:\t" << a.variance() << " " << b.variance()
     229           << std::endl;
     230  }
     231  if ( fabs(a.standard_error()-b.standard_error())>tol ) {
     232    equal =false;
     233    *error << "error for standard error:\t" << std::endl;
     234  }
     235  return equal;
     236}
     237
     238bool equal(const AveragerPair& a, const AveragerPair& b,
     239           const double tol, std::ostream* error)
     240{
     241  bool ok = true;
     242  if  ( fabs(a.covariance()-b.covariance())>tol){
     243    ok=false;
     244    *error << "error covariance: " << a.covariance() << "\t"
     245           << b.covariance() << std::endl;
     246  }
     247  if ( fabs(a.correlation()-b.correlation())>tol ) {
     248    ok=false;
     249    *error << "error correlation" << std::endl;
     250  }
     251  return ok;
     252}
     253
     254bool equal(const AveragerPair& a, const AveragerPairWeighted& b,
     255           const double tol, std::ostream* error)
     256{
     257  bool ok = true;
     258  if  ( fabs(a.covariance()-b.covariance())>tol){
     259    ok=false;
     260    *error << "error covariance: " << a.covariance() << "\t"
     261           << b.covariance() << std::endl;
     262  }
     263  if ( fabs(a.correlation()-b.correlation())>tol ) {
     264    ok=false;
     265    *error << "error correlation" << std::endl;
     266  }
     267  if ( !equal(a.x_averager(),b.x_averager(),tol,error)) {
     268    ok =false;
     269    *error << "error for x_averager():\t" << std::endl;
     270  }
     271  return ok;
     272}
     273bool equal(const AveragerPairWeighted& a, const AveragerPairWeighted& b,
     274           const double tol, std::ostream* error)
     275{
     276  bool ok = true;
     277  if  ( fabs(a.covariance()-b.covariance())>tol){
     278    ok=false;
     279    *error << "error covariance: " << a.covariance() << "\t"
     280           << b.covariance() << std::endl;
     281  }
     282  if ( fabs(a.correlation()-b.correlation())>tol ) {
     283    ok=false;
     284    *error << "error correlation" << std::endl;
     285  }
     286  if ( !equal(a.x_averager(),b.x_averager(),tol,error)) {
     287    ok =false;
     288    *error << "error for x_averager():\t" << std::endl;
     289  }
     290  return ok;
     291}
     292
     293
     294
Note: See TracChangeset for help on using the changeset viewer.