armasel.m
上传用户:joshua
上传日期:2008-09-12
资源大小:394k
文件大小:11k
源码类别:

matlab例程

开发平台:

Matlab

  1. function [ar,ma,ASAsellog,ASAcontrol]=armasel(sig,cand_ar_order,cand_ma_order,cand_arma_order,arma_order_diff,last)
  2. %ARMASEL ARMAsel model identification
  3. %   [AR,MA,SELLOG] = ARMASEL(SIG) estimates autoregressive, moving 
  4. %   average, and autoregressive moving average models from the data 
  5. %   vector SIG and selects the model with optimal predictive qualities. 
  6. %   The AR and MA parts of the selected model, each possibly of order 0, 
  7. %   are returned in the parameter vectors AR and MA. The structure SELLOG 
  8. %   provides additional information on the selection process.
  9. %   
  10. %   SELLOG contains the fields 'ar', 'ma' and 'arma', in which SELLOG 
  11. %   structures are nested, as returned by the functions SIG2AR, SIG2MA 
  12. %   and SIG2ARMA, invoked by ARMASEL. In the field 'armasel' a structure 
  13. %   is nested that reports information about the final stage of model 
  14. %   selection, where the preselected AR, MA and ARMA models are compared.
  15. %   
  16. %   ARMASEL(SIG,CAND_AR_ORDER,CAND_MA_ORDER,CAND_ARMA_ORDER,ARMA_ORDER_DIFF)
  17. %   narrows the selection to candidate models with orders provided by the 
  18. %   rows CAND_AR_ORDER, CAND_MA_ORDER, CAND_ARMA_ORDER and the scalar  
  19. %   ARMA_ORDER_DIFF, effecting the selection of AR, MA and ARMA models 
  20. %   independently. For any of these arguments it is allowed to pass an 
  21. %   empty array. Alternatively, additional arguments may be omitted from 
  22. %   the input list. In both cases, default values are automatically 
  23. %   determined and substituted for the missing arguments. The functions 
  24. %   SIG2AR, SIG2MA and SIG2ARMA provide additional information on 
  25. %   defining candidate orders. Note in this respect, that the candidate 
  26. %   AR orders of the ARMA model are called CAND_ARMA_ORDER in this help 
  27. %   text, while in SIG2ARMA they are called CAND_AR_ORDER.
  28. %   
  29. %   The selection of MA and ARMA models can be conditioned to the 
  30. %   selection of AR models from a specific set of candidate orders 
  31. %   CAND_AR_ORDER. See ASAGLOB_AR_COND for more information.
  32. %   
  33. %   Without user intervention, the mean of SIG is subtracted from the 
  34. %   data. To control the subtraction of the mean, see the help topics on 
  35. %   ASAGLOB_SUBTR_MEAN and ASAGLOB_MEAN_ADJ.
  36. %     
  37. %   ARMASEL is an ARMASA main function.
  38. %   
  39. %   See also: SIG2AR, SIG2MA, SIG2ARMA.
  40. %   References: P. M. T. Broersen, Facts and Fiction in Spectral
  41. %               Analysis, IEEE Transactions on Instrumentation and
  42. %               Measurement, Vol. 49, No. 4, August 2000, pp. 766-772.
  43. %Header
  44. %===================================================================================================
  45. %Declaration of variables
  46. %------------------------
  47. %Declare and assign values to local variables
  48. %according to the input argument pattern
  49. switch nargin
  50. case 1 
  51.    if isa(sig,'struct'), ASAcontrol=sig; sig=[]; 
  52.    else, ASAcontrol=[];
  53.    end
  54.    cand_ar_order=[]; cand_ma_order=[]; cand_arma_order=[]; arma_order_diff=[];
  55. case 2 
  56.    if isa(cand_ar_order,'struct'), ASAcontrol=cand_ar_order; cand_ar_order=[]; 
  57.    else, ASAcontrol=[]; 
  58.    end
  59.    cand_ma_order=[]; cand_arma_order=[]; arma_order_diff=[];
  60. case 3 
  61.    if isa(cand_ma_order,'struct'), ASAcontrol=cand_ma_order; cand_ma_order=[]; 
  62.    else, ASAcontrol=[]; 
  63.    end
  64.    cand_arma_order=[]; arma_order_diff=[];
  65. case 4 
  66.    if isa(cand_arma_order,'struct'), ASAcontrol=cand_arma_order; cand_arma_order=[]; 
  67.    else, ASAcontrol=[]; 
  68.    end
  69.    arma_order_diff=[];
  70. case 5 
  71.    if isa(arma_order_diff,'struct'), ASAcontrol=arma_order_diff; arma_order_diff=[]; 
  72.    else, ASAcontrol=[]; 
  73.    end
  74. case 6
  75.    if isa(last,'struct'), ASAcontrol=last;
  76.    else, error(ASAerr(39))
  77.    end
  78. otherwise
  79.    error(ASAerr(1,mfilename))
  80. end
  81. %Declare ASAglob variables 
  82. ASAglob = {'ASAglob_subtr_mean';'ASAglob_mean_adj';'ASAglob_rc';'ASAglob_ar';'ASAglob_final_f'; ...
  83.       'ASAglob_final_b';'ASAglob_ar_cond'};
  84. %Assign values to ASAglob variables by screening the
  85. %caller workspace
  86. for ASAcounter = 1:length(ASAglob)
  87.    ASAvar = ASAglob{ASAcounter};
  88.    eval(['global ' ASAvar]);
  89.    if evalin('caller',['exist(''' ASAvar ''',''var'')'])
  90.       eval([ASAvar '=evalin(''caller'',ASAvar);']);
  91.    else
  92.       eval([ASAvar '=[];']);
  93.    end
  94. end
  95. %ARMASA-function version information
  96. %-----------------------------------
  97. %This ARMASA-function is characterized by
  98. %its current version,
  99. ASAcontrol.is_version = [2000 12 30 20 0 0];
  100. %and its compatability with versions down to,
  101. ASAcontrol.comp_version = [2000 12 30 20 0 0];
  102. %This function calls other functions of the ARMASA
  103. %toolbox. The versions of these other functions
  104. %must be greater than or equal to:
  105. ASAcontrol.req_version.sig2ar = [2000 12 30 20 0 0];
  106. ASAcontrol.req_version.sig2ma = [2000 12 30 20 0 0];
  107. ASAcontrol.req_version.sig2arma = [2000 12 30 20 0 0];
  108. %Checks
  109. %------
  110. if ~any(strcmp(fieldnames(ASAcontrol),'error_chk')) | ASAcontrol.error_chk
  111.       %Perform standard error checks
  112.    %Input argument format checks
  113.    ASAcontrol.error_chk = 1;
  114.    if ~isnum(sig)
  115.       error(ASAerr(11,'sig'))
  116.    elseif ~isvector(sig)
  117.       error([ASAerr(14) ASAerr(15,'sig')])
  118.    elseif size(sig,2)>1
  119.       sig = sig(:);
  120.       warning(ASAwarn(25,{'row';'sig';'column'},ASAcontrol))
  121.    end
  122.    if ~isempty(cand_ar_order)
  123.       if ~isnum(cand_ar_order) | ~isintvector(cand_ar_order) |...
  124.             cand_ar_order(1)<0 | ~isascending(cand_ar_order)
  125.          error(ASAerr(12,{'candidate';'cand_ar_order'}))
  126.       elseif size(cand_ar_order,1)>1
  127.          cand_ar_order = cand_ar_order';
  128.          warning(ASAwarn(25,{'column';'cand_ar_order';'row'},ASAcontrol))
  129.       end
  130.    end
  131.    if ~isempty(cand_ma_order)
  132.       if ~isnum(cand_ma_order) | ~isintvector(cand_ma_order) |...
  133.             cand_ma_order(1)<0 | ~isascending(cand_ma_order)
  134.          error(ASAerr(12,{'candidate';'cand_ma_order'}))
  135.       elseif size(cand_ma_order,1)>1
  136.          cand_ma_order = cand_ma_order';
  137.          warning(ASAwarn(25,{'column';'cand_ma_order';'row'},ASAcontrol))
  138.       end
  139.    end
  140.    if ~isempty(cand_arma_order)
  141.       if ~isnum(cand_arma_order) | ~isintvector(cand_arma_order) |...
  142.             cand_arma_order(1)<0 | ~isascending(cand_arma_order)
  143.          error(ASAerr(12,{'candidate';'cand_arma_order'}))
  144.       elseif size(cand_arma_order,1)>1
  145.          cand_arma_order = cand_arma_order';
  146.          warning(ASAwarn(25,{'column';'cand_arma_order';'row'},ASAcontrol))
  147.       end
  148.    end
  149.    if ~isempty(arma_order_diff) & ...
  150.          (~isnum(arma_order_diff) | ...
  151.          ~isintscalar(arma_order_diff) |...
  152.          arma_order_diff<0)
  153.       error(ASAerr(17,'arma_order_diff'))
  154.    end
  155.    %Input argument value checks
  156.    if ~isreal(sig)
  157.       error(ASAerr(13))
  158.    end
  159.    if max(cand_ar_order) > length(sig)-1
  160.       error(ASAerr(37,'cand_ar_order'))
  161.    end
  162.    if max(cand_ma_order) > length(sig)-1
  163.       error(ASAerr(37,'cand_ma_order'))
  164.    end
  165.    if ~isempty(cand_arma_order) & ...
  166.          ~isempty(arma_order_diff)
  167.       if cand_arma_order(1)~=0 & ...
  168.             (arma_order_diff < 1 | ...
  169.             arma_order_diff > cand_arma_order(1))
  170.          error(ASAerr(18,{'arma_order_diff';'1';...
  171.                num2str(cand_arma_order(1))}))
  172.       elseif length(cand_arma_order)>1 & ...
  173.             (arma_order_diff < 1 | ...
  174.             arma_order_diff > cand_arma_order(2))
  175.          error(ASAerr(18,{'arma_order_diff';'1';...
  176.                num2str(cand_arma_order(2))}))
  177.       end
  178.    end
  179. end
  180. if ~any(strcmp(fieldnames(ASAcontrol),'version_chk')) | ASAcontrol.version_chk
  181.       %Perform version check
  182.    ASAcontrol.version_chk = 1;
  183.       
  184.    %Make sure the requested version of this function
  185.    %complies with its actual version
  186.    ASAversionchk(ASAcontrol);
  187.    
  188.    %Make sure the requested versions of the called
  189.    %functions comply with their actual versions
  190.    sig2ar(ASAcontrol);
  191.    sig2ma(ASAcontrol);
  192.    sig2arma(ASAcontrol);
  193. end
  194. if ~any(strcmp(fieldnames(ASAcontrol),'run')) | ASAcontrol.run
  195.       %Run the computational kernel
  196.    ASAcontrol.run = 1;
  197.    ASAcontrol.version_chk = 0;
  198.    ASAcontrol.error_chk = 0;
  199.    ASAdate = now;
  200. %Main   
  201. %========================================================================================
  202.    
  203. %Initialization of variables
  204. %---------------------------
  205. if isempty(ASAglob_subtr_mean) | ASAglob_subtr_mean
  206.    sig = sig-mean(sig);
  207.    ASAglob_subtr_mean = 0;
  208.    if isempty(ASAglob_mean_adj)
  209.       ASAglob_mean_adj = 1;
  210.    end
  211. elseif isempty(ASAglob_mean_adj)
  212.    ASAglob_mean_adj = 0;
  213. end
  214. if isempty(cand_ar_order) & isempty(cand_ma_order) & isempty(cand_arma_order)
  215.    default_order = 1;
  216. else
  217.    default_order = 0;
  218. end
  219. if ASAglob_ar_cond
  220.    ASAglob_ar_cond = 1;
  221. else
  222.    if default_order
  223.       ASAglob_ar_cond = 1;
  224.    else
  225.       ASAglob_ar_cond = 0;
  226.    end
  227. end
  228.    
  229. %AR-, MA- and ARMA-model identification
  230. %--------------------------------------
  231. [ar_ar,ar_sellog] = sig2ar(sig,cand_ar_order,ASAcontrol);
  232. [ma_ma,ma_sellog] = sig2ma(sig,cand_ma_order,ASAcontrol);
  233. [arma_ar,arma_ma,arma_sellog] = sig2arma(sig,cand_arma_order,arma_order_diff,ASAcontrol);
  234. %Selection of the ARMAsel model
  235. %------------------------------
  236. %Asess the selected model orders
  237. order = [length(ar_ar)-1;length(ma_ma)-1;length(arma_ar)-1];
  238. %Asess the corresponding prediction error estimates
  239. if default_order
  240.    sel_location = order+1;
  241. else
  242.    sel_location = ...
  243.       [find(order(1) == ar_sellog.cand_order);...
  244.        find(order(2) == ma_sellog.cand_order);...    
  245.        find(order(3) == arma_sellog.cand_ar_order)];
  246. end
  247. pe_est = [ar_sellog.pe_est(sel_location(1));...
  248.       ma_sellog.pe_est(sel_location(2));...
  249.       arma_sellog.pe_est(sel_location(3))];
  250. %Select the model with the smallest prediction error estimate
  251. [sel_pe_est,model] = min(pe_est);
  252. %Arranging output arguments
  253. %--------------------------
  254. %Retrieve the parameters of the selected model
  255. ar = 1;
  256. ma = 1;
  257. switch model
  258. case 1 %The AR model has been selected
  259.    ar = ar_ar;
  260. case 2 %The MA model has been selected
  261.    ma = ma_ma;
  262. case 3 %The ARMA model has been selected
  263.    ar = arma_ar;
  264.    ma = arma_ma;
  265. end
  266. %Gernerate a structure variable ASAsellog to report the selection process
  267. if nargout>2
  268.    ASAsellog.funct_name = mfilename;
  269.    ASAsellog.funct_version = ASAcontrol.is_version;
  270.    ASAsellog.date_time = [datestr(ASAdate,8) 32 datestr(ASAdate,0)];
  271.    ASAsellog.comp_time = ar_sellog.comp_time+ma_sellog.comp_time+arma_sellog.comp_time;
  272.    ASAsellog.armasel.ar = ar;
  273.    ASAsellog.armasel.ma = ma;
  274.    ASAsellog.armasel.ar_pe_est = pe_est(1);
  275.    ASAsellog.armasel.ma_pe_est = pe_est(2);
  276.    ASAsellog.armasel.arma_pe_est = pe_est(3);
  277.    ASAsellog.armasel.ar_cond = ASAglob_ar_cond;
  278.    ASAsellog.armasel.mean_adj = ASAglob_mean_adj;
  279.    ASAsellog.ar = ar_sellog;
  280.    ASAsellog.ma = ma_sellog;
  281.    ASAsellog.arma = arma_sellog;
  282. end
  283. %Footer
  284. %=====================================================
  285. else %Skip the computational kernel
  286.    %Return ASAcontrol as the first output argument
  287.    if nargout>1
  288.       warning(ASAwarn(9,mfilename,ASAcontrol))
  289.    end
  290.    ar = ASAcontrol;
  291.    ASAcontrol = [];
  292. end
  293. %Program history
  294. %======================================================================
  295. %
  296. % Version                Programmer(s)          E-mail address
  297. % -------                -------------          --------------
  298. % former versions        P.M.T. Broersen        broersen@tn.tudelft.nl
  299. %                        S. de Waele            waele@tn.tudelft.nl
  300. % [2000 12 30 20 0 0]    W. Wunderink           wwunderink01@freeler.nl