clear;
%--------------------------------油价序列零均值化后的数据如下----------------------------------------%:
P=[ 19.5900 14.9100 15.7400 15.4000 13.0600 19.0700 15.2800 15.8200 12.7700 12.0500...
11.6900 13.8500 13.8500 10.0700 9.1700 10.7900 13.4400 21.1700 18.6400 13.2100...
15.5400 21.9400 23.1100 18.6400 14.9400 16.9000 15.4600 11.1500 13.1300 12.4800...
12.9500 12.5900 10.5800 10.5800 12.3900 15.5300 13.0600 10.2200 16.3300 19.7200...
21.3100 18.8400 24.8400 15.6700 15.5700 12.7300 13.5600 15.5400 17.2200 12.1400...
11.0700 12.0200 11.5500 6.9200 10.3300 8.3800 12.1100 11.4600 12.7500 13.3200...
13.0000 11.9000 11.7900 12.5500 11.8400 11.2500 11.1500 10.9900 11.7000 14.0100...
17.5100 17.2700 16.9000 15.7900 15.4500 6.2400 16.7100 16.7700 16.6400 17.8000...
16.8700 16.1300 15.7600 15.6600 15.5400 15.3000 15.0500 14.6900 14.3900 14.1800...
13.70 13.66 13.27 13.56 13.14 14.19 ];
F=[ 19.5900 14.9100 15.7400 15.4000 13.0600 19.0700 15.2800 15.8200 12.7700 12.0500...
11.6900 13.8500 13.8500 10.0700 9.1700 10.7900 13.4400 21.1700 18.6400 13.2100...
15.5400 21.9400 23.1100 18.6400 14.9400 16.9000 15.4600 11.1500 13.1300 12.4800...
12.9500 12.5900 10.5800 10.5800 12.3900 15.5300 13.0600 10.2200 16.3300 19.7200...
21.3100 18.8400 24.8400 15.6700 15.5700 12.7300 13.5600 15.5400 17.2200 12.1400...
11.0700 12.0200 11.5500 6.9200 10.3300 8.3800 12.1100 11.4600 12.7500 13.3200...
13.0000 11.9000 11.7900 12.5500 11.8400 11.2500 11.1500 10.9900 11.7000 14.0100...
17.5100 17.2700 16.9000 15.7900 15.4500 6.2400 16.7100 16.7700 16.6400 17.8000...
16.8700 16.1300 15.7600 15.6600 15.5400 15.3000 15.0500 14.6900 14.3900 14.180];
%----------------------由于时间序列有不平稳趋势,进行两次差分运算,消除趋势性----------------------%
for i=2:96
Yt(i)=P(i)-P(i-1);
end
for i=3:96
L(i)=Yt(i)-Yt(i-1);
end
figure;
L=L(3:96);
Y=L(1:88);
plot(P);
title('原数据序列图');
hold on;
pause
plot(Y,'r');
title('两次差分后的序列图和原数对比图');
pause
%--------------------------------------对数据标准化处理----------------------------------------------%
Ux=sum(Y)/88 % 求序列均值
yt=Y-Ux;
b=0;
for i=1:88
b=yt(i)^2/88+b;
end
v=sqrt(b) % 求序列方差
Y=(Y-Ux)/v; % 标准化处理公式
f=F(1:88);
t=1:88;
figure;
plot(t,f,t,Y,'r')
title('原始数据和标准化处理后对比图');
xlabel('时间t'),ylabel('油价y');
legend('原始数据 F ','标准化后数据Y ');
pause
%--------------------------------------对数据标准化处理----------------------------------------------%
%------------------------检验预处理后的数据是否符合AR建模要求,计算自相关和偏相关系数---------------%
%---------------------------------------计算自相关系数-----------------------------------%
R0=0;
for i=1:88
R0=Y(i)^2/88+R0;
end
R0
for k=1:20
R(k)=0;
for i=k+1:88
R(k)=Y(i)*Y(i-k)/88+R(k);
end
R %自协方差函数R
end
x=R/R0 %自相关系数x
figure;
plot(x)
title('自相关系数分析图');
pause
%-----------------------------------计算自相关系数-------------------------------------%
%-----------------------解Y-W方程,其系数矩阵是Toepli矩阵。求得偏相关函数X-----------------------%
X1=x(1);
X11=x(1);
B=[x(1) x(2)]';
x2=[1 x(1)];
A=toeplitz(x2);
X2=A\B
X22=X2(2)
B=[x(1) x(2) x(3)]';
x3=[1 x(1) x(2)];
A=toeplitz(x3);
X3=A\B
X33=X3(3)
B=[x(1) x(2) x(3) x(4)]';
x4=[1 x(1) x(2) x(3)];
A=toeplitz(x4);
X4=A\B
X44=X4(4)
B=[x(1) x(2) x(3) x(4) x(5)]';
x5=[1 x(1) x(2) x(3) x(4)];
A=toeplitz(x5);
X5=A\B
X55=X5(5)
B=[x(1) x(2) x(3) x(4) x(5) x(6)]';
x6=[1 x(1) x(2) x(3) x(4) x(5)];
A=toeplitz(x6);
X6=A\B
X66=X6(6)
B=[x(1) x(2) x(3) x(4) x(5) x(6) x(7)]';
x7=[1 x(1) x(2) x(3) x(4) x(5) x(6)];
A=toeplitz(x7);
X7=A\B
X77=X7(7)
B=[x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8)]';
x8=[1 x(1) x(2) x(3) x(4) x(5) x(6) x(7)];
A=toeplitz(x8);
X8=A\B
X88=X8(8)
B=[x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) x(9)]';
x9=[1 x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8)];
A=toeplitz(x9);
X9=A\B
X99=X9(9)
B=[x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) x(9) x(10)]';
x10=[1 x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) x(9)];
A=toeplitz(x10);
X10=A\B
X1010=X10(10)
B=[x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) x(9) x(10) x(11)]';
x11=[1 x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) x(9) x(10)];
A=toeplitz(x11);
X101=A\B
X1111=X101(11)
B=[x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) x(9) x(10) x(11) x(12)]';
x12=[1 x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) x(9) x(10) x(11)];
A=toeplitz(x12);
X12=A\B
X1212=X12(12)
X=[X11 X22 X33 X44 X55 X66 X77 X88 X99 X1010 X1111 X1212]
%-----------------------------------解Y-W方程,得偏相关函数X-------------------------------------%
figure;
plot(X);
title('偏相关函数图');
pause
%-----根据偏相关函数截尾性,初判模型阶次为5。用最小二乘法估计参数,计算10阶以内的模型残差方差和AIC值,应用AIC准则为模型定阶------%
S=[R0 R(1) R(2) R(3) R(4)];
G=toeplitz(S);
W=inv(G)*[R(1:5)]' % 参数W(i) 与X5相同
K=0;
for t=6:88
r=0;
for i=1:5
r=W(i)*Y(t-i)+r;
end
at= Y(t)-r;
K=(at)^2+K;
end
U(5)=K/(88-5) % 5阶模型残差方差 0.4420
K=0;T=X1;
for t=2:88
at=Y(t)-T(1)*Y(t-1);
K=(at)^2+K;
end
U(1)=K/(89-1) % 1阶模型残差方差0.6954
K=0;T=X2;
for t=3:88
r=0;
for i=1:2
r=T(i)*Y(t-i)+r;
end
at= Y(t)-r;
K=(at)^2+K;
end
U(2)=K/(88-2) % 2阶模型残差方差 0.6264
K=0;T=X3;
for t=4:88
r=0;
for i=1:3
r=T(i)*Y(t-i)+r;
end
at= Y(t)-r;
K=(at)^2+K;
end
U(3)=K/(88-3) % 3阶模型残差方差 0.5327
K=0;T=X4;
for t=5:88
r=0;
for i=1:4
r=T(i)*Y(t-i)+r;
end
at= Y(t)-r;
K=(at)^2+K;
end
U(4)=K/(88-4) % 4阶模型残差方差 0.4751
K=0;T=X6;
for t=7:88
r=0;
for i=1:6
r=T(i)*Y(t-i)+r;
end
at= Y(t)-r;
K=(at)^2+K;
end
U(6)=K/(88-6) % 6阶模型残差方差 0.4365
K=0;T=X7;
for t=8:88
r=0;
for i=1:7
r=T(i)*Y(t-i)+r;
end
at= Y(t)-r;
K=(at)^2+K;
end
U(7)=K/(88-7) % 7阶模型残差方差 0.4331
K=0;T=X8;
for t=9:88
r=0;
for i=1:8
r=T(i)*Y(t-i)+r;
end
at= Y(t)-r;
K=(at)^2+K;
end
U(8)=K/(88-8) % 8阶模型残差方差0.4310
K=0;T=X9;
for t=10:88
r=0;
for i=1:9
r=T(i)*Y(t-i)+r;
end
at= Y(t)-r;
K=(at)^2+K;
end
U(9)=K/(88-9) %9阶模型残差方差 0.4297
K=0;T=X1