UtilBlockedRunningAverage Class Reference
[Util]

Maintain a blocked running average. More...

#include <UtilBlockedRunningAverage.h>

Inheritance diagram for UtilBlockedRunningAverage:
UtilRunningAverage

List of all members.

Public Member Functions

 UtilBlockedRunningAverage (int size=10)
virtual ~UtilBlockedRunningAverage ()
void SetSampleSize (int size)
int Add (double val)
int Add (double mean, double sigma, int nsamples)
double LastSample () const
virtual void Clear (bool clear_population=true)

Private Attributes

std::deque< double > fMeans
std::deque< double > fSigmas
std::deque< size_t > fNsamples

Detailed Description

Maintain a blocked running average.

This class is like UtilRunningAverage but instead of only allowing single samples to be accumulated, it allows blocks of samples that already have statistics calculated on them to be accumulated such that correct statistics can be formed on the union of all blocks.

In this class, the "population" statistics will be identical to that of UtilRunningAverage.

The "sample" statistics will in general differ because a different rule governs the sample size. Namely this class will keep all blocked samples such that removing the oldest does not bring the sample size below the target.

All blocked samples 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.
This class uses UtilRunningAverage to perform some of the lifting.
Author:
(last to touch it)
Author
bv
Version:
Revision
1.2
Date:
Date
2005/04/15 14:19:15

Contact: bv@bnl.gov

Created on: Fri Apr 15 10:13:08 2005

Id
UtilBlockedRunningAverage.h,v 1.2 2005/04/15 14:19:15 bv Exp

Definition at line 49 of file UtilBlockedRunningAverage.h.


Constructor & Destructor Documentation

UtilBlockedRunningAverage::UtilBlockedRunningAverage ( int  size = 10  ) 

Definition at line 3 of file UtilBlockedRunningAverage.cxx.

00004     : UtilRunningAverage(size)
00005 {
00006 }

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

Definition at line 52 of file UtilBlockedRunningAverage.h.

00052 {}


Member Function Documentation

int UtilBlockedRunningAverage::Add ( double  mean,
double  sigma,
int  nsamples 
)

Add a new block of samples. Return the size of the sample set after this addition. Note, this size be be smaller than before the addition if a larger block is pushed out of the set. However, once the sample size is larger than the target, it will never be allowed to go smaller than the target.

Definition at line 40 of file UtilBlockedRunningAverage.cxx.

References fMeans, fNsamples, UtilRunningAverage::fPn, UtilRunningAverage::fPx, UtilRunningAverage::fPxx, UtilRunningAverage::fSampleTargetSize, fSigmas, UtilRunningAverage::fSn, UtilRunningAverage::fSx, UtilRunningAverage::fSxx, and SetSampleSize().

00041 {
00042     fNsamples.push_front(nsamples);
00043     fMeans.push_front(mean);
00044     fSigmas.push_front(sigma);
00045 
00046     double x = nsamples*mean;
00047     double xx = sigma*sigma*(nsamples-1) + nsamples*mean*mean;
00048 
00049     fSx += x;
00050     fSxx += xx;
00051 
00052     fPx += x;
00053     fPxx += xx;
00054 
00055     fPn += nsamples;
00056     fSn += nsamples;
00057 
00058     this->SetSampleSize(fSampleTargetSize); // remove any blocks if total sample is full
00059     return fSn;
00060 }

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

Add a new single (not blocked!) value to the sample and population. Returns the size of the sample set after this addition.

Reimplemented from UtilRunningAverage.

Definition at line 36 of file UtilBlockedRunningAverage.cxx.

00037 {
00038     return this->Add(val,0,1);
00039 }

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

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

Reimplemented from UtilRunningAverage.

Definition at line 67 of file UtilBlockedRunningAverage.cxx.

References fMeans, fNsamples, and fSigmas.

00068 {
00069     this->UtilRunningAverage::Clear(clear_population);
00070     fMeans.clear();
00071     fSigmas.clear();
00072     fNsamples.clear();
00073 }

double UtilBlockedRunningAverage::LastSample (  )  const [virtual]

Return the last sample, ie, the last mean added. Returns 0.0 if samples are empty.

Reimplemented from UtilRunningAverage.

Definition at line 61 of file UtilBlockedRunningAverage.cxx.

References fMeans.

00062 {
00063     if (!fMeans.size()) return 0;
00064     return fMeans.back();
00065 }

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

Set the target sample size. The actual sample size may be temporarily smaller than this. Once full, the actual sample size will never go below this but in general will fluctuate higher as more blocks of samples are added or removed.

Reimplemented from UtilRunningAverage.

Definition at line 7 of file UtilBlockedRunningAverage.cxx.

References fMeans, fNsamples, UtilRunningAverage::fSampleTargetSize, fSigmas, UtilRunningAverage::fSn, UtilRunningAverage::fSx, and UtilRunningAverage::fSxx.

Referenced by Add().

00008 {
00009     fSampleTargetSize = size;                   // target size
00010 
00011     size_t current_size = fNsamples.size();
00012     if (!current_size) return;
00013 
00014     size_t nlost = fNsamples.back();    
00015     while (fSn - nlost >= fSampleTargetSize) {
00016 
00017         double lost_mean = fMeans.back();
00018         double lost_sigma = fSigmas.back();
00019         
00020         double x = nlost*lost_mean;
00021         double xx = lost_sigma*lost_sigma*(nlost-1) + nlost*lost_mean*lost_mean;
00022         
00023         fSn -= nlost;
00024         fSx -= x;
00025         fSxx -= xx;
00026 
00027         fNsamples.pop_back();
00028         fMeans.pop_back();
00029         fSigmas.pop_back();
00030 
00031         if (!fNsamples.size()) break;
00032         nlost = fNsamples.back();
00033     }
00034 }


Member Data Documentation

std::deque<double> UtilBlockedRunningAverage::fMeans [private]

Definition at line 82 of file UtilBlockedRunningAverage.h.

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

std::deque<size_t> UtilBlockedRunningAverage::fNsamples [private]

Definition at line 83 of file UtilBlockedRunningAverage.h.

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

std::deque<double> UtilBlockedRunningAverage::fSigmas [private]

Definition at line 82 of file UtilBlockedRunningAverage.h.

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


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

Generated on 2 Nov 2017 for loon by  doxygen 1.6.1