From a9292c5086d04ec0b681f88e665421ad09f266b5 Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx,de>
Date: Wed, 6 Nov 2013 08:26:22 +0100
Subject: [PATCH] Add warning if prior allows for negative variances and if
 negative estimated variances are encountered. Closes #522

Due to the use of variances for Sigma_e and subsequently backing out the standard deviation from these variances, the sign of the standard deviation does not matter and no bound needs to be imposed.
---
 matlab/display_estimation_results_table.m |  7 +++--
 matlab/initial_estimation_checks.m        | 36 ++++++++++++++++++++---
 2 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/matlab/display_estimation_results_table.m b/matlab/display_estimation_results_table.m
index 7943faf5c3..0fecc8f38c 100644
--- a/matlab/display_estimation_results_table.m
+++ b/matlab/display_estimation_results_table.m
@@ -20,7 +20,7 @@ function oo_=display_estimation_results_table(xparam1,stdh,M_,options_,estim_par
 % SPECIAL REQUIREMENTS
 %   None.
 
-% Copyright (C) 2013 Dynare Team
+% Copyright (C) 2014 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -169,6 +169,9 @@ if ncn
     end
     skipline()
 end
+if any(xparam1(1:nvx+nvn)<0)
+    warning('Some estimated standard deviations are negative. Dynare internally works with variances so that the sign does not matter. Nevertheless, it is recommended to impose either prior restrictions (Bayesian Estimation) or a lower bound (ML) to assure positive values.')
+end
 
 OutputDirectoryName = CheckPath('Output',M_.dname);
 
@@ -556,4 +559,4 @@ elseif all(bayestopt_.pshape == 0) && options_.TeX %% MLE and GMM Latex output
         fclose(fidTeX);
     end
 
-end
\ No newline at end of file
+end
diff --git a/matlab/initial_estimation_checks.m b/matlab/initial_estimation_checks.m
index 80c499789f..9dee5461e9 100644
--- a/matlab/initial_estimation_checks.m
+++ b/matlab/initial_estimation_checks.m
@@ -3,9 +3,14 @@ function DynareResults = initial_estimation_checks(objective_function,xparam1,Dy
 % Checks data (complex values, ML evaluation, initial values, BK conditions,..)
 %
 % INPUTS
-%    xparam1:          vector of parameters to be estimated
-%    gend:             scalar specifying the number of observations
-%    data:             matrix of data
+%   objective_function  [function handle] of the objective function
+%   xparam1:            [vector] of parameters to be estimated
+%   DynareDataset:      [structure] storing the dataset information
+%   Model:              [structure] decribing the model
+%   EstimatedParameters [structure] characterizing parameters to be estimated
+%   DynareOptions       [structure] describing the options
+%   BayesInfo           [structure] describing the priors
+%   DynareResults       [structure] storing the results
 %
 % OUTPUTS
 %    DynareResults     structure of temporary results
@@ -13,7 +18,7 @@ function DynareResults = initial_estimation_checks(objective_function,xparam1,Dy
 % SPECIAL REQUIREMENTS
 %    none
 
-% Copyright (C) 2003-2012 Dynare Team
+% Copyright (C) 2003-2014 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -41,6 +46,29 @@ end
 % check if steady state solves static model (except if diffuse_filter == 1)
 [DynareResults.steady_state] = evaluate_steady_state(DynareResults.steady_state,Model,DynareOptions,DynareResults,DynareOptions.diffuse_filter==0);
 
+if any(BayesInfo.pshape) % if Bayesian estimation
+    nvx=EstimatedParameters.nvx;
+    if nvx && any(BayesInfo.p3(1:nvx)<0) 
+        warning('Your prior allows for negative standard deviations for structural shocks. Due to working with variances, Dynare will be able to continue, but it is recommended to change your prior.')
+    end
+    offset=nvx;
+    nvn=EstimatedParameters.nvn;
+    if nvn && any(BayesInfo.p3(1+offset:offset+nvn)<0) 
+        warning('Your prior allows for negative standard deviations for measurement error. Due to working with variances, Dynare will be able to continue, but it is recommended to change your prior.')
+    end
+    offset = nvx+nvn;
+    ncx=EstimatedParameters.ncx; 
+    if ncx && (any(BayesInfo.p3(1+offset:offset+ncx)<-1) || any(BayesInfo.p4(1+offset:offset+ncx)>1)) 
+        warning('Your prior allows for correlations between measurement errors larger than +-1 and will not integrate to 1 due to truncation. Please change your prior')
+    end
+    offset = nvx+nvn+ncx;
+    ncn=EstimatedParameters.ncn; 
+    if ncn && (any(BayesInfo.p3(1+offset:offset+ncn)<-1) || any(BayesInfo.p4(1+offset:offset+ncn)>1)) 
+        warning('Your prior allows for correlations between structural shocks larger than +-1 and will not integrate to 1 due to truncation. Please change your prior')
+    end
+end
+
+    
 % Evaluate the likelihood.
 ana_deriv = DynareOptions.analytic_derivation;
 DynareOptions.analytic_derivation=0;
-- 
GitLab