home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 9.ddi / IDENT.DI$ / MVARX.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  4.0 KB  |  121 lines

  1. function eta=mvarx(z,nn,maxsize,Tsamp);
  2. %MVARX    Estimates (multivariate) ARX-models
  3. %    Auxiliary routine to ARX
  4. %    TH = mvarx(Z,[NA NB NK])
  5. %
  6. %    Z: The output-input data Z = [Y U] with the outputs Y
  7. %    and the inputs U arranged in column vectors
  8. %    [NA NB NK]: defines the model structure as follows:
  9. %    NA: an ny|ny matrix whose i-j entry is the order of the
  10. %        polynomial (in the delay operator) that relates the
  11. %        j:th output to the i:th output
  12. %    NB: an ny|nu matrix whose i-j entry is the order of the 
  13. %        polynomial that related the j:th input to the i:th output
  14. %    NK: an ny|nu matrix whose i-j entry is the delay from
  15. %        the j:th input to the i:th output
  16. %    (ny: the number of outputs, nu: the number of inputs)
  17. %    
  18. %    Each row of [NA NB NK] is thus consistent with the way
  19. %    single output ARX structures is defined (See Help ARX)
  20. %
  21. %    TH: the resulting model, given in the THETA-format (see HELP THETA)
  22. %
  23. %    With TH=mvarx(Z,[NA NB NK],MAXSIZE,T) some additional
  24. %    options can be achieved. See HELP AUXVAR for details
  25.  
  26. %    
  27. %    L. Ljung 10-2-90
  28. %    Copyright (c) 1987-90 by the MathWorks, Inc.
  29. %    All Rights Reserved.
  30.  
  31. [nnr,nnc]=size(nn);
  32. ny=nnr;nu=(nnc-ny)/2;
  33. [Ncap,nz]=size(z); 
  34. if nz>Ncap,error(' Data should be organized in column vectors!'),return,end
  35. if nz~=(nu+ny),disp(' Incorrect number of orders specified!'),disp('For an AR-model nn=na'),disp('For an ARX-model, nn=[na nb nk]'),
  36. error('see above'),return,end
  37. na=nn(:,1:ny);
  38. if nu>0,nb=nn(:,ny+1:ny+nu);else nb=zeros(ny,1);end
  39. if nu>0,nk=nn(:,ny+nu+1:nnc);else nk=zeros(ny,1);end
  40.  
  41.  
  42. nma=max(max(na)');nbkm=max(max(nb+nk)')-1;nkm=min(min(nk)');
  43. nd=sum(sum(na)')+sum(sum(nb)');
  44. n=nma*ny+(nbkm-nkm+1)*nu;
  45.  
  46. % *** Set up default values **
  47. maxsdef=idmsize(Ncap,nd);
  48. if nargin<4, Tsamp=1;end
  49. if nargin<3, maxsize=maxsdef;end
  50. if isempty(Tsamp),Tsamp=1;end,if Tsamp<0,Tsamp=1;end  % Changed T to Tsamp, JNL 9/19/91
  51. if isempty(maxsize),maxsize=maxsdef;end,if maxsize<0, maxsize=maxsdef; end
  52.  
  53.  
  54. % *** construct regression matrix ***
  55.  
  56.  nmax=max([nma nbkm]');
  57. M=floor(maxsize/n);
  58. R=zeros(n);F=zeros(n,ny);
  59. for k=nmax:M:Ncap
  60.       if min(Ncap,k+M)<k+1,ntz=0;else ntz=1;end
  61.     
  62.       if ntz,jj=(k+1:min(Ncap,k+M));phi=zeros(length(jj),n);end
  63.       
  64.       for kl=1:nma, 
  65.       if ntz,
  66.     phi(:,(kl-1)*ny+1:kl*ny)=z(jj-kl,1:ny);end
  67.       end
  68.       ss=nma*ny;
  69.   
  70.       for kl=nkm:nbkm,
  71.       if ntz,phi(:,ss+(kl-nkm)*nu+1:ss+(kl-nkm+1)*nu)=z(jj-kl,ny+1:nz);end
  72.       end    
  73.       if ntz,R=R+phi'*phi; F=F+phi'*z(jj,1:ny);end
  74.       
  75. end
  76. %
  77. % *** compute loss functions ***
  78. %
  79. par=[];parb=[];p11=[];p12=[];p22=[];lamm=[];
  80. for outp=1:ny
  81.     rowind=[];
  82.     for kk=1:ny
  83.     rowind=[rowind kk:ny:ny*na(outp,kk)];
  84.     end
  85.     for kk=1:nu
  86.     baslev=nma*ny+(nk(outp,kk)-nkm)*nu;
  87.     rowind=[rowind baslev+kk:nu:baslev+nu*nb(outp,kk)];
  88.     end
  89.    rowind=sort(rowind);
  90.  
  91.    if Ncap>M,
  92.     th=R(rowind,rowind)\F(rowind,outp); 
  93.    else th=phi(:,rowind)\z(jj,outp);
  94.    end
  95.    pth=inv(R(rowind,rowind));
  96.    LAM=(z(nmax+1:Ncap,outp)'*z(nmax+1:Ncap,outp)-F(rowind,outp)'*pth*...
  97.    F(rowind,outp))/(Ncap-nmax);
  98.    pth=LAM*pth;
  99.    lamm=[[lamm,zeros(length(lamm),1)];[zeros(1,length(lamm)),LAM]];
  100. k00=sum((nk(outp,:)==0).*(nb(outp,:)>0));
  101. naux=sum(na(outp,:));
  102. ind1=1:naux;ind2=naux+k00+1:length(th);ind3=naux+1:naux+k00;
  103. par=[par th(ind1)' th(ind2)'];parb=[parb th(ind3)'];
  104. [sp11,ssp11]=size(p11);lni=length(ind1)+length(ind2);
  105. p11=[[p11,zeros(sp11,lni)];[zeros(lni,sp11),[[pth(ind1,ind1),pth(ind1,ind2)];...
  106. [pth(ind2,ind1),pth(ind2,ind2)]]]];
  107. [sp12,ssp12]=size(p12);
  108. [sp22,ssp22]=size(p22);
  109. p12=[[p12,zeros(sp11,length(ind3))];[zeros(lni,ssp12),[pth(ind1,ind3);pth(ind2,ind3)]]];
  110. p22=[[p22,zeros(sp22,length(ind3))];[zeros(length(ind3),ssp22),pth(ind3,ind3)]];
  111.  
  112. end
  113.  
  114. eta=mketaarx(nn,[par parb],lamm,Tsamp);
  115. eta(2,7)=31;e=pe(z,eta); lamm=e'*e/(Ncap-nmax);
  116. pvar=[[p11,p12];[p12',p22]];
  117. d=length([par parb]);
  118. eta(4:3+d,1:d)=pvar;rarg=eta(1,6);
  119. eta(4+d+rarg:3+ny+d+rarg,1:ny)=lamm;eta(1,1)=det(lamm);
  120. eta(2,1)=eta(1,1)*(1+nd/Ncap)/(1-nd/Ncap);
  121.