PSCF v1.1
AverageStage.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
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 : sum_(0.0),
22 sumSq_(0.0),
23 blockSum_(0.0),
24 nSample_(0),
25 nBlockSample_(0),
26 stageInterval_(1),
27 childPtr_(0),
28 rootPtr_(0),
29 stageId_(0),
30 blockFactor_(blockFactor)
31 { rootPtr_ = this; }
32
33 /*
34 * Constructor for dynamically generated objects with stageId > 0 (private).
35 */
36 AverageStage::AverageStage(long stageInterval, int stageId,
37 AverageStage* rootPtr, int blockFactor)
38 : sum_(0.0),
39 sumSq_(0.0),
40 blockSum_(0.0),
41 nSample_(0),
42 nBlockSample_(0),
43 stageInterval_(stageInterval),
44 childPtr_(0),
45 rootPtr_(rootPtr),
46 stageId_(stageId),
47 blockFactor_(blockFactor)
48 {}
49
50 /*
51 * Destructor.
52 */
54 {
55 if (childPtr_) {
56 delete childPtr_;
57 }
58 }
59
60 /*
61 * Reset the block factor.
62 */
63 void AverageStage::setBlockFactor(int blockFactor)
64 {
65 if (nSample_ > 0) {
66 UTIL_THROW("Attempt to reset block factor when nSample > 0");
67 }
68 if (blockFactor < 2) {
69 UTIL_THROW("Invalid value of blockFactor");
70 }
71 blockFactor_ = blockFactor;
72 }
73
74 void AverageStage::registerDescendant(AverageStage* descendantPtr)
75 {}
76
77 /*
78 * Reset all accumulators and counters to zero.
79 */
81 {
82 sum_ = 0.0;
83 sumSq_ = 0.0;
84 blockSum_ = 0.0;
85 nSample_ = 0;
86 nBlockSample_ = 0;
87 if (childPtr_) {
88 delete childPtr_;
89 }
90 }
91
92 /*
93 * Add a sampled value to the ensemble.
94 */
95 void AverageStage::sample(double value)
96 {
97
98 // Increment global accumulators
99 sum_ += value;
100 sumSq_ += (value*value);
101 ++nSample_;
102
103 // Increment block accumulators
104 blockSum_ += value;
105 ++nBlockSample_;
106
107 if (nBlockSample_ == blockFactor_) {
108
109 if (!childPtr_) {
110 long nextStageInterval = stageInterval_*blockFactor_;
111 int nextStageId = stageId_ + 1;
112 childPtr_ = new AverageStage(nextStageInterval, nextStageId,
113 rootPtr_, blockFactor_);
114 rootPtr_->registerDescendant(childPtr_);
115 }
116
117 // Add block average to child ensemble
118 childPtr_->sample(blockSum_ / double(blockFactor_));
119
120 // Reset block accumulators
121 blockSum_ = 0.0;
122 nBlockSample_ = 0;
123
124 }
125
126 }
127
128 /*
129 * Return the average of all sampled values.
130 */
132 { return sum_/double(nSample_); }
133
134 /*
135 * Return the variance of all sampled values
136 */
138 {
139 double rSample_ = double(nSample_);
140 double ave = sum_/rSample_;
141 double aveSq = sumSq_/rSample_;
142 return aveSq - ave*ave;
143 }
144
145 /*
146 * Return the standard deviation of all sampled values.
147 */
149 { return sqrt(variance()); }
150
151 /*
152 * Return the number of sampled values.
153 */
155 { return nSample_; }
156
157 /*
158 * Return the number of measured values per sample at this stage.
159 */
161 { return stageInterval_; }
162
163 /*
164 * Estimated statistical error of the average.
165 */
166 double AverageStage::error() const
167 { return sqrt(variance()/double(nSample_-1)); }
168
169}
Evaluate average with hierarchical blocking error analysis.
Definition: AverageStage.h:67
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:51
Utility classes for scientific computation.
Definition: accumulators.mod:1