AutocorrelationAnalyzer

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#ifndef Statistics_AutocorrelationAnalyzer_h
#define Statistics_AutocorrelationAnalyzer_h

#include "CLHEP/Matrix/Vector.h"
#include "CLHEP/Matrix/SymMatrix.h"
#include "CLHEP/Matrix/Matrix.h"
#include <iosfwd>

/** This class accepts objects which support the [] operator,
 *  such as a digi or a vector,
 *  and calculates the correlation matrix between the components
 *  \Author Rick Wilkinson, Fedor Ratnikov              
 */

class AutocorrelationAnalyzer {
public:
  explicit AutocorrelationAnalyzer(int size);

  /// indexing starts from 0
  double mean(int i);
  double covariance(int i, int j);
  double correlation(int i, int j);

  template <class T>
  void analyze(const T& t) {
    for (int ii = 0; ii < theSize; ii++) {
      theMeans[ii] += t[ii];
      for (int ij = ii; ij < theSize; ij++) {
        theCovariances[ii][ij] += t[ii] * t[ij];
      }
    }
    ++theNTotal;
  }

  friend std::ostream& operator<<(std::ostream& os, AutocorrelationAnalyzer& aa);

private:
  void calculate();

  int theSize;
  int theNTotal;
  CLHEP::HepVector theMeans;
  CLHEP::HepSymMatrix theCovariances;
  CLHEP::HepSymMatrix theCorrelations;
  bool calculated_;
};

#endif