将函数应用于numpy数组



我有一个numpy数组,其中包含来自雅虎财经的数据,如下所示:

!pip install yfinance
import yfinance
tickers = yfinance.Tickers('GCV22.CMX CLV22.NYM')

因此,对于每个符号,我每天都有开盘价,收盘价和成交量:

Open        High        Low        Close    Volume Dividends Stock Splits
Date                            
2021-09-20  1752.000000 1766.000000 1740.500000 1761.800049 3656    0   0
2021-09-21  1763.400024 1780.800049 1756.300049 1776.099976 11490   0   0
2021-09-22  1773.099976 1785.900024 1762.800049 1776.699951 6343    0   0
2021-09-23  1766.900024 1774.500000 1736.300049 1747.699951 10630   0   0
2021-09-24  1741.300049 1755.599976 1738.300049 1749.699951 10630   0   0

我在一篇论文(https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2422183)中发现了这个函数,我想把它应用到我的数据集,但我不明白如何应用它:

def fitKCA(t,z,q,fwd=0):    
'''
Inputs:
t: Iterable with time indices
z: Iterable with measurements
q: Scalar that multiplies the seed states covariance
fwd: number of steps to forecast (optional, default=0)
Output:
x[0]: smoothed state means of position velocity and acceleration
x[1]: smoothed state covar of position velocity and acceleration
Dependencies: numpy, pykalman
'''
#1) Set up matrices A,H and a seed for Q
h=(t[-1]-t[0])/t.shape[0]
A=np.array([[1,h,.5*h**2],
[0,1,h],
[0,0,1]])
Q=q*np.eye(A.shape[0])
#2) Apply the filter    
kf=KalmanFilter(transition_matrices=A,transition_covariance=Q)
#3) EM estimates
kf=kf.em(z)
#4) Smooth
x_mean,x_covar=kf.smooth(z)
#5) Forecast
for fwd_ in range(fwd):
x_mean_,x_covar_=kf.filter_update(filtered_state_mean=x_mean[-1], 
filtered_state_covariance=x_covar[-1])
x_mean=np.append(x_mean,x_mean_.reshape(1,-1),axis=0)
x_covar_=np.expand_dims(x_covar_,axis=0)
x_covar=np.append(x_covar,x_covar_,axis=0)
#6) Std series
x_std=(x_covar[:,0,0]**.5).reshape(-1,1)
for i in range(1,x_covar.shape[1]):
x_std_=x_covar[:,i,i]**.5
x_std=np.append(x_std,x_std_.reshape(-1,1),axis=1)
return x_mean,x_std,x_covar

在论文中他们说:Numpy数组t传递的是索引观察。Numpy数组z传递观测值。的种子值初始化状态协方差的EM估计。我如何用我的数据调用这个函数?我知道t应该是每个符号的索引列,那是数据列,z是我的numpy数组中每个符号的收盘价,q是一个随机种子,但我不能使它工作

论文中的函数说明您需要:

t: Iterable with time indices
z: Iterable with measurements
q: Scalar that multiplies the seed states covariance

计算方法如下:

import yfinance
from random import random
tickers = yfinance.Ticker('MSFT')
history = tickers.history()
# t is the timestamps indexed at 0 for each row
t = [h[0] for h in history.values]
# z is the measurement here choosing open price
z = history.values.Open
# q random seeds 
q = [random() for _ in t]
# finally call the function
fitKCA(t,z, q)

最新更新