我想通过插入卡尔曼滤波器使用多个变量(开盘价、低价、高价、成交量、收盘价)预测股票的收盘价(“收盘价”)。但是,我收到错误消息“所有参数的形状都不一致。请重新检查它们的值。我不确定我做错了什么。
要明确:我不希望基于每个变量的五个不同的股票预测,而是基于所有 5 个变量的一个价格。
代码如下:
import numpy as np
import yfinance as yf
from pykalman import KalmanFilter
spy_data = yf.download('SPY', start='2010-01-01', end='2023-03-17')
price = spy_data['Close'].values.reshape(-1, 1)
open_price = spy_data['Open'].values.reshape(-1, 1)
low_price = spy_data['Low'].values.reshape(-1, 1)
high_price = spy_data['High'].values.reshape(-1, 1)
volume = spy_data['Volume'].values.reshape(-1, 1)
initial_state = np.zeros(5)
initial_covariance = np.diag([100, 100, 100, 100, 100])
transition_matrix = np.array([[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1]])
observation_matrix = np.array([[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
process_noise = np.diag([0.001, 0.001, 0.001, 0.001, 0.001])
observation_noise = np.diag([0.1])
kf = KalmanFilter(
initial_state_mean=initial_state,
initial_state_covariance=initial_covariance,
transition_matrices=transition_matrix,
observation_matrices=observation_matrix,
observation_covariance=observation_noise,
transition_covariance=process_noise)
state_means, state_covariances = kf.filter(np.hstack([price, open_price, low_price, high_price, volume]))
# Predict next day's closing price
last_state_mean = state_means[-1]
last_state_covariance = state_covariances[-1]
next_state_mean, next_state_covariance = kf.filter_update(
last_state_mean, last_state_covariance, observation= np.array([spy_data['Adj Close'][-1], spy_data['Open'][-1], spy_data['Low'][-1], spy_data['High'][-1], spy_data['Volume'][-1]])
)
predicted_price = next_state_mean[0]
print(f"Today's SPY closing price: {price[-1][0]}")
print(f"Predicted SPY closing price for tomorrow: {predicted_price}")
这是您输入的形状,不幸的是,
KalmanFilter
没有指定细节,例如哪些形状引发错误,形状是什么等等。但是制作 list
s 而不是 array
s 解决了这个问题:
spy_data = yf.download('SPY', start='2010-01-01', end='2023-03-17')
price = spy_data['Close'].values.reshape(-1, 1)
open_price = spy_data['Open'].values.reshape(-1, 1)
low_price = spy_data['Low'].values.reshape(-1, 1)
high_price = spy_data['High'].values.reshape(-1, 1)
volume = spy_data['Volume'].values.reshape(-1, 1)
initial_state = [0.0]*5
initial_covariance = [100, 100, 100, 100, 100]
transition_matrix = [[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1]]
observation_matrix = [[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
process_noise = [0.001]*5
observation_noise = [0.1]*5
kf = KalmanFilter(
initial_state_mean=initial_state,
transition_matrices=transition_matrix,
observation_matrices=observation_matrix)
state_means, state_covariances = kf.filter(np.hstack([price, open_price, low_price, high_price, volume]))
# Predict next day's closing price
last_state_mean = state_means[-1]
last_state_covariance = state_covariances[-1]
next_state_mean, next_state_covariance = kf.filter_update(
last_state_mean, last_state_covariance, observation= np.array([spy_data['Adj Close'][-1], spy_data['Open'][-1], spy_data['Low'][-1], spy_data['High'][-1], spy_data['Volume'][-1]])
)
predicted_price = next_state_mean[0]
print(f"Today's SPY closing price: {price[-1][0]}")
print(f"Predicted SPY closing price for tomorrow: {predicted_price}")
输出:
[*********************100%***********************] 1 of 1 completed
Today's SPY closing price: 396.1099853515625
Predicted SPY closing price for tomorrow: 395.14577091668565