GetPosteriorParametersStatistics.m 16.5 KB
Newer Older
adjemian's avatar
adjemian committed
1
function oo_ = GetPosteriorParametersStatistics(estim_params_, M_, options_, bayestopt_, oo_)
2
3
4
5
% This function prints and saves posterior estimates after the mcmc
% (+updates of oo_ & TeX output). 
% 
% INPUTS 
adjemian's avatar
adjemian committed
6
7
8
9
10
%   estim_params_    [structure] 
%   M_               [structure]
%   options_         [structure]
%   bayestopt_       [structure]
%   oo_              [structure]
11
12
%  
% OUTPUTS 
adjemian's avatar
adjemian committed
13
%   oo_              [structure]  
14
15
16
%
% SPECIAL REQUIREMENTS
%   None.
17

Sébastien Villemot's avatar
Sébastien Villemot committed
18
% Copyright (C) 2006-2012 Dynare Team
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
%
% 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/>.
34

35
%if ~options_.mh_replic && options_.load_mh_file
adjemian's avatar
adjemian committed
36
37
%   load([M_.fname '_results.mat'],'oo_'); 
%end
38

39
40
41
42
43
44
45
46
TeX     = options_.TeX;
nblck   = options_.mh_nblck;
nvx     = estim_params_.nvx;
nvn     = estim_params_.nvn;
ncx     = estim_params_.ncx;
ncn     = estim_params_.ncn;
np      = estim_params_.np ;
nx      = nvx+nvn+ncx+ncn+np;
47

48
49
DirectoryName = CheckPath('metropolis',M_.dname);
OutputDirectoryName = CheckPath('Output',M_.dname);
50
51
52

load([ DirectoryName '/'  M_.fname '_mh_history'])
FirstMhFile = record.KeepedDraws.FirstMhFile;
adjemian's avatar
adjemian committed
53
FirstLine = record.KeepedDraws.FirstLine;
54
TotalNumberOfMhFiles = sum(record.MhDraws(:,2));
55
56
57
58
59
TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
FirstMhFile = record.KeepedDraws.FirstMhFile;
NumberOfDraws = TotalNumberOfMhDraws-floor(options_.mh_drop*TotalNumberOfMhDraws);
clear record;

60
pnames=['     ';'beta ';'gamma';'norm ';'invg ';'unif ';'invg2'];
61
62
63
header_width = row_header_width(M_,estim_params_,bayestopt_);
tit2 = sprintf('%-*s %10s %10s %16s %6s %10s\n',header_width+2,' ','prior mean','post. mean','conf. interval','prior','pstdev');
pformat = '%-*s %10.3f %10.4f %10.4f %8.4f %6s %10.4f';
64
65

disp(' ');disp(' ');disp('ESTIMATION RESULTS');disp(' ');
adjemian's avatar
adjemian committed
66
67
68
try
    disp(sprintf('Log data density is %f.',oo_.MarginalDensity.ModifiedHarmonicMean))
catch
69
    [marginal,oo_] = marginal_density(M_, options_, estim_params_, oo_);
adjemian's avatar
adjemian committed
70
71
    disp(sprintf('Log data density is %f.',oo_.MarginalDensity.ModifiedHarmonicMean))
end
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
if np
    type = 'parameters';
    if TeX
        fid = TeXBegin(OutputDirectoryName,M_.fname,1,type);
    end
    disp(' ')
    disp(type)
    disp(tit2)
    ip = nvx+nvn+ncx+ncn+1;
    for i=1:np
        if options_.mh_replic
            Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
            [post_mean, post_median, post_var, hpd_interval, post_deciles, ...
             density] = posterior_moments(Draws,1,options_.mh_conf_sig);
            name = bayestopt_.name{ip};
            oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
        else
adjemian's avatar
adjemian committed
89
90
91
92
93
94
95
96
97
98
            try
                name = bayestopt_.name{ip};
                [post_mean,hpd_interval,post_var] = Extractoo(oo_,name,type);
            catch
                Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
                [post_mean, post_median, post_var, hpd_interval, post_deciles, ...
                 density] = posterior_moments(Draws,1,options_.mh_conf_sig);
                name = bayestopt_.name{ip};
                oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);                
            end
99
        end
100
        disp(sprintf(pformat,header_width,name,bayestopt_.p1(ip),...
101
102
103
                     post_mean, ...
                     hpd_interval, ...
                     pnames(bayestopt_.pshape(ip)+1,:), ...
stepan's avatar
   
stepan committed
104
                     bayestopt_.p2(ip)));    
105
        if TeX
106
107
            k = estim_params_.param_vals(i,1);
            name = deblank(M_.param_names_tex(k,:));
stepan's avatar
   
stepan committed
108
109
            TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.p1(ip),...
                    bayestopt_.p2(ip),post_mean,sqrt(post_var),hpd_interval);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        end
        ip = ip+1;
    end
    if TeX
        TeXEnd(fid,1,type);
    end
end
if nvx
    type = 'shocks_std';
    if TeX
        fid = TeXBegin(OutputDirectoryName,M_.fname,2,'standard deviation of structural shocks');
    end
    ip = 1;
    disp(' ')
    disp('standard deviation of shocks')
    disp(tit2)
    for i=1:nvx
        if options_.mh_replic
            Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
            [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
                posterior_moments(Draws,1,options_.mh_conf_sig);
            k = estim_params_.var_exo(i,1);
            name = deblank(M_.exo_names(k,:));
            oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
            M_.Sigma_e(k,k) = post_mean*post_mean;
        else
adjemian's avatar
adjemian committed
136
137
138
139
140
141
142
143
144
145
146
147
148
            try
                k = estim_params_.var_exo(i,1);
                name = deblank(M_.exo_names(k,:));
                [post_mean,hpd_interval,post_var] = Extractoo(oo_,name,type);
            catch
                Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
                [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
                    posterior_moments(Draws,1,options_.mh_conf_sig);
                k = estim_params_.var_exo(i,1);
                name = deblank(M_.exo_names(k,:));
                oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
                M_.Sigma_e(k,k) = post_mean*post_mean;
            end
149
        end
150
        disp(sprintf(pformat,header_width,name,bayestopt_.p1(ip),post_mean,hpd_interval,...
stepan's avatar
   
stepan committed
151
                     pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.p2(ip)));
152
153
        if TeX,
            name = deblank(M_.exo_names_tex(k,:));
stepan's avatar
   
stepan committed
154
155
            TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.p1(ip),...
                    bayestopt_.p2(ip),post_mean,sqrt(post_var),hpd_interval);
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
        end
        ip = ip+1;
    end
    if TeX
        TeXEnd(fid,2,'standard deviation of structural shocks');        
    end
end
if nvn
    type = 'measurement_errors_std';
    if TeX
        fid = TeXBegin(OutputDirectoryName,M_.fname,3,'standard deviation of measurement errors')
    end
    disp(' ')
    disp('standard deviation of measurement errors')
    disp(tit2)
    ip = nvx+1;
    for i=1:nvn
        if options_.mh_replic
            Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
            [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
                posterior_moments(Draws,1,options_.mh_conf_sig);
            name = deblank(options_.varobs(estim_params_.var_endo(i,1),:));
            oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
        else
adjemian's avatar
adjemian committed
180
181
182
183
184
185
186
187
188
189
            try
                name = deblank(options_.varobs(estim_params_.var_endo(i,1),:));
                [post_mean,hpd_interval,post_var] = Extractoo(oo_,name,type);
            catch
                Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
                [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
                    posterior_moments(Draws,1,options_.mh_conf_sig);
                name = deblank(options_.varobs(estim_params_.var_endo(i,1),:));
                oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
            end
190
        end
191
        disp(sprintf(pformat,header_width,name,bayestopt_.p1(ip),post_mean,hpd_interval, ...
stepan's avatar
   
stepan committed
192
                     pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.p2(ip)));
193
        if TeX
194
195
            k = estim_params_.var_endo(i,1);
            name = deblank(M_.endo_names_tex(k,:));
stepan's avatar
   
stepan committed
196
197
            TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.p1(ip),...
                    bayestopt_.p2(ip),post_mean,sqrt(post_var),hpd_interval);
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
        end
        ip = ip+1;
    end
    if TeX
        TeXEnd(fid,3,'standard deviation of measurement errors');        
    end
end
if ncx
    type = 'shocks_corr';
    if TeX
        fid = TeXBegin(OutputDirectoryName,M_.fname,4,'correlation of structural shocks');
    end
    disp(' ')
    disp('correlation of shocks')
    disp(tit2)
    ip = nvx+nvn+1;
    for i=1:ncx
        if options_.mh_replic
            Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
            [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
                posterior_moments(Draws,1,options_.mh_conf_sig);
            k1 = estim_params_.corrx(i,1);
            k2 = estim_params_.corrx(i,2);
            name = [deblank(M_.exo_names(k1,:)) ',' deblank(M_.exo_names(k2,:))];
            NAME = [deblank(M_.exo_names(k1,:)) '_' deblank(M_.exo_names(k2,:))];
            oo_ = Filloo(oo_,NAME,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
            M_.Sigma_e(k1,k2) = post_mean*sqrt(M_.Sigma_e(k1,k1)*M_.Sigma_e(k2,k2));
            M_.Sigma_e(k2,k1) = M_.Sigma_e(k1,k2);
        else
adjemian's avatar
adjemian committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
            try
                k1 = estim_params_.corrx(i,1);
                k2 = estim_params_.corrx(i,2);
                name = [deblank(M_.exo_names(k1,:)) ',' deblank(M_.exo_names(k2,:))];
                NAME = [deblank(M_.exo_names(k1,:)) '_' deblank(M_.exo_names(k2,:))];
                [post_mean,hpd_interval,post_var] = Extractoo(oo_,NAME,type);
            catch
                Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
                [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
                    posterior_moments(Draws,1,options_.mh_conf_sig);
                k1 = estim_params_.corrx(i,1);
                k2 = estim_params_.corrx(i,2);
                name = [deblank(M_.exo_names(k1,:)) ',' deblank(M_.exo_names(k2,:))];
                NAME = [deblank(M_.exo_names(k1,:)) '_' deblank(M_.exo_names(k2,:))];
                oo_ = Filloo(oo_,NAME,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
                M_.Sigma_e(k1,k2) = post_mean*sqrt(M_.Sigma_e(k1,k1)*M_.Sigma_e(k2,k2));
                M_.Sigma_e(k2,k1) = M_.Sigma_e(k1,k2);
            end
245
        end
246
        disp(sprintf(pformat, header_width,name,bayestopt_.p1(ip),post_mean,hpd_interval, ...
stepan's avatar
   
stepan committed
247
                     pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.p2(ip)));
248
        if TeX
249
            name = ['(',deblank(M_.exo_names_tex(k1,:)) ',' deblank(M_.exo_names_tex(k2,:)),')'];
stepan's avatar
   
stepan committed
250
251
            TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.p1(ip),...
                    bayestopt_.p2(ip),post_mean,sqrt(post_var),hpd_interval);
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
        end
        ip = ip+1;
    end
    if TeX
        TeXEnd(fid,4,'correlation of structural shocks');
    end
end
if ncn
    type = 'measurement_errors_corr';
    if TeX
        fid = TeXBegin(OutputDirectoryName,M_.fname,5,'correlation of measurement errors');
    end
    disp(' ')
    disp('correlation of measurement errors')
    disp(tit2)
    ip = nvx+nvn+ncx+1;
    for i=1:ncn
        if options_.mh_replic
            Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
            [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
                posterior_moments(Draws,1,options_.mh_conf_sig);
            k1 = estim_params_.corrn(i,1);
            k2 = estim_params_.corrn(i,2);
            name = [deblank(M_.endo_names(k1,:)) ',' deblank(M_.endo_names(k2,:))];
            NAME = [deblank(M_.endo_names(k1,:)) '_' deblank(M_.endo_names(k2,:))];
            oo_ = Filloo(oo_,NAME,type,post_mean,hpd_interval,...
                         post_median,post_var,post_deciles,density);
        else
adjemian's avatar
adjemian committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
            try
                k1 = estim_params_.corrn(i,1);
                k2 = estim_params_.corrn(i,2);
                name = [deblank(M_.endo_names(k1,:)) ',' deblank(M_.endo_names(k2,:))];
                NAME = [deblank(M_.endo_names(k1,:)) '_' deblank(M_.endo_names(k2,:))];
                [post_mean,hpd_interval,post_var] = Extractoo(oo_,NAME,type);
            catch
                Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
                [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
                    posterior_moments(Draws,1,options_.mh_conf_sig);
                k1 = estim_params_.corrn(i,1);
                k2 = estim_params_.corrn(i,2);
                name = [deblank(M_.endo_names(k1,:)) ',' deblank(M_.endo_names(k2,:))];
                NAME = [deblank(M_.endo_names(k1,:)) '_' deblank(M_.endo_names(k2,:))];
                oo_ = Filloo(oo_,NAME,type,post_mean,hpd_interval,...
                             post_median,post_var,post_deciles,density);
            end
297
        end
298
        disp(sprintf(pformat, header_width,name,bayestopt_.p1(ip),post_mean,hpd_interval, ...
stepan's avatar
   
stepan committed
299
                     pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.p2(ip)));
300
301
        if TeX,
            name = ['(',deblank(M_.endo_names_tex(k1,:)) ',' deblank(M_.endo_names_tex(k2,:)),')'];
stepan's avatar
   
stepan committed
302
303
            TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.p1(ip),...
                    bayestopt_.p2(ip),post_mean,sqrt(post_var),hpd_interval);            
304
305
306
307
308
309
310
311
312
313
314
315
316
        end
        ip = ip+1;
    end
    if TeX
        TeXEnd(fid,5,'correlation of measurement errors');        
    end
end


%
%% subfunctions:
%
function fid = TeXBegin(directory,fname,fnum,title)
317
318
319
320
321
322
323
TeXfile = [directory '/' fname '_Posterior_Mean_' int2str(fnum) '.TeX'];
fidTeX = fopen(TeXfile,'w');
fprintf(fidTeX,'%% TeX-table generated by Dynare.\n');
fprintf(fidTeX,['%% RESULTS FROM METROPOLIS HASTINGS (' title ')\n']);
fprintf(fidTeX,['%% ' datestr(now,0)]);
fprintf(fidTeX,' \n');
fprintf(fidTeX,' \n');
324
325
326
327
328
329
330
331
332
333
fprintf(fidTeX,'\\begin{center}\n');
fprintf(fidTeX,'\\begin{longtable}{l|lcccccc} \n');
fprintf(fidTeX,['\\caption{Results from Metropolis-Hastings (' title ')}\n ']);
fprintf(fidTeX,['\\label{Table:MHPosterior:' int2str(fnum)  '}\\\\\n']);
fprintf(fidTeX,'\\hline\\hline \\\\ \n');
fprintf(fidTeX,['  & Prior distribution & Prior mean  & Prior ' ...
                's.d. & Posterior mean & Posterior s.d.  & HPD inf & HPD sup\\\\ \n']);
fprintf(fidTeX,'\\hline \\endfirsthead \n');
fprintf(fidTeX,['\\caption{(continued)}']);
fprintf(fidTeX,['\\label{Table:MHPosterior:' int2str(fnum)  '}\\\\\n']);
334
335
336
fprintf(fidTeX,'\\hline\\hline \\\\ \n');
fprintf(fidTeX,['  & Prior distribution & Prior mean  & Prior ' ...
                's.d. & Posterior mean & Posterior s.d.  & HPD inf & HPD sup\\\\ \n']);
337
338
339
340
341
342
fprintf(fidTeX,'\\hline \\endhead \n');

fprintf(fidTeX,'\\hline \\multicolumn{8}{r}{(Continued on next page)} \\\\ \\hline \\endfoot \n');
fprintf(fidTeX,'\\hline \\hline \\endlastfoot \n');


343
344
fid = fidTeX;

345
346

function TeXCore(fid,name,shape,priormean,priorstd,postmean,poststd,hpd)
347
348
349
350
351
352
353
354
355
356
fprintf(fid,['$%s$ & %s & %7.3f & %6.4f & %7.3f& %6.4f & %7.4f & %7.4f \\\\ \n'],... 
        name,...
        shape,...
        priormean,...
        priorstd,...
        postmean,...
        poststd,...
        hpd(1),...
        hpd(2));

357
358

function TeXEnd(fid,fnum,title)
359
360
fprintf(fid,'\\end{longtable}\n ');    
fprintf(fid,'\\end{center}\n');
361
362
363
364
fprintf(fid,'%% End of TeX file.\n');
fclose(fid);


365
function oo = Filloo(oo,name,type,postmean,hpdinterval,postmedian,postvar,postdecile,density)
366
367
368
369
370
371
372
373
eval(['oo.posterior_mean.' type '.' name ' = postmean;']);
eval(['oo.posterior_hpdinf.' type '.' name ' = hpdinterval(1);']); 
eval(['oo.posterior_hpdsup.' type '.' name ' = hpdinterval(2);']);      
eval(['oo.posterior_median.' type '.' name ' = postmedian;']);
eval(['oo.posterior_variance.' type '.' name ' = postvar;']);
eval(['oo.posterior_deciles.' type '.' name ' = postdecile;']);
eval(['oo.posterior_density.' type '.' name ' = density;']);

374
function [post_mean,hpd_interval,post_var] = Extractoo(oo,name,type)
375
376
377
378
379
hpd_interval = zeros(2,1);
eval(['post_mean = oo.posterior_mean.' type '.' name ';']);
eval(['hpd_interval(1) = oo.posterior_hpdinf.' type '.' name ';']); 
eval(['hpd_interval(2) = oo.posterior_hpdsup.' type '.' name ';']);
eval(['post_var = oo.posterior_variance.' type '.' name ';']);