#include <UtilRunningAverage.h>
Inheritance diagram for UtilRunningAverage:

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 |
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.
Created on: Fri Apr 15 10:15:53 2005
Definition at line 38 of file UtilRunningAverage.h.
|
|
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 }
|
|
|
Definition at line 42 of file UtilRunningAverage.h. 00042 {}
|
|
|
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 fPx, fPxx, fSamples, 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 }
|
|
|
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. Referenced by UtilBlockedRunningAverage::Clear(). 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 }
|
|
|
Get the current population size.
Definition at line 29 of file UtilRunningAverage.cxx. 00030 {
00031 return fPn;
00032 }
|
|
|
Get the current sample size.
Definition at line 25 of file UtilRunningAverage.cxx. 00026 {
00027 return fSn;
00028 }
|
|
|
Return true if the sample set is up to the configured size.
Definition at line 34 of file UtilRunningAverage.cxx. References fSn. 00035 {
00036 return fSn >= fSampleTargetSize;
00037 }
|
|
|
Return the last sample. Returns 0.0 if samples are empty.
Reimplemented in UtilBlockedRunningAverage. Definition at line 93 of file UtilRunningAverage.cxx. References fSamples. Referenced by BDScalar::GetValue(). 00094 {
00095 if (!fSn) return 0;
00096 return fSamples.front();
00097 }
|
|
|
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 }
|
|
|
As above but for the population.
Definition at line 68 of file UtilRunningAverage.cxx. References fPx. Referenced by PopulationDeviation(). 00069 {
00070 if (!fPn) return 0.0;
00071 return fPx/fPn;
00072 }
|
|
|
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 }
|
|
|
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 }
|
|
|
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 fSx. Referenced by SampleDeviation(). 00064 {
00065 if (!fSn) return 0.0;
00066 return fSx/fSn;
00067 }
|
|
|
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 }
|
|
|
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 }
|
|
|
Definition at line 98 of file UtilRunningAverage.h. Referenced by Clear(), and PopulationSigma(). |
|
|
Definition at line 95 of file UtilRunningAverage.h. Referenced by Add(), Clear(), PopulationMean(), and PopulationSigma(). |
|
|
Definition at line 95 of file UtilRunningAverage.h. Referenced by Add(), Clear(), and PopulationSigma(). |
|
|
Definition at line 94 of file UtilRunningAverage.h. Referenced by Add(), Clear(), LastSample(), and SetSampleSize(). |
|
|
Definition at line 98 of file UtilRunningAverage.h. Referenced by SetSampleSize(). |
|
|
Definition at line 98 of file UtilRunningAverage.h. Referenced by Add(), Clear(), IsSampleFull(), SampleSigma(), and SetSampleSize(). |
|
|
Definition at line 95 of file UtilRunningAverage.h. Referenced by Add(), Clear(), SampleMean(), SampleSigma(), and SetSampleSize(). |
|
|
Definition at line 95 of file UtilRunningAverage.h. Referenced by Add(), Clear(), SampleSigma(), and SetSampleSize(). |
1.3.9.1