It's okay to play a bit matlab
Discover that the code on the Web is a set of data predictions
So I'm going to write a prediction of the batch data.
Learn Matlab by the way
----------------------------------I am the Happy dividing line------------------------------------
The main ideas of grey prediction are:
1. Given a set of data
2, the accumulation, that is
X (1) 1=x (0) 1
X (1) 2=x (0) 1+x (0) 2
X (1) 3=x (0) 1+x (0) 2+x (0) 3
...
3. The ultimate goal is to construct the predictive equation:
which
In order to obtain the formula, it is necessary to:
All right, that's it. Gray prediction is finished, the following is a variety of loops written
----------------------------------I am the Happy dividing line------------------------------------
To predict multiple sets of data in batches
So joined an open Excel:
% reading of Excel data = xlsread (filename, sheet, range) data=xlsread (' D:\MATLAB\GM.xlsx ', 1, ' a1:e2 ');
Then write a large for loop:
M=ndims (data);% dimension for i = 1:m...end
Inside the ellipsis is a set of data prediction programs.
The idea of grey prediction is simple
I'm not going to explain it in paragraphs.
The entire code is as follows
function GM (t) CLC; % clear screen to allow the calculated results to be displayed independently of format long g; % set calculation accuracy% read Excel data = xlsread (filename, sheet, range) data=xlsread (' D:\MATLAB\GM.xlsx ', 1, ' a1:e2 ');%fprintf (data (1,:));%x=data (1,:)%n=length (data)% added; The semicolon indicates that the result is not displayed m=ndims (data); The% dimension t=input (' How many periods do you want to predict? '); for i = 1:m z=0; X=data (i,:); % all of the contents below are in this loop. Note the length of each line below the end of the n=length (data); for j = 1:n Z = z + x (j); Be (j,:) =z; End%matlab variable definition is messy, can be arbitrarily defined, is a local variable% this article here in addition to I other than the local variable it calculates the first column of the data Matrix B and the second column of data for j = 1:n-1% the first column C (j,:) =-0.5* (Be (J,:) +be (j+1,:)); % second column E (j,:) = 1; % substituting matrix B in B (j,1) =c (J,:); B (j,2) =e (J,:); End% parallel shift to original series for J=2:n y (j-1,:) =x (1,J); End% calculates the parameter α, μ matrix ALPHA=INV (B. ' *b) *b. ' *y; The cumulative number of calculated data estimates for k = 0:n-1+t ago (k+1,:) = (IS (1)-alpha (2,:)/alpha (1,:)) *exp (-alpha (1,:) * (k)) +alpha (2,:)/alpha (1,: ); The end% estimates the cumulative sequence of restores and calculates the next predicted value for j=1:n-1+t var (j+1,:) =ago (j+1,:)-ago (J,:); End% gives the predicted result (I,:) =var (n+t,1); % calculated residuals for j=2:n error (J,:) =var (J,:)-X (1,J); End% calls the standard deviation function of the statistics Toolbox to calculate the ratio of the posterior difference C s1=std (x) s2=std (Error) C=s2/s1enddisp (' predicted as: ') disp (Result (:, 1))
Spent an afternoon to review a bit of MATLAB
Find writing M files is not difficult
But Matlab's language logic is a little messy.
If it wasn't for my logic, it was a mess.
---------------------------------------Orz-----------------------------------
MATLAB Batch Grey Forecast