一、写在前面
这一期,我们使用Matlab进行SARIMA模型的构建。
不同样,这里使用另一个数据:
采用《PLoS One》2015年一篇题目为《Comparison of Two Hybrid Models for Forecasting the Incidence of Hemorrhagic Fever with Renal Syndrome in Jiangsu Province, China》文章的公开数据做演示。数据为江苏省2004年1月至2012年12月肾综合症出血热月发病率。运用2004年1月至2011年12月的数据预测2012年12个月的发病率数据。
Matlab语言,估计很多人没听说过。
所以,直接上我封装好的Matlab小程序即可。
二、Matlab建立SARIMA实战
1.界面介绍
1.1.数据输入 输入用于预测的原始数据,如图一。
1.2.原始数据绘图 点击弹出原始数据曲线图,以便观察数据的消长趋势以及周期性。
1.3.实际值输入 如果用于测试模型的预测精度,此时要预测的数据已知,则输入该数据集。如果用于预测数据,则要预测的数据未知,此时全部输入0。例如要预测6个数据,则需输入6个0。
1.4.平稳检验和相关图模块
1.4.1.原始数据 点击原始数据按钮,在相关图模块显示原始数据的自相关图以及偏相关图,并在显示检验结果(是否平稳)。
1.4.2.一般拆分 填入拆分次数,点击一般拆分按钮,在相关图模块显示经过一般拆分数据的自相关图以及偏相关图,并在显示检验结果(是否平稳);拆分次数即为d值。
1.4.2.季节性拆分 填入拆分次数,点击季节性拆分按钮,在相关图模块显示经过季节性拆分数据的自相关图以及偏相关图,并在显示检验结果(是否平稳);拆分次数即为D值。
1.4.3.一般+季节性拆分 点击一般+季节性拆分按钮,在相关图模块显示经过一般和季节性拆分数据的自相关图以及偏相关图,并在显示检验结果(是否平稳)。
1.5.自动参数寻找模块
1.5.1.Log 勾选后,原始数据经过Log变换。此选项影响所有步骤,包括平稳性检验、模型构建、拟合和预测。
1.5.2寻找 必须手动输入参数d和D,周期s默认为12,可以根据实际情况修改。点击寻找按钮后,程序自动寻找最优p、q、P、Q参数(四个参数取值均从0至3);并将最优参数值输出。
1.6.预测模块
1.6.1.预测数目(个) 显示需要预测数据的个数,程序自动从实际值输入文本框识别个数并输出。
1.6.2.模型参数 输入模型7个参数,其中周期s默认为12,可以根据需要修改。
1.6.3.预测 点击预测按钮后,输出AIC值、BIC值、预测值、实际值,弹出模型参数检验的窗口,并在曲线图模块显示实际值和预测值的曲线图,在误差模块显示预测误差的四个指标。
1.7.曲线图模块
1.7.1.拟合 点击拟合按钮后,显示模型拟合效果曲线图,并在误差模块输出拟合误差的四个指标,弹出显示模型具体拟合值的窗口。
1.7.2.预测 点击拟合按钮后,显示模型拟合效果曲线图,并在误差模块输出拟合误差的四个指标。
1.8.误差模块
1.8.1.拟合误差 MAE 输出模型预测的平均绝对误差;
MAPE 输出模型预测的平均相对误差;
MSE 输出模型预测的均方误差;
RMSE 输出模型预测的均方根误差。
1.8.2.预测误差 MAE 输出模型预测的平均绝对误差;
MAPE 输出模型预测的平均相对误差;
MSE 输出模型预测的均方误差;
RMSE 输出模型预测的均方根误差。
2.实例演示
2.1.打开软件,输入原始数据和实际值,点击原始数据绘图(这里我们用于测试模型精度,故实际值已知),如图二。
2.2.点击原始数据检验按钮,检验结果显示不平稳,如图三;
2.3.可以看到在12logs处数值很高,显示具有季节性,需进行一次季节性拆分;在季节性拆分按钮后的文本框输入1,点击季节性拆分按钮,检验结果显示平稳,如图四;
2.4.此时可在进行一次一般拆分,在一般拆分按钮后的文本框输入1,点击一般+季节性拆分按钮,检验结果显示平稳,如图五;
2.5.输入参数d、D,点击寻找按钮,程序开始寻找最优参数并输出,如图六、七;
2.6.输入参数七个模型参数,点击预测按钮,输出AIC值、BIC值、预测值、实际值,弹出模型参数检验的窗口,并在曲线图模块显示实际值和预测值的曲线图,在误差模块显示预测误差的四个指标,如图八;
2.7.在曲线图模块点击拟合按钮,显示模型拟合效果曲线图,并在误差模块输出拟合误差的四个指标,弹出显示模型具体拟合值的窗口,如图九。
2.8.拟合预测结果比较
模型 |
预测误差 |
拟合误差 |
||
MAE |
MAPE |
MAE |
MAPE |
|
Matlab ARIMA |
0.0074 |
0.4148 |
0.0085 |
0.4246 |
SPSS ARIMA |
0.0090 |
0.4859 |
0.0089 |
0.4219 |
由以上图表可知,Matlab所构建的ARIMA模型拟合以及预测效果跟SPSS基本一致。
三、小软件安装在此处
1. 双击MCRInstaller.exe安装
2. 自动解压:
3. 无脑点击下一步
4. 安装完成。
四、底层代码
可能有需要,我就贴上吧:
function varargout = ARIMAb(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ARIMA2_OpeningFcn, ...
'gui_OutputFcn', @ARIMA2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function ARIMA2_OpeningFcn(hObject, eventdata, handles, varargin)
global objs
handles.output = hObject;
movegui(hObject,'north')
objs.log=handles.logp;
objs.axes = [handles.axes1;handles.axes2;handles.axes3];
objs.disps1=[handles.jianyanjieguo;handles.yibanchaifen;handles.jijiexingchaifen];
objs.disps2=[handles.ppx;handles.d;handles.q;handles.pp;handles.dd;handles.qq];
objs.disps3 =[handles.ppp;handles.d2;handles.q2;handles.pp2;handles.dd2;handles.qq2];
objs.disps4=[handles.aic2;handles.bic2;handles.yucezhi2;handles.shijizhi2];
objs.disps5=[handles.mae;handles.mse;handles.mape;handles.rmse;handles.mae2;handles.mse2;handles.mape2;handles.rmse2];
objs.disps6=[handles.yuceshumu2;handles.s;handles.s2];
guidata(hObject, handles);
function varargout = ARIMA2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function shujushuru_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
guidata(hObject,handles);
function shujushuru_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function yuanshishujujianyan_Callback(hObject, eventdata, handles)
global X objs
X = str2num(get(handles.shujushuru,'String'));
if isempty(X)
return
end
if eq(get(objs.log,'Value'),1)
X = log(X);
end
h = adftest (X);
if h == 1
U = {'平稳'}
set (handles.jianyanjieguo,'String',U);
else
U = {'不平稳'}
set (handles.jianyanjieguo,'String',U);
end
axes(handles.axes1)
autocorr(X);
axes(handles.axes2)
parcorr(X);
guidata(hObject,handles);
function chaifenshujujianyan_Callback(hObject, eventdata, handles)
global X T W Z objs
X = str2num(get(handles.shujushuru,'String'));
W = str2num(get(handles.jijiexingchaifen,'String'));
Z = str2num(get(handles.zhouqi,'String'));
if isempty(X) || isempty(T) || isempty(W) || isempty(Z)
return
end
if eq(get(objs.log,'Value'),1)
X = log(X);
end
o = length (X')/Z;
for R = 1:1:o;
Q = X (:,((R-1)*Z+1):Z*R)
data{R} = Q
end;
for S= 1:1:W;
test =[];
for R = 1:1:(o-S);
sea = data{R+1} - data{R}
test=[test,sea]
end
end;
h = adftest(test);
if h == 1
U={'ƽÎÈ'}
set(handles.jianyanjieguo,'String',U)
else
U={'²»Æ½ÎÈ'}
set(handles.jianyanjieguo,'String',U)
end
axes(handles.axes1)
autocorr(test);
axes(handles.axes2)
parcorr(test);
guidata(hObject,handles);
function yuceshumu1_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
guidata(hObject,handles);
function yuceshumu1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function ppx_Callback(hObject, eventdata, handles)
function ppx_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function d_Callback(hObject, eventdata, handles)
function d_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function q_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
guidata(hObject,handles);
function q_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function pp_Callback(hObject, eventdata, handles)
function pp_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function dd_Callback(hObject, eventdata, handles)
function dd_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function qq_Callback(hObject, eventdata, handles)
function qq_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function s_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
guidata(hObject,handles);
function s_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function pushbutton5_Callback(hObject, eventdata, handles)
global X objs
X = str2num(get(handles.shujushuru,'String'));
ss = str2num(get(handles.s,'String'));
value1 = str2num(get(handles.d,'String'));
value2 = str2num(get(handles.dd,'String'));
if isempty(X) || isempty(value1) || isempty(value2) || isempty(ss)
return
end
X = X';
if eq(get(objs.log,'Value'),1)
X = log(X);
end
test =[];
ind = [];
for p =0:1:3
for d = value1
for q = 0:1:3
for dap =0:1:3
for dad =value2;
for daq = 0:1:3
ind = [ind;p d q dap dad daq];
end
end
end
end
end
end
T = size(ind,1);
tl = length(X);
v1 = zeros(T,1);
v2 = zeros(T,1);
h = waitbar(0,'1','Name','优化ARIMA参数',...
'CreateCancelBtn',...
'setappdata(gcbf,''canceling'',1)');
setappdata(h,'canceling',0)
for i = 1:T
if getappdata(h,'canceling')
break
end
p = ind(i,1);
d = ind(i,2);
q = ind(i,3);
dap=ind(i,4);
dad=ind(i,5);
daq=ind(i,6);
if eq(p,0) && eq(q,0) && eq(dap,0) && eq(daq,0)
amodel = arima('D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && eq(q,0) && eq(dap,0) && eq(daq,0)
amodel = arima('ARLags',1:p,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && ~eq(q,0) && eq(dap,0) && eq(daq,0)
amodel = arima('MALags',1:q,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && eq(q,0) && ~eq(dap,0) && eq(daq,0)
amodel = arima('SARLags',1:dap,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && eq(q,0) && eq(dap,0) && ~eq(daq,0)
amodel = arima('SMALags',1:daq,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && ~eq(q,0) && eq(dap,0) && eq(daq,0)
amodel = arima('ARLags',1:p,'MALags',1:q,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && eq(q,0) && ~eq(dap,0) && eq(daq,0)
amodel = arima('ARLags',1:p,'SARLags',(1:dap)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && eq(q,0) && eq(dap,0) && ~eq(daq,0)
amodel = arima('ARLags',1:p,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && ~eq(q,0) && ~eq(dap,0) && eq(daq,0)
amodel = arima('MALags',1:q,'SARLags',(1:dap)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && ~eq(q,0) && eq(dap,0) && ~eq(daq,0)
amodel = arima('MALags',1:q,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && eq(q,0) && ~eq(dap,0) && ~eq(daq,0)
amodel = arima('SARLags',(1:dap)*ss,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && ~eq(q,0) && ~eq(dap,0) && eq(daq,0)
amodel = arima('ARLags',1:p,'MALags',1:q,'SARLags',(1:dap)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && ~eq(q,0) && eq(dap,0) && ~eq(daq,0)
amodel = arima('ARLags',1:p,'MALags',1:q,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && eq(q,0) && ~eq(dap,0) && ~eq(daq,0)
amodel = arima('ARLags',1:p,'SARLags',(1:dap)*ss,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && ~eq(q,0) && ~eq(dap,0) && ~eq(daq,0)
amodel = arima('MALags',1:q,'SARLags',(1:dap)*ss,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
else
amodel = arima('ARLags',1:p,'MALags',1:q,'SARLags',(1:dap)*ss,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
end
if numel(X) >= max(amodel.P + amodel.Q + 1);
[EstMdl,VarCov,logL,info]=estimate(amodel,X,'Display','off');
f = length(X)-EstMdl.P;
errors = sqrt(diag(VarCov));
parameters = info.X;
tvalues = parameters./errors;
pvalues = zeros(size(tvalues));
for ii = 1:length(pvalues)
pvalues(ii) = (1-tcdf(abs(tvalues(ii)),f))*2;
end
n = size (VarCov,1);
[aic,bic] = aicbic (logL,n,tl);
v1(i) = aic;
v2(i) = bic;
if length(pvalues)>2
pvalues = pvalues(2:end-1);
if any(pvalues>0.05)
v1(i) = 3*10e8;
end
end
else
v1(i) = 4*10e8;
v2(i) = 4*10e8;
end
waitbar(i/T,h,['Runing ',num2str(i),' out of ',num2str(T)]);
end
delete(h);
[a,b] = min(v1);
re = ind(b,:);
set(handles.ppx,'String',num2str(re(1)));
set(handles.d,'String',num2str(re(2)));
set(handles.q,'String',num2str(re(3)));
set(handles.pp,'String',num2str(re(4)));
set(handles.dd,'String',num2str(re(5)));
set(handles.qq,'String',num2str(re(6)));
set(handles.s,'String',num2str(12));
guidata(hObject,handles);
function yuanshishujuxiangguantu_Callback(hObject, eventdata, handles)
global X objs
if eq(get(objs.log,'Value'),1)
X = log(X);
end
axes(handles.axes1)
autocorr(X);
axes(handles.axes2)
parcorr(X);
function chaifenshujuxiangguantu_Callback(hObject, eventdata, handles)
global X T W Z objs
X = str2num(get(handles.shujushuru,'String'));
T = str2num(get(handles.yibanchaifend,'String'));
W = str2num(get(handles.jijiexingchaifen,'String'));
Z = str2num(get(handles.zhouqi,'String'));
if eq(get(objs.log,'Value'),1)
X = log(X);
end
o = length (X')/Z;
for R = 1:1:o;
Q= X (:,((R-1)*Z+1):Z*R)
data{R}=Q
end;
for S= 1:1:W;
test =[];
for R = 1:1:(o-S);
sea = data{R+1} - data{R};
test=[test,sea] ;
end
end;
Y = diff(test,T);
axes(handles.axes1)
autocorr(Y);
axes(handles.axes2)
parcorr(Y);
function yibanchaifend_Callback(hObject, eventdata, handles)
global X T objs
X = str2num(get(handles.shujushuru,'String'));
T = str2num(get(handles.yibanchaifen,'String'));
if isempty(X) || isempty(T)
return
end
if eq(get(objs.log,'Value'),1)
X = log(X);
end
X =X';
Y = diff (X,T);
h = adftest (Y);
if h == 1
U={'ƽÎÈ'};
set(handles.jianyanjieguo,'String',U)
else
U={'²»Æ½ÎÈ'};
set(handles.jianyanjieguo,'String',U)
end
axes(handles.axes1)
autocorr(Y);
axes(handles.axes2)
parcorr(Y);
guidata(hObject,handles);
function yibanchaifend_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function jijiexingchaifen_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
guidata(hObject,handles);
function jijiexingchaifen_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function zhouqi_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
guidata(hObject,handles);
function zhouqi_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function jianyanjieguo_Callback(hObject, eventdata, handles)
function jianyanjieguo_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function shijizhi_Callback(hObject, eventdata, handles)
function shijizhi_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function aic_Callback(hObject, eventdata, handles)
function aic_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function yucezhi_Callback(hObject, eventdata, handles)
function yucezhi_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function yuceshumu2_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
guidata(hObject,handles);
function yuceshumu2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function canshup_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','0')
end
guidata(hObject,handles);
function canshup_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function d2_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','NaN')
end
guidata(hObject,handles);
function d2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function q2_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','NaN')
end
guidata(hObject,handles);
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function pp2_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','NaN')
end
guidata(hObject,handles);
function pp2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function dd2_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','NaN')
end
guidata(hObject,handles);
function dd2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function qq2_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','NaN')
end
guidata(hObject,handles);
function qq2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function s2_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','NaN')
end
guidata(hObject,handles);
function s2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function yuce2_Callback(hObject, eventdata, handles)
global X K B C A E F G H I L M T objs model
X = str2num(get(handles.shujushuru,'String'));
K = str2num(get(handles.ppp,'String'));
B = str2num(get(handles.d2,'String'));
C = str2num(get(handles.q2,'String'));
A = str2num(get(handles.pp2,'String'));
E = str2num(get(handles.dd2,'String'));
F = str2num(get(handles.qq2,'String'));
G = str2num(get(handles.s2,'String'));
H = str2num(get(handles.yuceshumu2,'String'));
M = str2num(get(handles.shijizhishuru,'String'));
if isempty(X) || isempty(K) || isempty(B) || isempty(C) || isempty(A) || isempty(E) || isempty(F) || isempty(G) || isempty(H) ||isempty(M)
return
end
if eq(get(objs.log,'Value'),1)
I = log(X');
else
I = X';
end
p = K;d=B;q = C;dap=A;daq=F;dad=E;ss=G;
if eq(p,0) && eq(q,0) && eq(dap,0) && eq(daq,0)
amodel = arima('D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && eq(q,0) && eq(dap,0) && eq(daq,0)
amodel = arima('ARLags',1:p,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && ~eq(q,0) && eq(dap,0) && eq(daq,0)
amodel = arima('MALags',1:q,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && eq(q,0) && ~eq(dap,0) && eq(daq,0)
amodel = arima('SARLags',1:dap,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && eq(q,0) && eq(dap,0) && ~eq(daq,0)
amodel = arima('SMALags',1:daq,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && ~eq(q,0) && eq(dap,0) && eq(daq,0)
amodel = arima('ARLags',1:p,'MALags',1:q,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && eq(q,0) && ~eq(dap,0) && eq(daq,0)
amodel = arima('ARLags',1:p,'SARLags',(1:dap)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && eq(q,0) && eq(dap,0) && ~eq(daq,0)
amodel = arima('ARLags',1:p,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && ~eq(q,0) && ~eq(dap,0) && eq(daq,0)
amodel = arima('MALags',1:q,'SARLags',(1:dap)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && ~eq(q,0) && eq(dap,0) && ~eq(daq,0)
amodel = arima('MALags',1:q,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && eq(q,0) && ~eq(dap,0) && ~eq(daq,0)
amodel = arima('SARLags',(1:dap)*ss,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && ~eq(q,0) && ~eq(dap,0) && eq(daq,0)
amodel = arima('ARLags',1:p,'MALags',1:q,'SARLags',(1:dap)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && ~eq(q,0) && eq(dap,0) && ~eq(daq,0)
amodel = arima('ARLags',1:p,'MALags',1:q,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif ~eq(p,0) && eq(q,0) && ~eq(dap,0) && ~eq(daq,0)
amodel = arima('ARLags',1:p,'SARLags',(1:dap)*ss,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
elseif eq(p,0) && ~eq(q,0) && ~eq(dap,0) && ~eq(daq,0)
amodel = arima('MALags',1:q,'SARLags',(1:dap)*ss,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
else
amodel = arima('ARLags',1:p,'MALags',1:q,'SARLags',(1:dap)*ss,'SMALags',(1:daq)*ss,'D',d,'Seasonality',ss*dad,'Constant',0);
end
model3=amodel;
if numel(X) <= max(amodel.P + amodel.Q + 1);
msgbox('待估计变量数目大于样本数目,无法计算!');
return
end
[fit3,VarCov,logL,info] = estimate(model3,I);
f = length(I)-fit3.P;
errors = sqrt(diag(VarCov));
parameters = info.X;
tvalues = parameters./errors;
pvalues = zeros(size(tvalues));
for i = 1:length(pvalues)
pvalues(i) = (1-tcdf(abs(tvalues(i)),f))*2;
end
strs = [];
if ~eq(p,0)
for i = 1:p
strs = [strs,{['AR(',num2str(i),')']}];%%%%
end
end
if ~eq(dap,0)
for i = 1:dap
strs = [strs,{['SAR(',num2str(i*ss),')']}];%%%%
end
end
if ~eq(q,0)
for i = 1:q
strs = [strs,{['MA(',num2str(i),')']}];%%%%
end
end
if ~eq(daq,0)
for i=1:daq
strs = [strs,{['SMA(',num2str(i*ss),')']}];
end
end
info1 = {'Parameter','Value','Standard Error','t Statistic','p Statistic'};
info2 = [{'Constant'},num2cell(0),{'Fixed'},{'Fixed'},{'Fixed'}];
info3 = [strs,{'Variance'}]';
info4 = [info.X(2:end),errors(2:end),tvalues(2:end),pvalues(2:end)];
info5 = [info1;info2;info3 num2cell(info4)];
f = figure;
setpixelposition(f,[680,558,560,220]);
uitable(f,'Data',info5,'unit','normalized','position',[0 0 1 1],'ColumnWidth',{100});
[yF,yMSE] = forecast(fit3,H,'Y0',I);
if eq(get(objs.log,'Value'),1)
L = exp(yF);
end
M = M';
MAE = mean(abs(L - M));
MAPE = mean(abs(L - M)./M);
MSE = mse (L - M);
RMSE = sqrt(mse(L - M));
[hang,lie] = size (VarCov);
T = length(X);
[aic,bic] = aicbic (logL,hang,T);
set (handles.aic2,'String',num2str(aic));
set (handles.bic2,'String',num2str(bic));
set (handles.mae2,'String',num2str(MAE));
set (handles.mape2,'String',num2str(MAPE));
set (handles.mse2,'String',num2str(MSE));
set (handles.rmse2,'String',num2str(RMSE));
set (handles.yucezhi2,'String',num2str(L),'Max',2);
set (handles.shijizhi2,'String',num2str(M),'Max',2);
axes(handles.axes3)
plot(M,'r','LineWidth',2);
hold on
plot(L,'k--','LineWidth',1.5);
xlim([0,H])
title('预测效果拟合曲线')
legend('实际值','预测值','Location','NorthEast')
hold off
model = fit3;
guidata(hObject,handles);
function shijizhi2_Callback(hObject, eventdata, handles)
function shijizhi2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function aic2_Callback(hObject, eventdata, handles)
function aic2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function yucezhi2_Callback(hObject, eventdata, handles)
function yucezhi2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function nihexiaoguo_Callback(hObject, eventdata, handles)
global model X objs
if isempty(X) || isempty(model)
return
end
if eq(get(objs.log,'Value'),1)
y = log(X');
else
y = X';
end
yv = zeros(size(y));
h = waitbar(0,'ÇëµÈ´ý');
for i = model.P:length(yv)-1
vF1 = forecast(model,1,'Y0',y(1:i));
yv(i+1) = vF1;
waitbar(i/(length(yv)-1),h);
end
delete(h);
yv(1:model.P) =[];
y(1:model.P)=[];
if eq(get(objs.log,'Value'),1)
y = exp(y);
yv=exp(yv);
end
f = figure;
dispdata = [{'y','yp'};num2cell([y yv])];
uitable(f,'Data',dispdata,'unit','normalized','position',[0 0 1 1],'ColumnWidth',{100});
MAE = mean(abs(y - yv));
MAPE = mean(abs(y - yv)./y);
MSE = mse(y - yv);
RMSE = sqrt(mse(y - yv));
set(objs.disps5(1),'String',MAE);
set(objs.disps5(2),'String',MSE);
set(objs.disps5(3),'String',MAPE);
set(objs.disps5(4),'String',RMSE);
axes(handles.axes3)
plot(y,'r','LineWidth',2);
hold on
plot(yv,'k--','LineWidth',1.5);
xlim([0,length(y)])
title('拟合曲线')
legend('实际值','拟合值','Location','NorthEast')
hold off
guidata(hObject,handles);
function yucexiaoguo_Callback(hObject, eventdata, handles)
global X K B C A E F G H I L M T objs model
X = str2num(get(handles.shujushuru,'String'));
K = str2num(get(handles.ppp,'String'));
B = str2num(get(handles.d2,'String'));
C = str2num(get(handles.q2,'String'));
A = str2num(get(handles.pp2,'String'));
E = str2num(get(handles.dd2,'String'));
F = str2num(get(handles.qq2,'String'));
G = str2num(get(handles.s2,'String'));
H = str2num(get(handles.yuceshumu2,'String'));
M = str2num(get(handles.shijizhishuru,'String'));
if isempty(X) || isempty(K) || isempty(B) || isempty(C) || isempty(A) || isempty(E) || isempty(F) || isempty(G) || isempty(H) ||isempty(M) ||isempty(model)
return
end
if eq(get(objs.log,'Value'),1)
I = log(X');
else
I = X';
end
fit3=model;
[yF,yMSE] = forecast(fit3,H,'Y0',I);
if eq(get(objs.log,'Value'),1)
L = exp(yF);
end
axes(handles.axes3)
plot(M,'r','LineWidth',2);
hold on
plot(L,'k--','LineWidth',1.5);
xlim([0,H])
title('预测效果拟合曲线')
legend('实际值','预测值','Location','NorthEast')
hold off
model = fit3;
guidata(hObject,handles);
function mae_Callback(hObject, eventdata, handles)
function mae_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function mape_Callback(hObject, eventdata, handles)
function mape_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function mse_Callback(hObject, eventdata, handles)
function mse_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function rmse_Callback(hObject, eventdata, handles)
function rmse_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function mae2_Callback(hObject, eventdata, handles)
function mae2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function mape2_Callback(hObject, eventdata, handles)
function mape2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function mse2_Callback(hObject, eventdata, handles)
function mse2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function rmse2_Callback(hObject, eventdata, handles)
function rmse2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function ppp_Callback(hObject, eventdata, handles)
input = str2num(get(hObject,'String'));
if (isempty(input))
set(hObject,'String','NaN')
end
guidata(hObject,handles);
function ppp_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function shijizhishuru_Callback(hObject, eventdata, handles)
global objs
num = str2num(get(hObject,'String'));
if length(num)>1
set(objs.disps6(1),'String',num2str(length(num)));
end
guidata(hObject,handles);
function shijizhishuru_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function yuanshishujuhuitu_Callback(hObject, eventdata, handles)
global X T
X = str2num(get(handles.shujushuru,'String'));
if isempty(X)
return
end
T = length (X);
figure
hold on
plot(X,'k-','LineWidth',1.5)
xlim([0,T])
title('原始数据曲线图')
hold off
guidata(hObject,handles);
function yibanjijiexingchaifen_Callback(hObject, eventdata, handles)
global X T W Z objs
X = str2num(get(handles.shujushuru,'String'));
T = str2num(get(handles.yibanchaifen,'String'));
W = str2num(get(handles.jijiexingchaifen,'String'));
Z = str2num(get(handles.zhouqi,'String'));
if isempty(X) || isempty(T) || isempty(W) || isempty(Z)
return
end
if eq(get(objs.log,'Value'),1)
X = log(X);
end
o = length (X')/Z;
for R = 1:1:o;
Q = X (:,((R-1)*Z+1):Z*R)
data{R} = Q
end;
for S= 1:1:W;
test =[];
for R = 1:1:(o-S);
sea = data{R+1} - data{R}
test=[test,sea]
end
end;
Y = diff(test,T);
h = adftest(Y);
if h == 1
U={'平稳'}
set(handles.jianyanjieguo,'String',U)
else
U={'不平稳'}
set(handles.jianyanjieguo,'String',U)
end
axes(handles.axes1)
autocorr(Y);
axes(handles.axes2)
parcorr(Y);
guidata(hObject,handles);
function bic2_Callback(hObject, eventdata, handles)
function bic2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function bic_Callback(hObject, eventdata, handles)
function bic_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function pushbutton12_Callback(hObject, eventdata, handles)
global X K B C A E F G H I L M T
X = str2num(get(handles.shujushuru,'String'));
K = str2num(get(handles.ppx,'String'));
B = str2num(get(handles.d,'String'));
C = str2num(get(handles.q,'String'));
A = str2num(get(handles.pp,'String'));
E = str2num(get(handles.dd,'String'));
F = str2num(get(handles.qq,'String'));
G = str2num(get(handles.s,'String'));
H = str2num(get(handles.yuceshumu,'String'));
M = str2num(get(handles.shijizhishuru,'String'));
I = log(X');
model3 = arima('ARLags',K,'MALags',C,'D',B,'SARLags',A*G,'SMALags',F*G,'Seasonality',G,'Constant',0);
[fit3,VarCov,logL,info] = estimate(model3,I);
[yF,yMSE] = forecast(fit3,H,'Y0',I);
L = exp (yF);
M = M';
MAE = mean(abs(L - M));
MAPE = mean(abs(L - M)./M);
MSE = mse (L - M);
RMSE = sqrt(mse(L - M));
[hang,lie] = size (VarCov);
T = length(X);
[aic,bic] = aicbic (logL,hang,T);
set (handles.aic,'String',num2str(aic));
set (handles.bic,'String',num2str(bic));
set (handles.mae2,'String',num2str(MAE));
set (handles.mape2,'String',num2str(MAPE));
set (handles.mse2,'String',num2str(MSE));
set (handles.rmse2,'String',num2str(RMSE));
set (handles.yucezhi,'String',num2str(L),'Max',2);
set (handles.shijizhi,'String',num2str(M),'Max',2);
axes(handles.axes3)
plot(M,'r','LineWidth',2);
hold on
plot(L,'k--','LineWidth',1.5);
xlim([0,H])
title('Ô¤²âЧ¹ûÄâºÏÇúÏß')
legend('ʵ¼ÊÖµ','Ô¤²âÖµ','Location','NorthEast')
hold off
guidata(hObject,handles);
function yibanchaifen_Callback(hObject, eventdata, handles)
function yibanchaifen_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function logp_Callback(hObject, eventdata, handles)
global objs
for i = 1:length(objs.axes)
cla(objs.axes(i))
end
set(objs.disps1,'String','');
set(objs.disps2,'String','');
set(objs.disps3,'String','');
set(objs.disps4,'String','');
set(objs.disps5,'String','');
function tStatistic= print1(Mdl, covariance)
if size(covariance,1) ~= size(covariance,2)
error(message('econ:arima:print:NonSquareCovarianceMatrix'))
end
constant = Mdl.Constant;
beta = Mdl.Beta;
beta = beta(:)';
AR = reflect(getLagOp(Mdl, 'AR'));
SAR = reflect(getLagOp(Mdl, 'SAR'));
MA = getLagOp(Mdl, 'MA');
SMA = getLagOp(Mdl, 'SMA');
LagsAR = AR.Lags;
LagsSAR = SAR.Lags;
LagsMA = MA.Lags;
LagsSMA = SMA.Lags;
LagsAR = LagsAR(LagsAR > 0);
LagsSAR = LagsSAR(LagsSAR > 0);
LagsMA = LagsMA(LagsMA > 0);
LagsSMA = LagsSMA(LagsSMA > 0);
if isempty(LagsAR)
AR = [];
else
AR = AR.Coefficients;
AR = [AR{LagsAR}];
end
if isempty(LagsSAR)
SAR = [];
else
SAR = SAR.Coefficients;
SAR = [SAR{LagsSAR}];
end
if isempty(LagsMA)
MA = [];
else
MA = MA.Coefficients;
MA = [MA{LagsMA}];
end
if isempty(LagsSMA)
SMA = [];
else
SMA = SMA.Coefficients;
SMA = [SMA{LagsSMA}];
end
isDistributionT = strcmpi(Mdl.Distribution.Name, 'T');
isVarianceConstant = ~isa(Mdl.Variance, 'internal.econ.LagIndexableTimeSeries');
nARIMA = 1 + numel(LagsAR) + numel(LagsSAR) + ...
numel(LagsMA) + numel(LagsSMA) + numel(beta);
if isVarianceConstant
if isDistributionT
parameters = zeros(nARIMA + 2,1);
parameters(end - 1) = Mdl.Variance;
parameters(end) = Mdl.Distribution.DoF;
else
parameters = zeros(nARIMA + 1,1);
parameters(end) = Mdl.Variance;
end
parameters(1:nARIMA) = [constant AR SAR MA SMA beta];
if numel(parameters) ~= numel(solve)
error(message('econ:arima:print:ModelCovarianceInconsistency'))
end
else
if isDistributionT
parameters(nARIMA + 1) = Mdl.Distribution.DoF;
parameters(1:nARIMA) = [constant AR SAR MA SMA beta];
else
parameters = [constant AR SAR MA SMA beta];
end
end
Fix = ~solve;
errors = sqrt(diag(covariance));
tStatistic = parameters ./ errors;
end
function pushbutton13_Callback(hObject, eventdata, handles)
close ARIMAb
main
function pushbutton14_Callback(hObject, eventdata, handles)
set(handles.shujushuru,'String',[]);
set(handles.shijizhishuru,'String',[]);
set(handles.jianyanjieguo,'String',[]);
set(handles.yibanchaifen,'String',[]);
set(handles.jijiexingchaifen,'String',[]);
set(handles.zhouqi,'String',[]);
set(handles.ppx,'String',[]);
set(handles.d,'String',[]);
set(handles.p,'String',[]);
set(handles.pp,'String',[]);
set(handles.dd,'String',[]);
set(handles.qq,'String',[]);
set(handles.s,'String',[]);
set(handles.yuceshumu2,'String',[]);
set(handles.ppp,'String',[]);
set(handles.d2,'String',[]);
set(handles.p2,'String',[]);
set(handles.pp2,'String',[]);
set(handles.dd2,'String',[]);
set(handles.qq2,'String',[]);
set(handles.s2,'String',[]);
cla(handles.axes2);
cla(handles.axes3);
set(handles.aic2,'String',[]);
set(handles.bic2,'String',[]);
set(handles.yucezhi2,'String',[]);
set(handles.shijizhi2,'String',[]);
set(handles.mae,'String',[]);
set(handles.mape,'String',[]);
set(handles.mse,'String',[]);
set(handles.rmse,'String',[]);
cla(handles.axes1);
set(handles.mae2,'String',[]);
set(handles.mape2,'String',[]);
set(handles.mse2,'String',[]);
set(handles.rmse2,'String',[]);
五、软件和数据
链接:https://pan.baidu.com/s/1ZZtT6VTUnqGtQaAZ9NlyCg?pwd=hk4p
提取码:hk4p