1#ifndef UTIL_AUTOCORR_STAGE_TPP
2#define UTIL_AUTOCORR_STAGE_TPP
11#include "AutoCorrStage.h"
13#include <util/accumulators/setToZero.h>
14#include <util/accumulators/product.h>
15#include <util/format/Int.h>
16#include <util/format/write.h>
29 template <
typename Data,
typename Product>
31 : bufferCapacity_(64),
52 template <
typename Data,
typename Product>
54 int stageId,
int maxStageId,
57 : bufferCapacity_(rootPtr->bufferCapacity()),
58 maxStageId_(maxStageId),
59 blockFactor_(blockFactor),
66 stageInterval_(stageInterval),
78 template <
typename Data,
typename Product>
89 template <
typename Data,
typename Product>
91 int maxStageId,
int blockFactor)
93 bufferCapacity_ = bufferCapacity;
94 maxStageId_ = maxStageId;
95 blockFactor_ = blockFactor;
102 template <
typename Data,
typename Product>
108 if (bufferCapacity_ > 0) {
109 for (
int i=0; i < bufferCapacity_; ++i) {
120 template <
typename Data,
typename Product>
123 if (bufferCapacity_ <= 0) {
124 bufferCapacity_ = 64;
131 buffer_.append(value);
132 for (
int i=0; i < buffer_.size(); ++i) {
133 corr_[i] +=
product(buffer_[i], value);
142 if (nBlockSample_ == blockFactor_) {
143 if (stageId_ < maxStageId_) {
145 long nextStageInterval = stageInterval_*blockFactor_;
146 int nextStageId = stageId_ + 1;
147 childPtr_ =
new AutoCorrStage(nextStageInterval, nextStageId,
148 maxStageId_, rootPtr_, blockFactor_);
149 rootPtr_->registerDescendant(childPtr_);
152 blockSum_ /= double(blockFactor_);
153 childPtr_->sample(blockSum_);
165 template <
typename Data,
typename Product>
166 template <
class Archive>
169 const unsigned int version)
172 ar & bufferCapacity_;
177 serializePrivate(ar, version);
183 template <
typename Data,
typename Product>
184 template <
class Archive>
187 const unsigned int version)
203 if (Archive::is_saving()) {
204 hasChild = (childPtr_ == 0) ? 0 : 1;
210 if (Archive::is_loading()) {
211 long nextStageInterval = stageInterval_*blockFactor_;
212 int nextStageId = stageId_ + 1;
214 nextStageId, maxStageId_,
215 rootPtr_, blockFactor_);
216 rootPtr_->registerDescendant(childPtr_);
220 if (Archive::is_loading()) {
230 template <
typename Data,
typename Product>
232 {
return bufferCapacity_; }
237 template <
typename Data,
typename Product>
239 {
return buffer_.size(); }
244 template <
typename Data,
typename Product>
251 template <
typename Data,
typename Product>
253 {
return stageInterval_; }
258 template <
typename Data,
typename Product>
263 output(outFile, aveSq);
269 template <
typename Data,
typename Product>
276 min = bufferCapacity_ / blockFactor_;
280 for (
int i = min; i < buffer_.size(); ++i) {
281 autocorr = corr_[i]/double(nCorr_[i]);
283 outFile <<
Int(i*stageInterval_) <<
" ";
284 write<Product>(outFile, autocorr);
285 outFile << std::endl;
288 childPtr_->output(outFile, aveSq);
295 template <
typename Data,
typename Product>
300 return autoCorrelation(t, aveSq);
306 template <
typename Data,
typename Product>
309 assert(t < buffer_.size());
310 Product autocorr = corr_[t]/double(nCorr_[t]);
318 template <
typename Data,
typename Product>
323 return corrTime(aveSq);
329 template <
typename Data,
typename Product>
333 Product variance = corr_[0];
334 variance /= double(nCorr_[0]);
338 Product autocorr, sum;
340 int size = buffer_.size();
341 for (
int i = 1; i < size/2; ++i) {
343 autocorr /= double(nCorr_[i]);
356 template <
typename Data,
typename Product>
359 if (bufferCapacity_ > 0) {
360 corr_.allocate(bufferCapacity_);
361 nCorr_.allocate(bufferCapacity_);
362 buffer_.allocate(bufferCapacity_);
370 template <
typename Data,
typename Product>
374 if (bufferCapacity_ != corr_.capacity()) valid =
false;
375 if (bufferCapacity_ != nCorr_.capacity()) valid =
false;
376 if (bufferCapacity_ != buffer_.capacity()) valid =
false;
383 template <
typename Data,
typename Product>
Hierarchical auto-correlation function algorithm.
long stageInterval() const
Return the number of primary values per block at this stage.
int bufferCapacity() const
Return capacity of history buffer.
void output(std::ostream &out)
Output the autocorrelation function, assuming zero mean.
AutoCorrStage()
Constructor.
int bufferSize() const
Return current size of history buffer.
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
void clear()
Clear accumulators and destroy descendants.
void serializePrivate(Archive &ar, const unsigned int version)
Serialize private data members, and descendants.
virtual void sample(Data value)
Sample a value.
Product autoCorrelation(int t) const
Return autocorrelation at a given time, assuming zero average.
virtual ~AutoCorrStage()
Destructor.
double corrTime() const
Estimate of autocorrelation time, in samples.
long nSample() const
Return the number of sampled values.
virtual void registerDescendant(AutoCorrStage< Data, Product > *ptr)
Register the creation of a descendant stage.
void allocate()
Allocate memory and initialize to empty state.
void setParam(int bufferCapacity=64, int maxStageId=0, int blockFactor=2)
Set all parameters and allocate to initialize state.
Wrapper for an int, for formatted ostream output.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Utility classes for scientific computation.
void setToZero(int &value)
Set an int variable to zero.
float product(float a, float b)
Product for float Data.