2024年MathorCup数模竞赛C题详解-代码

时间:2024-04-13 10:48:18

混合ARIMA-LSTM模型构建

clc
clear
close all;
load('appendix1.mat') %读入附件1
mape_record = [];
W_record = [];
pre_record = [];
for c=1:size(appendix1,2) % 遍历每一个分拣中心
    data = appendix1{1,c}; %将分拣中心另存出来
    train_num = 5; %用几期的值作为参考来训练权重
    %% 下面是ARIMA计算权重
    [y_train_ARIMA]=ARIMA(data(1:end-train_num,3),train_num); 
    y_ture_ARIMA = data(end-train_num+1:end,3);
    wmape_ARIMA = sum(abs(y_train_ARIMA-y_ture_ARIMA)./y_ture_ARIMA);
    % 下面计算MAPE
    APE_ARIMA = abs(y_ture_ARIMA - y_train_ARIMA) ./ abs(y_ture_ARIMA);  
    MAPE_ARIMA = mean(APE_ARIMA);
    %% 下面是LSTM计算权重
    ref = 30; %参考历史的多少期
    [y_train_LSTM]=LSTM(data(1:end-train_num,3),ref,train_num);
    y_ture_LSTM = data(end-train_num+1:end,3);
    wmape_LSTM = sum(abs(y_train_LSTM-y_ture_LSTM)./y_ture_LSTM);
    % 下面计算MAPE
    APE_LSTM = abs(y_ture_LSTM - y_train_LSTM) ./ abs(y_ture_LSTM);  
    MAPE_LSTM = mean(APE_LSTM);
    %% 下面计算权重
    W_ARIMA = (1/wmape_ARIMA)/((1/wmape_ARIMA)+(1/wmape_LSTM));
    W_LSTM = 1-W_ARIMA;
    W_record(c,1) = data(1,1); %记录id
    W_record(c,2) = W_ARIMA; %记录ARIMAMAPE
    W_record(c,3) = W_LSTM; %记录LSTMMAPE
    %% 下面记录MAPE
    mape_record(c,1) = data(1,1); %记录id
    mape_record(c,2) = MAPE_ARIMA; %记录ARIMAMAPE
    mape_record(c,3) = MAPE_LSTM; %记录LSTMMAPE
    %% 下面正式预测
    pre_step = 30; %预测未来多少期的值
    % ARIMA
    [pre_ARIMA]=ARIMA(data(:,3),pre_step); 
    % LSTM
    [pre_LSTM]=LSTM(data(:,3),ref,pre_step);
    % 混合
    pre = pre_ARIMA*W_ARIMA + pre_LSTM*W_LSTM;
    pre_record(1,c) = data(1,1); %记录id
    pre_record(2:1+pre_step,c) = pre; %记录预测值
end

完整数据与代码

第一问

在这里插入图片描述

第二问

在这里插入图片描述

完整资料获取,关注公众号云顶数模,或者直接点击下方咨询~