From 111347469fe0a155f47e8fd09497f4ba20351697 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Mon, 9 Jan 2012 21:23:17 +0100 Subject: [PATCH] adding comment explaining initialization of persistent variable penalty in dsge_likelihood.m and dsge_likelihood_hh.m Removed misleading initialization code. Added call to dsge_likelihood_hh in initial_estimation_checks to initialize persistent variable in that function as well. --- matlab/dsge_likelihood.m | 24 ++++++++++++++---------- matlab/dsge_likelihood_hh.m | 23 +++++++++++++---------- matlab/initial_estimation_checks.m | 6 ++++++ 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/matlab/dsge_likelihood.m b/matlab/dsge_likelihood.m index 08393a1a9..5a3eead06 100644 --- a/matlab/dsge_likelihood.m +++ b/matlab/dsge_likelihood.m @@ -127,17 +127,21 @@ function [fval,exit_flag,ys,trend_coeff,info,Model,DynareOptions,BayesInfo,Dynar % AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT FR % Declaration of the penalty as a persistent variable. -persistent penalty -% Initialization of the persistent variable. -if ~nargin || isempty(penalty) - penalty = 1e8; - if ~nargin, return, end -end -if nargin==1 - penalty = xparam1; - return -end +% Persistent variable 'penalty' is used to compute an endogenous penalty to +% the value 'fval' when various conditions are encountered. These conditions +% set also 'exit_flag' equal to 0 instead of 1. It is only when +% dsge_likelihood() is called by an optimizer called by +% dynare_estimation_1() that 'exit_flag' is ignored and penalized 'fval' is +% actually used. +% In that case, 'penalty' is properly initialized, at the very end of the +% present function, by a call to dsge_likelihood() made in +% initial_estimation_checks(). If a condition triggers exit_flag == +% 0, initial_estimation_checks() triggers an error. +% In summary, an initial call to the present function, without triggering +% any condition, guarantees that 'penalty' is properly initialized when needed. + +persistent penalty % Initialization of the returned variables and others... fval = []; diff --git a/matlab/dsge_likelihood_hh.m b/matlab/dsge_likelihood_hh.m index a77982236..9c54b609e 100644 --- a/matlab/dsge_likelihood_hh.m +++ b/matlab/dsge_likelihood_hh.m @@ -41,17 +41,20 @@ function [fval,llik,cost_flag,ys,trend_coeff,info] = dsge_likelihood_hh(xparam1, % Declaration of the penalty as a persistent variable. -persistent penalty -% Initialization of the persistent variable. -if ~nargin || isempty(penalty) - penalty = 1e8; - if ~nargin, return, end -end -if nargin==1 - penalty = xparam1; - return -end +% Persistent variable 'penalty' is used to compute an endogenous penalty to +% the value 'fval' when various conditions are encountered. These conditions +% set also 'exit_flag' equal to 0 instead of 1. It is only when +% dsge_likelihood_hh() is called by an newrat() called by +% dynare_estimation_1() that 'exit_flag' is ignored and penalized 'fval' is +% actually used. +% In that case, 'penalty' is properly initialized, at the very end of the +% present function, by a call to dsge_likelihood_hh() made in +% initial_estimation_checks(). If a condition triggers exit_flag == +% 0, initial_estimation_checks() triggers an error. +% In summary, an initial call to the present function, without triggering +% any condition, guarantees that 'penalty' is properly initialized when needed. +persistent penalty % Initialization of the returned variables and others... fval = []; diff --git a/matlab/initial_estimation_checks.m b/matlab/initial_estimation_checks.m index 09bf7fea3..3bf52e8e7 100644 --- a/matlab/initial_estimation_checks.m +++ b/matlab/initial_estimation_checks.m @@ -42,6 +42,12 @@ if DynareOptions.dsge_var [fval,cost_flag,info] = DsgeVarLikelihood(xparam1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults); else [fval,cost_flag,ys,trend_coeff,info] = dsge_likelihood(xparam1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults); + if DynareOptions.mode_compute == 5 + % this call is necessary to initialized persistent variable + % 'penalty' in dsge_likelihood_hh + [fval,llik,cost_flag,ys,trend_coeff,info] = ... + dsge_likelihood_hh(xparam1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults); + end end if info(1) > 0