UtilRunningAverage Class Reference
[Util]

Maintain a running average. More...

#include <UtilRunningAverage.h>

Inheritance diagram for UtilRunningAverage:
UtilBlockedRunningAverage

List of all members.

Public Member Functions

 UtilRunningAverage (int size=10)
 Create a UtilRunningAverage with optional sample size.
virtual ~UtilRunningAverage ()
virtual void SetSampleSize (int size)
 Set the number of samples to retain.
virtual int GetSampleSize () const
 Get the current sample size.
int GetPopulationSize () const
 Get the current population size.
bool IsSampleFull () const
 Return true if the sample set is up to the configured size.
virtual int Add (double val)
double SampleMean () const
double PopulationMean () const
 As above but for the population.
double SampleSigma () const
double PopulationSigma () const
 As above but for the population.
virtual double LastSample () const
 Return the last sample. Returns 0.0 if samples are empty.
virtual void Clear (bool clear_population=true)
double SampleDeviation (double value) const
double PopulationDeviation (double value) const
 Same as above but for the population.

Protected Attributes

std::deque< double > fSamples
double fSx
double fSxx
double fPx
double fPxx
size_t fSampleTargetSize
size_t fSn
size_t fPn

Detailed Description

Maintain a running average.

This class maintains "sample" and "population" statistics (mean and standard deviation) of some scalar quantity with unknown variance. The "population" include every value added to this object since creation or since calling the Clear() method. The sample consists of the most "recent" additions as defined by SetSize() or at construction time. All additions in the sample are retained so that the statistics can be calculated in O(1) time.

Note:
Although the labels "sample" and "population" are used, both are treated as samples for the purpose of statistic calculations.
Author:
(last to touch it)
Author
rhatcher
Version:
Revision
1.9
Date:
Date
2011/10/27 18:28:26

Contact: bv@bnl.gov

Created on: Fri Apr 15 10:15:53 2005

Id
UtilRunningAverage.h,v 1.9 2011/10/27 18:28:26 rhatcher Exp

Definition at line 39 of file UtilRunningAverage.h.


Constructor & Destructor Documentation

UtilRunningAverage::UtilRunningAverage ( int  size = 10  ) 

Create a UtilRunningAverage with optional sample size.

Definition at line 7 of file UtilRunningAverage.cxx.

References SetSampleSize().

00008     :fSx(0.0),fSxx(0.0),fPx(0.0),fPxx(0.0)
00009     , fSampleTargetSize(0), fSn(0), fPn(0)
00010 {
00011     this->SetSampleSize(size);
00012 }

virtual UtilRunningAverage::~UtilRunningAverage (  )  [inline, virtual]

Definition at line 43 of file UtilRunningAverage.h.

00043 {}


Member Function Documentation

int UtilRunningAverage::Add ( double  val  )  [virtual]

Add a new value to the sample and population. Returns the size of the sample set after this addition.

Reimplemented in UtilBlockedRunningAverage.

Definition at line 39 of file UtilRunningAverage.cxx.

References fPn, fPx, fPxx, fSamples, fSampleTargetSize, fSn, fSx, and fSxx.

Referenced by BDScalar::SetSpill().

00040 {
00041     // Remove effect of oldest sample if sample set is full
00042     if (fSn == fSampleTargetSize) {
00043         double old = fSamples.back();
00044         fSamples.pop_back();
00045         fSx -= old;
00046         fSxx -= old*old;
00047         --fSn;
00048     }
00049 
00050     // Add new sample
00051     fSamples.push_front(val);
00052     fSx += val;
00053     fSxx += val*val;
00054     ++fSn;
00055 
00056     fPx += val;
00057     fPxx += val*val;
00058     ++fPn;
00059     
00060     return fSn;
00061 }

void UtilRunningAverage::Clear ( bool  clear_population = true  )  [virtual]

Clear all samples. If clear_population is false, only the sample set is cleared.

Reimplemented in UtilBlockedRunningAverage.

Definition at line 99 of file UtilRunningAverage.cxx.

References fPn, fPx, fPxx, fSamples, fSn, fSx, and fSxx.

00100 {
00101     fSx = fSxx = 0.0;
00102     fSamples.clear();
00103     fSn = 0;
00104 
00105     if (clear_population) {
00106         fPx = fPxx = 0.0;
00107         fPn = 0;
00108     }
00109 }

int UtilRunningAverage::GetPopulationSize (  )  const

Get the current population size.

Definition at line 29 of file UtilRunningAverage.cxx.

References fPn.

00030 {
00031     return fPn;
00032 }

int UtilRunningAverage::GetSampleSize (  )  const [virtual]

Get the current sample size.

Definition at line 25 of file UtilRunningAverage.cxx.

References fSn.

00026 {
00027     return fSn;
00028 }

bool UtilRunningAverage::IsSampleFull (  )  const

Return true if the sample set is up to the configured size.

Definition at line 34 of file UtilRunningAverage.cxx.

References fSampleTargetSize, and fSn.

00035 {
00036     return fSn >= fSampleTargetSize;
00037 }

double UtilRunningAverage::LastSample (  )  const [virtual]

Return the last sample. Returns 0.0 if samples are empty.

Reimplemented in UtilBlockedRunningAverage.

Definition at line 93 of file UtilRunningAverage.cxx.

References fSamples, and fSn.

Referenced by BDScalar::GetValue().

00094 {
00095     if (!fSn) return 0;
00096     return fSamples.front();
00097 }

double UtilRunningAverage::PopulationDeviation ( double  value  )  const

Same as above but for the population.

Definition at line 122 of file UtilRunningAverage.cxx.

References PopulationMean(), and PopulationSigma().

00123 {
00124     double mean = this->PopulationMean();
00125     double sig = this->PopulationSigma();
00126     if (sig == 0.0) {
00127         if (value > mean)
00128             return numeric_limits<double>::infinity();
00129         return -numeric_limits<double>::infinity();
00130     }
00131     return (mean-value)/sig;
00132 }

double UtilRunningAverage::PopulationMean (  )  const

As above but for the population.

Definition at line 68 of file UtilRunningAverage.cxx.

References fPn, and fPx.

Referenced by PopulationDeviation().

00069 {
00070     if (!fPn) return 0.0;
00071     return fPx/fPn;
00072 }

double UtilRunningAverage::PopulationSigma (  )  const

As above but for the population.

Definition at line 83 of file UtilRunningAverage.cxx.

References fPn, fPx, and fPxx.

Referenced by PopulationDeviation().

00084 {
00085     if (fPn<=1) return 0.0;
00086     double dif = fPxx - fPx*fPx/fPn;
00087 
00088     if (dif < 0) dif = 0;
00089     double var = (dif)/(fPn-1);
00090     return sqrt(var);
00091 }

double UtilRunningAverage::SampleDeviation ( double  value  )  const

Convenience routine to return the number of sigma the value is away from the mean. The sign of the result reflects if the value is smaller or larger than the mean. The value can be +/- std::numeric_limits<double>::infinity() if Sigma is zero (either because of two few samples, or accidently).

Definition at line 111 of file UtilRunningAverage.cxx.

References SampleMean(), and SampleSigma().

Referenced by BDScalar::SetSpill().

00112 {
00113     double mean = this->SampleMean();
00114     double sig = this->SampleSigma();
00115     if (sig == 0.0) {
00116         if (value > mean)
00117             return numeric_limits<double>::infinity();
00118         return -numeric_limits<double>::infinity();
00119     }
00120     return (mean-value)/sig;
00121 }

double UtilRunningAverage::SampleMean (  )  const

Return the arithmetic mean. Will use the number of samples actually collected and will return 0.0 if there are no samples.

Definition at line 63 of file UtilRunningAverage.cxx.

References fSn, and fSx.

Referenced by SampleDeviation().

00064 {
00065     if (!fSn) return 0.0;
00066     return fSx/fSn;
00067 }

double UtilRunningAverage::SampleSigma (  )  const

Return the standard deviation of the arithmetic mean. Will use the number of samples actually collected and will return 0.0 if there are not more than 1 sample.

Definition at line 74 of file UtilRunningAverage.cxx.

References fSn, fSx, and fSxx.

Referenced by SampleDeviation().

00075 {
00076     if (fSn<=1) return 0.0;
00077     double dif = fSxx - fSx*fSx/fSn;
00078 
00079     if (dif < 0) dif = 0;
00080     double var = (dif)/(fSn-1);
00081     return sqrt(var);
00082 }

void UtilRunningAverage::SetSampleSize ( int  size  )  [virtual]

Set the number of samples to retain.

Reimplemented in UtilBlockedRunningAverage.

Definition at line 14 of file UtilRunningAverage.cxx.

References fSamples, fSampleTargetSize, fSn, fSx, and fSxx.

Referenced by BDScalar::SetSpillQueueSize(), and UtilRunningAverage().

00015 {
00016     fSampleTargetSize = size;
00017     while (fSn > fSampleTargetSize) {
00018         double old = fSamples.back();
00019         fSamples.pop_back();
00020         fSx -= old;
00021         fSxx -= old*old;
00022         --fSn;
00023     }
00024 }


Member Data Documentation

size_t UtilRunningAverage::fPn [protected]
double UtilRunningAverage::fPx [protected]
double UtilRunningAverage::fPxx [protected]

Definition at line 96 of file UtilRunningAverage.h.

Referenced by UtilBlockedRunningAverage::Add(), Add(), Clear(), and PopulationSigma().

std::deque<double> UtilRunningAverage::fSamples [protected]

Definition at line 95 of file UtilRunningAverage.h.

Referenced by Add(), Clear(), LastSample(), and SetSampleSize().

size_t UtilRunningAverage::fSn [protected]
double UtilRunningAverage::fSx [protected]
double UtilRunningAverage::fSxx [protected]

The documentation for this class was generated from the following files:

Generated on 22 Nov 2017 for loon by  doxygen 1.6.1