CMS 3D CMS Logo

Accumulator.cc

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

Generated on Tue Jun 9 17:26:02 2009 for CMSSW by  doxygen 1.5.4