Python 日赫斯特指数
我试图估计股票收益的每日赫斯特指数值(例如,每天都有赫斯特指数,类似于:) 我正在使用这段Python代码(摘自),但我不知道如何将其用于每日Hurst值,而不是一个值:Python 日赫斯特指数,python,Python,我试图估计股票收益的每日赫斯特指数值(例如,每天都有赫斯特指数,类似于:) 我正在使用这段Python代码(摘自),但我不知道如何将其用于每日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
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'])
我猜你的意思是:
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)
从日期时间导入时间增量
当前日期=日期时间(2012,1,3)
结束日期=日期时间(2015,9,18)
aapl=DataReader(“aapl”、“yahoo”、当前日期、结束日期)
索引=0
而索引
您正试图将数据帧
传递给需要列表的函数
print( hurst( aapl['Adj Close'].values ) )
你的问题是什么?代码应该做什么?非常感谢您的帮助!我想在你写的代码“aapl=DataReader(“aapl”,“yahoo”,current_date,current_date)”中,你的意思是“aapl=DataReader(“aapl”,“yahoo”,current_date,end_date)”。不-给出的代码计算每日版本。如果你把它改成你所写的,那么指数就不是每天的了。您还将多次检索相同的数据。@Marcus我认为您可以尝试另一种方法,仅从web获取一次数据,这已在我的回答中。@Marcus您可以发布回溯吗?2012-01-01 00:00:00---------------------------------------------------------------------------------KeyError回溯(最近一次调用最后一次)在()4中当前\u日期6打印hurst(aapl['Adj Close'][当前\u日期.strftime(%Y-%m-%d”))7当前\u日期+=时间增量(天=1)C:\Users\DELL\Anaconda\lib\site packages\pandas\core\series.pyc在\uuuuu获取项目\uuuuuuuuuuuuuuuuu(self,key)482 def获取项目(self,key)中:483尝试: