[Tutor] NameError: name 'hurst' is not defined

Alan Gauld alan.gauld at yahoo.co.uk
Tue Feb 14 05:11:14 EST 2017


On 14/02/17 01:55, Allan Tanaka via Tutor wrote:
> Hi. Not sure why this code produces the error like this. This error appears when i run the code of print "Hurst(GBM):   %s" % hurst(gbm): 
> Traceback (most recent call last):  File "<pyshell#31>", line 1, in <module>    print "Hurst(GBM):   %s" % hurst(gbm)NameError: name 'hurst' is not defined
> 
> Here is the full code:>>> import statsmodels.tsa.stattools as ts
>>>> import urllib>>> from datetime import datetime>>> from pandas_datareader import data, wb>>> from pandas_datareader.data import DataReader>>> goog = DataReader("GOOG", "yahoo", datetime(2000,1,1), datetime(2017,1,1))>>> ts.adfuller(goog['Adj Close'], 1>>> import numpy as np
>>>> from numpy import cumsum, log, polyfit, sqrt, std, subtract>>> from numpy.random import randn>>> def hurst(ts): lags = range(2, 100) tau = [np.sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags] poly = np.polyfit(log(lags), log(tau), 1) return poly[0]*2.0>>> gbm = log(cumsum(randn(100000))+1000)>>> mr = log(randn(100000)+1000)>>> tr = log(cumsum(randn(100000)+1)+1000)>>> print "Hurst(GBM):   %s" % hurst(gbm)

The mail system has mangled your code, you need to post
in plain text to preserve layout. I'll try to reconstruct
it based on >>> as the interpreter prompt:

>>> import urllib
>>> from datetime import datetime
>>> from pandas_datareader import data, wb
>>> from pandas_datareader.data import DataReader
>>> goog = DataReader("GOOG", "yahoo", datetime(2000,1,1),
                      datetime(2017,1,1))
>>> ts.adfuller(goog['Adj Close'], 1

there seems to be a missing closing  paren here?
Also where does 'ts' come from, its not defined above?

>>> import numpy as np
>>> from numpy import cumsum, log, polyfit, sqrt, std, subtract
>>> from numpy.random import randn
>>> def hurst(ts):
          lags = range(2, 100)
          tau = [np.sqrt(std(subtract(ts[lag:], ts[:-lag])))
                 for lag in lags]
          poly = np.polyfit(log(lags), log(tau), 1)
          return poly[0]*2.0
>>> gbm = log(cumsum(randn(100000))+1000)
>>> mr = log(randn(100000)+1000)
>>> tr = log(cumsum(randn(100000)+1)+1000)
>>> print "Hurst(GBM):   %s" % hurst(gbm)

Is that right?
Apart from the missing ')' and undefined ts, it looks OK.
Have you tried putting it into a file and running it
as a script? That will avoid risk of pollution from
any previous context in the interpreter.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list