Go to the documentation of this file.00001 #include "CommonTools/Statistics/interface/AutocorrelationAnalyzer.h"
00002 #include <iostream>
00003 #include <cassert>
00004
00005 AutocorrelationAnalyzer::AutocorrelationAnalyzer(int size)
00006 : theSize(size),
00007 theNTotal(0),
00008 theMeans(size, 0),
00009 theCovariances(theSize, 0),
00010 theCorrelations(theSize, 0),
00011 calculated_(false)
00012 {
00013 }
00014
00015
00016 double AutocorrelationAnalyzer::mean(int i)
00017 {
00018 if(!calculated_) calculate();
00019 assert(i < theSize);
00020 return theMeans[i];
00021 }
00022
00023
00024 double AutocorrelationAnalyzer::covariance(int i, int j)
00025 {
00026 if(!calculated_) calculate();
00027 assert(i<=theSize && j<=theSize);
00028 return theCovariances(i+1,j+1);
00029 }
00030
00031
00032 double AutocorrelationAnalyzer::correlation(int i, int j)
00033 {
00034 if(!calculated_) calculate();
00035 assert(i<=theSize && j<=theSize);
00036 return theCorrelations(i+1,j+1);
00037 }
00038
00039
00040
00041 void AutocorrelationAnalyzer::calculate()
00042 {
00043 for(int k = 0; k < theSize; ++k)
00044 {
00045 theMeans[k] /= theNTotal;
00046 for (int kk = k; kk < theSize; kk++)
00047 {
00048 theCovariances[k][kk] /= theNTotal;
00049 }
00050 }
00051
00052 for (int k = 0; k < theSize; k++)
00053 {
00054 for (int kk = k; kk < theSize; kk++)
00055 {
00056 theCorrelations[k][kk] = theCovariances[k][kk]
00057 / sqrt (theCovariances[k][k]*theCovariances[kk][kk]);
00058 }
00059 }
00060
00061 calculated_ = true;
00062 }
00063
00064
00065 std::ostream & operator<<(std::ostream & os, AutocorrelationAnalyzer & aa)
00066 {
00067 aa.calculate();
00068 os << "Means: " << std::endl << aa.theMeans << std::endl;
00069 os << "Covariances: " << std::endl << aa.theCovariances << std::endl;
00070 os << "Correlations: " << std::endl << aa.theCorrelations << std::endl;
00071 return os;
00072 }
00073