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

Interval prediction | MATLAB implements QRCNN-BiGRU convolution bidirectional 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-BiGRU quantile regression convolution bidirectional gated recurrent unit;
2. Multi-map output, multi-index output (MAE, RMSE, MSE, R2), multiple input and single output, Contains 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, the target is the last column, and can also be applied to load forecasting and wind speed forecasting; MainQRCNN_BiGRUTS is the main program , the rest are function files, no need to 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, which also includes kernel density estimation.

Model description

The QRCNN-BiGRU model combines convolutional neural networks and bidirectional gated recurrent units for quantile regression and interval forecasting of time series data.
Quantile regression is a regression analysis method used to predict the value of data at different quantiles, such as median, upper quartile, lower quartile, etc. Interval prediction refers to the value range of forecast data within a given time range.
Therefore, the QRCNN-BiGRU model can be used for quantile regression and interval prediction of time series data, and has better prediction performance.
The specific formula of the QRCNN-BiGRU model can be divided into two parts, one part is the formula of QRCNN, and the other part is the formula of BiGRU.

  • The formula of the QRCNN part is as follows:

  • The input data is x ∈ R n × px \in R^{n \times p}xRn × p , wherenn is the length of the time series,ppp is the number of features at each time step.

  • Convolution layer:
    zi , jc = f ( ∑ k = 1 p W j , kc ∗ xi , k + bjc ) z^c_{i,j} = f(\sum_{k=1}^p W^c_{ j,k} \ast x_{i,k} + b^c_j)zi,jc=f(k=1pWj,kcxi,k+bjc)

  • Among them , zi , jcz^c_{i,j}zi,jcright iiThe jjth of the i time stepThe output of j convolution kernels,W j , kc W^c_{j,k}Wj,kcis jjThe kkth of the j convolution kernelk weights,∗ \ast means convolution operation,bjcb^c_jbjcis jjOffset of j convolution kernels,fff is the activation function.

  • 最大池化层:
    z i , j p = max ⁡ k = 1 l ( z i , j + k − 1 c ) z^p_{i,j} = \max_{k=1}^{l}(z^c_{i,j+k-1}) zi,jp=k=1maxl(zi,j+k1c)

  • Among them , zi , jpz^p_{i,j}zi,jpright iiThe jjth of the i time stepThe maximum pooling output of j convolution kernels, lll is the pooling window size.

  • 全连接层:
    z i f = g ( ∑ j = 1 q W i , j f z i , j p + b i f ) z^f_i = g(\sum_{j=1}^q W^f_{i,j} z^p_{i,j} + b^f_i) zif=g(j=1qWi,jfzi,jp+bif)

  • Among them zifz^f_izifright iiFully connected layer output for i time steps,W i , jf W^f_{i,j}Wi,jfright iiThe i neuron is connected to the jjthneuronWeights of j pooled outputs,bifb^f_ibifright iiBias of i neurons,ggg is the activation function.

  • Output layer:
    y ^ i = ∑ j = 1 kqjzif + b \hat{y}_i = \sum_{j=1}^k q_j z^f_i + by^i=j=1kqjzif+b

  • Among them y ^ i \hat{y}_iy^iright iiPredicted value for i time steps,qj q_jqjis jjWeights for the j quantiles,bbb is the bias.

  • The formula of the BiGRU part is as follows:

  • The input data is x ∈ R n × px \in R^{n \times p}xRn × p , wherenn is the length of the time series,ppp is the number of features at each time step.

  • 前向GRU:
    z t f = σ ( W f x t + U f h t − 1 + b f ) r t f = σ ( W r f x t + U r f h t − 1 + b r f ) h ~ t f = tanh ⁡ ( W h f x t + r t f ⊙ U h f h t − 1 + b h f ) h t f = ( 1 − z t f ) ⊙ h t − 1 f + z t f ⊙ h ~ t f z^f_t = \sigma(W^f x_t + U^f h_{t-1} + b^f) \\ r^f_t = \sigma(W_r^f x_t + U_r^f h_{t-1} + b_r^f) \\ \tilde{h}_t^f = \tanh(W_h^f x_t + r_t^f \odot U_h^f h_{t-1} + b_h^f) \\ h_t^f = (1 - z_t^f) \odot h_{t-1}^f + z_t^f \odot \tilde{h}_t^f ztf=s ( Wfxt+Ufht1+bf)rtf=s ( Wrfxt+Urfht1+brf)h~tf=fishy ( Whfxt+rtfUhfht1+bhf)htf=(1ztf)ht1f+ztfh~tf

  • Among them ztfz^f_tztfis the update gate of the forward GRU, rtfr^f_trtfis the reset gate, h ~ tf \tilde{h}_t^fh~tfis the candidate hidden state, htf h_t^fhtfis the hidden state of the forward GRU.

  • 后向GRU:
    z t b = σ ( W b x t + U b h t + 1 + b b ) r t b = σ ( W r b x t + U r b h t + 1 + b r b ) h ~ t b = tanh ⁡ ( W h b x t + r t b ⊙ U h b h t + 1 + b h b ) h t b = ( 1 − z t b ) ⊙ h t + 1 b + z t b ⊙ h ~ t b z^b_t = \sigma(W^b x_t + U^b h_{t+1} + b^b) \\ r^b_t = \sigma(W_r^b x_t + U_r^b h_{t+1} + b_r^b) \\ \tilde{h}_t^b = \tanh(W_h^b x_t + r_t^b \odot U_h^b h_{t+1} + b_h^b) \\ h_t^b = (1 - z_t^b) \odot h_{t+1}^b + z_t^b \odot \tilde{h}_t^b ztb=s ( Wbxt+Ubht+1+bb)rtb=s ( Wrbxt+Urbht+1+brb)h~tb=fishy ( Whbxt+rtbUhbht+1+bhb)htb=(1ztb)ht+1b+ztbh~tb

  • Among them ztbz^b_tztbis the update gate of the backward GRU, rtbr^b_trtbis the reset gate, h ~ tb \tilde{h}_t^bh~tbis the candidate hidden state, htb h_t^bhtbis the hidden state of the backward GRU.

  • Concatenate the hidden states of the forward and backward GRUs:
    ht = [ htf ; htb ] h_t = [h_t^f ; h_t^b]ht=[htf;htb]

  • The formula of the final output layer is:
    y ^ i = ∑ j = 1 kqjhi + b \hat{y}_i = \sum_{j=1}^k q_j h_i + by^i=j=1kqjhi+b

  • Among them y ^ i \hat{y}_iy^iright iiPredicted value for i time steps,qj q_jqjis jjWeights for the j quantiles,bbb is the bias.

programming

  • Complete program and data acquisition method: private message blogger.
% 加载数据
load example_data.mat

% 分割数据集
train_ratio = 0.7;
validation_ratio = 0.2;
test_ratio = 0.1;
[train_data, validation_data, test_data] = split_data(data, train_ratio, validation_ratio, test_ratio);

% 设置超参数
num_epochs = 100;
batch_size = 32;
learning_rate = 0.001;
num_conv_filters = 32;
conv_filter_size = 3;
pooling_size = 2;
num_hidden_units = 64;
num_quantiles = 3;

% 训练模型
model = train_qrcnn_bigru(train_data, validation_data, num_epochs, batch_size, learning_rate, num_conv_filters, conv_filter_size, pooling_size, num_hidden_units, num_quantiles);

% 测试模型
[test_loss, test_predictions] = test_qrcnn_bigru(model, test_data);

% 可视化结果
plot_results(test_data, test_predictions);
function [train_data, validation_data, test_data] = split_data(data, train_ratio, validation_ratio, test_ratio)
% 分割数据集
total_length = size(data, 1);
train_length = floor(total_length * train_ratio);
validation_length = floor(total_length * validation_ratio);
test_length = total_length - train_length - validation_length;

train_data = data(1:train_length, :);
validation_data = data(train_length+1:train_length+validation_length, :);
test_data = data(train_length+validation_length+1:end, :);
end

function model = train_qrcnn_bigru(train_data, validation_data, num_epochs, batch_size, learning_rate, num_conv_filters, conv_filter_size, pooling_size, num_hidden_units, num_quantiles)
% 训练QRCNN-BiGRU模型
input_size = size(train_data, 2) - 1;

% 构建模型
input_layer = sequenceInputLayer(input_size);
conv_layer = convolution1dLayer(conv_filter_size, num_conv_filters, 'Padding', 'same');
pool_layer = maxPooling1dLayer(pooling_size, 'Stride', 2);
flatten_layer = flattenLayer();
fc_layer = fullyConnectedLayer(num_hidden_units);
gru_layer = bidirectionalgruLayer(num_hidden_units, 'OutputMode', 'last');
output_layer = qrcnnRegressionLayer(num_quantiles);

% 组装网络层
layers = [input_layer;
          conv_layer;
          pool_layer;
          flatten_layer;
          fc_layer;
          gru_layer;
          output_layer];

% 设置训练选项
options = trainingOptions('adam', ...
                          'MaxEpochs', num_epochs, ...
                          'MiniBatchSize', batch_size, ...
                          'InitialLearnRate', learning_rate, ...
                          'ValidationData', validation_data, ...
                          'ValidationFrequency', 10, ...
                          'ValidationPatience', 5, ...
                          'CheckpointPath', tempdir);

% 训练模型
model = trainNetwork(train_data, layers, options);
end

function [test_loss, test_predictions] = test_qrcnn_bigru(model, test_data)
% 测试QRCNN-BiGRU模型
test_sequences = test_data(:, 2:end)';
num_test_samples = size(test_sequences, 2);
test_labels = test_data(:, 1);

% 测试模型
test_predictions = predict(model, test_sequences, 'MiniBatchSize', 1);

% 计算测试集损失
test_loss = qrcnnLoss(test_labels', test_predictions);

% 将预测结果转换为列向量
test_predictions = test_predictions(:);
end

function plot_results(test_data, test_predictions)
% 可视化测试结果
test_labels = test_data(:, 1);
num_test_samples = length(test_labels);

% 绘制真实值和预测值的对比图
figure;
plot(1:num_test_samples, test_labels, 'b-', 1:num_test_samples, test_predictions, 'r-');
xlabel('Time Step');
ylabel('Value');
legend('True', 'Predicted');

% 绘制预测误差的直方图
prediction_errors = test_predictions - test_labels;
figure;
histogram(prediction_errors);
xlabel('Prediction Error');
ylabel('Frequency');
end

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/130676589
Recommended