【问题标题】:Daily Hurst Exponent每日赫斯特指数
【发布时间】:2016-04-23 17:50:39
【问题描述】:

我正在尝试估计股票收益的每日赫斯特指数值(例如,每天也有赫斯特指数 - 类似的东西:https://www.quandl.com/data/PE/CKEC_HURST-Hurst-Exponent-of-Carmike-Cinemas-Inc-Common-Stock-CKEC-NASDAQ)。

我正在使用这个 Python 代码(取自 https://www.quantstart.com/articles/Basics-of-Statistical-Mean-Reversion-Testing),但我不知道如何将它用于每日 Hurst 值,而不仅仅是一个值:

from datetime import datetime
from pandas.io.data import DataReader
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


# Download the stock prices series from Yahoo
aapl = DataReader("AAPL", "yahoo", datetime(2012,1,1), datetime(2015,9,18))

# Call the function
hurst(aapl['Adj Close'])

【问题讨论】:

  • 您的问题是什么?代码应该做什么?

标签: python


【解决方案1】:

我猜你的意思是:

from datetime import timedelta
current_date = datetime(2012,1,3)
end_date = datetime(2015,9,18)
aapl = DataReader("AAPL", "yahoo", current_date, end_date)
index = 0
while index < len(aapl['Adj Close']):     
    print current_date.strftime("%Y-%m-%d")
    print hurst(aapl['Adj Close'][index:index + 1])
    index += 1
    current_date += timedelta(days=1)

【讨论】:

  • 非常感谢您的帮助!我认为在您在“aapl = DataReader("AAPL", "yahoo", current_date, current_date)" 行中编写的代码中,您的意思是“aapl = DataReader("AAPL", "yahoo", current_date, end_date)"。
  • 否 - 给出的代码计算每日版本。如果你把它改成你写的,那么指数就不是每天了。此外,您将多次检索相同的数据。
  • @Marcus 我想你可以尝试另一种方法从网络只获取一次数据,这在我现在的答案中。
  • @Marcus 你能发布回溯吗?
  • 2012-01-01 00:00:00 -------------------------------- ------------------------------------------------------- KeyError Traceback(最近一次调用最后一次) in () 4 while current_date 6 print hurst(aapl['Adj Close'][current_date.strftime("%Y -%m-%d")]) 7 current_date += timedelta(days=1) C:\Users\DELL\Anaconda\lib\site-packages\pandas\core\series.pyc in __getitem__(self, key) 482 def __getitem__(self, key): 483 尝试:
【解决方案2】:

您正在尝试将 dataframe 传递给需要 list 的函数

print( hurst( aapl['Adj Close'].values ) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多