我跟隨邁克爾·霍爾斯 - 摩爾算法交易書,並且遇到了一些代碼問題。當我把代碼粘貼到python中時,我得到了一些錯誤。邁克爾·霍爾斯 - 摩爾算法交易的錯誤
我在這裏錯過了些什麼,因爲它與書中寫的完全一樣嗎?
from __future__ import print_function
from numpy import cumsum, log, polyfit, sqrt, std, subtract
from numpy.random import randn
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
# Create a Geometric Brownian Motion, Mean-Reverting and Trending Series
gbm = log(cumsum(randn(100000))+1000)
mr = log(randn(100000)+1000)
tr = log(cumsum(randn(100000)+1)+1000)
# Output the Hurst Exponent for each of the above series
# and the price of Amazon (the Adjusted Close price) for
# the ADF test given above in the article
print("Hurst(GBM): %s" % hurst(gbm))
print("Hurst(MR): %s" % hurst(mr))
print("Hurst(TR): %s" % hurst(tr))
# Assuming you have run the above code to obtain 'amzn'!
print("Hurst(AMZN): %s" % hurst(amzn['Adj Close']))
下面
[email protected]:~$ python
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>>
>>> from numpy import cumsum, log, polyfit, sqrt, std, subtract
>>> from numpy.random import randn
>>>
>>>
>>> def hurst(ts):
... """Returns the Hurst Exponent of the time series vector ts"""
... # Create the range of lag values
...
>>> lags = range(2, 100)
File "<stdin>", line 1
lags = range(2, 100)
^
IndentationError: unexpected indent
>>>
>>> # Calculate the array of the variances of the lagged differences
...
>>> tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
File "<stdin>", line 1
tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
^
IndentationError: unexpected indent
>>>
>>> # Use a linear fit to estimate the Hurst Exponent
...
>>> poly = polyfit(log(lags), log(tau), 1)
File "<stdin>", line 1
poly = polyfit(log(lags), log(tau), 1)
^
IndentationError: unexpected indent
>>>
>>> # Return the Hurst exponent from the polyfit output
...
>>> return poly[0]*2.0
File "<stdin>", line 1
return poly[0]*2.0
^
IndentationError: unexpected indent
>>>
>>> # Create a Gometric Brownian Motion, Mean-Reverting and Trending Series
... gbm = log(cumsum(randn(100000))+1000)
>>> mr = log(randn(100000)+1000)
>>> tr = log(cumsum(randn(100000)+1)+1000)
>>>
>>> # Output the Hurst Exponent for each of the above series
... # and the price of Amazon (the Adjusted Close price) for
... # the ADF test given above in the article
... print("Hurst(GBM): %s" % hurst(gbm))
Hurst(GBM): None
>>> print("Hurst(MR): %s" % hurst(mr))
Hurst(MR): None
>>> print("Hurst(TR): %s" % hurst(tr))
Hurst(TR): None
>>>
>>> # Assuming you have run the above code to obtain 'amzn'!
... print("Hurst(AMZN): %s" % hurst(amzn['Adj Close']))
Hurst(AMZN): None
這是一個縮進錯誤,所以...檢查您的縮進。使用空格而不是標籤,絕對不是標籤和空格。每個縮進級別必須是相同的空格數量。 –
縮進時是否有混合製表符和空格的機會?編輯:@ Two-BitAlchemist擊敗了我。 :) –
它實際上在堆棧跟蹤中說4次IndentationError。檢查你的標籤與空格。 – lonewaft