gpt4 book ai didi

python - python 中的赫斯特指数

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:34 26 4
gpt4 key购买 nike

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
# Here it calculates the variances, but why it uses
# standard deviation and then make a root of it?
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'])

从这段估计Hurst Exponent的代码来看,当我们要计算滞后差的方差时,为什么还要用一个标准差,还要取平方根呢?困惑了很久,不知道为什么别人没有同样的困惑。我误解了它背后的数学原理吗?谢谢!

最佳答案

我也很困惑。我也不明白 std 的 sqrt 来自哪里,并且花了 3 天时间试图弄清楚。最后我注意到 QuantStart 归功于 Tom Starke 博士,他使用的代码略有不同。 Tom Starke 博士感谢 Ernie Chan 博士,并转至 his blog .我能够找到足够的信息来根据他的原则编写我自己的代码。这不使用 sqrt,使用方差而不是标准差,并在末尾使用 2.0 除数而不是 2.0 乘数。最后,它似乎给出了与您发布的 quantstart 代码相同的结果,但我能够从第一原则理解它,我认为这很重要。我整理了一个 Jupyter Notebook 使它更清晰,但我不确定我是否可以在这里发布它,所以我会尽力在这里解释。先贴代码,再贴解释。

lags = range(2,100)
def hurst_ernie_chan(p):

variancetau = []; tau = []

for lag in lags:

# Write the different lags into a vector to compute a set of tau or lags
tau.append(lag)

# Compute the log returns on all days, then compute the variance on the difference in log returns
# call this pp or the price difference
pp = subtract(p[lag:], p[:-lag])
variancetau.append(var(pp))

# we now have a set of tau or lags and a corresponding set of variances.
#print tau
#print variancetau

# plot the log of those variance against the log of tau and get the slope
m = polyfit(log10(tau),log10(variancetau),1)

hurst = m[0] / 2

return hurst

Chan 博士在此页面上没有提供任何代码(我相信他在 MATLAB 中工作,而不是 Python)。因此,我需要根据他在博客中给出的注释和他对博客上提出的问题的回答,将我自己的代码放在一起。

  1. 陈博士指出,如果 z 是对数价格,则以 τ 为间隔采样的波动率是波动率 (τ)=√(Var(z(t)-z(t-τ)))。对我来说,另一种描述波动率的方法是标准差,所以 std(τ)=√(Var(z(t)-z(t-τ)))

  2. std 只是方差根,所以 var(τ)=(Var(z(t)-z(t-τ)))

  3. 陈博士接着指出:一般来说,我们可以写成 Var(τ) ∝ τ^(2H),其中 H 是 Hurst 指数

  4. 因此 (Var(z(t)-z(t-τ))) ∝ τ^(2H)

  5. 对每边取对数,我们得到 log (Var(z(t)-z(t-τ))) ∝ 2H log τ

  6. [ log (Var(z(t)-z(t-τ)))/log τ ]/2 ∝ H(给出 Hurst 指数),我们知道最左边方括号中的项是tau 的对数-对数图的斜率和相应的一组方差。

如果您运行该函数并比较 Quantstart 函数的答案,它们应该是相同的。不确定是否有帮助。

关于python - python 中的赫斯特指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39488806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com