Skip to content

Instantly share code, notes, and snippets.

@jcorrius
Last active September 18, 2018 15:17
Show Gist options
  • Save jcorrius/159a85b3f3f6180c3884fe7df5040f72 to your computer and use it in GitHub Desktop.
Save jcorrius/159a85b3f3f6180c3884fe7df5040f72 to your computer and use it in GitHub Desktop.
Hurst Exponent
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