У меня есть функция, которая использует 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 к фрейму данных, чтобы я мог построить его ...