2008-09-18 18:28:57 +02:00
|
|
|
function [posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix()
|
2008-01-17 12:10:12 +01:00
|
|
|
|
|
|
|
% function [m0,s0] = compute_mh_covariance_matrix()
|
2007-05-19 18:54:39 +02:00
|
|
|
% Estimation of the posterior covariance matrix and expectation.
|
2006-07-28 12:00:31 +02:00
|
|
|
%
|
|
|
|
% INPUTS
|
|
|
|
% None.
|
|
|
|
%
|
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).
|
|
|
|
% o posterior_mode [double] (n*1) vector, posterior mode of the parameters.
|
|
|
|
% 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
|
|
|
|
|
|
|
% Copyright (C) 2006-2008 Dynare Team
|
|
|
|
%
|
|
|
|
% 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
|
|
|
|
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
2008-01-17 12:10:12 +01:00
|
|
|
|
2007-05-19 18:54:39 +02:00
|
|
|
global M_ options_ estim_params_
|
2006-07-28 12:00:31 +02:00
|
|
|
|
2008-09-18 18:28:57 +02:00
|
|
|
|
2007-05-19 18:54:39 +02:00
|
|
|
n = estim_params_.np + ...
|
2009-12-16 18:17:34 +01:00
|
|
|
estim_params_.nvn+ ...
|
|
|
|
estim_params_.ncx+ ...
|
|
|
|
estim_params_.ncn+ ...
|
|
|
|
estim_params_.nvx;
|
2006-07-28 12:00:31 +02:00
|
|
|
nblck = options_.mh_nblck;
|
|
|
|
|
|
|
|
MhDirectoryName = CheckPath('metropolis');
|
2008-06-24 20:20:48 +02:00
|
|
|
load([ MhDirectoryName '/' M_.fname '_mh_history.mat'])
|
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));
|
|
|
|
|
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
|
|
|
|
%for b = 1:nblck
|
|
|
|
load([ MhDirectoryName '/' M_.fname '_mh' int2str(n) '_blck' int2str(b) '.mat'],'x2','logpo2');
|
|
|
|
[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;
|
2007-05-19 18:54:39 +02:00
|
|
|
|
2008-06-24 20:20:48 +02:00
|
|
|
save([M_.fname '_mh_mode.mat'],'xparam1','hh','fval');
|