2015-09-25 18:23:21 +02:00
|
|
|
function [marginal,oo_] = marginal_density(M_, options_, estim_params_, oo_, bayestopt_)
|
2008-01-17 15:53:48 +01:00
|
|
|
% function marginal = marginal_density()
|
|
|
|
% Computes the marginal density
|
|
|
|
%
|
|
|
|
% INPUTS
|
2008-09-03 18:05:35 +02:00
|
|
|
% options_ [structure]
|
|
|
|
% estim_params_ [structure]
|
|
|
|
% M_ [structure]
|
|
|
|
% oo_ [structure]
|
2008-01-17 15:53:48 +01:00
|
|
|
%
|
|
|
|
% OUTPUTS
|
2008-09-03 18:05:35 +02:00
|
|
|
% marginal: [double] marginal density (modified harmonic mean)
|
|
|
|
% oo_ [structure]
|
2008-01-17 15:53:48 +01:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% none
|
|
|
|
|
2022-04-13 13:15:19 +02:00
|
|
|
% Copyright © 2005-2018 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/>.
|
2008-01-17 15:53:48 +01:00
|
|
|
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2005-09-10 12:04:22 +02:00
|
|
|
npar = estim_params_.np+estim_params_.nvn+estim_params_.ncx+estim_params_.ncn+estim_params_.nvx;
|
|
|
|
nblck = options_.mh_nblck;
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2013-11-20 18:03:12 +01:00
|
|
|
MetropolisFolder = CheckPath('metropolis',M_.dname);
|
|
|
|
ModelName = M_.fname;
|
|
|
|
BaseName = [MetropolisFolder filesep ModelName];
|
|
|
|
|
|
|
|
load_last_mh_history_file(MetropolisFolder, ModelName);
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2005-09-10 12:04:22 +02:00
|
|
|
FirstMhFile = record.KeepedDraws.FirstMhFile;
|
|
|
|
FirstLine = record.KeepedDraws.FirstLine; ifil = FirstLine;
|
|
|
|
TotalNumberOfMhFiles = sum(record.MhDraws(:,2));
|
|
|
|
TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
|
|
|
|
MAX_nruns = ceil(options_.MaxNumberOfBytes/(npar+2)/8);
|
2005-10-01 21:07:57 +02:00
|
|
|
TODROP = floor(options_.mh_drop*TotalNumberOfMhDraws);
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2013-11-20 18:03:12 +01:00
|
|
|
fprintf('Estimation::marginal density: I''m computing the posterior mean and covariance... ');
|
2008-09-18 18:28:57 +02:00
|
|
|
[posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix();
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2008-09-18 18:28:57 +02:00
|
|
|
MU = transpose(posterior_mean);
|
|
|
|
SIGMA = posterior_covariance;
|
|
|
|
lpost_mode = posterior_kernel_at_the_mode;
|
|
|
|
xparam1 = posterior_mean;
|
2006-09-13 14:39:23 +02:00
|
|
|
hh = inv(SIGMA);
|
2005-09-10 12:04:22 +02:00
|
|
|
fprintf(' Done!\n');
|
2017-02-03 12:59:34 +01:00
|
|
|
if ~isfield(oo_,'posterior_mode') || (options_.mh_replic && isequal(options_.posterior_sampler_options.posterior_sampling_method,'slice'))
|
2015-09-25 18:23:21 +02:00
|
|
|
oo_=fill_mh_mode(posterior_mode',NaN(npar,1),M_,options_,estim_params_,bayestopt_,oo_,'posterior');
|
|
|
|
end
|
2008-09-18 18:28:57 +02:00
|
|
|
|
2013-11-20 18:03:12 +01:00
|
|
|
% save the posterior mean and the inverse of the covariance matrix
|
|
|
|
% (usefull if the user wants to perform some computations using
|
2017-05-16 15:10:20 +02:00
|
|
|
% the posterior mean instead of the posterior mode ==> ).
|
2017-02-20 15:15:39 +01:00
|
|
|
parameter_names = bayestopt_.name;
|
2021-07-22 17:46:08 +02:00
|
|
|
save([M_.dname filesep 'Output' filesep M_.fname '_mean.mat'],'xparam1','hh','parameter_names','SIGMA');
|
2013-11-20 18:03:12 +01:00
|
|
|
|
|
|
|
fprintf('Estimation::marginal density: I''m computing the posterior log marginal density (modified harmonic mean)... ');
|
2022-01-25 10:27:47 +01:00
|
|
|
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.');
|
|
|
|
end
|
2015-07-23 18:18:31 +02:00
|
|
|
invSIGMA = hh;
|
2005-09-10 12:04:22 +02:00
|
|
|
marginal = zeros(9,2);
|
|
|
|
linee = 0;
|
|
|
|
check_coverage = 1;
|
|
|
|
increase = 1;
|
|
|
|
while check_coverage
|
2017-05-16 12:42:01 +02:00
|
|
|
for p = 0.1:0.1:0.9
|
2009-12-16 18:17:34 +01:00
|
|
|
critval = chi2inv(p,npar);
|
|
|
|
ifil = FirstLine;
|
|
|
|
tmp = 0;
|
|
|
|
for n = FirstMhFile:TotalNumberOfMhFiles
|
|
|
|
for b=1:nblck
|
2013-11-20 18:03:12 +01:00
|
|
|
load([ BaseName '_mh' int2str(n) '_blck' int2str(b) '.mat'],'x2','logpo2');
|
2009-12-16 18:17:34 +01:00
|
|
|
EndOfFile = size(x2,1);
|
|
|
|
for i = ifil:EndOfFile
|
2015-07-23 17:41:23 +02:00
|
|
|
deviation = ((x2(i,:)-MU)*invSIGMA*(x2(i,:)-MU)')/increase;
|
2009-12-16 18:17:34 +01:00
|
|
|
if deviation <= critval
|
2015-07-23 18:13:15 +02:00
|
|
|
lftheta = -log(p)-(npar*log(2*pi)+(npar*log(increase)+logdetSIGMA)+deviation)/2;
|
2009-12-16 18:17:34 +01:00
|
|
|
tmp = tmp + exp(lftheta - logpo2(i) + lpost_mode);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ifil = 1;
|
|
|
|
end
|
|
|
|
linee = linee + 1;
|
|
|
|
warning_old_state = warning;
|
|
|
|
warning off;
|
|
|
|
marginal(linee,:) = [p, lpost_mode-log(tmp/((TotalNumberOfMhDraws-TODROP)*nblck))];
|
|
|
|
warning(warning_old_state);
|
2005-09-10 12:04:22 +02:00
|
|
|
end
|
2019-12-12 11:49:57 +01:00
|
|
|
if abs((marginal(9,2)-marginal(1,2))/marginal(9,2)) > options_.marginal_data_density.harmonic_mean.tolerance || isinf(marginal(1,2))
|
2013-11-20 18:03:12 +01:00
|
|
|
fprintf('\n')
|
2009-12-16 18:17:34 +01:00
|
|
|
if increase == 1
|
2013-11-20 18:03:12 +01:00
|
|
|
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.')
|
2009-12-16 18:17:34 +01:00
|
|
|
increase = 1.2*increase;
|
2017-05-16 15:10:20 +02:00
|
|
|
linee = 0;
|
2009-12-16 18:17:34 +01:00
|
|
|
else
|
2013-11-20 18:03:12 +01:00
|
|
|
disp('Estimation::marginal density: Let me try again.')
|
2009-12-16 18:17:34 +01:00
|
|
|
increase = 1.2*increase;
|
|
|
|
linee = 0;
|
|
|
|
if increase > 20
|
|
|
|
check_coverage = 0;
|
|
|
|
clear invSIGMA detSIGMA increase;
|
2017-05-16 15:10:20 +02:00
|
|
|
disp('Estimation::marginal density: There''s probably a problem with the modified harmonic mean estimator.')
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
end
|
2005-09-10 12:04:22 +02:00
|
|
|
else
|
2009-12-16 18:17:34 +01:00
|
|
|
check_coverage = 0;
|
|
|
|
clear invSIGMA detSIGMA increase;
|
2013-11-20 18:03:12 +01:00
|
|
|
fprintf('Done!\n')
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2005-09-10 12:04:22 +02:00
|
|
|
end
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2015-09-25 18:23:21 +02:00
|
|
|
oo_.MarginalDensity.ModifiedHarmonicMean = mean(marginal(:,2));
|
|
|
|
|