PSCF v1.5.0
AverageStage.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, David Morse (morse012@um.edu)
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "AverageStage.h"
9#include <util/format/Int.h>
10#include <util/format/Dbl.h>
11
12#include <math.h>
13
14namespace Util
15{
16
17 /*
18 * Constructor for rootPtr AverageStage, with stageId = 0.
19 */
21 : shift_(0.0),
22 sum_(0.0),
23 sumSq_(0.0),
24 blockSum_(0.0),
25 nSample_(0),
26 nBlockSample_(0),
27 stageInterval_(1),
28 childPtr_(0),
29 rootPtr_(0),
30 stageId_(0),
31 blockFactor_(blockFactor)
32 { rootPtr_ = this; }
33
34 /*
35 * Constructor for dynamically generated objects with stageId > 0 (private).
36 */
37 AverageStage::AverageStage(long stageInterval, int stageId,
38 AverageStage* rootPtr, int blockFactor)
39 : shift_(0.0),
40 sum_(0.0),
41 sumSq_(0.0),
42 blockSum_(0.0),
43 nSample_(0),
44 nBlockSample_(0),
45 stageInterval_(stageInterval),
46 childPtr_(0),
47 rootPtr_(rootPtr),
48 stageId_(stageId),
49 blockFactor_(blockFactor)
50 {}
51
52 /*
53 * Destructor.
54 */
56 {
57 if (childPtr_) {
58 delete childPtr_;
59 }
60 }
61
62 /*
63 * Reset the block factor.
64 */
65 void AverageStage::setBlockFactor(int blockFactor)
66 {
67 if (nSample_ > 0) {
68 UTIL_THROW("Attempt to reset block factor when nSample > 0");
69 }
70 if (blockFactor < 2) {
71 UTIL_THROW("Invalid value of blockFactor");
72 }
73 blockFactor_ = blockFactor;
74 }
75
76 void AverageStage::registerDescendant(AverageStage* descendantPtr)
77 {}
78
79 /*
80 * Reset all accumulators and counters to zero.
81 */
83 {
84 shift_ = 0.0;
85 sum_ = 0.0;
86 sumSq_ = 0.0;
87 blockSum_ = 0.0;
88 nSample_ = 0;
89 nBlockSample_ = 0;
90 if (childPtr_) {
91 // The child's destructor deletes its own descendants.
92 delete childPtr_;
93 childPtr_ = 0;
94 }
95 }
96
97 /*
98 * Add a sampled value to the ensemble.
99 */
100 void AverageStage::sample(double value)
101 {
102
103 // Increment global accumulators. The sums are taken relative to
104 // shift_, the first sampled value (or, after loading, the average
105 // of the loaded values): sumSq/n - (sum/n)^2 of raw values loses
106 // all precision when the standard deviation is small compared to
107 // the mean (it can even be negative).
108 if (nSample_ == 0) {
109 shift_ = value;
110 }
111 const double d = value - shift_;
112 sum_ += d;
113 sumSq_ += d*d;
114 ++nSample_;
115
116 // Increment block accumulators
117 blockSum_ += value;
118 ++nBlockSample_;
119
120 if (nBlockSample_ == blockFactor_) {
121
122 if (!childPtr_) {
123 long nextStageInterval = stageInterval_*blockFactor_;
124 int nextStageId = stageId_ + 1;
125 childPtr_ = new AverageStage(nextStageInterval, nextStageId,
126 rootPtr_, blockFactor_);
127 rootPtr_->registerDescendant(childPtr_);
128 }
129
130 // Add block average to child ensemble
131 childPtr_->sample(blockSum_ / double(blockFactor_));
132
133 // Reset block accumulators
134 blockSum_ = 0.0;
135 nBlockSample_ = 0;
136
137 }
138
139 }
140
141 /*
142 * Return the average of all sampled values.
143 */
145 { return shift_ + sum_/double(nSample_); }
146
147 /*
148 * Return the variance of all sampled values
149 */
151 {
152 double rSample_ = double(nSample_);
153 double ave = sum_/rSample_;
154 double aveSq = sumSq_/rSample_;
155 double var = aveSq - ave*ave;
156 // Rounding errors can make var slightly negative
157 return (var < 0.0) ? 0.0 : var;
158 }
159
160 /*
161 * Return the standard deviation of all sampled values.
162 */
164 { return sqrt(variance()); }
165
166 /*
167 * Return the number of sampled values.
168 */
170 { return nSample_; }
171
172 /*
173 * Return the number of measured values per sample at this stage.
174 */
176 { return stageInterval_; }
177
178 /*
179 * Estimated statistical error of the average.
180 */
181 double AverageStage::error() const
182 { return sqrt(variance()/double(nSample_-1)); }
183
184}
Evaluate average with hierarchical blocking error analysis.
void setBlockFactor(int blockFactor)
Reset the value of blockFactor.
virtual ~AverageStage()
Destructor.
long stageInterval() const
Return the number of sampled values per block at this stage.
virtual void sample(double value)
Add a sampled value to the ensemble.
virtual void clear()
Initialize all accumulators and recursively destroy all children.
AverageStage(int blockFactor=2)
Constructor.
double average() const
Return the average of all sampled values.
double variance() const
Return the variance of all sampled values.
double error() const
Return a naive estimate for the std deviation of the average.
long nSample() const
Return the number of sampled values in this sequence.
double stdDeviation() const
Return the standard deviation of all sampled values.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
Utility classes for scientific computation.