forrestbao / pyeeg

Python + EEG/MEG = PyEEG
GNU General Public License v3.0
241 stars 85 forks source link

Result of the hurst exponent are wrong #17

Open fabiofumarola opened 8 years ago

fabiofumarola commented 8 years ago

from numpy import cumsum, log, polyfit, sqrt, std, subtract
from numpy.random import randn

def hurst(ts):
    """Returns the Hurst Exponent of the time series vector ts"""
    # Create the range of lag values
    lags = range(2, 100)

    # Calculate the array of the variances of the lagged differences
    tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]

    # Use a linear fit to estimate the Hurst Exponent
    poly = polyfit(log(lags), log(tau), 1)

    # Return the Hurst exponent from the polyfit output
    return poly[0]*2.0

# Create a Gometric Brownian Motion, Mean-Reverting and Trending Series
gbm = log(cumsum(randn(100000))+1000)
mr = log(randn(100000)+1000)
tr = log(cumsum(randn(100000)+1)+1000)

# Output the Hurst Exponent for each of the above series
# and the price of Google (the Adjusted Close price) for 
# the ADF test given above in the article
print "Hurst(GBM):   %s" % hurst(gbm)
print "Hurst(MR):    %s" % hurst(mr)
print "Hurst(TR):    %s" % hurst(tr)

I compared with the results of pyeeg.hurst and they are quite different. I you want I can do a pull request to update the method

Borzou commented 8 years ago

@fabiofumarola Could you please tell us based on which article you have implemented this algorithm for calculating the hurst exponent? Why are you taking the square root of standard deviation? We have implemented hurst exponent based on this http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.137.207&rep=rep1&type=pdf article. And as to my understanding you have not implemented the same algorithm as one disclosed in the above article.

fabiofumarola commented 8 years ago

Hi @Borzou, sorry I was to rude in the post. Anyway I got the function from QuantStart blog post. The blog post talks about mean reversion testing but it uses the Hurst Exponent as a measure to evaluate is the time series is: reverting, a Geometric Brownian Motion or trending. I tested its formula and it looks ok for me. What do you think?