Changeset 487


Ignore:
Timestamp:
Jan 4, 2006, 6:10:27 PM (16 years ago)
Author:
Peter
Message:

mad covariance calculation coherent with variance and added tests for
AveragerPair?

Location:
trunk
Files:
2 edited

Legend:

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

    r483 r487  
    1212namespace theplu{
    1313namespace statistics{
     14
    1415  ///
    1516  /// Class for taking care of mean and covariance of two variables.
     
    6768 
    6869    ///
    69     /// The covariance is calculated using the \f$ (n-1) \f$
    70     /// correction, which means it is the best unbiased estimator of
    71     /// the covariance \f$ \frac{1}{N-1}\sum_i (x_i-m_x)(y_i-m_y)\f$,
     70    /// Calculating covariance using
     71    /// \f$ \frac{1}{N}\sum_i (x_i-m_x)(y_i-m_y)\f$,
    7272    /// where \f$m\f$ is the mean.
    7373    ///
     
    7575    ///
    7676    inline double covariance(void) const
    77       { return (n()>1) ? (xy_ - x_.sum_x()*y_.mean()) / (n()-1): 0; }
     77      { return (n()>1) ? (xy_ - x_.sum_x()*y_.mean()) / n(): 0; }
    7878 
    7979    ///
  • trunk/test/averager_test.cc

    r486 r487  
    148148 
    149149
    150   theplu::statistics::AveragerPair ap;
     150  AveragerPair ap;
    151151  for (int i=0; i<10; i++)
    152152    ap.add(i,i);
    153 
     153  if (fabs(ap.correlation()-1)>tol){
     154    ok=false;
     155    *error << "correlation: " << ap.correlation() << std::endl;
     156    *error << "error: correlation between identical vectors should be unity"
     157           << std::endl;
     158  }
     159  if (ap.x_averager().variance()!=ap.covariance()){
     160    ok=false;
     161    *error << "error: covariance of identical vectors should equal to variance"
     162           << std::endl;
     163  }
     164  AveragerPair* ap2 = new AveragerPair(ap);
     165  delete ap2;
    154166
    155167  if (error!=&std::cerr)
Note: See TracChangeset for help on using the changeset viewer.