Matlab实现季节性时间序列ARIMA模型预测这段是实现预测的关键代码,需要⼤家根据个⼈情况编写剩余代码
参数定义 : x为原始序列数据
y为进⾏周期差分变换后的x
w为差分运算之后的y
话不多说,直接上代码,后⾯有分段的代码讲解
figure(1);
美的历程
plot(x,'k-h');
% t(gca,'XTicklabel',{'2005/01','2006/08','2008/04','2009/12','2011/08','2013/04', ...
% '2014/12','2016/08','2018/04','2019/12','2021/08'});
% xlabel('Time');
% ylabel('Elevation');
% legend('Elevation of Lake Mead : 2005 - 2020');
grid on;
title('Raw data image') %原始数据图像
% subplot(1,2,2)
figure(2);
autocorr(x)
title('Autocorrelation function graph') % ⾃相关函数图像
%做1阶4步差分
s=12; %周期s=12
n=18; %预报数据的个数
m1=length(x); % x的个数
for i=s+1:m1
y(i-s)=x(i)-x(i-s); % 进⾏周期差分变换
end
w=diff(y,1); % 差分运算,消除趋势性
m2=length(w); w的个数
figure(3);
% subplot(1,2,1)
plot(w);
grid on;
% t(gca,'XTicklabel',{'2005/01','2006/08','2008/04','2009/12','2011/08','2013/04', ...
% '2014/12','2016/08','2018/04','2019/12','2021/08'});
% xlabel('Time');
% ylabel('Elevation');
% legend('Elevation of Lake Mead Data (2005 - 2020) after The differential');
title('Differential post quence image') % 差分后序列图像
% subplot(1,2,2)
figure(4);
autocorr(w)
title('Autocorrelation function graph')
% norm test 需要序列服从正态分布
figure(5);
normplot(x);
xlabel('Elevation Data');
ylabel('Posibility');
title('Normal probability graph');
%% lect the model
k = 0;
for i = 0 : 3 % 确定模型结构
for j = 0 : 3
for j = 0 : 3
if i == 0 & j == 0
continue;
elif i == 0
ToEstMd = arima('MALags',1 : j,'Constant',0);
elif j == 0
ToEstMd = arima('ARLags',1 : i,'Constant',0);
el
ToEstMd = arima('ARLags',1 : i,'MALags',1 : j,'Constant',0);
end
k = k + 1;R(k) = i;M(k) = j;
[EstMd,EstParamCov,logL,info] = estimate(ToEstMd,w');
numParams = sum(any(EstParamCov));
[aic(k),bic(k)] = aicbic(logL,numParams,m2);
妇女节贺卡
end
end
fprintf('R,M,AIC,BIC的对应值如下\n%f'); % 根据AIC、BIC准则定阶
check = [R',M',aic',bic']
r = input('输⼊阶数R = ');m = input('输⼊阶数M = ');
ToEstMd = arima('ARLags',1 : r,'MALags',1:m,'Constant',0); %指定模型的结构
%% estimate && forecast && results
[EstMd,EstParamCov,logL,info] = estimate(ToEstMd,w');%模型拟合
w_Forecast = forecast(EstMd,n,'Y0',w');
yhat = y(end) + cumsum(w_Forecast); %⼀阶差分的还原值
for j = 1:n
x(m1 + j) = yhat(j) + x(m1+j-s); %x的预测值
end
xhat = x(m1 + 1 : end); % 提取x预测的值
养老常识1.⾸先是将序列变为平稳的
figure(1);
甲减的临床表现plot(x,'k-h');
% t(gca,'XTicklabel',{'2005/01','2006/08','2008/04','2009/12','2011/08','2013/04', ... % '2014/12','2016/08','2018/04','2019/12','2021/08'});
% xlabel('Time');
% ylabel('Elevation');美丽人生电影
% legend('Elevation of Lake Mead : 2005 - 2020');
grid on;
title('Raw data image') %原始数据图像
% subplot(1,2,2)
figure(2);
autocorr(x)
title('Autocorrelation function graph') % ⾃相关函数图像
%做1阶4步差分
s=12; %周期s=12
劳动创造美好生活n=18; %预报数据的个数
m1=length(x); % x的个数
for i=s+1:m1
y(i-s)=x(i)-x(i-s); % 进⾏周期差分变换
end
w=diff(y,1); % 差分运算,消除趋势性
m2=length(w); w的个数
figure(3);
% subplot(1,2,1)
plot(w);
姐妹聚会grid on;
% t(gca,'XTicklabel',{'2005/01','2006/08','2008/04','2009/12','2011/08','2013/04', ... % '2014/12','2016/08','2018/04','2019/12','2021/08'});
% xlabel('Time');
% ylabel('Elevation');
% legend('Elevation of Lake Mead Data (2005 - 2020) after The differential');
title('Differential post quence image') % 差分后序列图像
% subplot(1,2,2)
figure(4);
autocorr(w)
title('Autocorrelation function graph')
Figure 1
可以看到原序列具有显著的趋势,初步判断为⾮平稳序列。
再看⾃相关函数图:
Figure 2
可以看到⾃相关函数图并未较快的衰减为0,因此该序列并⾮为平稳的。
1.1 确定季节性周期时间,进⾏周期性差分变换
确定序列为⾮平稳之后,在处理平稳性之前先需要做⼀件事:消除周期性、季节性,这⾥就⽤到了差分变换
%做1阶4步差分
s=12; %周期s=12
n=18; %预报数据的个数
m1=length(x); % x的个数
for i=s+1:m1
y(i-s)=x(i)-x(i-s); % 进⾏周期差分变换
end
1.2 如何变为平稳
利⽤差分运算,对数据进⾏⼀阶差分运算,具体⽤diff函数实现
w=diff(y,1); % 差分运算,消除趋势性
m2=length(w); w的个数
figure(3);
% subplot(1,2,1)
星座图标plot(w);
grid on;
% t(gca,'XTicklabel',{'2005/01','2006/08','2008/04','2009/12','2011/08','2013/04', ... % '2014/12','2016/08','2018/04','2019/12','2021/08'});
% xlabel('Time');
% ylabel('Elevation');
% legend('Elevation of Lake Mead Data (2005 - 2020) after The differential');
title('Differential post quence image') % 差分后序列图像
% subplot(1,2,2)
figure(4);
autocorr(w)
title('Autocorrelation function graph')
这⾥画了两张图,⽤plot和autocorr函数实现的
Figure 3
Figure 4