Last active
September 18, 2018 15:17
-
-
Save jcorrius/159a85b3f3f6180c3884fe7df5040f72 to your computer and use it in GitHub Desktop.
Hurst Exponent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hurst(ts): | |
""" | |
Returns the Hurst Exponent of the time series vector ts | |
""" | |
# make sure we are working with an array, convert if necessary | |
ts = np.asarray(ts) | |
# Helper variables used during calculations | |
lagvec = [] | |
tau = [] | |
# Create the range of lag values | |
lags = range(2, 100) | |
# Step through the different lags | |
for lag in lags: | |
# produce value difference with lag | |
pdiff = np.subtract(ts[lag:],ts[:-lag]) | |
# Write the different lags into a vector | |
lagvec.append(lag) | |
# Calculate the variance of the difference vector | |
tau.append(np.sqrt(np.std(pdiff))) | |
# linear fit to double-log graph | |
m = np.polyfit(np.log10(np.asarray(lagvec)), | |
np.log10(np.asarray(tau).clip(min=0.0000000001)), | |
1) | |
# return the calculated hurst exponent | |
return m[0]*2.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment