Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

autocorr.cpp

Go to the documentation of this file.
00001 
00002 #include <math.h>
00003 
00004 #include "lregress.h"
00005 #include "autocorr.h"
00006 
00007 using namespace std;
00008 
00009 
00027 void autocorr::acorrSlope( acorrInfo &info )
00028 {
00029 
00030   const size_t len = info.points().size();
00031   if (len > 0) {
00032     lregress::lineInfo regressInfo;
00033     lregress lr;
00034     vector<lregress::point> regressPoints;
00035     for (size_t i = 0; i < len; i++) {
00036       double x = log(i+1);
00037       double y = log((info.points())[i]);
00038       regressPoints.push_back( lregress::point(x,y) );
00039     }
00040     lr.lr( regressPoints, regressInfo );
00041     info.slope( regressInfo.slope() );
00042     info.slopeErr( regressInfo.slopeErr() );
00043   }
00044 } // acorrSlope
00045 
00046 
00050 void autocorr::ACF( const double *v, 
00051                     const size_t N, 
00052                     acorrInfo &info )
00053 {
00054   if (! info.points().empty() ) {
00055     info.points().clear();
00056   }
00057 
00058   // The devMean array will contain the deviation from the mean.
00059   // That is, v[i] - mean(v).
00060   double *devMean = new double[ N ];
00061   
00062   double sum;
00063   size_t i;
00064 
00065   // Calculate the mean and copy the vector v, into devMean
00066   sum = 0;
00067   for (i = 0; i < N; i++) {
00068     sum = sum + v[i];
00069     devMean[i] = v[i];
00070   }
00071   double mean = sum / static_cast<double>(N);
00072 
00073   // Compute the values for the devMean array.  Also calculate the
00074   // denominator for the autocorrelation function.
00075   sum = 0;
00076   for (i = 0; i < N; i++) {
00077     devMean[i] = devMean[i] - mean;
00078     sum = sum + (devMean[i]*devMean[i]);
00079   }
00080   double denom = sum / static_cast<double>(N);
00081 
00082   // Calculate a std::vector of values which will make up
00083   // the autocorrelation function.
00084   double cor = 1.0;
00085   for (size_t shift = 1; shift <= numPoints_ && cor > limit_; shift++) {
00086     info.points().push_back( cor );
00087     size_t n = N - shift;
00088     sum = 0.0;
00089     for (i = 0; i < n; i++) {
00090       sum = sum + (devMean[i] * devMean[i+shift]);
00091     }
00092     double numerator = sum / static_cast<double>(n)
00093     cor = numerator / denom;
00094   }
00095 
00096   // calculate the log/log slope of the autocorrelation
00097   acorrSlope( info );
00098 
00099   delete [] m;
00100 } // ACF

Generated at Wed May 21 21:19:26 2003 for Basic Statistics Functions by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001