Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:12

0001 #include "CommonTools/Statistics/interface/AutocorrelationAnalyzer.h"
0002 #include <iostream>
0003 #include <cassert>
0004 
0005 AutocorrelationAnalyzer::AutocorrelationAnalyzer(int size)
0006     : theSize(size),
0007       theNTotal(0),
0008       theMeans(size, 0),
0009       theCovariances(theSize, 0),
0010       theCorrelations(theSize, 0),
0011       calculated_(false) {}
0012 
0013 double AutocorrelationAnalyzer::mean(int i) {
0014   if (!calculated_)
0015     calculate();
0016   assert(i < theSize);
0017   return theMeans[i];
0018 }
0019 
0020 double AutocorrelationAnalyzer::covariance(int i, int j) {
0021   if (!calculated_)
0022     calculate();
0023   assert(i <= theSize && j <= theSize);
0024   return theCovariances(i + 1, j + 1);
0025 }
0026 
0027 double AutocorrelationAnalyzer::correlation(int i, int j) {
0028   if (!calculated_)
0029     calculate();
0030   assert(i <= theSize && j <= theSize);
0031   return theCorrelations(i + 1, j + 1);
0032 }
0033 
0034 void AutocorrelationAnalyzer::calculate() {
0035   for (int k = 0; k < theSize; ++k) {
0036     theMeans[k] /= theNTotal;
0037     for (int kk = k; kk < theSize; kk++) {
0038       theCovariances[k][kk] /= theNTotal;
0039     }
0040   }
0041 
0042   for (int k = 0; k < theSize; k++) {
0043     for (int kk = k; kk < theSize; kk++) {
0044       theCorrelations[k][kk] = theCovariances[k][kk] / sqrt(theCovariances[k][k] * theCovariances[kk][kk]);
0045     }
0046   }
0047 
0048   calculated_ = true;
0049 }
0050 
0051 std::ostream& operator<<(std::ostream& os, AutocorrelationAnalyzer& aa) {
0052   aa.calculate();
0053   os << "Means: " << std::endl << aa.theMeans << std::endl;
0054   os << "Covariances: " << std::endl << aa.theCovariances << std::endl;
0055   os << "Correlations: " << std::endl << aa.theCorrelations << std::endl;
0056   return os;
0057 }