00001
00002 #include "rescaled_range.h"
00003
00004 using namespace std;
00005
00006
00010 double rescaled_range::calc_mean_( const double *v, const size_t N)
00011 {
00012 double sum = 0.0;
00013
00014 for (size_t i = 0; i < N; i++) {
00015 sum = sum + v[i];
00016 }
00017 double mean = sum / N;
00018 return mean;
00019 }
00020
00021
00029 double rescaled_range::calc_RS_( const double *v,
00030 const size_t boxSize )
00031 {
00032 double RS = 0.0;
00033 if (v != 0 && boxSize > 0) {
00034 double min;
00035 double max;
00036 double runningSum;
00037 double runningSumSqr;
00038
00039 double mean = calc_mean_( v, boxSize );
00040
00041 min = 0.0;
00042 max = 0.0;
00043 runningSum = 0.0;
00044 runningSumSqr = 0.0;
00045 for (size_t i = 0; i < boxSize; i++) {
00046 double devFromMean = v[i] - mean;
00047 runningSum = runningSum + devFromMean;
00048 runningSumSqr = runningSumSqr + (devFromMean * devFromMean);
00049 if (runningSum < min)
00050 min = runningSum;
00051 if (runningSum > max)
00052 max = runningSum;
00053 }
00054 double variance = runningSumSqr / static_cast<double>(boxSize);
00055 double stdDev = sqrt( variance );
00056
00057 double range = max - min;
00058 RS = range / stdDev;
00059 }
00060
00061 return RS;
00062 }
00063
00064
00081 double rescaled_range::calc_RS_ave_( const double *v,
00082 const size_t N,
00083 const size_t boxSize )
00084 {
00085 size_t i;
00086 double stdDev;
00087 double range;
00088 double RS;
00089 double RSSum;
00090 double mean;
00091 double RSAve = 0.0;
00092
00093 size_t numBoxes = N / boxSize;
00094 if (numBoxes > 0) {
00095 RSSum = 0;
00096 for (i = 0; i+boxSize <= N; i = i + boxSize) {
00097 const double *boxStart = &v[i];
00098 RS = calc_RS_( boxStart, boxSize );
00099 RSSum = RSSum + RS;
00100 }
00101
00102 RSAve = RSSum / static_cast<double>( numBoxes );
00103 }
00104
00105 return RSAve;
00106 }
00107
00108
00109
00136 void rescaled_range::calc_hurst_est( const double *v,
00137 const size_t N,
00138 hurst_base::hurstInfo &info )
00139 {
00140
00141 double hurstEst = 0.0;
00142
00143 if (v != 0 && N > 0) {
00144 const size_t minBox = 8;
00145
00146 (info.points()).clear();
00147 size_t boxSize;
00148 for (boxSize = N; boxSize >= minBox; boxSize = (boxSize >> 1)) {
00149 double RSAve = calc_RS_ave_( v, N, boxSize );
00150 double logRSAve = log2( RSAve );
00151 double logBoxSize = log2( boxSize );
00152 (info.points()).push_back( lregress::point( logBoxSize, logRSAve ) );
00153 }
00154
00155
00156
00157 lregress lr;
00158
00159 lregress::lineInfo lineInfo;
00160 lr.lr( info.points(), lineInfo );
00161
00162 double slope = lineInfo.slope();
00163 double intercept = lineInfo.intercept();
00164 double slopeError = lineInfo.slopeErr();
00165
00166 info.slope( slope );
00167 info.slopeErr( slopeError );
00168 info.intercept( intercept );
00169
00170
00171
00172 }
00173
00174 }