Home > . > marginal_density.m

marginal_density

PURPOSE ^

stephane.adjemian@ens.fr [09-09-2005]

SYNOPSIS ^

function marginal = marginal_density()

DESCRIPTION ^

 stephane.adjemian@ens.fr [09-09-2005]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function marginal = marginal_density()
0002 % stephane.adjemian@ens.fr [09-09-2005]
0003 global M_ options_ estim_params_ oo_
0004 
0005 npar = estim_params_.np+estim_params_.nvn+estim_params_.ncx+estim_params_.ncn+estim_params_.nvx;
0006 nblck = options_.mh_nblck;
0007 
0008 MhDirectoryName = CheckPath('metropolis');
0009 load([ MhDirectoryName '/'  M_.fname '_mh_history'])
0010 
0011 FirstMhFile = record.KeepedDraws.FirstMhFile;
0012 FirstLine = record.KeepedDraws.FirstLine; ifil = FirstLine;
0013 TotalNumberOfMhFiles = sum(record.MhDraws(:,2));
0014 TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
0015 MAX_nruns = ceil(options_.MaxNumberOfBytes/(npar+2)/8);
0016 TODROP = floor(options_.mh_drop*TotalNumberOfMhDraws);
0017 
0018 MU = zeros(1,npar);
0019 SIGMA = zeros(npar,npar);
0020 lpost_mode = -Inf;
0021 
0022 fprintf('MH: I''m computing the posterior mean... ');
0023 for n = FirstMhFile:TotalNumberOfMhFiles
0024   for b = 1:nblck
0025     load([ MhDirectoryName '/' M_.fname '_mh' int2str(n) '_blck' int2str(b)],'x2','logpo2'); 
0026     MU = MU + sum(x2(ifil:end,:));
0027     lpost_mode = max(lpost_mode,max(logpo2));
0028   end
0029   ifil = 1;
0030 end
0031 MU = MU/((TotalNumberOfMhDraws-TODROP)*nblck);
0032 MU1 = repmat(MU,MAX_nruns,1);
0033 %% lpost_mode is the value of the log posterior kernel at the mode.
0034 fprintf(' Done!\n');
0035 fprintf('MH: I''m computing the posterior covariance matrix... ');
0036 ifil = FirstLine;
0037 for n = FirstMhFile:TotalNumberOfMhFiles
0038   for b = 1:nblck
0039     load([MhDirectoryName '/' M_.fname '_mh' int2str(n) '_blck' int2str(b)],'x2');
0040     x = x2(ifil:end,:)-MU1(1:size(x2(ifil:end,:),1),:);
0041     SIGMA = SIGMA + x'*x;
0042   end
0043   ifil = 1;
0044 end
0045 SIGMA =  SIGMA/((TotalNumberOfMhDraws-TODROP)*nblck);%<=== Variance of the parameters (ok!)
0046 fprintf(' Done!\n');
0047 disp(' ');
0048 disp('MH: I''m computing the posterior log marginale density (modified harmonic mean)... ');
0049 detSIGMA = det(SIGMA);
0050 invSIGMA = inv(SIGMA);
0051 marginal = zeros(9,2);
0052 linee = 0;
0053 check_coverage = 1;
0054 increase = 1;
0055 while check_coverage
0056   for p = 0.1:0.1:0.9;
0057     critval = qchisq(p,npar);
0058     ifil = FirstLine;
0059     tmp = 0;
0060     for n = FirstMhFile:TotalNumberOfMhFiles
0061       for b=1:nblck
0062     load([ MhDirectoryName '/' M_.fname '_mh' int2str(n) '_blck' int2str(b)],'x2','logpo2');
0063     EndOfFile = size(x2,1);
0064     for i = ifil:EndOfFile
0065       deviation  = (x2(i,:)-MU)*invSIGMA*(x2(i,:)-MU)';
0066       if deviation <= critval
0067         lftheta = -log(p)-(npar*log(2*pi)+log(detSIGMA)+deviation)/2;
0068         tmp = tmp + exp(lftheta - logpo2(i) + lpost_mode);
0069       end
0070     end
0071       end
0072       ifil = 1;
0073     end
0074     linee = linee + 1;
0075     warning off all
0076     marginal(linee,:) = [p, lpost_mode-log(tmp/((TotalNumberOfMhDraws-TODROP)*nblck))];
0077     warning on all
0078   end
0079   if abs((marginal(9,2)-marginal(1,2))/marginal(9,2)) > 0.01 | isinf(marginal(1,2))
0080     if increase == 1
0081       disp('MH: The support of the weighting density function is not large enough...')
0082       disp('MH: I increase the variance of this distribution.')
0083       increase = 1.2*increase;
0084       invSIGMA = inv(SIGMA*increase);
0085       detSIGMA = det(SIGMA*increase);
0086       linee    = 0;   
0087     else
0088       disp('MH: Let me try again.')
0089       increase = 1.2*increase;
0090       invSIGMA = inv(SIGMA*increase);
0091       detSIGMA = det(SIGMA*increase);
0092       linee    = 0;
0093       if increase > 20
0094     check_coverage = 0;
0095     clear invSIGMA detSIGMA increase;
0096     disp('MH: There''s probably a problem with the modified harmonic mean estimator.')    
0097       end
0098     end  
0099   else
0100     check_coverage = 0;
0101     clear invSIGMA detSIGMA increase;
0102     disp('MH: Modified harmonic mean estimator, done!')
0103   end
0104 end
0105 
0106 oo_.MarginalDensity.ModifiedHarmonicMean = mean(marginal(:,2));

Generated on Fri 16-Jun-2006 09:09:06 by m2html © 2003