1function internals(flag, varargin)
2
3%@info:
4%! @deftypefn {Function File} internals (@var{flag},@var{a},@var{b}, ...)
5%! @anchor{internals}
6%! @sp 1
7%! This command provides internal documentation and unitary tests for the matlab routines.
8%! @sp 2
9%! @strong{Inputs}
10%! @sp 1
11%! @table @ @var
12%! @item flag
13%! Mandatory argument: --doc (for displaying internal documentation) or --test (for performing unitary tests).
14%! @item b
15%! Name of the routine to be tested or for which internal documentation is needed.
16%! @item c
17%! Name of the routine to be tested.
18%! @item d
19%! @end table
20%! @sp 1
21%! @strong{Outputs}
22%! @sp 1
23%! None.
24%! @sp 2
25%! @strong{Examples}
26%! @sp 1
27%! The following instruction:
28%! @sp 1
29%! @example
30%! internals --info particle/local_state_iteration
31%! @end example
32%! will display the internal documentation of the routine local_state_iteration located in the particle subfolder of the matlab directory.
33%! @sp 1
34%! The following instruction:
35%! @sp 1
36%! @example
37%! internals --test particle/local_state_iteration
38%! @end example
39%! will execute the unitary tests associated the routine local_state_iteration.
40%! @sp 2
41%! @strong{Remarks}
42%! @sp 1
43%! [1] It is not possible to display the internal documentation of more than one routine.
44%! @sp 1
45%! [2] It is possible to perform unitary tests on a list of routines.
46%! @sp 1
47%! [3] For displaying the internal documentation, matlab calls texinfo which has to be installed.
48%! @sp 2
49%! @strong{This function is called by:}
50%! @sp 2
51%! None.
52%! @sp 2
53%! @strong{This function calls:}
54%! @sp 1
55%! @ref{utilities/tests/dynTest} @ref{utilities/doc/dynInfo}
56%! @end deftypefn
57%@eod:
58
59% Copyright (C) 2011-2014 Dynare Team
60%
61% This file is part of Dynare.
62%
63% Dynare is free software: you can redistribute it and/or modify
64% it under the terms of the GNU General Public License as published by
65% the Free Software Foundation, either version 3 of the License, or
66% (at your option) any later version.
67%
68% Dynare is distributed in the hope that it will be useful,
69% but WITHOUT ANY WARRANTY; without even the implied warranty of
70% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
71% GNU General Public License for more details.
72%
73% You should have received a copy of the GNU General Public License
74% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
75
76% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
77
78more off
79
80if strcmpi(flag,'--test')
81    if nargin>1
82        dynare_path = dynare_config();
83        number_of_matlab_routines = length(varargin);
84        for i=1:number_of_matlab_routines
85            dtest(varargin{i},[dynare_path '..' filesep 'tests']);
86        end
87    else
88        disp('You have to specify at least one Matlab routine after --test flag!')
89    end
90    return
91end
92
93if strcmpi(flag,'--load-mh-history') || strcmpi(flag,'--display-mh-history')
94    switch length(varargin)
95      case 3
96        fname = varargin{1};
97        if ~isequal(varargin{2},'in')
98            error('internals:: Calling sequence must be of the form: internals --load-mh-history fname in dname')
99        end
100        dname = varargin{3};
101      case 1
102        fname = varargin{1};
103        dname = varargin{1};
104      otherwise
105        error('internals:: Wrong calling sequence! You should read the manual...')
106    end
107    o = load_last_mh_history_file([dname filesep 'metropolis'],fname);
108    if strcmpi(flag,'--load-mh-history')
109        assignin('caller','mcmc_informations',o);
110    else
111        oo = load_first_mh_history_file([dname filesep 'metropolis'],fname);
112        local = load([fname '_results'],'bayestopt_');
113        names = local.bayestopt_.name; %evalin('base','bayestopt_.name');
114        str = ['MCMC set-up for ' fname ' mod file'];
115        ltr = length(str);
116        skipline()
117        disp(repmat('=',1,ltr))
118        disp(str)
119        disp(repmat('=',1,ltr))
120        skipline(2)
121        oar = compute_overall_acceptance_ratio([dname filesep 'metropolis'],fname);
122        for b=1:o.Nblck
123            str = ['MCMC chain number ' num2str(b) ':'];
124            ltr = length(str);
125            disp(str);
126            disp(repmat('-',1,ltr));
127            skipline()
128            disp([' o Number of MCMC files is ' num2str(sum(o.MhDraws(:,2)))]);
129            disp([' o Number of draws per chain is ' num2str(sum(o.MhDraws(:,1)))]);
130            disp([' o Acceptance ratio in the current chain is ' num2str(oar(b)*100,'%5.2f') '%']);
131            disp([' o Initial value of the posterior kernel is: ' num2str(oo.InitialLogPost(b),'%10.5f')])
132            disp([' o Last value of the posterior kernel is: ' num2str(o.LastLogPost(b),'%10.5f')])
133            disp([' o State of the chain:'])
134            skipline()
135            d1 = num2str(transpose(oo.InitialParameters(b,:)),'%10.5f\n');
136            d2 = num2str(transpose(o.LastParameters(b,:)),'%10.5f\n');
137            d1s = size(d1,2);
138            d2s = size(d2,2);
139            c0 = repmat('   ',length(names)+2,1);
140            c1 = char(' ', repmat('+',1,size(char(names),2)), char(names));
141            s1 = char(' || ','++++',repmat(' || ', length(names),1));
142            t1 = repmat(' ',1,d1s);
143            if d1s<=7
144                t1 = 'Initial';
145            else
146                diff = d1s-7;
147                if isequal(mod(diff,2),0)
148                    start = diff/2+1;
149                else
150                    start = (diff-1)/2+1;
151                end
152                t1(start:start+6) = 'Initial';
153            end
154            c2 = char(t1,repmat('+',1,size(d1,2)),d1);
155            s2 = char(' | ','+++',repmat(' | ', length(names),1));
156            t2 = repmat(' ',1,d2s);
157            if d2s<=7
158                t2 = 'Current';
159            else
160                diff = d2s-7;
161                if isequal(mod(diff,2),0)
162                    start = diff/2+1;
163                else
164                    start = (diff-1)/2+1;
165                end
166                t2(start:start+6) = 'Current';
167            end
168            c3 = char(t2,repmat('+',1,size(d2,2)), d2);
169            disp([c0, c1, s1, c2, s2, c3]);
170            skipline()
171        end
172    end
173    return
174end
175
176if strcmpi(flag,'--info')
177    if nargin==2
178        dynare_config();
179        dynInfo(varargin{1})
180    else
181        if nargin<2
182            disp('You have to specify a Matlab routine after --info flag!')
183        else
184            disp('I can only show internal documentation for one Matlab routine!')
185        end
186    end
187    return
188end
189
190disp('You should read the manual...')
191