简体   繁体   English

将函数应用于 numpy 数组

[英]Apply a function to a numpy array

I have a numpy array with data from yahoo finance that i got like this:我有一个 numpy 数组,其中包含来自 yahoo Finance 的数据,我是这样得到的:

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

So for each symbol I have open low high close prices as well as volume, on a daily basis:因此,对于每个符号,我每天都有开盘低高收盘价和交易量:

                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

I found this function in a paper ( https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2422183 ) and I would like to apply it to my dataset, but I can't understand how to apply it:我在一篇论文( 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

In the paper they say: Numpy array t conveys the index of observations.他们在论文中说: Numpy 数组 t 传达了观察的索引。 Numpy array z passes the observations. Numpy 数组 z 通过观察结果。 Scalar q provides a seed value for initializing the EM estimation of the states covariance.标量 q 为初始化状态协方差的 EM 估计提供了一个种子值。 How can i call this function with my data?我如何用我的数据调用这个函数? I understand t should be the index column of each symbol, that is the data column, the z is the close price for each symbol of my numpy array, and qa random seed, but i can't make it works我知道 t 应该是每个符号的索引列,即数据列,z 是我的 numpy 数组的每个符号的收盘价,以及 qa 随机种子,但我无法使其工作

The function in the paper states that you need :论文中的功能说明您需要:

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

here is how you would compute them :这是您计算它们的方法:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM