compute_mh_covariance_matrix: remove globals

kalman-mex
Willi Mutschler 2023-09-01 15:48:28 +02:00
parent d8105a3237
commit dd2c816df0
No known key found for this signature in database
GPG Key ID: 91E724BF17A73F6D
5 changed files with 22 additions and 22 deletions

View File

@ -1,10 +1,11 @@
function [posterior_sampler_options, options_, bayestopt_] = check_posterior_sampler_options(posterior_sampler_options, options_, bounds, bayestopt_)
% function [posterior_sampler_options, options_, bayestopt_] = check_posterior_sampler_options(posterior_sampler_options, options_, bounds, bayestopt_)
function [posterior_sampler_options, options_, bayestopt_] = check_posterior_sampler_options(posterior_sampler_options, fname, dname, options_, bounds, bayestopt_)
% function [posterior_sampler_options, options_, bayestopt_] = check_posterior_sampler_options(posterior_sampler_options, fname, dname, options_, bounds, bayestopt_)
% initialization of posterior samplers
%
% INPUTS
% posterior_sampler_options: posterior sampler options
% fname: name of the mod-file
% dname: name of directory with metropolis folder
% options_: structure storing the options
% bounds: structure containing prior bounds
% bayestopt_: structure storing information about priors
@ -17,7 +18,7 @@ function [posterior_sampler_options, options_, bayestopt_] = check_posterior_sam
% SPECIAL REQUIREMENTS
% none
% Copyright © 2015-2022 Dynare Team
% Copyright © 2015-2023 Dynare Team
%
% This file is part of Dynare.
%
@ -391,7 +392,7 @@ if ~strcmp(posterior_sampler_options.posterior_sampling_method,'slice')
end
if options_.load_mh_file && posterior_sampler_options.use_mh_covariance_matrix
[~, invhess] = compute_mh_covariance_matrix;
[~, invhess] = compute_mh_covariance_matrix(bayestopt_,fname,dname);
posterior_sampler_options.invhess = invhess;
end
@ -413,7 +414,7 @@ if strcmp(posterior_sampler_options.posterior_sampling_method,'slice')
error('check_posterior_sampler_options:: This error should not occur, please contact developers.')
end
% % % if options_.load_mh_file && options_.use_mh_covariance_matrix,
% % % [~, invhess] = compute_mh_covariance_matrix;
% % % [~, invhess] = compute_mh_covariance_matrix(bayestopt_,M_.fname,M_.dname));
% % % posterior_sampler_options.invhess = invhess;
% % % end
[V1, D]=eig(invhess);

View File

@ -1,10 +1,13 @@
function [posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix()
function [posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix(bayestopt_,fname,dname)
% function [posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix(bayestopt_,fname,dname)
% 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
% a file <M_.fname>_mh_mode.mat.
% a file <fname>_mh_mode.mat.
%
% INPUTS
% None.
% o bayestopt_ [struct] characterizing priors
% o fname [string] name of model
% o dname [string] name of directory with metropolis folder
%
% OUTPUTS
% o posterior_mean [double] (n*1) vector, posterior expectation of the parameters.
@ -31,14 +34,10 @@ function [posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
MetropolisFolder = CheckPath('metropolis',dname);
BaseName = [MetropolisFolder filesep fname];
global M_ bayestopt_
MetropolisFolder = CheckPath('metropolis',M_.dname);
ModelName = M_.fname;
BaseName = [MetropolisFolder filesep ModelName];
record=load_last_mh_history_file(MetropolisFolder, ModelName);
record=load_last_mh_history_file(MetropolisFolder, fname);
FirstMhFile = record.KeepedDraws.FirstMhFile;
FirstLine = record.KeepedDraws.FirstLine;
@ -71,4 +70,4 @@ hh = inv(posterior_covariance);
fval = posterior_kernel_at_the_mode;
parameter_names = bayestopt_.name;
save([M_.dname filesep 'Output' filesep M_.fname '_mh_mode.mat'],'xparam1','hh','fval','parameter_names');
save([dname filesep 'Output' filesep fname '_mh_mode.mat'],'xparam1','hh','fval','parameter_names');

View File

@ -463,7 +463,7 @@ if (any(bayestopt_.pshape >0 ) && options_.mh_replic) || ...
%get invhess in case of use_mh_covariance_matrix
posterior_sampler_options_temp = options_.posterior_sampler_options.current_options;
posterior_sampler_options_temp.invhess = invhess;
posterior_sampler_options_temp = check_posterior_sampler_options(posterior_sampler_options_temp, options_);
posterior_sampler_options_temp = check_posterior_sampler_options(posterior_sampler_options_temp, M_.fname, M_.dname, options_);
options = options_.mh_tune_jscale;
options.rwmh = options_.posterior_sampler_options.rwmh;
@ -481,7 +481,7 @@ if (any(bayestopt_.pshape >0 ) && options_.mh_replic) || ...
if options_.mh_replic || options_.load_mh_file
posterior_sampler_options = options_.posterior_sampler_options.current_options;
posterior_sampler_options.invhess = invhess;
[posterior_sampler_options, options_, bayestopt_] = check_posterior_sampler_options(posterior_sampler_options, options_, bounds, bayestopt_);
[posterior_sampler_options, options_, bayestopt_] = check_posterior_sampler_options(posterior_sampler_options, M_.fname, M_.dname, options_, bounds, bayestopt_);
% store current options in global
options_.posterior_sampler_options.current_options = posterior_sampler_options;
if options_.mh_replic
@ -494,7 +494,7 @@ if (any(bayestopt_.pshape >0 ) && options_.mh_replic) || ...
%% Here I discard first mh_drop percent of the draws:
CutSample(M_, options_, estim_params_);
if options_.mh_posterior_mode_estimation
[~,~,posterior_mode,~] = compute_mh_covariance_matrix();
[~,~,posterior_mode,~] = compute_mh_covariance_matrix(bayestopt_,M_.fname,M_.dname);
oo_=fill_mh_mode(posterior_mode',NaN(length(posterior_mode),1),M_,options_,estim_params_,bayestopt_,oo_,'posterior');
%reset qz_criterium
options_.qz_criterium=qz_criterium_old;

View File

@ -627,7 +627,7 @@ if options_.load_results_after_load_mh
end
if options_.mh_replic || options_.load_mh_file
[current_options, options_, bayestopt_] = check_posterior_sampler_options([], options_, bounds, bayestopt_);
[current_options, options_, bayestopt_] = check_posterior_sampler_options([], M_.fname, M_.dname, options_, bounds, bayestopt_);
options_.posterior_sampler_options.current_options = current_options;
end

View File

@ -48,7 +48,7 @@ TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
TODROP = floor(options_.mh_drop*TotalNumberOfMhDraws);
fprintf('Estimation::marginal density: I''m computing the posterior mean and covariance... ');
[posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix();
[posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix(bayestopt_,M_.fname,M_.dname);
MU = transpose(posterior_mean);
SIGMA = posterior_covariance;