2023-09-13 18:10:16 +02:00
function [posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix ( bayestopt_,fname,dname,outputFolderName)
% function [posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix(bayestopt_,fname,dname,outputFolderName)
2010-09-01 22:15:47 +02:00
% Estimation of the posterior covariance matrix, posterior mean, posterior mode and evaluation of the posterior kernel at the
% estimated mode, using the draws from a metropolis-hastings. The estimated posterior mode and covariance matrix are saved in
2023-09-01 15:48:28 +02:00
% a file <fname>_mh_mode.mat.
2017-05-16 15:10:20 +02:00
%
% INPUTS
2023-09-01 15:48:28 +02:00
% o bayestopt_ [struct] characterizing priors
% o fname [string] name of model
% o dname [string] name of directory with metropolis folder
2023-09-13 18:10:16 +02:00
% o outputFolderName [string] name of directory to store results
2017-05-16 15:10:20 +02:00
%
2007-05-19 18:54:39 +02:00
% OUTPUTS
2008-09-18 18:28:57 +02:00
% o posterior_mean [double] (n*1) vector, posterior expectation of the parameters.
% o posterior_covariance [double] (n*n) matrix, posterior covariance of the parameters (computed from previous metropolis hastings).
2017-05-16 15:10:20 +02:00
% o posterior_mode [double] (n*1) vector, posterior mode of the parameters.
2010-09-01 22:15:47 +02:00
% o posterior_kernel_at_the_mode [double] scalar.
2006-07-28 12:00:31 +02:00
%
% SPECIAL REQUIREMENTS
% None.
2008-08-01 14:40:33 +02:00
2023-07-13 18:01:02 +02:00
% Copyright © 2006-2023 Dynare Team
2008-08-01 14:40:33 +02:00
%
% 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
2021-06-09 17:33:48 +02:00
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
2023-09-13 18:10:16 +02:00
if nargin < 4
outputFolderName = ' Output' ;
end
2023-09-01 15:48:28 +02:00
MetropolisFolder = CheckPath ( ' metropolis' , dname ) ;
BaseName = [ MetropolisFolder filesep fname ] ;
2008-01-17 12:10:12 +01:00
2023-09-01 15:48:28 +02:00
record = load_last_mh_history_file ( MetropolisFolder , fname ) ;
2006-07-28 12:00:31 +02:00
FirstMhFile = record . KeepedDraws . FirstMhFile ;
2008-09-18 18:28:57 +02:00
FirstLine = record . KeepedDraws . FirstLine ;
2006-07-28 12:00:31 +02:00
TotalNumberOfMhFiles = sum ( record . MhDraws ( : , 2 ) ) ;
2023-07-13 18:01:02 +02:00
[ nblck , n ] = size ( record . LastParameters ) ;
2008-09-18 18:28:57 +02:00
posterior_kernel_at_the_mode = - Inf ;
posterior_mean = zeros ( n , 1 ) ;
posterior_mode = NaN ( n , 1 ) ;
posterior_covariance = zeros ( n , n ) ;
2007-05-19 18:54:39 +02:00
offset = 0 ;
2007-05-20 08:55:40 +02:00
2008-09-18 18:28:57 +02:00
for b = 1 : nblck
first_line = FirstLine ;
for n = FirstMhFile : TotalNumberOfMhFiles
2017-05-16 15:10:20 +02:00
load ( [ BaseName ' _mh' int2str ( n ) ' _blck' int2str ( b ) ' .mat' ] , ' x2' , ' logpo2' ) ;
2008-09-18 18:28:57 +02:00
[ tmp , idx ] = max ( logpo2 ) ;
if tmp > posterior_kernel_at_the_mode
posterior_kernel_at_the_mode = tmp ;
posterior_mode = x2 ( idx , : ) ;
end
[ posterior_mean , posterior_covariance , offset ] = recursive_moments ( posterior_mean , posterior_covariance , x2 ( first_line : end , : ) , offset ) ;
first_line = 1 ;
2007-05-19 18:54:39 +02:00
end
2006-07-28 12:00:31 +02:00
end
2007-05-19 18:54:39 +02:00
2008-09-18 18:28:57 +02:00
xparam1 = posterior_mode ' ;
hh = inv ( posterior_covariance ) ;
fval = posterior_kernel_at_the_mode ;
2014-07-02 15:20:07 +02:00
parameter_names = bayestopt_ . name ;
2007-05-19 18:54:39 +02:00
2023-09-13 18:10:16 +02:00
save ( [ dname filesep outputFolderName filesep fname ' _mh_mode.mat' ] , ' xparam1' , ' hh' , ' fval' , ' parameter_names' ) ;