将历史功能值保存到数据框

时间:2019-01-19 16:50:24

标签: pandas function dataframe

我有一个函数,该函数使用大型数据框中的21天历史数据来计算数学公式。我不只是通过调用函数来获取静态value,而是希望将所有历史值保存在一个新的数据帧中,该数据帧可以看作一个时间序列。有什么想法吗?

代码:

from datetime import datetime
from numpy import cumsum, log, polyfit, sqrt, std, subtract
from numpy.random import randn
import fix_yahoo_finance as yf


def hurst(ts):

"""Returns the Hurst Exponent of the time series vector ts"""
# Create the range of lag values
lags = range(2, 10)

# 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

从Yahoo下载价格系列

sym=yf.download('^gspc','2000-06-01',).tail(25)

调用函数

hurst(sym['Adj Close'])

最终,我想在数据框中添加一个rolling(21) hurst列,以便对其进行绘图...

0 个答案:

没有答案