From 9a4e022eb790daf24ff10aeff0d323bb6d49003b Mon Sep 17 00:00:00 2001 From: Willi Mutschler Date: Thu, 14 Sep 2023 08:51:04 +0200 Subject: [PATCH] remove global from GetAllPosteriorDraws --- matlab/GetAllPosteriorDraws.m | 20 +++++++++---------- matlab/GetPosteriorParametersStatistics.m | 20 +++++++++---------- .../mcmc_diagnostics.m | 2 +- matlab/mh_autocorrelation_function.m | 2 +- matlab/prior_posterior_statistics.m | 4 ++-- matlab/trace_plot.m | 4 ++-- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/matlab/GetAllPosteriorDraws.m b/matlab/GetAllPosteriorDraws.m index f8cde8ef2..23222b40d 100644 --- a/matlab/GetAllPosteriorDraws.m +++ b/matlab/GetAllPosteriorDraws.m @@ -1,8 +1,10 @@ -function Draws = GetAllPosteriorDraws(column, FirstMhFile, FirstLine, TotalNumberOfMhFile, NumberOfDraws, nblcks, blck) -% function Draws = GetAllPosteriorDraws(column, FirstMhFile, FirstLine, TotalNumberOfMhFile, NumberOfDraws, nblcks, blck) +function Draws = GetAllPosteriorDraws(dname, fname, column, FirstMhFile, FirstLine, TotalNumberOfMhFile, NumberOfDraws, nblcks, blck) +% function Draws = GetAllPosteriorDraws(dname, fname, column, FirstMhFile, FirstLine, TotalNumberOfMhFile, NumberOfDraws, nblcks, blck) % Gets all posterior draws % % INPUTS +% dname: name of directory with results +% fname: name of mod file % column: column of desired parameter in draw matrix % FirstMhFile: first mh file % FirstLine: first line @@ -34,20 +36,18 @@ function Draws = GetAllPosteriorDraws(column, FirstMhFile, FirstLine, TotalNumbe % You should have received a copy of the GNU General Public License % along with Dynare. If not, see . -global M_ - iline = FirstLine; linee = 1; -DirectoryName = CheckPath('metropolis',M_.dname); +DirectoryName = CheckPath('metropolis',dname); -if nblcks>1 && nargin<7 +if nblcks>1 && nargin<9 Draws = zeros(NumberOfDraws*nblcks,1); iline0=iline; if column>0 for blck = 1:nblcks iline=iline0; for file = FirstMhFile:TotalNumberOfMhFile - load([DirectoryName '/' M_.fname '_mh' int2str(file) '_blck' int2str(blck)],'x2') + load([DirectoryName '/' fname '_mh' int2str(file) '_blck' int2str(blck)],'x2') NumberOfLines = size(x2(iline:end,:),1); Draws(linee:linee+NumberOfLines-1) = x2(iline:end,column); linee = linee+NumberOfLines; @@ -58,7 +58,7 @@ if nblcks>1 && nargin<7 for blck = 1:nblcks iline=iline0; for file = FirstMhFile:TotalNumberOfMhFile - load([DirectoryName '/' M_.fname '_mh' int2str(file) '_blck' int2str(blck)],'logpo2') + load([DirectoryName '/' fname '_mh' int2str(file) '_blck' int2str(blck)],'logpo2') NumberOfLines = size(logpo2(iline:end),1); Draws(linee:linee+NumberOfLines-1) = logpo2(iline:end); linee = linee+NumberOfLines; @@ -72,7 +72,7 @@ else end if column>0 for file = FirstMhFile:TotalNumberOfMhFile - load([DirectoryName '/' M_.fname '_mh' int2str(file) '_blck' int2str(blck)],'x2') + load([DirectoryName '/' fname '_mh' int2str(file) '_blck' int2str(blck)],'x2') NumberOfLines = size(x2(iline:end,:),1); Draws(linee:linee+NumberOfLines-1) = x2(iline:end,column); linee = linee+NumberOfLines; @@ -80,7 +80,7 @@ else end else for file = FirstMhFile:TotalNumberOfMhFile - load([DirectoryName '/' M_.fname '_mh' int2str(file) '_blck' int2str(blck)],'logpo2') + load([DirectoryName '/' fname '_mh' int2str(file) '_blck' int2str(blck)],'logpo2') NumberOfLines = size(logpo2(iline:end,:),1); Draws(linee:linee+NumberOfLines-1) = logpo2(iline:end); linee = linee+NumberOfLines; diff --git a/matlab/GetPosteriorParametersStatistics.m b/matlab/GetPosteriorParametersStatistics.m index 1bec62ce6..bd29b1f61 100644 --- a/matlab/GetPosteriorParametersStatistics.m +++ b/matlab/GetPosteriorParametersStatistics.m @@ -94,7 +94,7 @@ if np ip = nvx+nvn+ncx+ncn+1; for i=1:np if options_.mh_replic || (options_.load_mh_file && ~options_.load_results_after_load_mh) - Draws = GetAllPosteriorDraws(ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); [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); @@ -103,7 +103,7 @@ if np name = bayestopt_.name{ip}; [post_mean, hpd_interval, post_var] = Extractoo(oo_, name, type); catch - Draws = GetAllPosteriorDraws(ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); [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); @@ -137,7 +137,7 @@ if nvx disp(tit2) for i=1:nvx if options_.mh_replic || (options_.load_mh_file && ~options_.load_results_after_load_mh) - Draws = GetAllPosteriorDraws(ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); [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 = M_.exo_names{k}; @@ -149,7 +149,7 @@ if nvx name = M_.exo_names{k}; [post_mean, hpd_interval, post_var] = Extractoo(oo_, name, type); catch - Draws = GetAllPosteriorDraws(ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); [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); @@ -181,7 +181,7 @@ if nvn ip = nvx+1; for i=1:nvn if options_.mh_replic || (options_.load_mh_file && ~options_.load_results_after_load_mh) - Draws = GetAllPosteriorDraws(ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(Draws, 1, options_.mh_conf_sig); name = options_.varobs{estim_params_.nvn_observable_correspondence(i,1)}; oo_ = Filloo(oo_, name, type, post_mean, hpd_interval, post_median, post_var, post_deciles, density); @@ -190,7 +190,7 @@ if nvn name = options_.varobs{estim_params_.nvn_observable_correspondence(i,1)}; [post_mean,hpd_interval,post_var] = Extractoo(oo_,name,type); catch - Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws, mh_nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws, mh_nblck); [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(Draws,1,options_.mh_conf_sig); name = options_.varobs{estim_params_.nvn_observable_correspondence(i,1)}; oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density); @@ -220,7 +220,7 @@ if ncx ip = nvx+nvn+1; for i=1:ncx if options_.mh_replic || (options_.load_mh_file && ~options_.load_results_after_load_mh) - Draws = GetAllPosteriorDraws(ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); [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); @@ -237,7 +237,7 @@ if ncx NAME = sprintf('%s_%s', M_.exo_names{k1}, M_.exo_names{k2}); [post_mean,hpd_interval,post_var] = Extractoo(oo_, NAME, type); catch - Draws = GetAllPosteriorDraws(ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); [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); @@ -270,7 +270,7 @@ if ncn ip = nvx+nvn+ncx+1; for i=1:ncn if options_.mh_replic || (options_.load_mh_file && ~options_.load_results_after_load_mh) - Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws, mh_nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws, mh_nblck); [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); @@ -285,7 +285,7 @@ if ncn NAME = sprintf('%s_%s', M_.endo_names{k1}, M_.endo_names{k2}); [post_mean,hpd_interval,post_var] = Extractoo(oo_, NAME, type); catch - Draws = GetAllPosteriorDraws(ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, ip, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, mh_nblck); [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); diff --git a/matlab/convergence_diagnostics/mcmc_diagnostics.m b/matlab/convergence_diagnostics/mcmc_diagnostics.m index 93e19b0bc..518172c49 100644 --- a/matlab/convergence_diagnostics/mcmc_diagnostics.m +++ b/matlab/convergence_diagnostics/mcmc_diagnostics.m @@ -79,7 +79,7 @@ for jj = 1:npar par_name_temp = get_the_name(jj, options_.TeX, M_, estim_params_, options_); param_name = vertcat(param_name, par_name_temp); end - Draws = GetAllPosteriorDraws(jj, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, nblck); + Draws = GetAllPosteriorDraws(M_.dname, M_.fname, jj, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, nblck); Draws = reshape(Draws, [NumberOfDraws nblck]); Nc = min(1000, NumberOfDraws/2); for ll = 1:nblck diff --git a/matlab/mh_autocorrelation_function.m b/matlab/mh_autocorrelation_function.m index 8c07143bd..97295b1c7 100644 --- a/matlab/mh_autocorrelation_function.m +++ b/matlab/mh_autocorrelation_function.m @@ -60,7 +60,7 @@ nblck = size(record.LastParameters,1); clear record; % Get all the posterior draws: -PosteriorDraws = GetAllPosteriorDraws(column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, nblck, blck); +PosteriorDraws = GetAllPosteriorDraws(M_.dname, M_.fname, column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, nblck, blck); % Compute the autocorrelation function: [autocov,autocor] = sample_autocovariance(PosteriorDraws,options_.mh_autocorrelation_function_size); diff --git a/matlab/prior_posterior_statistics.m b/matlab/prior_posterior_statistics.m index 51ab6f02d..ffba5567b 100644 --- a/matlab/prior_posterior_statistics.m +++ b/matlab/prior_posterior_statistics.m @@ -218,9 +218,9 @@ if strcmpi(type,'posterior') mh_nblck = options_.mh_nblck; if B==NumberOfDraws*mh_nblck % we load all retained MH runs ! - logpost=GetAllPosteriorDraws(0, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, nblck); + logpost=GetAllPosteriorDraws(M_.dname, M_.fname, 0, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, nblck); for column=1:npar - x(:,column) = GetAllPosteriorDraws(column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, nblck); + x(:,column) = GetAllPosteriorDraws(M_.dname, M_.fname, column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, nblck); end else logpost=NaN(B,1); diff --git a/matlab/trace_plot.m b/matlab/trace_plot.m index 369f9adf5..c08943a66 100644 --- a/matlab/trace_plot.m +++ b/matlab/trace_plot.m @@ -67,7 +67,7 @@ n_nblocks_to_plot=length(blck); if n_nblocks_to_plot==1 % Get all the posterior draws: -PosteriorDraws = GetAllPosteriorDraws(column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, TotalNumberOfMhDraws, mh_nblck, blck); +PosteriorDraws = GetAllPosteriorDraws(M_.dname,M_.fname,column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, TotalNumberOfMhDraws, mh_nblck, blck); else PosteriorDraws=NaN(TotalNumberOfMhDraws,n_nblocks_to_plot); save_string=''; @@ -75,7 +75,7 @@ else title_string_tex=''; end for block_iter=1:n_nblocks_to_plot - PosteriorDraws(:,block_iter) = GetAllPosteriorDraws(column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, TotalNumberOfMhDraws, mh_nblck, blck(block_iter)); + PosteriorDraws(:,block_iter) = GetAllPosteriorDraws(M_.dname, M_.fname, column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, TotalNumberOfMhDraws, mh_nblck, blck(block_iter)); save_string=[save_string,'_',num2str(blck(block_iter))]; if options_.TeX title_string_tex=[title_string_tex, ', ' num2str(blck(block_iter))];