estimation: more flexible handling of displayed string in command window

Also removed estim_params_ as unused input argutment to CutSample
kalman-mex
Willi Mutschler 2023-09-13 18:09:38 +02:00
parent 9a4e022eb7
commit a41fbdefbc
No known key found for this signature in database
GPG Key ID: 91E724BF17A73F6D
12 changed files with 50 additions and 51 deletions

View File

@ -1,5 +1,5 @@
function CutSample(M_, options_, estim_params_)
% function CutSample(M_, options_, estim_params_)
function CutSample(M_, options_, dispString)
% function CutSample(M_, options_, dispString)
% Takes a subset from metropolis draws by storing the required information
% like the first MH-file to be loaded and the first line in that file to be
% loaded into the record structure saved on harddisk into the
@ -8,7 +8,7 @@ function CutSample(M_, options_, estim_params_)
% INPUTS
% M_ [structure] Dynare model structure
% options_ [structure] Dynare options structure
% estim_params_ [structure] Parameter structure
% dispString [string] String to be displayed in the command window
%
% OUTPUTS
% none
@ -33,8 +33,6 @@ function CutSample(M_, options_, estim_params_)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
dispString = 'Estimation::mcmc';
% Get the path to the metropolis files.
MetropolisFolder = CheckPath('metropolis',M_.dname);

View File

@ -1,9 +1,10 @@
function PosteriorIRF(type)
function PosteriorIRF(type,dispString)
% Builds posterior IRFs after the MH algorithm.
%
% INPUTS
% o type [char] string specifying the joint density of the
% deep parameters ('prior','posterior').
% o dispString [char] string to display in the console.
%
% OUTPUTS
% None (oo_ and plots).
@ -36,8 +37,6 @@ function PosteriorIRF(type)
global options_ estim_params_ oo_ M_ bayestopt_ dataset_ dataset_info
dispString = 'Estimation::mcmc';
% Set the number of periods
if isempty(options_.irf) || ~options_.irf
options_.irf = 40;

View File

@ -403,12 +403,12 @@ if (any(bayestopt_.pshape >0 ) && options_.mh_replic) || ...
if options_.mh_replic
ana_deriv_old = options_.analytic_derivation;
options_.analytic_derivation = 0;
posterior_sampler(objective_function,posterior_sampler_options.proposal_distribution,xparam1,posterior_sampler_options,bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_);
posterior_sampler(objective_function,posterior_sampler_options.proposal_distribution,xparam1,posterior_sampler_options,bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_,dispString);
options_.analytic_derivation = ana_deriv_old;
end
end
%% Here I discard first mh_drop percent of the draws:
CutSample(M_, options_, estim_params_);
CutSample(M_, options_, dispString);
if options_.mh_posterior_mode_estimation
[~,~,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');
@ -467,7 +467,7 @@ if (any(bayestopt_.pshape >0 ) && options_.mh_replic) || ...
if error_flag
error('%s: I cannot compute the posterior IRFs!',dispString)
end
PosteriorIRF('posterior');
PosteriorIRF('posterior',dispString);
end
if options_.moments_varendo
if error_flag
@ -499,7 +499,7 @@ if (any(bayestopt_.pshape >0 ) && options_.mh_replic) || ...
error('%s: I cannot compute the posterior statistics!',dispString)
end
if options_.order==1 && ~options_.particle.status
prior_posterior_statistics('posterior',dataset_,dataset_info); %get smoothed and filtered objects and forecasts
prior_posterior_statistics('posterior',dataset_,dataset_info,dispString); %get smoothed and filtered objects and forecasts
else
error('%s: Particle Smoothers are not yet implemented.',dispString)
end

View File

@ -410,9 +410,9 @@ if options_gsa.rmse
end
end
prior_posterior_statistics('gsa',dataset_, dataset_info);
prior_posterior_statistics('gsa',dataset_, dataset_info,'gsa::mcmc');
if options_.bayesian_irf
PosteriorIRF('gsa');
PosteriorIRF('gsa','gsa::mcmc');
end
options_gsa.load_rmse=0;
% else

View File

@ -52,7 +52,7 @@ n_draws=options_.sampling_draws;
if strcmpi(type,'posterior')
% Get informations about the _posterior_draws files.
% discard first mh_drop percent of the draws:
CutSample(M_, options_, estim_params_);
CutSample(M_, options_, 'prior_posterior_function');
% initialize metropolis draws
options_.sub_draws = n_draws; % set draws for sampling; changed value is not returned to base workspace
[error_flag, ~, options_] = metropolis_draw(1, options_, estim_params_, M_);

View File

@ -69,14 +69,14 @@ end
parameter_names = bayestopt_.name;
save([M_.dname filesep outputFolderName filesep M_.fname '_mean.mat'],'xparam1','hh','parameter_names','SIGMA');
fprintf('Estimation::marginal density: I''m computing the posterior log marginal density (modified harmonic mean)... ');
fprintf('marginal density: I''m computing the posterior log marginal density (modified harmonic mean)... ');
try
% use this robust option to avoid inf/nan
logdetSIGMA = 2*sum(log(diag(chol(SIGMA))));
catch
% in case SIGMA is not positive definite
logdetSIGMA = nan;
fprintf('Estimation::marginal density: the covariance of MCMC draws is not positive definite. You may have too few MCMC draws.');
fprintf('marginal density: the covariance of MCMC draws is not positive definite. You may have too few MCMC draws.');
end
invSIGMA = hh;
marginal = zeros(9,2);
@ -111,18 +111,18 @@ while check_coverage
if abs((marginal(9,2)-marginal(1,2))/marginal(9,2)) > options_.marginal_data_density.harmonic_mean.tolerance || isinf(marginal(1,2))
fprintf('\n')
if increase == 1
disp('Estimation::marginal density: The support of the weighting density function is not large enough...')
disp('Estimation::marginal density: I increase the variance of this distribution.')
disp('marginal density: The support of the weighting density function is not large enough...')
disp('marginal density: I increase the variance of this distribution.')
increase = 1.2*increase;
linee = 0;
else
disp('Estimation::marginal density: Let me try again.')
disp('marginal density: Let me try again.')
increase = 1.2*increase;
linee = 0;
if increase > 20
check_coverage = 0;
clear invSIGMA detSIGMA increase;
disp('Estimation::marginal density: There''s probably a problem with the modified harmonic mean estimator.')
disp('marginal density: There''s probably a problem with the modified harmonic mean estimator.')
end
end
else

View File

@ -1,4 +1,4 @@
function pm3(n1,n2,ifil,B,tit1,tit2,tit3,tit_tex,names1,names2,name3,DirectoryName,var_type)
function pm3(n1,n2,ifil,B,tit1,tit2,tit3,tit_tex,names1,names2,name3,DirectoryName,var_type,dispString)
% Computes, stores and plots the posterior moment statistics.
%
@ -19,6 +19,7 @@ function pm3(n1,n2,ifil,B,tit1,tit2,tit3,tit_tex,names1,names2,name3,DirectoryNa
% where to read
% var_type [string] suffix of the filename from which to load moment
% matrix
% dispString [string] string to be displayes in the command window
% PARALLEL CONTEXT
% See also the comment in posterior_sampler.m funtion.
@ -43,8 +44,6 @@ function pm3(n1,n2,ifil,B,tit1,tit2,tit3,tit_tex,names1,names2,name3,DirectoryNa
global options_ M_ oo_
dispString = 'Estimation::mcmc';
nn = 3;
MaxNumberOfPlotsPerFigure = nn^2; % must be square
varlist = names2;

View File

@ -1,5 +1,5 @@
function posterior_sampler(TargetFun,ProposalFun,xparam1,sampler_options,mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_)
% function posterior_sampler(TargetFun,ProposalFun,xparam1,sampler_options,mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_)
function posterior_sampler(TargetFun,ProposalFun,xparam1,sampler_options,mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_,dispString)
% function posterior_sampler(TargetFun,ProposalFun,xparam1,sampler_options,mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_,dispString)
% Random Walk Metropolis-Hastings algorithm.
%
% INPUTS
@ -17,6 +17,7 @@ function posterior_sampler(TargetFun,ProposalFun,xparam1,sampler_options,mh_boun
% o estim_params_ [structure] estimated parameters structure
% o bayestopt_ [structure] prior specification structure
% o oo_ [structure] output structure
% o dispString [string] string prependening the messages printed to the command window
%
% SPECIAL REQUIREMENTS
% None.
@ -53,12 +54,10 @@ function posterior_sampler(TargetFun,ProposalFun,xparam1,sampler_options,mh_boun
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
dispString = 'Estimation::mcmc';
vv = sampler_options.invhess;
% Initialization of the sampler
[ ix2, ilogpo2, ModelName, MetropolisFolder, fblck, fline, npar, nblck, nruns, NewFile, MAX_nruns, d, bayestopt_] = ...
posterior_sampler_initialization(TargetFun, xparam1, vv, mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_);
posterior_sampler_initialization(TargetFun, xparam1, vv, mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_, dispString);
InitSizeArray = min([repmat(MAX_nruns,nblck,1) fline+nruns-1],[],2);

View File

@ -1,7 +1,7 @@
function [ ix2, ilogpo2, ModelName, MetropolisFolder, FirstBlock, FirstLine, npar, NumberOfBlocks, nruns, NewFile, MAX_nruns, d, bayestopt_] = ...
posterior_sampler_initialization(TargetFun, xparam1, vv, mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_)
posterior_sampler_initialization(TargetFun, xparam1, vv, mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_, dispString)
% function [ ix2, ilogpo2, ModelName, MetropolisFolder, FirstBlock, FirstLine, npar, NumberOfBlocks, nruns, NewFile, MAX_nruns, d, bayestopt_] = ...
% metropolis_hastings_initialization(TargetFun, xparam1, vv, mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_)
% metropolis_hastings_initialization(TargetFun, xparam1, vv, mh_bounds,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,oo_, dispString)
% Metropolis-Hastings initialization.
%
% INPUTS
@ -17,6 +17,7 @@ function [ ix2, ilogpo2, ModelName, MetropolisFolder, FirstBlock, FirstLine, npa
% o estim_params_ estimated parameters structure
% o bayestopt_ estimation options structure
% o oo_ outputs structure
% o dispString string to be displayed in the command window
%
% OUTPUTS
% o ix2 [double] (NumberOfBlocks*npar) vector of starting points for different chains
@ -55,8 +56,6 @@ function [ ix2, ilogpo2, ModelName, MetropolisFolder, FirstBlock, FirstLine, npa
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
dispString = 'Estimation::mcmc';
%Initialize outputs
ix2 = [];
ilogpo2 = [];

View File

@ -26,7 +26,7 @@ function [par, logpost, accepted, neval] = posterior_sampler_iteration(TargetFu
% SPECIAL REQUIREMENTS
% none
% Copyright © 2015-18 Dynare Team
% Copyright © 2015-2023 Dynare Team
%
% This file is part of Dynare.
%

View File

@ -1,5 +1,5 @@
function prior_posterior_statistics(type,dataset,dataset_info)
% function prior_posterior_statistics(type,dataset,dataset_info)
function prior_posterior_statistics(type,dataset,dataset_info,dispString)
% function prior_posterior_statistics(type,dataset,dataset_info,dispString)
% Computes Monte Carlo filter smoother and forecasts
%
% INPUTS
@ -8,6 +8,7 @@ function prior_posterior_statistics(type,dataset,dataset_info)
% gsa
% dataset: data structure
% dataset_info: dataset structure
% dispString: string to display in the command window
%
% OUTPUTS
% none
@ -36,6 +37,10 @@ function prior_posterior_statistics(type,dataset,dataset_info)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
if nargin < 4
dispString = 'prior_posterior_statistics';
end
global options_ estim_params_ oo_ M_ bayestopt_
localVars=[];
@ -327,27 +332,27 @@ end
if options_.smoother
pm3(endo_nbr,gend,ifil(1),B,'Smoothed variables',...
'',varlist, M_.endo_names_tex,M_.endo_names,...
varlist,'SmoothedVariables',DirectoryName,'_smooth');
varlist,'SmoothedVariables',DirectoryName,'_smooth',dispString);
pm3(exo_nbr,gend,ifil(2),B,'Smoothed shocks',...
'',M_.exo_names,M_.exo_names_tex,M_.exo_names,...
M_.exo_names,'SmoothedShocks',DirectoryName,'_inno');
M_.exo_names,'SmoothedShocks',DirectoryName,'_inno',dispString);
pm3(endo_nbr,1,ifil(9),B,'Trend_coefficients',...
'',varlist,M_.endo_names_tex,M_.endo_names,...
varlist,'TrendCoeff',DirectoryName,'_trend_coeff');
varlist,'TrendCoeff',DirectoryName,'_trend_coeff',dispString);
pm3(endo_nbr,gend,ifil(10),B,'Smoothed constant',...
'',varlist,M_.endo_names_tex,M_.endo_names,...
varlist,'Constant',DirectoryName,'_smoothed_constant');
varlist,'Constant',DirectoryName,'_smoothed_constant',dispString);
pm3(endo_nbr,gend,ifil(11),B,'Smoothed trend',...
'',varlist,M_.endo_names_tex,M_.endo_names,...
varlist,'Trend',DirectoryName,'_smoothed_trend');
varlist,'Trend',DirectoryName,'_smoothed_trend',dispString);
pm3(endo_nbr,gend,ifil(1),B,'Updated Variables',...
'',varlist,M_.endo_names_tex,M_.endo_names,...
varlist,'UpdatedVariables',DirectoryName, ...
'_update');
'_update',dispString);
if smoothed_state_uncertainty
pm3(endo_nbr,endo_nbr,ifil(13),B,'State Uncertainty',...
'',varlist,M_.endo_names_tex,M_.endo_names,...
varlist,'StateUncertainty',DirectoryName,'_state_uncert');
varlist,'StateUncertainty',DirectoryName,'_state_uncert',dispString);
end
if nvn
@ -357,23 +362,23 @@ if options_.smoother
end
pm3(meas_err_nbr,gend,ifil(3),B,'Smoothed measurement errors',...
'',meas_error_names,texnames,meas_error_names,...
meas_error_names,'SmoothedMeasurementErrors',DirectoryName,'_error')
meas_error_names,'SmoothedMeasurementErrors',DirectoryName,'_error',dispString)
end
end
if options_.filtered_vars
pm3(endo_nbr,gend,ifil(4),B,'One step ahead forecast (filtered variables)',...
'',varlist,M_.endo_names_tex,M_.endo_names,...
varlist,'FilteredVariables',DirectoryName,'_filter_step_ahead');
varlist,'FilteredVariables',DirectoryName,'_filter_step_ahead',dispString);
end
if options_.forecast
pm3(endo_nbr,horizon,ifil(6),B,'Forecasted variables (mean)',...
'',varlist,M_.endo_names_tex,M_.endo_names,...
varlist,'MeanForecast',DirectoryName,'_forc_mean');
varlist,'MeanForecast',DirectoryName,'_forc_mean',dispString);
pm3(endo_nbr,horizon,ifil(7),B,'Forecasted variables (point)',...
'',varlist,M_.endo_names_tex,M_.endo_names,...
varlist,'PointForecast',DirectoryName,'_forc_point');
varlist,'PointForecast',DirectoryName,'_forc_point',dispString);
if ~isequal(M_.H,0) && ~isempty(intersect(options_.varobs,varlist))
texnames = cell(length(options_.varobs), 1);
obs_names = cell(length(options_.varobs), 1);
@ -384,14 +389,14 @@ if options_.forecast
varlist_forecast_ME=intersect(options_.varobs,varlist);
pm3(meas_err_nbr,horizon,ifil(12),B,'Forecasted variables (point) with ME',...
'',varlist_forecast_ME,texnames,obs_names,...
varlist_forecast_ME,'PointForecastME',DirectoryName,'_forc_point_ME')
varlist_forecast_ME,'PointForecastME',DirectoryName,'_forc_point_ME',dispString)
end
end
if options_.filter_covariance
pm3(endo_nbr,endo_nbr,ifil(8),B,'Filtered covariances',...
'',varlist,M_.endo_names_tex,M_.endo_names,...
varlist,'FilterCovariance',DirectoryName,'_filter_covar');
varlist,'FilterCovariance',DirectoryName,'_filter_covar',dispString);
end

View File

@ -7,7 +7,7 @@ function update_last_mh_history_file(MetropolisFolder, ModelName, record)
% record [structure] structure storing the MH history
% Outputs: none
% Copyright © 2013-2017 Dynare Team
% Copyright © 2013-2023 Dynare Team
%
% This file is part of Dynare.
%
@ -31,7 +31,7 @@ mh_history_files = dir([BaseName '_mh_history_*.mat']);
% Check the existence of mh-files (assuming version 2, ie dynare version greater than 4.3.x).
if isequal(length(mh_history_files),0)
error(['Estimation::update_mh_file: I cannot find any mh-history file in ' MetropolisFolder '!'])
error(['update_last_mh_history_file: I cannot find any mh-history file in ' MetropolisFolder '!'])
end
BaseName = [BaseName '_mh_history_'];