MATLAB数学建模——时间序列ARIMA及可视化

% 运行之前注意保存已有数据
clc;clear;
close all;

% 是否强制确定ARIMA模型(不理想的情况下选择1)
isKill = 1;

% 预测时间长度
PreTime = 100;

% 是否进行新时间段的预测,建议先用0再用1
isPre = 1;

% 导入数据集
data = readtable('data.xlsx');
y = data.Value;

% 拆分数据集为训练集和测试集
train_size = floor(0.8 * length(y));
train_data = y(1:train_size);
if isPre == 1
    train_data = y;
end
test_data = y(train_size+1:end);

% 平稳性检验 - ADF检验
[h_adf, p_adf] = adftest(y);
fprintf('ADF检验结果:\n');
fprintf('是否拒绝原假设(序列非平稳): %d\n', h_adf);
fprintf('p值: %.4f\n', p_adf);

% 平稳性检验 - KPSS检验
[h_kpss, p_kpss] = kpsstest(y);
fprintf('KPSS检验结果:\n');
fprintf('是否拒绝原假设(序列平稳): %d\n', ~h_kpss);
fprintf('p值: %.4f\n', p_kpss);

if isKill == 0
    % 差分确定d参数
    if h_adf == 1 && h_kpss == 0
        d = 0; 
        disp('序列已经平稳')
    elseif h_adf == 0 && h_kpss == 1
        d = 1; 
        disp('序列需要一阶差分')
    elseif h_adf == 1 && h_kpss == 1
        d = 2; 
        disp('序列需要二阶差分')
    else
        error('无法确定d参数');
    end
    
    % 差分处理
    if d > 0
        y_diff = diff(train_data, d);
    else
        y_diff = train_data;
    end
else
    y_diff = train_data;
end

% 自相关法确定p参数
figure
autocorr(y_diff)
title('Autocorrelation')

% 偏自相关法确定q参数
figure
parcorr(y_diff)
title('Partial Autocorrelation')

% 训练ARIMA模型
if isKill == 0
    model = arima('D', d, 'ARLags', [1 2], 'MALags', 1); % ARIMA(p, d, q) model
else 
    model = arima(2, 1, 1);
end
model = estimate(model, y_diff);

% 测试未来时间预测情况
if isPre == 0
    horizon = length(y) - train_size;
else
    horizon = PreTime;
end
[forecastTest, YMSE, forecastCI] = forecast(model, horizon, 'Y0', y_diff);
ResultScale(:, 1) = forecastTest - 1.64*sqrt(YMSE); %90置信区间下限 %95 改成1.96
ResultScale(:, 2) = forecastTest + 1.64*sqrt(YMSE); %90置信区间上限

% 自动确定的可能修正方法
if isKill == 0
    forecastTest = forecastTest + train_data(end) - forecastTest(1);
    ResultScale(:, 1) = forecastTest - 1.64*sqrt(YMSE); %90置信区间下限
    ResultScale(:, 2) = forecastTest + 1.64*sqrt(YMSE); %90置信区间上限
end

if isPre == 0
% 可视化测试结果
    figure
    plot(y, 'ko-')
    hold on
    plot(train_size+1:length(y), forecastTest, 'b-', 'LineWidth', 1.5)
    xlabel('Time')
    ylabel('Value')
    legend('Observations', 'Predictions')
    title('ARIMA Forecast Results with Prediction Range')
    % 预测结果取值范围可视化
    plot(train_size+1:length(y), ResultScale(:, 1), 'r--', 'LineWidth', 1.5)
    hold on
    plot(train_size+1:length(y), ResultScale(:, 2), 'r--', 'LineWidth', 1.5)
    fill([train_size+1:length(y), fliplr(train_size+1:length(y))], [ResultScale(:, 1)', fliplr(ResultScale(:, 2)')], 'r', 'FaceAlpha', 0.3)
    plot(train_size+1:length(y), forecastTest, 'b-', 'LineWidth', 1.5)
    xlabel('Time')
    ylabel('Value')
    legend('Lower Bound', 'Upper Bound', 'Predictions')
else
    train_size = length(y);
    figure
    plot(y, 'ko-')
    hold on
    plot(train_size+1:length(y)+PreTime, forecastTest, 'b-', 'LineWidth', 1.5)
    xlabel('Time')
    ylabel('Value')
    legend('Observations', 'Predictions')
    title('ARIMA Forecast Results with Prediction Range')
    % 预测结果取值范围可视化
    plot(train_size+1:length(y)+PreTime, ResultScale(:, 1), 'r--', 'LineWidth', 1.5)
    hold on
    plot(train_size+1:length(y)+PreTime, ResultScale(:, 2), 'r--', 'LineWidth', 1.5)
    fill([train_size+1:length(y)+PreTime, fliplr(train_size+1:length(y)+PreTime)], [ResultScale(:, 1)', fliplr(ResultScale(:, 2)')], 'r', 'FaceAlpha', 0.3)
    plot(train_size+1:length(y)+PreTime, forecastTest, 'b-', 'LineWidth', 1.5)
    xlabel('Time')
    ylabel('Value')
    legend('Lower Bound', 'Upper Bound', 'Predictions')  
end

if isPre == 0
    % 误差分析
    error = y(train_size+1:end) - forecastTest;
    mse = mean(error.^2);
    rmse = sqrt(mse);
    mae = mean(abs(error));
    
    fprintf('Mean Squared Error (MSE): %.4f\n', mse);
    fprintf('Root Mean Squared Error (RMSE): %.4f\n', rmse);
    fprintf('Mean Absolute Error (MAE): %.4f\n', mae);
    
    % 残差检验
    residuals = infer(model, y_diff);
    figure
    plot(residuals)
    xlabel('Time')
    ylabel('Residuals')
    title('Residuals')
    
    % 残差自相关检验
    figure
    autocorr(residuals)
    title('Autocorrelation of Residuals')
    
    % 残差偏自相关检验
    figure
    parcorr(residuals)
    title('Partial Autocorrelation of Residuals')
end

运行结果如下图所示:

  • 13
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
1. 准备数据。将时间序列数据导入MATLAB,确保数据的时间步长是固定的,没有丢失或重复的时间步。 2. 拟合ARIMA模型。使用“arima”函数拟合ARIMA模型。该函数需要输入时间序列数据和ARIMA模型的参数,例如AR(p)、MA(q)和差分阶数d。可以使用自动模型选择算法来选择最佳模型,也可以手动指定参数。例如,以下代码将拟合一个ARIMA(2,1,1)模型: ```MATLAB model = arima(2,1,1); fit = estimate(model, data); ``` 其中“data”是时间序列数据,fit包含了拟合的模型。 3. 模型诊断。使用“infer”函数对拟合的模型进行诊断,检查残差是否符合白噪声假设。如果残差不是白噪声,可以尝试调整模型参数或使用其他模型。例如,以下代码将检查拟合的模型的残差: ```MATLAB resid = infer(fit, data); whitenessTest(resid) ``` 其中“resid”是拟合模型的残差,whitenessTest函数检查残差是否符合白噪声假设。 4. 预测。使用“forecast”函数对未来时间步进行预测。例如,以下代码将预测未来10个时间步: ```MATLAB forecast = forecast(fit, 10); ``` 其中“10”是预测的时间步数,forecast包含预测的值和置信区间。 5. 可视化。使用MATLAB的图形函数将预测结果可视化。例如,以下代码将绘制预测结果和原始数据: ```MATLAB plot(data) hold on forecastPlot = plot(forecast); set(forecastPlot, 'Color', 'red') legend('Original', 'Forecast') ``` 其中“plot”函数绘制原始数据,“forecastPlot”绘制预测结果,set函数设置预测结果的颜色,legend函数绘制图例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值