Home > . > McMCDiagnostics.m

McMCDiagnostics

PURPOSE ^

stephane.adjemian@ens.fr [06-04-2005]

SYNOPSIS ^

function McmcDiagnostic

DESCRIPTION ^

 stephane.adjemian@ens.fr [06-04-2005]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function McmcDiagnostic
0002 % stephane.adjemian@ens.fr [06-04-2005]
0003 
0004 global options_ estim_params_ M_ 
0005 
0006 DirectoryName = CheckPath('Output');
0007 MhDirectoryName = CheckPath('metropolis');
0008 
0009 TeX = options_.TeX;
0010 nblck = options_.mh_nblck;
0011 % Brooks and Gelman tests need more than one block
0012 if nblck == 1
0013   return;
0014 end
0015 npar = estim_params_.nvx;
0016 npar = npar + estim_params_.nvn;
0017 npar = npar + estim_params_.ncx;
0018 npar = npar + estim_params_.ncn;
0019 npar = npar + estim_params_.np ;
0020 MAX_nruns = ceil(options_.MaxNumberOfBytes/(npar+2)/8);
0021 
0022 load([MhDirectoryName '/'  M_.fname '_mh_history.mat'])
0023 
0024 mcfiles = [];
0025 for blck = 1:nblck
0026   mcfiles = cat(3,mcfiles,dir([MhDirectoryName '/' M_.fname '_mh*_blck' int2str(blck) '.mat']));
0027 end
0028 NumberOfMcFilesPerBlock = size(mcfiles,1);
0029 
0030   
0031 PastDraws = sum(record.MhDraws,1);
0032 LastFileNumber = PastDraws(2);
0033 LastLineNumber = record.MhDraws(end,3);
0034 NumberOfDraws  = PastDraws(1);
0035 
0036 Origin = 1000;
0037 StepSize = ceil((NumberOfDraws-Origin)/100);% So that the computational time does not
0038 ALPHA = 0.2;                    % increase too much with the number of simulations.
0039 time = 1:NumberOfDraws;
0040 xx = Origin:StepSize:NumberOfDraws;
0041 NumberOfLines = length(xx);
0042 tmp = zeros(NumberOfDraws*nblck,3);
0043 UDIAG = zeros(NumberOfLines,6,npar);
0044 
0045 if NumberOfDraws < Origin
0046     disp('MCMC Diagnostics :: The number of simulations is to small to compute the MCMC convergence diagnostics.')
0047     return
0048 end
0049 
0050 if TeX
0051   fidTeX = fopen([DirectoryName '/' M_.fname '_UnivariateDiagnostics.TeX'],'w');
0052   fprintf(fidTeX,'%% TeX eps-loader file generated by McmcDiagnostics.m (Dynare).\n');
0053   fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
0054   fprintf(fidTeX,' \n');
0055 end
0056 
0057 
0058 disp('MCMC Diagnostics: Univariate convergence diagnostic, Brooks and Gelman (1998):')
0059 for j=1:npar
0060   fprintf('    Parameter %d...  ',j);
0061   for b = 1:nblck
0062     startline = 0;
0063     for n = 1:NumberOfMcFilesPerBlock-1
0064       %load([MhDirectoryName '/' mcfiles(n,1,b).name],'x2');
0065       load([MhDirectoryName '/' M_.fname '_mh',int2str(n),'_blck' int2str(b) '.mat'],'x2');
0066       tmp((b-1)*NumberOfDraws+startline+1:(b-1)*NumberOfDraws+MAX_nruns*n,1) = x2(:,j);
0067       clear x2;
0068       startline = startline + MAX_nruns;
0069     end
0070     %load([MhDirectoryName '/' mcfiles(NumberOfMcFilesPerBlock,1,b).name],'x2');
0071     load([MhDirectoryName '/' M_.fname '_mh',int2str(NumberOfMcFilesPerBlock),'_blck' int2str(b) '.mat'],'x2');
0072     tmp((b-1)*NumberOfDraws+startline+1:(b-1)*NumberOfDraws+MAX_nruns*(LastFileNumber-1)+LastLineNumber,1) = x2(:,j);
0073     clear x2;
0074     startline = startline + LastLineNumber;
0075   end
0076   tmp(:,2) = kron(transpose(1:nblck),ones(NumberOfDraws,1));
0077   tmp(:,3) = kron(ones(nblck,1),time'); 
0078   tmp = sortrows(tmp,1);
0079   ligne   = 0;
0080   for iter  = Origin:StepSize:NumberOfDraws
0081     ligne = ligne+1;
0082     linea = ceil(0.5*iter);
0083     n     = iter-linea+1;
0084     cinf  = round(n*ALPHA/2);
0085     csup  = round(n*(1-ALPHA/2));
0086     CINF  = round(nblck*n*ALPHA/2);
0087     CSUP  = round(nblck*n*(1-ALPHA/2));
0088     temp  = tmp(find((tmp(:,3)>=linea) & (tmp(:,3)<=iter)),1:2);
0089     UDIAG(ligne,1,j) = temp(CSUP,1)-temp(CINF,1);
0090     moyenne = mean(temp(:,1));%% Pooled mean.
0091     UDIAG(ligne,3,j) = sum((temp(:,1)-moyenne).^2)/(nblck*n-1);
0092     UDIAG(ligne,5,j) = sum(abs(temp(:,1)-moyenne).^3)/(nblck*n-1);
0093     for i=1:nblck
0094       pmet = temp(find(temp(:,2)==i));
0095       UDIAG(ligne,2,j) = UDIAG(ligne,2,j) + pmet(csup,1)-pmet(cinf,1);
0096       moyenne = mean(pmet,1); %% Within mean.
0097       UDIAG(ligne,4,j) = UDIAG(ligne,4,j) + sum((pmet(:,1)-moyenne).^2)/(n-1);
0098       UDIAG(ligne,6,j) = UDIAG(ligne,6,j) + sum(abs(pmet(:,1)-moyenne).^3)/(n-1);
0099     end
0100   end
0101   fprintf('Done! \n');
0102 end
0103 UDIAG(:,[2 4 6],:) = UDIAG(:,[2 4 6],:)/nblck;
0104 disp(' ')
0105 clear pmet temp moyenne CSUP CINF csup cinf n linea iter tmp;    
0106 pages = floor(npar/3);
0107 k = 0;  
0108 for i = 1:pages
0109   if options_.nograph
0110     h = figure('Name','MCMC univariate diagnostic (Brooks and Gelman,1998)','Visible','off');
0111   else
0112     h = figure('Name','MCMC univariate diagnostic (Brooks and Gelman,1998)');
0113   end    
0114   boxplot = 1;
0115   if TeX
0116     NAMES = [];
0117     TEXNAMES = [];
0118   end
0119   for j = 1:3 % Loop over parameters
0120     k = k+1;
0121     [nam,namtex] = get_the_name(k,TeX);
0122     for crit = 1:3% Loop over criteria
0123       if crit == 1
0124         plt1 = UDIAG(:,1,k);
0125         plt2 = UDIAG(:,2,k);
0126         namnam  = [nam , ' (Interval)']; 
0127       elseif crit == 2
0128         plt1 = UDIAG(:,3,k);
0129         plt2 = UDIAG(:,4,k);
0130         namnam  = [nam , ' (m2)'];
0131       elseif crit == 3    
0132         plt1 = UDIAG(:,5,k);
0133         plt2 = UDIAG(:,6,k);
0134         namnam  = [nam , ' (m3)'];
0135       end
0136       if TeX
0137         NAMES = strvcat(NAMES,deblank(namnam));
0138         TEXNAMES = strvcat(TEXNAMES,deblank(namtex));
0139       end
0140       subplot(3,3,boxplot);
0141       plot(xx,plt1,'-b');     % Pooled
0142       hold on;
0143       plot(xx,plt2,'-r');     % Within (mean)
0144       hold off;
0145       xlim([xx(1) xx(NumberOfLines)])
0146       title(namnam,'Interpreter','none')
0147       boxplot = boxplot + 1;
0148     end
0149   end
0150   eval(['print -depsc2 ' DirectoryName '/' M_.fname '_udiag' int2str(i)]);
0151   eval(['print -dpdf ' DirectoryName '/' M_.fname '_udiag' int2str(i)]);
0152   if options_.nograph, set(h,'visible','on'), end
0153   saveas(h,[DirectoryName '/' M_.fname '_udiag' int2str(i) '.fig']);
0154   if options_.nograph, close(h), end
0155   if TeX
0156     fprintf(fidTeX,'\\begin{figure}[H]\n');
0157     for jj = 1:size(NAMES,1)
0158       fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:)));
0159     end    
0160     fprintf(fidTeX,'\\centering \n');
0161     fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_udiag%s}\n',M_.fname,int2str(i));
0162     fprintf(fidTeX,'\\caption{Univariate convergence diagnostics for the Metropolis-Hastings.\n');
0163     fprintf(fidTeX,'The first, second and third columns are respectively the criteria based on\n');
0164     fprintf(fidTeX,'the eighty percent interval, the second and third moments.}');
0165     fprintf(fidTeX,'\\label{Fig:UnivariateDiagnostics:%s}\n',int2str(i));
0166     fprintf(fidTeX,'\\end{figure}\n');
0167     fprintf(fidTeX,'\n');
0168   end
0169 end
0170 reste = npar-k;
0171 if reste
0172   if reste == 1
0173     nr = 3;
0174     nc = 1;
0175   elseif reste == 2;
0176     nr = 2;
0177     nc = 3;
0178   end
0179   if TeX
0180     NAMES = [];
0181     TEXNAMES = [];
0182   end
0183   if options_.nograph
0184     h = figure('Name','MCMC univariate diagnostic (Brooks and Gelman, 1998)','Visible','off');
0185   else
0186     h = figure('Name','MCMC univariate diagnostic (Brooks and Gelman, 1998)');
0187   end  
0188   boxplot = 1;
0189   for j = 1:reste
0190     k = k+1;
0191     [nam,namtex] = get_the_name(k,TeX);
0192     for crit = 1:3
0193       if crit == 1
0194         plt1 = UDIAG(:,1,k);
0195         plt2 = UDIAG(:,2,k);
0196         namnam  = [nam , ' (Interval)']; 
0197       elseif crit == 2
0198         plt1 = UDIAG(:,3,k);
0199         plt2 = UDIAG(:,4,k);
0200         namnam  = [nam , ' (m2)'];
0201       elseif crit == 3    
0202         plt1 = UDIAG(:,5,k);
0203         plt2 = UDIAG(:,6,k);
0204         namnam  = [nam , ' (m3)'];
0205       end
0206       if TeX
0207         NAMES = strvcat(NAMES,deblank(namnam));
0208         TEXNAMES = strvcat(TEXNAMES,deblank(namtex));
0209       end
0210       subplot(nr,nc,boxplot);
0211       plot(xx,plt1,'-b');                    % Pooled
0212       hold on;
0213       plot(xx,plt2,'-r');                    % Within (mean)
0214       hold off;
0215       xlim([xx(1) xx(NumberOfLines)]);
0216       title(namnam,'Interpreter','none');
0217       boxplot = boxplot + 1;
0218     end
0219   end
0220   eval(['print -depsc2 ' DirectoryName '/' M_.fname '_udiag' int2str(pages+1)]);
0221   eval(['print -dpdf ' DirectoryName '/' M_.fname '_udiag' int2str(pages+1)]);
0222   if options_.nograph, set(h,'visible','on'), end
0223   saveas(h,[DirectoryName '/' M_.fname '_udiag' int2str(pages+1) '.fig']);
0224   if options_.nograph, close(h), end
0225   if TeX
0226     fprintf(fidTeX,'\\begin{figure}[H]\n');
0227     for jj = 1:size(NAMES,1);
0228       fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:)));
0229     end    
0230     fprintf(fidTeX,'\\centering \n');
0231     fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_udiag%s}\n',M_.fname,int2str(pages+1));
0232     if reste == 2
0233       fprintf(fidTeX,'\\caption{Univariate convergence diagnostics for the Metropolis-Hastings.\n');
0234       fprintf(fidTeX,'The first, second and third columns are respectively the criteria based on\n');
0235       fprintf(fidTeX,'the eighty percent interval, the second and third moments.}');
0236     elseif reste == 1
0237       fprintf(fidTeX,'\\caption{Univariate convergence diagnostics for the Metropolis-Hastings.\n');
0238       fprintf(fidTeX,'The first, second and third rows are respectively the criteria based on\n');
0239       fprintf(fidTeX,'the eighty percent interval, the second and third moments.}');
0240     end
0241     fprintf(fidTeX,'\\label{Fig:UnivariateDiagnostics:%s}\n',int2str(pages+1));
0242     fprintf(fidTeX,'\\end{figure}\n');
0243     fprintf(fidTeX,'\n');
0244     fprintf(fidTeX,'% End Of TeX file.');
0245     fclose(fidTeX);
0246   end
0247 end % if reste > 0
0248 clear UDIAG;
0249 %%
0250 %% Multivariate diagnostic.
0251 %%
0252 if TeX
0253   fidTeX = fopen([DirectoryName '/' M_.fname '_MultivariateDiagnostics.TeX'],'w');
0254   fprintf(fidTeX,'%% TeX eps-loader file generated by McmcDiagnostics.m (Dynare).\n');
0255   fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
0256   fprintf(fidTeX,' \n');
0257   NAMES = [];
0258 end
0259 tmp = zeros(NumberOfDraws*nblck,3);
0260 MDIAG = zeros(NumberOfLines,6);
0261 for b = 1:nblck
0262   startline = 0;
0263   for n = 1:NumberOfMcFilesPerBlock-1
0264     %load([MhDirectoryName '/' mcfiles(n,1,b).name],'logpo2');
0265     load([MhDirectoryName '/' M_.fname '_mh',int2str(n),'_blck' int2str(b) '.mat'],'logpo2');
0266     tmp((b-1)*NumberOfDraws+startline+1:(b-1)*NumberOfDraws+MAX_nruns*n,1) = logpo2;
0267     startline = startline+MAX_nruns;
0268   end
0269   %load([MhDirectoryName '/' mcfiles(NumberOfMcFilesPerBlock,1,b).name],'logpo2');
0270   load([MhDirectoryName '/' M_.fname '_mh',int2str(NumberOfMcFilesPerBlock),'_blck' int2str(b) '.mat'],'logpo2');
0271   tmp((b-1)*NumberOfDraws+startline+1:(b-1)*NumberOfDraws+ MAX_nruns*(LastFileNumber-1)+LastLineNumber,1) = logpo2;
0272 end
0273 clear logpo2;
0274 tmp(:,2) = kron(transpose(1:nblck),ones(NumberOfDraws,1));
0275 tmp(:,3) = kron(ones(nblck,1),time'); 
0276 tmp = sortrows(tmp,1);
0277 ligne   = 0;
0278 for iter  = Origin:StepSize:NumberOfDraws
0279   ligne = ligne+1;
0280   linea = ceil(0.5*iter);
0281   n     = iter-linea+1;
0282   cinf  = round(n*ALPHA/2);
0283   csup  = round(n*(1-ALPHA/2));
0284   CINF  = round(nblck*n*ALPHA/2);
0285   CSUP  = round(nblck*n*(1-ALPHA/2));
0286   temp  = tmp(find((tmp(:,3)>=linea) & (tmp(:,3)<=iter)),1:2);
0287   MDIAG(ligne,1) = temp(CSUP,1)-temp(CINF,1);
0288   moyenne = mean(temp(:,1));%% Pooled mean.
0289   MDIAG(ligne,3) = sum((temp(:,1)-moyenne).^2)/(nblck*n-1);
0290   MDIAG(ligne,5) = sum(abs(temp(:,1)-moyenne).^3)/(nblck*n-1);
0291   for i=1:nblck
0292     pmet = temp(find(temp(:,2)==i));
0293     MDIAG(ligne,2) = MDIAG(ligne,2) + pmet(csup,1)-pmet(cinf,1);
0294     moyenne = mean(pmet,1); %% Within mean.
0295     MDIAG(ligne,4) = MDIAG(ligne,4) + sum((pmet(:,1)-moyenne).^2)/(n-1);
0296     MDIAG(ligne,6) = MDIAG(ligne,6) + sum(abs(pmet(:,1)-moyenne).^3)/(n-1);
0297   end
0298 end
0299 MDIAG(:,[2 4 6],:) = MDIAG(:,[2 4 6],:)/nblck;    
0300 if options_.nograph
0301   h = figure('Name','Multivatiate diagnostic','Visible','off');
0302 else
0303   h = figure('Name','Multivatiate diagnostic');
0304 end
0305 boxplot = 1;
0306 for crit = 1:3
0307   if crit == 1
0308     plt1 = MDIAG(:,1);
0309     plt2 = MDIAG(:,2);
0310     namnam  = 'Interval'; 
0311   elseif crit == 2
0312     plt1 = MDIAG(:,3);
0313     plt2 = MDIAG(:,4);
0314     namnam  = 'm2';
0315   elseif crit == 3    
0316     plt1 = MDIAG(:,5);
0317     plt2 = MDIAG(:,6);
0318     namnam  = 'm3';
0319   end
0320   if TeX
0321     NAMES = strvcat(NAMES,namnam);
0322   end
0323   subplot(3,1,boxplot);
0324   plot(xx,plt1,'-b');  % Pooled
0325   hold on
0326   plot(xx,plt2,'-r');  % Within (mean)
0327   hold off
0328   xlim([xx(1) xx(NumberOfLines)])
0329   title(namnam,'Interpreter','none');
0330   boxplot = boxplot + 1;
0331 end
0332 eval(['print -depsc2 ' DirectoryName '/' M_.fname '_mdiag']);
0333 eval(['print -dpdf ' DirectoryName '/' M_.fname '_mdiag']);
0334 if options_.nograph, set(h,'visible','on'), end
0335 saveas(h,[DirectoryName '/' M_.fname '_mdiag.fig']);
0336 if options_.nograph, close(h), end
0337 if TeX
0338   fprintf(fidTeX,'\\begin{figure}[H]\n');
0339   for jj = 1:3
0340     fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(jj,:)),' ');
0341   end    
0342   fprintf(fidTeX,'\\centering \n');
0343   fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_mdiag}\n',M_.fname);
0344   fprintf(fidTeX,'\\caption{Multivariate convergence diagnostics for the Metropolis-Hastings.\n');
0345   fprintf(fidTeX,'The first, second and third rows are respectively the criteria based on\n');
0346   fprintf(fidTeX,'the eighty percent interval, the second and third moments. The different \n');
0347   fprintf(fidTeX,'parameters are aggregated using the posterior kernel.}');
0348   fprintf(fidTeX,'\\label{Fig:MultivariateDiagnostics}\n');
0349   fprintf(fidTeX,'\\end{figure}\n');
0350   fprintf(fidTeX,'\n');
0351   fprintf(fidTeX,'% End Of TeX file.');
0352   fclose(fidTeX);
0353 end

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