Home > matlab > compute_mh_covariance_matrix.m

compute_mh_covariance_matrix

PURPOSE ^

Estimation of the posterior covariance matrix, posterior mean, posterior mode and evaluation of the posterior kernel at the

SYNOPSIS ^

function [posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix()

DESCRIPTION ^

 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. 
 
 INPUTS 
   None.
  
 OUTPUTS
   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.

 SPECIAL REQUIREMENTS
   None.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [posterior_mean,posterior_covariance,posterior_mode,posterior_kernel_at_the_mode] = compute_mh_covariance_matrix()
0002 % Estimation of the posterior covariance matrix, posterior mean, posterior mode and evaluation of the posterior kernel at the
0003 % estimated mode, using the draws from a metropolis-hastings. The estimated posterior mode and covariance matrix are saved in
0004 % a file <M_.fname>_mh_mode.mat.
0005 %
0006 % INPUTS
0007 %   None.
0008 %
0009 % OUTPUTS
0010 %   o  posterior_mean                [double]  (n*1) vector, posterior expectation of the parameters.
0011 %   o  posterior_covariance          [double]  (n*n) matrix, posterior covariance of the parameters (computed from previous metropolis hastings).
0012 %   o  posterior_mode                [double]  (n*1) vector, posterior mode of the parameters.
0013 %   o  posterior_kernel_at_the_mode  [double]  scalar.
0014 %
0015 % SPECIAL REQUIREMENTS
0016 %   None.
0017 
0018 % Copyright (C) 2006-2010 Dynare Team
0019 %
0020 % This file is part of Dynare.
0021 %
0022 % Dynare is free software: you can redistribute it and/or modify
0023 % it under the terms of the GNU General Public License as published by
0024 % the Free Software Foundation, either version 3 of the License, or
0025 % (at your option) any later version.
0026 %
0027 % Dynare is distributed in the hope that it will be useful,
0028 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0029 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0030 % GNU General Public License for more details.
0031 %
0032 % You should have received a copy of the GNU General Public License
0033 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0034 
0035 global M_ options_ estim_params_
0036 
0037 
0038 n = estim_params_.np + ...
0039     estim_params_.nvn+ ...
0040     estim_params_.ncx+ ...
0041     estim_params_.ncn+ ...
0042     estim_params_.nvx;
0043 nblck = options_.mh_nblck;
0044 
0045 MhDirectoryName = CheckPath('metropolis',M_.dname);
0046 load([ MhDirectoryName '/'  M_.fname '_mh_history.mat'])
0047 
0048 FirstMhFile = record.KeepedDraws.FirstMhFile;
0049 FirstLine   = record.KeepedDraws.FirstLine;
0050 TotalNumberOfMhFiles = sum(record.MhDraws(:,2));
0051 
0052 posterior_kernel_at_the_mode = -Inf;
0053 posterior_mean = zeros(n,1);
0054 posterior_mode = NaN(n,1);
0055 posterior_covariance = zeros(n,n);
0056 offset = 0;
0057 
0058 for b=1:nblck
0059     first_line = FirstLine;
0060     for n = FirstMhFile:TotalNumberOfMhFiles
0061         %for b = 1:nblck
0062         load([ MhDirectoryName '/' M_.fname '_mh' int2str(n) '_blck' int2str(b) '.mat'],'x2','logpo2'); 
0063         [tmp,idx] = max(logpo2);
0064         if tmp>posterior_kernel_at_the_mode
0065             posterior_kernel_at_the_mode = tmp;
0066             posterior_mode = x2(idx,:);
0067         end
0068         [posterior_mean,posterior_covariance,offset] = recursive_moments(posterior_mean,posterior_covariance,x2(first_line:end,:),offset);
0069         first_line = 1;
0070     end
0071 end
0072 
0073 xparam1 = posterior_mode';
0074 hh = inv(posterior_covariance);
0075 fval = posterior_kernel_at_the_mode;
0076 
0077 save([M_.fname '_mh_mode.mat'],'xparam1','hh','fval');

Generated on Mon 21-May-2012 02:42:43 by m2html © 2005