Statistical signal processing - Kalman filter stock forecast based on AR model - matlab simulation - with code

  1. Topics and design ideas

  1. topic

A Kalman filter stock forecast based on the AR model is given.

  1. Design ideas

This experiment realizes the stock price prediction of ZTE Corporation. The AR model is used to predict the stock price, and the Kalman filter is applied to the forecast result of the AR model. Filtering the predicted stock price can predict the stock price trend more accurately.

The first step is to load the stock data, and then divide the stock data into training data and prediction data, where the training data is the stock price of the previous 300 days, and the prediction data is the stock price of the 301-400 days.

The second step is to use the AR model for training, use the AR model to fit the stock price of the first 300 days, and use the fitted AR model to predict the stock price for the next 100 days.

The third step is to use the Kalman filter, first define the measurement noise covariance and process noise covariance matrices, then initialize the state transition matrix and the observation matrix, then initialize the state estimation and state estimation covariance respectively, and finally use the Kalman filter to process the state Iteratively estimate to obtain the estimated state, and finally compare the stock price predicted by the AR model with the stock price estimated by the AR model + Kalman filter to observe whether the predicted stock price is improved after adding the Kalman filter.

  1. Principles of AR Model and Kalman Filter Forecasting Stock Prices

  1. AR model

The AR model is an autoregressive model, a statistical model that connects the observed value at the current moment with the observed value at the previous moment, and this connection satisfies the autoregressive equation. The mathematical representation of the AR model is:

Where yt represents the observed value at the current moment, ϕ1, ϕ2, ⋯, ϕp represent autoregressive coefficients, yt-1, yt-2,

⋯, yt-p represents the observation value of the first p moments, ϵt represents the residual item, satisfying E(ϵt)=0 and E(ϵt2)=σ2 .

  1. Kalman filter

Kalman filter (KalmanFilter) is an optimal filter based on the state space model, which is used to deal with the problem of system state estimation. It can effectively estimate the random process affected by noise. The mathematical principle of Kalman filtering can be expressed by the following formula:

Prediction steps:

Update steps:

Where: xk represents the state vector, Pk represents the covariance matrix of the state vector, ϕk represents the state transition matrix, Hk represents the observation matrix, Qk represents the process noise covariance matrix, Rk represents the measurement noise covariance matrix, zk represents the observed value.

  1. Result analysis

Taking the 400-day stock price of ZTE as data, the stock price data of the first 300 days are used to train the model, and the data of the next 100 days are used for prediction, as shown in the figure below. The AR model prediction data on days 301-400 is shown as the red line.

Intercept the forecast area of ​​301-400 days and add the original data for comparison. The effect is as shown in the figure below. It can be seen that the AR model is effective in predicting stock prices.

As can be seen from the above figure, only using the AR model can predict the stock price to a certain extent. In terms of trend, the predicted data is basically consistent with the actual data.

After adding the Kalman filter, the predicted stock price chart is as follows

As can be seen from the above figure, the stock price after Kalman filtering is smoother than the original stock price, and some noise interference is filtered out, which makes the predicted stock price more accurate to a certain extent.

The figure below is the stock price prediction chart after AR model and Kalman filter.

It can be seen from the above figure that after Kalman filtering is performed on the data, the smoothness of the data becomes higher, and the error of the prediction data caused by noise is reduced, so it is helpful for the overall prediction accuracy.

  1. key code

The following code is the backbone code of the AR model and Kalman filter, and the rest of the code is in the appendix.

AR model and Kalman filter backbone code

model = ar(train_data, 50);

predictions = predict(model, pred_data, 100);

x_hat = x_hat_0;

P = P_0;

for i = 2:100

x_hat_minus = A * x_hat;

P_minus = A * P * A' + Q;

K = P_minus * C' / (C * P_minus * C' + R);

x_hat = x_hat_minus + K * (data1(i) - C * x_hat_minus);

P = (1 - K * C) * P_minus;

estimated_states(i) = x_hat;

end

完整代码:

data = csvread('D:\360downloads\stockdata.csv');
train_data = data(1:300, 2);
pred_data = data(301:400, 2);
model = ar(train_data, 50);
predictions = predict(model, pred_data, 100);
% 后300天股价预测值
figure;
time = data(301:400, 1);
plot(time, pred_data, 'b', time, predictions, 'r');
legend('真实股价', '预测股价');
xlabel('时间 (天)');
ylabel('股票价格');
title('使用AR模型对中兴通讯第301-400天的股价预测');
% 后300天
figure;
plot(data(1:400,1),data(:,2),'b-',time, predictions, 'r')
legend('真实股价', '预测股价');
xlabel('时间 (天)');
ylabel('股票价格');
title('使用AR模型对中兴通讯400天的股价预测');
%% 卡尔曼滤波
data1= predictions;
R = 1; 
Q = 0.1; 
A = 1;
C = 1;
x_hat_0 = data1(1);
P_0 = 1;
x_hat = x_hat_0;
P = P_0;
% Kalman filter
for i = 2:100
    % Predict the next state
    x_hat_minus = A * x_hat;
    P_minus = A * P * A' + Q;
    % Update the state estimate using the measurement
    K = P_minus * C' / (C * P_minus * C' + R);
    x_hat = x_hat_minus + K * (data1(i) - C * x_hat_minus);
    P = (1 - K * C) * P_minus;
    % Store the estimated state
    estimated_states(i) = x_hat;
end
time = 302:400;
figure;
plot(time, data1(2:100), 'b', time, estimated_states(2:100), 'r');
legend('AR模型预测的第301到400天的股价', 'AR模型+卡尔曼滤波得到的第301到400天的股价');
xlabel('时间 (天)');
ylabel('股价');
title('AR模型预测的股价加入卡尔曼滤波');
figure;
plot(data(1:400,1),data(:,2),'b-',time, estimated_states(2:100), 'r','linewidth',1)
legend('真实股价', 'AR+卡尔曼滤波预测股价');
xlabel('时间 (天)');
ylabel('股票价格');
title('AR模型+卡尔曼滤波对中兴通讯第301-400天的股价预测');

Guess you like

Origin blog.csdn.net/qq_22471349/article/details/129168101