From 45899a80c62f30b2dcec779a303fe96c4f60453b Mon Sep 17 00:00:00 2001
From: Marco Ratto <marco.ratto@jrc.ec.europa.eu>
Date: Sat, 7 Dec 2013 10:52:27 +0100
Subject: [PATCH] Fixes to mode_compute=5. (1) force univariate steps at every
 iteration: improve robustness at the expenses of a bit less efficiency; (2)
 fix gradient computation for special cases when the value of the parameters
 get stacked at the prior boundary. (cherry picked from commit
 e5854cb2014c0906bd1d797f251547b77fc415aa)

---
 matlab/mr_hessian.m | 1 +
 matlab/newrat.m     | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/matlab/mr_hessian.m b/matlab/mr_hessian.m
index 9fe04ddad9..a428a3c923 100644
--- a/matlab/mr_hessian.m
+++ b/matlab/mr_hessian.m
@@ -101,6 +101,7 @@ while i<n
                 h1(i)=2.1*h1(i);
             end
             h1(i) = min(h1(i),0.5*hmax(i));
+            h1(i) = max(h1(i),1.e-10);
             xh1(i)=x(i)+h1(i);
             try
                 [fx, ffx]=feval(func,xh1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
diff --git a/matlab/newrat.m b/matlab/newrat.m
index 5b2c7e7d9e..1df0157766 100644
--- a/matlab/newrat.m
+++ b/matlab/newrat.m
@@ -122,7 +122,7 @@ while norm(gg)>gtol && check==0 && jit<nit
         fval=fval1;
         x0=x01;
     end
-    if icount==1 || (icount>1 && (fval0(icount-1)-fval0(icount))>1) || ((fval0(icount)-fval)<1.e-2*(gg'*(H*gg))/2 && igibbs),
+%     if icount==1 || (icount>1 && (fval0(icount-1)-fval0(icount))>1) || ((fval0(icount)-fval)<1.e-2*(gg'*(H*gg))/2 && igibbs),
         if length(find(ig))<nx
             ggx=ggx*0;
             ggx(find(ig))=gg(find(ig));
@@ -139,7 +139,7 @@ while norm(gg)>gtol && check==0 && jit<nit
         nig=[nig ig];
         disp('Sequence of univariate steps!!')
         fval=fvala;
-    end
+%     end
     if (fval0(icount)-fval)<ftol && flagit==0
         disp('Try diagonal Hessian')
         ihh=diag(1./(diag(hhg)));
-- 
GitLab