ARMA模型带拟合的程序暂存

function[ddb,bbb,aaa, e,pos,yucezhi]=armaminerrordainihe2(cD)
%没有作平稳性检验 相关函数https://blog.csdn.net/chamyto98/article/details/5181623
%以下为系统自带函数
%  edit arima
%  edit garch
aaa=ones(50,500)*10000;
bbb=ones(50,500)*10000;
gaotie=cD;
N=size(gaotie,1);
for k=1:N/6
    %k=1 p=3 n=2 m=1 第p+1个有残差 
p=4*k-1;n=2*k;m=2*k-1;
[a0,b0] = arburg(gaotie,p);%%%https://www.ilovematlab.cn/thread-296619-1-1.html  a0为系数
c=N-p;
dd=ones(1,c);
for i=1:c
ddd=gaotie(i:i+p-1);
%ddd=ddd';
dd(i)=a0(2:p+1)*ddd;
end
%残差:
dd=dd';
cancha=gaotie(p+1:N)-dd;cancha=cancha';
         %从第p+1个才有残差%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%11.11 pp=N-(p)-m;
         %ARMA(n,m) AR的自回归基数一直是有的 MA拟合第一个则需要第m个残差即总数的p+m
y=gaotie(p+m+1:N);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%构造x矩阵
gaotie=gaotie';
gaotie1=toeplitz(gaotie);%预先分配一个N*N的内存
for kk=2:N
    gaotie1(kk,:)=gaotie;
    for kkk=1:kk-1       
gaotie1(kk,:)=circshift(gaotie1(kk,:),-1);
    end
end
gaotie1=gaotie1(p+m-n+1:N-n,1:n);
%%%%前p个残差设置为零就和书上一样了
cancha1=toeplitz(cancha);
for kk=2:length(cancha)
    cancha1(kk,:)=cancha;
    for kkk=1:kk-1       
cancha1(kk,:)=circshift(cancha1(kk,:),-1);
    end
end
%cancha1=cancha1(p+m-1:N-2,1:m);
cancha1=cancha1(1:size(gaotie1,1),1:m);
x=[gaotie1 cancha1];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%x=toeplitz(xx); %MATLAB提供了一个称为toeplitz的函数,可根据第一行和第一列生成toeplitz矩阵。用此函数开发另一个MATLAB函数来执行线性卷积
%例子 a=[1 2 3 4] ;  b=toeplitz(a)
   %inv(G)返回G的反函数
   %W=inv(G)*[R(1:5)]' 
   
   
   
geta=inv(x'*x)*x'*y;


%输出拟合误差
error(k)=sum(abs(x*geta-y))/size(y,1);
 [Q,pValue,stat,cValue] = lbqtest(x*geta-y);
 if pValue<0.05
     error(k)=10000;
 end

 %https://www.ilovematlab.cn/thread-213882-1-1.html
 %https://bbs.pinggu.org/thread-3560191-1-1.html
%对误差的序列进行保存
bbbb=x*geta;
ccc=x*geta-y;%列向量
    for j=1:size(ccc)
     aaa(k,j)=ccc(j);
     bbb(k,j)=bbbb(j);
    end 
 yuce(k)=[gaotie(N-n+1:N) cancha(length(cancha)-m+1:length(cancha))]*geta;   
gaotie=cD;
end
[e,pos]=min(error)
yucezhi=yuce(pos);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:500
    if bbb(pos,i)<10000
    db(i)=bbb(pos,i);
    end
end
%figure(20);plot(db);
ddb=[cD(1:6*pos-2)' db];
% if p==2
% ddb=[b4(1:10)' db];
% end
% if p==3
% ddb=[b4(1:6*pos-2)' db];
% end
% if p==4
% ddb=[b4(1:22)' db4];
% end


end

你可能感兴趣的:(数据分析)