Interval prediction | MATLAB implements QRCNN-GRU convolution gated recurrent unit quantile regression time series interval prediction

Interval prediction | MATLAB implements QRCNN-GRU convolution gated recurrent unit quantile regression time series interval prediction

List of effects

1
2
3

basic introduction

1. Matlab realizes the time series interval prediction model based on QRCNN-GRU quantile regression convolution gated recurrent unit;
2. Multi-map output, multi-index output (MAE, RMSE, MSE, R2), multi-input and single-output, including Different confidence interval diagrams and probability density diagrams;
3. data is a data set, power data set, using variables in the past period of time to predict the target, and the target is the last column, which is also applicable to load forecasting and wind speed forecasting; MainQRCNN_GRUTS is the main program, The rest are function files and do not need to be run;
4. The code is of high quality, with clear comments, including data preprocessing, and handling missing values. If it is nan, replace it with the previous line, and also includes kernel density estimation;

Model description

QRCNN-GRU is a neural network model mainly used for time series interval prediction tasks. Its full name is Quantile Regression Convolutional Gated Recurrent Unit, which includes technologies such as convolution, gated recurrent unit and quantile regression.
Convolutional layers are used to extract local features in time series, gated recurrent units are used to capture long-term dependencies between sequences, and quantile regression is used to estimate the uncertainty of predicted values. The combination of these techniques enables QRCNN-GRU to achieve better results in time series forecasting.
Specifically, QRCNN-GRU first uses convolutional layers to extract local features of time series, and then uses gated recurrent units to model these features to obtain a high-dimensional time series representation. Next, QRCNN-GRU uses quantile regression to learn the uncertainty of the predicted values, resulting in a distribution of predicted values. Finally, this distribution can be used to make interval predictions as needed.
In summary, QRCNN-GRU utilizes techniques such as convolution, gated recurrent unit, and quantile regression, which can effectively process time series data and show good performance in time series interval forecasting tasks.

  • The specific formula of QRCNN-GRU is as follows:

  • First, input time series x 1 : T x_{1:T}x1:TThe local feature representation c 1 : T ′ c_{1:T'} is obtained through the convolutional layerc1:T

c t = ∑ i = 1 k w i x t + i − 1 , t = 1 , … , T ′ c_t = \sum_{i=1}^k w_i x_{t+i-1},\qquad t=1,\ldots,T' ct=i=1kwixt+i1,t=1,,T

  • Among them, kkk is the size of the convolution kernel,wi w_iwiis the convolution kernel parameter.

  • Next, represent the local features c 1 : T ′ c_{1:T'}c1:TInput the gated recurrent unit to get a high-dimensional time series representation h 1 : T ′ ′ h_{1:T''}h1:T′′

r t = σ ( W r x t + U r h t − 1 + b r ) r_t = \sigma(W_r x_t + U_r h_{t-1} + b_r) rt=s ( Wrxt+Urht1+br)

z t = σ ( W z x t + U z h t − 1 + b z ) z_t = \sigma(W_z x_t + U_z h_{t-1} + b_z) zt=s ( Wzxt+Uzht1+bz)

h ~ t = tanh ⁡ ( W x t + U ( r t ⊙ h t − 1 ) + b ) \tilde{h}_t = \tanh(W x_t + U(r_t \odot h_{t-1}) + b) h~t=tanh ( W xt+U(rtht1)+b)

h t = ( 1 − z t ) ⊙ h t − 1 + z t ⊙ h ~ t h_t = (1-z_t) \odot h_{t-1} + z_t \odot \tilde{h}_t ht=(1zt)ht1+zth~t

  • Among them, σ \sigmaσ is the sigmoid function,⊙ \odot represents element-wise multiplication,W r W_rWr U r U_r Ur b r b_r br W z W_z Wz U z U_z Uz b z b_z bz W W W U U U b b b are the parameters of the gated recurrent unit, respectively.

  • Finally, quantile regression is used to learn the distribution of predicted values ​​P ( y ∣ x 1 : T ) P(y|x_{1:T})P(yx1:T)

y ^ ( τ ∣ x 1 : T ) = f τ ( h T ′ ′ ) \hat{y}(\tau|x_{1:T}) = f_\tau(h_{T''}) y^(τx1:T)=ft(hT′′)

  • Among them, τ \tauτ is the quantile,f τ f_\tauftis a quantile regression model.

  • According to needs, the distribution of predicted values ​​​​P ( y ∣ x 1 : T ) P(y|x_{1:T}) can be usedP(yx1:T) for interval prediction. For example, for a confidence level of1 − α 1-\alpha1For the interval prediction of α , you can calculatey ^ ( τ 1 ∣ x 1 : T ) \hat{y}(\tau_1|x_{1:T})y^( t1x1:T)y ^ ( τ 2 ∣ x 1 : T ) \hat{y}(\tau_2|x_{1:T})y^( t2x1:T),whereτ1 = α / 2 \tau_1 = \alpha/2t1=α /2τ 2 = 1 − α / 2 \tau_2 = 1-\alpha/2t2=1α /2 , and then get the interval[ y ^ ( τ 1 ∣ x 1 : T ) , y ^ ( τ 2 ∣ x 1 : T ) ] [\hat{y}(\tau_1|x_{1:T}), \hat{y}(\tau_2|x_{1:T})][y^( t1x1:T),y^( t2x1:T)]

programming

  • Complete program and data acquisition method: private message blogger.
% 定义QRCNN-GRU模型
num_filters = 16;
kernel_size = 3;
hidden_size = 32;
num_layers = 2;
num_quantiles = 3;
model = qrcnn_gru(num_filters, kernel_size, hidden_size, num_layers, num_quantiles);

% 定义损失函数和优化器
criterion = @(y_pred, y) mean((y_pred - y).^2);
optimizer = @(params, lr) adam(params, lr);

% 训练模型
num_epochs = 100;
batch_size = 32;
lr = 0.001;
params = model.get_params();
for epoch = 1:num_epochs
    for i = 1:num_batches
        x_batch = train_data(i, :, :);
        y_batch = train_labels(i, :);
        y_pred_batch = model.forward(x_batch);
        loss = criterion(y_pred_batch, y_batch);
        grad = model.backward(x_batch, y_pred_batch - y_batch);
        params = optimizer(params, grad, lr);
        model.set_params(params);
    end
end

% 预测
y_pred = model.forward(x_test);
lower = y_pred(:, 1);  % 取第一个分位数
upper = y_pred(:, end);  % 取最后一个分位数

The following is a sample code based on MATLAB for the training and prediction of the QRCNN-GRU model:

% 定义QRCNN-GRU模型
num_filters = 16;
kernel_size = 3;
hidden_size = 32;
num_layers = 2;
num_quantiles = 3;
model = qrcnn_gru(num_filters, kernel_size, hidden_size, num_layers, num_quantiles);

% 定义损失函数和优化器
criterion = @(y_pred, y) mean((y_pred - y).^2);
optimizer = @(params, lr) adam(params, lr);

% 训练模型
num_epochs = 100;
batch_size = 32;
lr = 0.001;
params = model.get_params();
for epoch = 1:num_epochs
    for i = 1:num_batches
        x_batch = train_data(i, :, :);
        y_batch = train_labels(i, :);
        y_pred_batch = model.forward(x_batch);
        loss = criterion(y_pred_batch, y_batch);
        grad = model.backward(x_batch, y_pred_batch - y_batch);
        params = optimizer(params, grad, lr);
        model.set_params(params);
    end
end

% 预测
y_pred = model.forward(x_test);
lower = y_pred(:, 1);  % 取第一个分位数
upper = y_pred(:, end);  % 取最后一个分位数

In this sample code, we first define a qrcnn_gru function for building a QRCNN-GRU model. During training, we use mean squared error as the loss function and Adam optimizer for model update. When predicting, we use the trained model to perform forward propagation on the test data, and then get the upper and lower bounds of the interval according to the prediction results.
It should be noted that since QRCNN-GRU is a relatively new model, its specific implementation may vary according to different papers and authors. Therefore, when implementing the QRCNN-GRU model, it needs to be adjusted and optimized in combination with specific papers and experimental results. In addition, since MATLAB is not a mainstream programming language in the field of deep learning, it may be subject to certain limitations when implementing the QRCNN-GRU model.

References

[1] https://blog.csdn.net/kjm13182345320/article/details/127931217
[2] https://blog.csdn.net/kjm13182345320/article/details/127418340

Guess you like

Origin blog.csdn.net/kjm13182345320/article/details/130663277