一、简介
1 时间序列的描述性分析
2 时间序列预测的程序
3 平稳序列的预测
4 趋势型序列的预测
5 复合型序列的分解预测
二、源代码
clear
syms a b;
c=[a b]';
A=[89677,99215,109655,120333,135823,159878,182321,209407,246619,300670];
B=cumsum(A); % 原始数据累加
n=length(A);
for i=1:(n-1)
C(i)=(B(i)+B(i+1))/2; % 生成累加矩阵
end
% 计算待定参数的值
D=A;D(1)=[];
D=D';
E=[-C;ones(1,n-1)];
c=inv(E*E')*E*D;
c=c';
a=c(1);b=c(2);
% 预测后续数据
F=[];F(1)=A(1);
for i=2:(n+10)
F(i)=(A(1)-b/a)/exp(a*(i-1))+b/a ;
end
G=[];G(1)=A(1);
for i=2:(n+10)
G(i)=F(i)-F(i-1); %得到预测出来的数据
end
Y=xlsread('sdata','Sheet1','E1:E227');
N = length(Y);
%% 原始数据可视化
figure(1)
plot(Y); xlim([1,N])
set(gca,'XTick',[1:18:N])
title('原始股票价格')
ylabel('元')
%% ARIMA模型
model = arima('Constant',0,'D',1,'Seasonality',12,...
'MALags',1,'SMALags',12)
Y0 = Y(1:13);
[fit,VarCov] = estimate(model,Y(14:end),'Y0',Y0);
%% 评估预测效果
Y1 = Y(1:100);
Y2 = Y(101:end);
Yf1 = forecast(fit,100,'Y0',Y1);
figure(2)
plot(1:N,Y,'b','LineWidth',2)
hold on
plot(101:200,Yf1,'k--','LineWidth',1.5)
xlim([0,200])
title('Prediction Error')
legend('Observed','Forecast','Location','NorthWest')
hold off
%% 预测未来股票趋势.
[Yf,YMSE] = forecast(fit,60,'Y0',Y);
upper = Yf + 1.96*sqrt(YMSE);
lower = Yf - 1.96*sqrt(YMSE);
三、运行结果
四、备注
版本:2014a