From 791e5e9d97a823421f801a09497dc731e9ec27d6 Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx.de>
Date: Fri, 17 Jul 2020 15:15:23 +0200
Subject: [PATCH] RBC_MoM-test: filter out pathological cases

[skip CI]
---
 .../estimation/method_of_moments/RBC_MoM_common.inc |  2 +-
 .../method_of_moments/RBC_MoM_steady_helper.m       | 13 +++++++++++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/tests/estimation/method_of_moments/RBC_MoM_common.inc b/tests/estimation/method_of_moments/RBC_MoM_common.inc
index 625a9c50b8..d480e35c06 100644
--- a/tests/estimation/method_of_moments/RBC_MoM_common.inc
+++ b/tests/estimation/method_of_moments/RBC_MoM_common.inc
@@ -60,7 +60,7 @@ IV_O_N = DELTA*K_O_N;
 Y_O_N = A*K_O_N^(1-ALFA);
 C_O_N = Y_O_N - IV_O_N;
 
-N=RBC_MoM_steady_helper(THETA,ETAl,ETAc,BETTA,B,C_O_N,W);
+[N, info]=RBC_MoM_steady_helper(THETA,ETAl,ETAc,BETTA,B,C_O_N,W);
 C=C_O_N*N;
 Y=Y_O_N*N;
 IV=IV_O_N*N;
diff --git a/tests/estimation/method_of_moments/RBC_MoM_steady_helper.m b/tests/estimation/method_of_moments/RBC_MoM_steady_helper.m
index 08185c1e16..df37641486 100644
--- a/tests/estimation/method_of_moments/RBC_MoM_steady_helper.m
+++ b/tests/estimation/method_of_moments/RBC_MoM_steady_helper.m
@@ -1,8 +1,17 @@
-function N = RBC_MoM_steady_helper(THETA,ETAl,ETAc,BETTA,B,C_O_N,W)
+function [N, info]= RBC_MoM_steady_helper(THETA,ETAl,ETAc,BETTA,B,C_O_N,W)
+info=0;
+if ~isreal(C_O_N)
+    info=1;
+    N=NaN;
+    return;
+end
 if ETAc == 1 && ETAl == 1
     N = (1-BETTA*B)*(C_O_N*(1-B))^-1*W/THETA/(1+(1-BETTA*B)*(C_O_N*(1-B))^-1*W/THETA);
 else
     % No closed-form solution use a fixed-point algorithm
     N0 = 1/3;
-    N = fsolve(@(N) THETA*(1-N)^(-ETAl)*N^ETAc - (1-BETTA*B)*(C_O_N*(1-B))^(-ETAc)*W, N0,optimset('Display','off','TolX',1e-12,'TolFun',1e-12));
+    [N, ~, exitflag] = fsolve(@(N) THETA*(1-N)^(-ETAl)*N^ETAc - (1-BETTA*B)*(C_O_N*(1-B))^(-ETAc)*W, N0,optimset('Display','off','TolX',1e-12,'TolFun',1e-12));        
+    if exitflag<1
+        info=1;
+    end
 end
\ No newline at end of file
-- 
GitLab