/tags/R2002-05-09/octave-forge/extra/tsa/arfit2.m

# · MATLAB · 100 lines · 54 code · 18 blank · 28 comment · 13 complexity · f7e2471bac09735911ae7d52511c7d22 MD5 · raw file

  1. function [w, MAR, C, sbc, fpe, th]=arfit(Y, pmin, pmax, selector, no_const)
  2. % ARFIT2 estimates multivariate autoregressive parameters
  3. % using MVAR with the Nuttall-Strand method [1,2].
  4. % ARFIT2 is included for combatibility reasons to ARFIT [3]
  5. %
  6. % [w, A, C, sbc, fpe] = arfit2(v, pmin, pmax, selector, no_const)
  7. %
  8. % see also: ARFIT, MVAR
  9. %
  10. % REFERENCES:
  11. % [1] M.S. Kay "Modern Spectral Estimation" Prentice Hall, 1988.
  12. % [2] S.L. Marple "Digital Spectral Analysis with Applications" Prentice Hall, 1987.
  13. % [3] T. Schneider and A. Neumaier, A. 2001.
  14. % Algorithm 808: ARFIT-a Matlab package for the estimation of parameters and eigenmodes
  15. % of multivariate autoregressive models. ACM-Transactions on Mathematical Software. 27, (Mar.), 58-65.
  16. % Copyright (C) 1996-2002 by Alois Schloegl <a.schloegl@ieee.org>
  17. %%%%% checking of the input arguments was done the same way as ARFIT
  18. if (pmin ~= round(pmin) | pmax ~= round(pmax))
  19. error('Order must be integer.');
  20. end
  21. if (pmax < pmin)
  22. error('PMAX must be greater than or equal to PMIN.')
  23. end
  24. % set defaults and check for optional arguments
  25. if (nargin == 3) % no optional arguments => set default values
  26. mcor = 1; % fit intercept vector
  27. selector = 'sbc'; % use SBC as order selection criterion
  28. elseif (nargin == 4) % one optional argument
  29. if strcmp(selector, 'zero')
  30. mcor = 0; % no intercept vector to be fitted
  31. selector = 'sbc'; % default order selection
  32. else
  33. mcor = 1; % fit intercept vector
  34. end
  35. elseif (nargin == 5) % two optional arguments
  36. if strcmp(no_const, 'zero')
  37. mcor = 0; % no intercept vector to be fitted
  38. else
  39. error(['Bad argument. Usage: ', '[w,A,C,SBC,FPE,th]=AR(v,pmin,pmax,SELECTOR,''zero'')'])
  40. end
  41. end
  42. %%%%% Implementation of the MVAR estimation
  43. [N,M]=size(Y);
  44. if mcor,
  45. [m,N] = sumskipnan(Y,2); % calculate mean
  46. m = m./N;
  47. Y = Y - repmat(S./N,size(Y)./size(m)); % remove mean
  48. end;
  49. [MAR,RCF,PE] = mvar(Y, pmax, 2); % estimate MVAR(pmax) model
  50. %if 1;nargout>3;
  51. ne = N-mcor-(pmin:pmax);
  52. for p=pmin:pmax,
  53. % Get downdated logarithm of determinant
  54. logdp(p-pmin+1) = log(det(PE(:,p+(1:M))*(N-p-mcor)));
  55. end;
  56. % Schwarz's Bayesian Criterion
  57. sbc = logdp/M - log(ne) .* (1-(M*(pmin:pmax)+mcor)./ne);
  58. % logarithm of Akaike's Final Prediction Error
  59. fpe = logdp/M - log(ne.*(ne-M*(pmin:pmax)-mcor)./(ne+M*(pmin:pmax)+mcor));
  60. % Modified Schwarz criterion (MSC):
  61. % msc(i) = logdp(i)/m - (log(ne) - 2.5) * (1 - 2.5*np(i)/(ne-np(i)));
  62. % get index iopt of order that minimizes the order selection
  63. % criterion specified by the variable selector
  64. [val, iopt] = min(eval(selector));
  65. % select order of model
  66. popt = pmin + iopt-1; % estimated optimum order
  67. if popt<pmax,
  68. pmax=popt;
  69. [MAR, RCF, PE] = mvar(Y, pmax, 2);
  70. end;
  71. %end
  72. C = PE(:,size(PE,2)+(1-M:0));
  73. if mcor,
  74. I = eye(M);
  75. for k = 1:pmax,
  76. I = I - MAR(:,k*M+(1-M:0));
  77. end;
  78. w = I*m;
  79. else
  80. w = zeros(M,1);
  81. end;
  82. if nargout>5, th=[]; fprintf(2,'Warning ARFIT2: output TH not defined\n'); end;