# Why does the Hurst exponent pseudo code not match the Python implementation?

I am working on understanding the Hurst exponent calculation by Ernest Chan; however, the description of the algorithm does not match the Python implementation.

Chan [Algorithmic Trading: Winning Strategies and Their Rationale] outlines following steps for Hurst Exponent computation using R/S method:

▪ Divides the time series of length 𝑁 into 2𝑘 (𝑘 = 0,1,2…) adjacent subperiods of the same length 𝑛, such that 2𝑘 × 𝑛 = 𝑁.

▪ For each sub period of length 𝑛, a partial time series is created based on various subperiods, 𝑋 = 𝑋1,𝑋2,…𝑋𝑛.

▪ Arithmetic mean (𝑥̅ = 1 𝑛 ∑ 𝑋𝑖 𝑛 𝑖=1 ) is calculated and a new mean-adjusted series (𝑌 𝑡 = 𝑥𝑡 − 𝑥̅) column is constructed.

▪ Cumulated deviations from the arithmetic mean are noted in a new series (𝑍𝑡 = ∑ 𝑌 𝑡 𝑡 𝑡=1 ).

▪ An adjusted range is calculated by 𝑅(𝑛) = max(𝑍1,𝑍2,…𝑍𝑛) − min (𝑍1,𝑍2,…𝑍𝑛)

▪ Standard deviation is computed 𝑆(𝑛) = √1 𝑛 ∑ (𝑋𝑖 − 𝑥̅)2 𝑛 𝑖=1

▪ Each adjusted range 𝑅(𝑛) is then standardized by corresponding standard deviation 𝑆(𝑛), to form the rescaled range 𝑅(𝑛)/𝑆(𝑛)

▪ An average rescaled range over all the partial time series of length 𝑛 is calculated The process is repeated iteratively using 𝑘 = 0,1,2… for each length 𝑛 = 𝑁/2𝑘 of subperiods.

from numpy import *
from pylab import plot, show
# first, create an arbitrary time series, ts
ts = [0]
for i in range(1,100000):
ts.append(ts[i-1]*1.0 + random.randn())
# calculate standard deviation of differenced series using various lags
lags = range(2, 20)
tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
# plot on log-log scale
plot(log(lags), log(tau)); show()
# calculate Hurst as slope of log-log plot
m = polyfit(log(lags), log(tau), 1)
hurst = m[0]*2.0
print 'hurst = ',hurst


Could someone please explain the difference of the description and the implementation in case my math is not good enough? Or are these different ways to implement the Hurst expoenent?