www.pudn.com > arima.rar > x_arima.m, change:2009-03-11,size:2514b


 
%function [Yo_train,Yo_test,Yt_train,Yt_test,m,p_test,d_test,q_test] = x_arima(filename,test_num) 
clear; 
data=w_lodedata('aa.txt'); 
test_num=200; 
n=size(data,1); 
test = []; 
ll = length(data); 
%生成差分矩阵 
dd=3; 
matrix = zeros(ll,dd+1); 
matrix(:,1) = data; 
 
for i = 1:dd 
    for j = i+1:ll 
    matrix(j,i+1) = matrix(j,i) - matrix(j-1,i); 
    end 
end 
 
% matrix(:,1)=data; 
% n=size(data,1); 
% for i=1:n-1 
%     diff1(i)=data(i+1)-data(i); 
% end  
% for i=1:n-2 
%     diff2(i)=diff1(i+1)-diff1(i); 
% end     
% for i=1:n-3 
%     diff3(i)=diff2(i+1)-diff2(i); 
% end 
%          
% matrix(1,2)=0; 
% matrix(2:n,2)=diff1; 
% matrix(1:2,3)=0; 
% matrix(3:n,3)=diff2;    
% matrix(1:3,4)=0; 
% matrix(4:n,4)=diff3; 
 
%d = [0 1 2 3]; 
%p = [0 1 2 3 4 5 6 7 8 9 10 11 12]; 
%q = [0 1 2 3 4 5 6 7 8 9 10 11 12];       
 
for d=0:dd 
    x=matrix(:,d+1); 
    v=x(d+1:end-test_num); 
    u = iddata(v); 
    for p = 0:12 
        for q = 0:12 
            m = armax(u,[p ])   
            AIC = aic(m)       
            test = [test;p q d AIC]; 
        end 
    end 
end  
 
%得出最优ARIMA模型的p,d,q参数 
for k = 1:size(test,1) 
    if test(k,4) == min(test(:,4))  
       p_test = test(k,1); 
       q_test = test(k,2); 
       d_test = test(k,3); 
    break; 
    end 
end 
 
x=matrix(:,d_test+1); 
v=x(d_test+1:end-test_num); 
u = iddata(v); 
%生成最优ARIMA模型 
m = armax(u,[p_test q_test])  
AIC = aic(m)   
disp(['  p_test',' ',p_test,]) 
disp(p_test) 
disp(['  d_test',' ',d_test,]) 
disp(d_test) 
disp(['  q_test',' ',q_test,]) 
disp(q_test) 
predict_train = predict(m,u);  
Yt_arima_train=predict_train.OutputData; 
     
 v=x(end-test_num+1:end); 
 u = iddata(v); 
 predict_test = predict(m,u);  
 Yt_arima_test=predict_test.OutputData; 
 y=0; 
     
 if d_test==0 
    Yt_train=Yt_arima_train; 
    Yt_test=Yt_arima_test; 
 else  
    for i=1:(size(Yt_arima_train,1)) 
        for j=1:d_test 
            y = matrix(i+d_test-1,j)+y; 
        end 
        Yt_train(i)=Yt_arima_train(i,1)+y; 
        y=0; 
    end 
    Yt_train=Yt_train'; 
    for i=1:(size(Yt_arima_test,1)) 
        for j=1:d_test 
            y = matrix(size(data,1)-test_num+i-1,j)+y; 
        end 
        Yt_test(i)=Yt_arima_test(i,1)+y; 
        y=0; 
    end 
    Yt_test=Yt_test'; 
 end  
    
  
 Yo_train=data(d_test+1:end-test_num); 
 Yo_test=data(end-test_num+1:end); 
 
 %显示结果 
  z_showresult(Yo_train,Yt_train, 'ARIMA train') 
  z_showresult(Yo_test,Yt_test, 'ARIMA test')