1function [posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix() 2% Estimation of the posterior covariance matrix, posterior mean, posterior mode and evaluation of the posterior kernel at the 3% estimated mode, using the draws from a metropolis-hastings. The estimated posterior mode and covariance matrix are saved in 4% a file <M_.fname>_mh_mode.mat. 5% 6% INPUTS 7% None. 8% 9% OUTPUTS 10% o posterior_mean [double] (n*1) vector, posterior expectation of the parameters. 11% o posterior_covariance [double] (n*n) matrix, posterior covariance of the parameters (computed from previous metropolis hastings). 12% o posterior_mode [double] (n*1) vector, posterior mode of the parameters. 13% o posterior_kernel_at_the_mode [double] scalar. 14% 15% SPECIAL REQUIREMENTS 16% None. 17 18% Copyright (C) 2006-2017 Dynare Team 19% 20% This file is part of Dynare. 21% 22% Dynare is free software: you can redistribute it and/or modify 23% it under the terms of the GNU General Public License as published by 24% the Free Software Foundation, either version 3 of the License, or 25% (at your option) any later version. 26% 27% Dynare is distributed in the hope that it will be useful, 28% but WITHOUT ANY WARRANTY; without even the implied warranty of 29% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 30% GNU General Public License for more details. 31% 32% You should have received a copy of the GNU General Public License 33% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 34 35global M_ options_ estim_params_ bayestopt_ 36 37 38n = estim_params_.np + ... 39 estim_params_.nvn+ ... 40 estim_params_.ncx+ ... 41 estim_params_.ncn+ ... 42 estim_params_.nvx; 43 44nblck = options_.mh_nblck; 45 46MetropolisFolder = CheckPath('metropolis',M_.dname); 47ModelName = M_.fname; 48BaseName = [MetropolisFolder filesep ModelName]; 49 50load_last_mh_history_file(MetropolisFolder, ModelName); 51 52FirstMhFile = record.KeepedDraws.FirstMhFile; 53FirstLine = record.KeepedDraws.FirstLine; 54TotalNumberOfMhFiles = sum(record.MhDraws(:,2)); 55 56posterior_kernel_at_the_mode = -Inf; 57posterior_mean = zeros(n,1); 58posterior_mode = NaN(n,1); 59posterior_covariance = zeros(n,n); 60offset = 0; 61 62for b=1:nblck 63 first_line = FirstLine; 64 for n = FirstMhFile:TotalNumberOfMhFiles 65 load([ BaseName '_mh' int2str(n) '_blck' int2str(b) '.mat'],'x2','logpo2'); 66 [tmp,idx] = max(logpo2); 67 if tmp>posterior_kernel_at_the_mode 68 posterior_kernel_at_the_mode = tmp; 69 posterior_mode = x2(idx,:); 70 end 71 [posterior_mean,posterior_covariance,offset] = recursive_moments(posterior_mean,posterior_covariance,x2(first_line:end,:),offset); 72 first_line = 1; 73 end 74end 75 76xparam1 = posterior_mode'; 77hh = inv(posterior_covariance); 78fval = posterior_kernel_at_the_mode; 79parameter_names = bayestopt_.name; 80 81save([M_.fname '_mh_mode.mat'],'xparam1','hh','fval','parameter_names');