CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/CommonTools/Statistics/src/Accumulator.cc

Go to the documentation of this file.
00001 #include "CommonTools/Statistics/interface/Accumulator.h"
00002 #include <ostream>
00003 
00004 Accumulator::Accumulator() 
00005 :  sum_(0.),
00006    sumOfSquares_(0.),
00007    weightedSum_(0.),
00008    sumOfWeights_(0.),
00009    n_(0)
00010 {
00011 }
00012 
00013 
00014 void Accumulator::addEntry(double value, double weight) {
00015   sum_ += value;
00016   sumOfSquares_ += (value*value);
00017   weightedSum_ += value*weight;
00018   sumOfWeights_ += weight;
00019   ++n_;
00020 }
00021 
00022 
00023 double Accumulator::mean() const {
00024   return sum_/n_;
00025 }
00026 
00027 
00028 double Accumulator::variance() const {
00029   double numerator = sumOfSquares_ - sum_*mean();
00030   unsigned long denominator = n_-1;
00031   return numerator/denominator;
00032 }
00033 
00034  
00035 double Accumulator::weightedMean() const  {
00036   return weightedSum_ / sumOfWeights_;
00037 }
00038 
00039 
00040 std::ostream& operator<<(std::ostream & os,const Accumulator & stat) {
00041   os << "entries: " << stat.nEntries();
00042   if(stat.nEntries() > 0) {
00043      os << "   Mean: " << stat.mean(); 
00044   }
00045   if(stat.nEntries() > 1) {      
00046                  os << "   Sigma: " << stat.sigma();
00047   }
00048   return os;
00049 }
00050 
00051