get_prior_info.m 7.38 KB
Newer Older
1
function results = get_prior_info(info,plt_flag)
stepan's avatar
   
stepan committed
2
% Computes various prior statistics.
3
%
stepan's avatar
stepan committed
4
% INPUTS
stepan's avatar
   
stepan committed
5
%   info     [integer]   scalar specifying what has to be done.
6
%
stepan's avatar
stepan committed
7
8
9
10
11
12
% OUTPUTS
%   none
%
% SPECIAL REQUIREMENTS
%   none

Sébastien Villemot's avatar
Sébastien Villemot committed
13
% Copyright (C) 2009-2012 Dynare Team
stepan's avatar
stepan committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
29
global options_ M_ estim_params_ oo_ objective_function_penalty_base
stepan's avatar
   
stepan committed
30

31
32
if ~nargin
    info = 0;
33
34
35
36
37
38
39
    plt_flag = 0;
end

if nargin==1
    plt_flag = 1;
end

40
41
42
% Initialize returned variable.
results = [];

43
44
45
46
changed_qz_criterium_flag  = 0;
if isempty(options_.qz_criterium)
    options_.qz_criterium = 1+1e-9;
    changed_qz_criterium_flag  = 1;
47
48
49
50
end

M_.dname = M_.fname;

51
52
53
54
% Temporarly set options_.order equal to one
order = options_.order;
options_.order = 1;

55
[xparam1,estim_params_,bayestopt_,lb,ub,M_] = set_prior(estim_params_,M_,options_);
56
57
58
if plt_flag
    plot_priors(bayestopt_,M_,options_);
end
59
60
61
62
63

PriorNames = { 'Beta' , 'Gamma' , 'Gaussian' , 'Inverted Gamma' , 'Uniform' , 'Inverted Gamma -- 2' };

if size(M_.param_names,1)==size(M_.param_names_tex,1)% All the parameters have a TeX name.
    fidTeX = fopen('priors_data.tex','w+');
64
    % Column 1: a string for the name of the prior distribution.
65
66
67
68
    % Column 2: the prior mean.
    % Column 3: the prior standard deviation.
    % Column 4: the lower bound of the prior density support.
    % Column 5: the upper bound of the prior density support.
69
    % Column 6: the lower bound of the interval containing 80% of the prior mass.
70
71
72
    % Column 7: the upper bound of the interval containing 80% of the prior mass.
    prior_trunc_backup = options_.prior_trunc ;
    options_.prior_trunc = (1-options_.prior_interval)/2 ;
73
    PriorIntervals = prior_bounds(bayestopt_,options_) ;
74
75
    options_.prior_trunc = prior_trunc_backup ;
    for i=1:size(bayestopt_.name,1)
76
        [tmp,TexName] = get_the_name(i,1,M_,estim_params_,options_);
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        PriorShape = PriorNames{ bayestopt_.pshape(i) };
        PriorMean = bayestopt_.p1(i);
        PriorStandardDeviation = bayestopt_.p2(i);
        switch bayestopt_.pshape(i)
          case { 1 , 5 }
            LowerBound = bayestopt_.p3(i);
            UpperBound = bayestopt_.p4(i);
          case { 2 , 4 , 6 }
            LowerBound = bayestopt_.p3(i);
            UpperBound = '$\infty$';
          case 3
            if isinf(bayestopt_.p3(i))
                LowerBound = '$-\infty$';
            else
stepan's avatar
stepan committed
91
                LowerBound = bayestopt_.p3(i);
92
93
            end
            if isinf(bayestopt_.p4(i))
stepan's avatar
stepan committed
94
                UpperBound = '$\infty$';
95
96
            else
                UpperBound = bayestopt_.p4(i);
stepan's avatar
stepan committed
97
            end
98
99
          otherwise
            error('get_prior_info:: Dynare bug!')
stepan's avatar
stepan committed
100
        end
101
102
103
104
105
106
107
108
109
110
        format_string = build_format_string(bayestopt_,i);
        fprintf(fidTeX,format_string, ...
                TexName, ...
                PriorShape, ...
                PriorMean, ...
                PriorStandardDeviation, ...
                LowerBound, ...
                UpperBound, ...
                PriorIntervals(i,1), ...
                PriorIntervals(i,2) );
stepan's avatar
stepan committed
111
    end
112
113
    fclose(fidTeX);
end
stepan's avatar
   
stepan committed
114

115
116
117
M_.dname = M_.fname;

if info==1% Prior simulations (BK).
118
    results = prior_sampler(0,M_,bayestopt_,options_,oo_,estim_params_);
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    % Display prior mass info
    disp(['Prior mass = ' num2str(results.prior.mass)])
    disp(['BK indeterminacy share                = ' num2str(results.bk.indeterminacy_share)])
    disp(['BK unstability share                  = ' num2str(results.bk.unstability_share)])
    disp(['BK singularity share                  = ' num2str(results.bk.singularity_share)])
    disp(['Complex jacobian share                = ' num2str(results.jacobian.problem_share)])
    disp(['mjdgges crash share                   = ' num2str(results.dll.problem_share)])
    disp(['Steady state problem share            = ' num2str(results.ss.problem_share)])
    disp(['Complex steady state  share           = ' num2str(results.ss.complex_share)])
    disp(['Analytical steady state problem share = ' num2str(results.ass.problem_share)])
end

if info==2% Prior optimization.
          % Initialize to the prior mode if possible
133
    oo_.dr=set_state_space(oo_.dr,M_,options_);
134
135
136
137
138
139
140
141
    k = find(~isnan(bayestopt_.p5));
    xparam1(k) = bayestopt_.p5(k);
    % Pertubation of the initial condition.
    look_for_admissible_initial_condition = 1;
    scale = 1.0;
    iter  = 0;
    while look_for_admissible_initial_condition
        xinit = xparam1+scale*randn(size(xparam1));
Stéphane Adjemian's avatar
Stéphane Adjemian committed
142
        if all(xinit(:)>bayestopt_.p3) && all(xinit(:)<bayestopt_.p4)
143
144
145
146
147
            M_ = set_all_parameters(xinit,estim_params_,M_);
            [dr,INFO,M_,options_,oo_] = resol(0,M_,options_,oo_);
            if ~INFO(1)
                look_for_admissible_initial_condition = 0;
            end
148
149
150
151
        else
            if iter == 2000;
                scale = scale/1.1;
                iter  = 0;
stepan's avatar
   
stepan committed
152
            else
153
                iter = iter+1;
stepan's avatar
   
stepan committed
154
155
            end
        end
156
    end
157
158
159
160
161
    objective_function_penalty_base = minus_logged_prior_density(xinit, bayestopt_.pshape, ...
                               bayestopt_.p6, ...
                               bayestopt_.p7, ...
                               bayestopt_.p3, ...
                               bayestopt_.p4,options_,M_,estim_params_,oo_);
162
163
164
165
166
167
    % Maximization
    [xparams,lpd,hessian] = ...
        maximize_prior_density(xinit, bayestopt_.pshape, ...
                               bayestopt_.p6, ...
                               bayestopt_.p7, ...
                               bayestopt_.p3, ...
168
                               bayestopt_.p4,options_,M_,estim_params_,oo_);
169
170
171
172
173
174
175
176
    % Display the results.
    disp(' ')
    disp(' ')
    disp('------------------')
    disp('PRIOR OPTIMIZATION')
    disp('------------------')
    disp(' ')
    for i = 1:length(xparams)
177
        disp(['deep parameter ' int2str(i) ': ' get_the_name(i,0,M_,estim_params_,options_) '.'])
178
179
180
        disp(['  Initial condition ....... ' num2str(xinit(i)) '.'])
        disp(['  Prior mode .............. ' num2str(bayestopt_.p5(i)) '.'])
        disp(['  Optimized prior mode .... ' num2str(xparams(i)) '.'])
stepan's avatar
   
stepan committed
181
        disp(' ')
stepan's avatar
   
stepan committed
182
    end
183
184
185
186
end

if info==3% Prior simulations (2nd order moments).
    oo_ = compute_moments_varendo('prior',options_,M_,oo_);
187
end
188

189
190
191
192
if changed_qz_criterium_flag
    options_.qz_criterium = [];
end

193
options_.order = order;
stepan's avatar
   
stepan committed
194

stepan's avatar
stepan committed
195
function format_string = build_format_string(bayestopt,i)
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
format_string = ['%s & %s & %6.4f &'];
if isinf(bayestopt.p2(i))
    format_string = [ format_string , ' %s &'];
else
    format_string = [ format_string , ' %6.4f &'];
end
if isinf(bayestopt.p3(i))
    format_string = [ format_string , ' %s &'];
else
    format_string = [ format_string , ' %6.4f &'];
end
if isinf(bayestopt.p4(i))
    format_string = [ format_string , ' %s &'];
else
    format_string = [ format_string , ' %6.4f &'];
end
format_string = [ format_string , ' %6.4f & %6.4f \\\\ \n'];