灰色预测
灰色预测适用于小样本的预测,常用来解决一些不确定性的问题。
- 对原始数据进行累加;
- 构造累加矩阵B与常数向量;
- 求解灰参数;
- 将参数代入预测模型进行数据预测。
1995 | 1996 | 1997 | 1998 | 1999 | 2000 | 2001 | 2002 | 2003 | 2004 |
174 | 179 | 183 | 189 | 207 | 234 | 220.5 | 256 | 270 | 285 |
clc;
close all;
clear all;
%A=xlsread('','B3:B35');
%A=[71.1,72.4,72.4,72.1,71.4,72.0,71.6];
%A=[6.2 5.8 6.1 6.0 6.4 8.5 11.1 8.5 8.2 8.0 7.8 7.5 7.2 7.0 8.2 11.7 13.4 12.6 15.6 14.2 16.3];
A=[89677,99215,109655,120333,135823,159878,182321,209407,246619,300670];
B=[];
D=[];
len=length(A);
m=input('请输入需要预测的数量:');
year=input('请输入开始年份:');
s=0;
for i=1:len
s=s+A(i);
B=[B,s];
end
Y=A(2:len)';
for i=2:len
D=[D;-1/2*(B(i)+B(i-1)),1];
end
C=inv(D'*D)*D'*Y
for i=2:len+m
y(i)=(A(1)-C(2)/C(1))*exp(-C(1)*(i-1))+C(2)/C(1);
end
y(1)=A(1);
yc(1)=A(1);
for i=2:len+m
yc(i)=y(i)-y(i-1);
end
k=year:year+len+m-1
x=year:year+len-1;
figure(1)
plot(k,yc,'r');hold on;
plot(x,A,'O');hold on;
xlabel('年份')
ylabel('利润')
yc=double(yc)
grid on;
figure (2)
cur=[];
for i=1:len
cur(i)=(A(i)-yc(i))/(A(i));
end
plot(x,cur,'-r');
title('相对残差')
line([year,year+len],[0.1 0.1],'Marker','.','LineStyle','--')
line([year,year+len],[-0.1 -0.1],'Marker','.','LineStyle','--')
xlabel('年份')
ylabel('残差')
grid on
figure(3)
p(1)=0;
for i=2:len
p(i)=1-((1-0.5*C(1))/(1+0.5*C(1)))*(A(i-1)/A(i));
end
plot(x,p,'-b');
axis([year year+len -0.5 0.5])
title('级比偏差值')
line([year year+len],[0.1 0.1],'Marker','.','LineStyle','--')
line([year year+len],[-0.1 -0.1],'Marker','.','LineStyle','--')
xlabel('年份')
ylabel('级比偏差')
grid on;
close all;
clear all;
%A=xlsread('','B3:B35');
%A=[71.1,72.4,72.4,72.1,71.4,72.0,71.6];
%A=[6.2 5.8 6.1 6.0 6.4 8.5 11.1 8.5 8.2 8.0 7.8 7.5 7.2 7.0 8.2 11.7 13.4 12.6 15.6 14.2 16.3];
A=[89677,99215,109655,120333,135823,159878,182321,209407,246619,300670];
B=[];
D=[];
len=length(A);
m=input('请输入需要预测的数量:');
year=input('请输入开始年份:');
s=0;
for i=1:len
end
Y=A(2:len)';
for i=2:len
end
C=inv(D'*D)*D'*Y
for i=2:len+m
end
y(1)=A(1);
yc(1)=A(1);
for i=2:len+m
end
k=year:year+len+m-1
x=year:year+len-1;
figure(1)
plot(k,yc,'r');hold on;
plot(x,A,'O');hold on;
xlabel('年份')
ylabel('利润')
yc=double(yc)
grid on;
figure (2)
cur=[];
for i=1:len
cur(i)=(A(i)-yc(i))/(A(i));
end
plot(x,cur,'-r');
title('相对残差')
line([year,year+len],[0.1 0.1],'Marker','.','LineStyle','--')
line([year,year+len],[-0.1 -0.1],'Marker','.','LineStyle','--')
xlabel('年份')
ylabel('残差')
grid on
figure(3)
p(1)=0;
for i=2:len
p(i)=1-((1-0.5*C(1))/(1+0.5*C(1)))*(A(i-1)/A(i));
end
plot(x,p,'-b');
axis([year year+len -0.5 0.5])
title('级比偏差值')
line([year year+len],[0.1 0.1],'Marker','.','LineStyle','--')
line([year year+len],[-0.1 -0.1],'Marker','.','LineStyle','--')
xlabel('年份')
ylabel('级比偏差')
grid on;
预测结果: