Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can calculate hurst exponent in python?(lags parameter issue)

I have hurst exponent code.
below code, default lags_count value is 100.

but, when I change 'lags_count' value, the hurst exponent result also change.
and if time series data term is shorter than default 100, below function doesn't work.
so, I tried to lags_count value = time_series.shape[0]*0.7, 0.6, 0.5, etc.
I don't know what value is correct.
plus, I don't want to negative value of hurst exponent.

I think there is accurate or trust-worthy lags parameter value!


def calcHurstExponent(ts,lags):  
    tau = [np.sqrt(np.std(np.subtract(ts[lag:], ts[:-lag]))) for lag in lags]  
    poly = np.polyfit(np.log(lags), np.log(tau), 1)  
    result = poly[0]*2.0  
    return result

def get_hurst_exponent(df,target_column,lags_count=100):  
    lags = range(2, lags_count)  
    ts = np.log(df[target_column])

    return calcHurstExponent(ts,lags)