python中的Hurst指数

2024-05-29 02:10:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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'])

从这个估计赫斯特指数的代码中,当我们要计算滞后差的方差时,为什么我们仍然使用标准差并取平方根?我困惑了很长一段时间,我不知道为什么其他人没有同样的困惑。我是否误解了背后的数学?谢谢!


Tags: ofthefromimportnumpylogdatetimeit
3条回答

我也很困惑。我也不知道性病的sqrt是从哪里来的,我花了3天的时间想弄清楚。最后,我注意到QuantStart信任Tom Starke博士,他使用的代码略有不同。汤姆斯塔克博士把功劳归功于陈恩尼博士,并打算his blog。我能从他的原则中找到足够的信息来整理我自己的代码。这不使用sqrt,使用方差而不是std,在末尾使用2.0除数而不是2.0乘数。最后,它似乎给出了与您发布的quantstart代码相同的结果,但是我能够从第一个原则来理解它,我认为这很重要。我整理了一本更清晰的笔记本,但我不确定是否可以把它贴在这里,所以我会尽量在这里解释清楚。先粘贴代码,然后粘贴解释。

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

陈博士在这一页上没有给出任何代码(我相信他是在MATLAB工作的,而不是Python)。因此,我需要把我自己的代码从他在博客上的笔记和他在博客上提出的问题的答案中整理出来。

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

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

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

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

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

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

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

这里发生的一切只是数学符号的变化

我来定义

d = subtract(ts[lag:], ts[:-lag])

很明显

np.log(np.std(d)**2) == np.log(np.var(d))
np.log(np.std(d)) == .5*np.log(np.var(d))

那么你就有了等价物

2*np.log(np.sqrt(np.std(d))) == .5*np.log(np.sqrt(np.var(d)))

polyfit的函数输出与其输入成比例地缩放

OP发布的代码是正确的。

混淆的原因是它先做一个平方根,然后用2乘以斜率(polyfit返回)。

要获得更详细的解释,请继续阅读。

tau是用一个“额外的”平方根来计算的。然后,计算其对数。对数(sqrt(x))=对数(x^0.5)=0.5*log(x)(这是键)。 polyfit现在执行y乘以“额外0.5”的拟合。因此,得到的结果也乘以近0.5。返回两次(return poly[0]*2.0)则表示初始值(看起来)额外的0.5。

希望这能让事情更清楚。

相关问题 更多 >

    热门问题