Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:12

0001 #include "CommonTools/Statistics/interface/Accumulator.h"
0002 #include <ostream>
0003 
0004 Accumulator::Accumulator() : sum_(0.), sumOfSquares_(0.), weightedSum_(0.), sumOfWeights_(0.), n_(0) {}
0005 
0006 void Accumulator::addEntry(double value, double weight) {
0007   sum_ += value;
0008   sumOfSquares_ += (value * value);
0009   weightedSum_ += value * weight;
0010   sumOfWeights_ += weight;
0011   ++n_;
0012 }
0013 
0014 double Accumulator::mean() const { return sum_ / n_; }
0015 
0016 double Accumulator::variance() const {
0017   double numerator = sumOfSquares_ - sum_ * mean();
0018   unsigned long denominator = n_ - 1;
0019   return numerator / denominator;
0020 }
0021 
0022 double Accumulator::weightedMean() const { return weightedSum_ / sumOfWeights_; }
0023 
0024 std::ostream& operator<<(std::ostream& os, const Accumulator& stat) {
0025   os << "entries: " << stat.nEntries();
0026   if (stat.nEntries() > 0) {
0027     os << "   Mean: " << stat.mean();
0028   }
0029   if (stat.nEntries() > 1) {
0030     os << "   Sigma: " << stat.sigma();
0031   }
0032   return os;
0033 }