How to use MATLAB to do gray prediction on a set of data? (example 1)

gray system prediction

       In the solution of the problem, the GM (1,1) gray system theory is used to predict, and according to the comprehensive data of many known elements in the system, the time series of the data is fitted according to the differential equation to approximate the time series mentioned above The described dynamic process can then be extrapolated to achieve the purpose of prediction. Generally speaking, the gray system theory has better predictability, effectively overcomes the good characteristics of "less data" modeling to seek realistic laws, and overcomes insufficient data or systematic Paradox of short cycle. The model fitted by the gray system theory is a first-order differential equation of time series, so the GM (1,1) model is abbreviated.

Note: When using gray forecasting to forecast a small amount of data, try not to forecast too long in the future, and the error will be large.

1. Model establishment

GM (1,1 ) model:

 

 

 

 The posterior difference test can be used to judge the accuracy of the model by referring to Table 1 :

Table 5-2-1 Posterior difference test discrimination reference table

C

model accuracy

<0.35

excellent

<0.5

pass

<0.65

good

>0.65

unqualified

2. Examples (such as global temperature prediction)

gray forecasting model

 GM(1,1) modeling

 

 As shown in the figure, the red dots are the real data we input, and the blue dots are the data we predicted. By calculating the percentage absolute error is 0.082598%, the error is very small, so the result is convincing.

     And based on the data before 2023, the temperature in 2023 and beyond can be predicted:

2023——

The predicted value is:

Table 5-2-2 Temperature forecast from 2023 to 2050

years

forecast temperature

years

forecast temperature

years

forecast temperature

2023

15.7692

2035

16.1319

2047

16.5028

2024

15.7991

2036

16.1625

2048

16.5341

2025

15.8291

2037

16.1931

2049

16.5655

2026

15.8591

2038

16.2238

2050

16.5969

2027

15.8892

2039

16.2546

2028

15.9193

2040

16.2854

2029

15.9495

2041

16.3163

2030

15.9798

2042

16.3472

2031

16.0101

2043

16.3782

2032

16.0404

2044

16.4093

2033

16.0709

2045

16.4404

2034

16.1013

2046

16.4716

 3. The code is as follows (y is the predicted value of the input)

% Matlab的灰色预测程序:
%y=input('请输入数据');
clc;clear
y=[15.47141667 15.36183333 15.38175 15.40033333 15.47175 15.6155 15.75225 15.65633333 15.58066667 15.71575 15.74108333 15.58908333 15.6526]
n=length(y);
yy=ones(n,1);
yy(1)=y(1);
for i=2:n
yy(i)=yy(i-1)+y(i) 
end
B=ones(n-1,2);
for i=1:(n-1)
B(i,1)=-(yy(i)+yy(i+1))/2;
B(i,2)=1;
end
BT=B';
for j=1:(n-1)
    YN(j)=y(j+1);
end
YN=YN';
A=inv(BT*B)*BT*YN;
a=A(1);
u=A(2);
t=u/a;
t_test=input('输入需要预测的个数');
i=1:t_test+n;
yys(i+1)=(y(1)-t).*exp(-a.*i)+t;
yys(1)=y(1);
for j=n+t_test:-1:2
    ys(j)=yys(j)-yys(j-1);
end
x=1:n;
xs=2:n+t_test;
yn=ys(2:n+t_test);
plot(x,y,'^r',xs,yn,'*-b');
det=0;
for i=2:n
det=det+abs(yn(i)-y(i));
end
det=det/(n-1);
disp(['百分绝对误差为:',num2str(det),'%']);
disp(['预测值为:',num2str(ys(n+1:n+t_test))]);
%

Of course, there is a simpler code that can still make predictions:

clc,clear
x0=[15.47141667 15.36183333 15.38175 15.40033333 15.47175 15.6155 15.75225 15.65633333 15.58066667 15.71575 15.74108333 15.58908333 15.6526];
n=length(x0);
lamda=x0(1:n-1)./x0(2:n)
range=minmax(lamda)
x1=cumsum(x0)
for i=2:n
    z(i)=0.5*(x1(i))+x1(i-1);
end
B=[-z(2:n)',ones(n-1,1)];
Y=x0(2:n)';
u=B\Y
x=dsolve('Dx+a*x=b','x(0)=x0');
x=subs(x,{'a','b','x0'},{u(1),u(2),x1(1)});
yuce1=subs(x,'t',[0:n-1]);
digits(6),y=vpa(x)
yuce=[x0(1),diff(yuce1)]
epsilon=x0-yuce    %计算残差
delta=abs(epsilon./x0)  %相对误差
rho=1-(1-0.5*u(1))/(1+0.5*u(1))*lamda   %计算级比偏差值

Guess you like

Origin blog.csdn.net/m0_73982095/article/details/131424390