title: calculate Hurst Exponent in excel
body: <p>i found this python code to calc the Hurst Exponent, given an array of numbers ts, but i'm having a hard time implementing this in excel (for mockup testing), just using worksheet functions (and eventually, C++ in Sierra Chart, which lacks some basic math transform functions like std and polyfit).  </p>
  
  <p>for excel, i think i can use the slope function in place of polyfit, and the rest of the functions (square root, standard dev) seem straightforward, but my brain is just having a hard time building the excel sheet.</p>
  
  <p>Can one of you guys take a crack?  Maybe just convert into VBA?</p>
  
  <p>*</p>
  
  <pre><code>from numpy import cumsum, log, polyfit, sqrt, std, subtract
  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
  </code></pre>
  
  <p>*</p>