]> Creatis software - CreaPhase.git/blob - octave_packages/tsa-4.2.4/invest1.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / tsa-4.2.4 / invest1.m
1 function [AutoCov,AutoCorr,ARPMX,E,C,s]=invest1(Y,Pmax,D);
2 % First Investigation of a signal (time series) - interactive
3 % [AutoCov,AutoCorr,ARPMX,E,CRITERIA,MOPS]=invest1(Y,Pmax,show);
4 %
5 % Y     time series
6 % Pmax  maximal order (optional)
7 % show  optional; if given the parameters are shown
8 %
9 % AutoCov       Autocorrelation 
10 % AutoCorr      normalized Autocorrelation
11 % PartACF       Partial Autocorrelation
12 % E     Error function E(p)
13 % CRITERIA curves of the various (see below) criteria, 
14 % MOPS=[optFPE optAIC optBIC optSBC optMDL optCAT optPHI];
15 %      optimal model order according to various criteria
16 %
17 % FPE   Final Prediction Error (Kay, 1987)
18 % AIC   Akaike Information Criterion (Marple, 1987)
19 % BIC   Bayesian Akaike Information Criterion (Wei, 1994)
20 % SBC   Schwartz's Bayesian Criterion (Wei, 1994)
21 % MDL   Minimal Description length Criterion (Marple, 1987)
22 % CAT   Parzen's CAT Criterion (Wei, 1994)
23 % PHI   Phi criterion (Pukkila et al. 1988)
24 % minE          order where E is minimal
25 %
26 % REFERENCES:
27 %  P.J. Brockwell and R.A. Davis "Time Series: Theory and Methods", 2nd ed. Springer, 1991.
28 %  S.   Haykin "Adaptive Filter Theory" 3ed. Prentice Hall, 1996.
29 %  M.B. Priestley "Spectral Analysis and Time Series" Academic Press, 1981. 
30 %  C.E. Shannon and W. Weaver "The mathematical theory of communication" University of Illinois Press, Urbana 1949 (reprint 1963).
31 %  W.S. Wei "Time Series Analysis" Addison Wesley, 1990.
32
33 % optFPE        order where FPE is minimal
34 % optAIC        order where AIC is minimal
35 % optBIC        order where BIC is minimal
36 % optSBC        order where SBC is minimal
37 % optMDL        order where MDL is minimal
38 % optCAT        order where CAT is minimal
39 % optPHI        order where PHI is minimal
40 % optRC2        max reflection coefficient larger than std-error
41
42 %       $Id: invest1.m 7687 2010-09-08 18:39:23Z schloegl $
43 %       Copyright (C) 1998-2002,2008,2010 by Alois Schloegl <a.schloegl@ieee.org>       
44 %       This is part of the TSA-toolbox. See also 
45 %       http://biosig-consulting.com/matlab/tsa/
46 %
47 %    This program is free software: you can redistribute it and/or modify
48 %    it under the terms of the GNU General Public License as published by
49 %    the Free Software Foundation, either version 3 of the License, or
50 %    (at your option) any later version.
51 %
52 %    This program is distributed in the hope that it will be useful,
53 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
54 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
55 %    GNU General Public License for more details.
56 %
57 %    You should have received a copy of the GNU General Public License
58 %    along with this program.  If not, see <http://www.gnu.org/licenses/>.
59
60 N=length(Y); 
61 [nr,nc]=size(Y);
62 if nc==1 Y=transpose(Y); nc=nr; nr=1; end;
63     
64 if nargin<2 Pmax=min([100 nc/3]); end;
65
66 if exist('OCTAVE_VERSION'),
67         fprintf(2,'Warning INVEST1: DIFF-based optimization not possible\n');
68         %%% missing DIM-argument in DIFF.M    
69 else
70         %tmp=Y-mean(Y,2)*ones(1,nc);
71         RMS(:,1) = mean(Y.^2,2);
72         Dmax = min(Pmax,5);
73         for k = 1:Dmax,
74                 RMS(:,k+1) = mean(diff(Y,k,2).^2,2);
75         end;
76         [tmp, orderDIFF] = min(RMS,[],2);
77         
78         % show a nice histogram
79         h = histo3(orderDIFF-1);
80         X = 0:Dmax; H = zeros(1,Dmax+1); for k=1:length(h.X), H(find(X==h.X(k)))=h.H(k); end;
81         %X = 0:Dmax; H = zeros(1,Dmax+1); for k=1:length(x), H(find(X==x(k)))=h(k); end;
82         bar(X,H);
83         drawnow;
84
85         if nargin>2
86                 oD=0;
87         else            
88                 oD=input('Which order should be used for differentiating [default=0] ?: ');
89         end;
90         if oD>0
91                 Y=diff(Y,oD,2);
92         end;
93 end;
94
95 [AutoCov, AutoCorr, ARPMX, E, NC] = invest0(Y,Pmax);
96
97 [FPE,AIC,BIC,SBC,MDL,CATcrit,PHI,optFPE,optAIC,optBIC,optSBC,optMDL,optCAT,optPHI,s,C] = selmo(E,NC);
98
99 if 0,
100 optRC2=zeros(nr+1,1);
101 for k=0:nr,
102         if k>0
103                 optRC2(k+1)=max(find(abs(ARPMX(k,(1:Pmax).*(2:Pmax+1)/2))*sqrt(size(Y,2))>1));
104         else 
105                 optRC2(k+1)=max(find(mean(abs(ARPMX(:,(1:Pmax).*(2:Pmax+1)/2))*sqrt(size(Y,2)),2)>1));
106         end;
107 end;
108 %GERSCH=min(find(rc.^2<(0.05/1.05)));
109 s=[s optRC2];
110 end;
111
112 %CRITERIA=([FPE;AIC;BIC;SBC;MDL;CATcrit;PHI])';
113 MOPS = s(1:size(s,1),:); %[optFPE optAIC optBIC optSBC optMDL optCAT optPHI];
114
115 if nargin==3,  
116         if size(ARPMX,2)==2*Pmax,
117                 %invest1(eeg8s,30,'s');
118                 AR=ARPMX(:,1:Pmax);
119                 RC=ARPMX(:,Pmax+1:2*Pmax);
120         else
121                 AR=ARPMX(:,Pmax/2*(Pmax-1)+(1:Pmax));
122                 RC=ARPMX(:,(1:Pmax).*(2:Pmax+1)/2);
123         end;
124         oo=optBIC; 
125         sinvest1;
126 end;