1#ifndef UTIL_AUTO_CORR_H
2#define UTIL_AUTO_CORR_H
11#include <util/param/ParamComposite.h>
12#include <util/containers/RingBuffer.h>
13#include <util/containers/DArray.h>
16#include <util/accumulators/setToZero.h>
17#include <util/accumulators/product.h>
18#include <util/space/Vector.h>
19#include <util/format/Int.h>
20#include <util/format/Dbl.h>
21#include <util/format/write.h>
48 template <
typename Data,
typename Product>
103 template <
class Archive>
104 void serialize(Archive& ar,
const unsigned int version);
118 void output(std::ostream& out);
182 template <
typename Data,
typename Product>
197 template <
typename Data,
typename Product>
204 template <
typename Data,
typename Product>
207 read<int>(in,
"capacity", bufferCapacity_);
214 template <
typename Data,
typename Product>
224 template <
typename Data,
typename Product>
239 template <
typename Data,
typename Product>
246 template <
typename Data,
typename Product>
252 if (bufferCapacity_ > 0) {
253 for (
int i=0; i < bufferCapacity_; ++i) {
264 template <
typename Data,
typename Product>
265 void AutoCorr<Data, Product>::allocate()
267 if (bufferCapacity_ > 0) {
268 corr_.allocate(bufferCapacity_);
269 nCorr_.allocate(bufferCapacity_);
270 buffer_.allocate(bufferCapacity_);
278 template <
typename Data,
typename Product>
279 bool AutoCorr<Data, Product>::isValid()
282 if (bufferCapacity_ != corr_.capacity()) valid =
false;
283 if (bufferCapacity_ != nCorr_.capacity()) valid =
false;
284 if (bufferCapacity_ != buffer_.capacity()) valid =
false;
294 template <
typename Data,
typename Product>
299 buffer_.append(value);
300 for (
int i=0; i < buffer_.size(); ++i) {
301 corr_[i] +=
product(buffer_[i], value);
309 template <
typename Data,
typename Product>
311 {
return bufferCapacity_; }
316 template <
typename Data,
typename Product>
323 template <
typename Data,
typename Product>
327 ave /= double(nSample_);
334 template <
typename Data,
typename Product>
343 ave /= double(nSample_);
347 for (
int i = 0; i < buffer_.size(); ++i) {
348 autocorr = corr_[i]/double(nCorr_[i]);
349 autocorr = autocorr - aveSq;
352 outFile << std::endl;
360 template <
typename Data,
typename Product>
364 Product aveSq, variance, autocorr, sum;
367 size = buffer_.size();
370 ave = sum_/double(nSample_);
371 ave /= double(nSample_);
375 variance = corr_[0]/double(nCorr_[0]);
376 variance = variance - aveSq;
379 for (
int i = 1; i < size/2; ++i) {
380 autocorr = corr_[i]/double(nCorr_[i]);
381 autocorr = autocorr - aveSq;
394 template <
typename Data,
typename Product>
403 ave /= double(nSample_);
407 assert(t < buffer_.size());
408 autocorr = corr_[t]/double(nCorr_[t]);
409 autocorr = autocorr - aveSq;
417 template <
typename Data,
typename Product>
418 template <
class Archive>
420 const unsigned int version)
422 ar & bufferCapacity_;
Data average() const
Return average of all sampled values.
double corrTime() const
Numerical integration of autocorrelation function.
Product autoCorrelation(int t) const
Return autocorrelation at a given lag time.
int bufferCapacity() const
Return capacity of history buffer.
void sample(Data value)
Sample a value.
void clear()
Reset to empty state.
void setParam(int bufferCapacity)
Set buffer capacity, allocate memory and initialize.
int nSample() const
Return number of values sampled thus far.
void readParameters(std::istream &in)
Read buffer capacity, allocate memory and initialize.
void output(std::ostream &out)
Output the autocorrelation function.
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
virtual void loadParameters(Serializable::IArchive &ar)
Load state from an archive.
virtual void save(Serializable::OArchive &ar)
Save state to an archive.
Dynamically allocatable contiguous array template.
Wrapper for an int, for formatted ostream output.
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
void setClassName(const char *className)
Set class name string.
ScalarParam< Type > & loadParameter(Serializable::IArchive &ar, const char *label, Type &value, bool isRequired)
Add and load a new ScalarParam < Type > object.
ParamComposite()
Constructor.
Class for storing history of previous values in an array.
BinaryFileIArchive IArchive
Type of input archive used by load method.
BinaryFileOArchive OArchive
Type of output archive used by save method.
#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.
void write(std::ostream &out, double data)
Explicit specialization of write for double data.
float product(float a, float b)
Product for float Data.