From f0a0561f33fc03e59891a724dbceee8a436bcb8f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?=
 <stephane.adjemian@univ-lemans.fr>
Date: Tue, 25 Feb 2014 17:24:34 +0100
Subject: [PATCH] Fixed bug.

MCMC convergence diagnostics should be computed even if mh_replic is less than 2000, provided that the total number of iterations is greater than 2000 (if option load_mh_file is used).

(cherry picked from commit a2ce59bfcd5795175bcb9b5e24afc1ad25245215)
---
 matlab/McMCDiagnostics.m     | 14 +++++++++-----
 matlab/dynare_estimation_1.m |  2 +-
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/matlab/McMCDiagnostics.m b/matlab/McMCDiagnostics.m
index 1091dbc03..7aa7b05ff 100644
--- a/matlab/McMCDiagnostics.m
+++ b/matlab/McMCDiagnostics.m
@@ -69,6 +69,15 @@ if issue_an_error_message
     error('Estimation::mcmc::diagnostics: I cannot proceed because some MCMC files are missing. Check your MCMC files...')
 end
 
+PastDraws = sum(record.MhDraws,1);
+LastFileNumber = PastDraws(2);
+LastLineNumber = record.MhDraws(end,3);
+NumberOfDraws  = PastDraws(1);
+
+if LastLineNumber<=2000
+    warning(['estimation:: MCMC convergence diagnostics are not computed because the total number of iterations is less than 2000!'])
+    return
+end
 
 if nblck == 1 % Brooks and Gelman tests need more than one block
     convergence_diagnostics_geweke=zeros(npar,4+2*length(options_.convergence.geweke.taper_steps));
@@ -121,11 +130,6 @@ if nblck == 1 % Brooks and Gelman tests need more than one block
     return;
 end
 
-PastDraws = sum(record.MhDraws,1);
-LastFileNumber = PastDraws(2);
-LastLineNumber = record.MhDraws(end,3);
-NumberOfDraws  = PastDraws(1);
-
 Origin = 1000;
 StepSize = ceil((NumberOfDraws-Origin)/100);% So that the computational time does not 
 ALPHA = 0.2;                                % increase too much with the number of simulations. 
diff --git a/matlab/dynare_estimation_1.m b/matlab/dynare_estimation_1.m
index 9bf5c2532..6ba4dd80f 100644
--- a/matlab/dynare_estimation_1.m
+++ b/matlab/dynare_estimation_1.m
@@ -789,7 +789,7 @@ if (any(bayestopt_.pshape  >0 ) && options_.mh_replic) || ...
         CutSample(M_, options_, estim_params_);
         return
     else
-        if ~options_.nodiagnostic && options_.mh_replic > 2000
+        if ~options_.nodiagnostic && options_.mh_replic>0
             oo_= McMCDiagnostics(options_, estim_params_, M_,oo_);
         end
         %% Here i discard first half of the draws:
-- 
GitLab