From 329b91d7178647bcaa113c7f3672facba218ae8e Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Wed, 1 Jun 2016 15:51:13 +0200 Subject: [PATCH] Harmonize output of objective functions Closes #1149 Mirrors 1ad8df4635d99ba775e22f5693bf18c2cbb6c0aa --- matlab/PosteriorIRF_core1.m | 2 +- matlab/TaRB_optimizer_wrapper.m | 13 ++++++++----- matlab/dsge_likelihood.m | 24 ++++++++++++------------ matlab/dsge_var_likelihood.m | 17 +++++++++++++---- matlab/dynare_estimation_1.m | 2 +- matlab/identification_analysis.m | 2 +- matlab/initial_estimation_checks.m | 10 ++-------- matlab/mode_check.m | 2 +- matlab/non_linear_dsge_likelihood.m | 24 ++++++++++++++++-------- matlab/osr1.m | 2 +- matlab/osr_obj.m | 8 +++++--- 11 files changed, 61 insertions(+), 45 deletions(-) diff --git a/matlab/PosteriorIRF_core1.m b/matlab/PosteriorIRF_core1.m index bc2b3160a..37f98209b 100644 --- a/matlab/PosteriorIRF_core1.m +++ b/matlab/PosteriorIRF_core1.m @@ -194,7 +194,7 @@ while fpar. par_vector(parameterindices,:)=optpar; %reassemble parameter -[fval,DLIK,Hess,exit_flag] = feval(TargetFun,par_vector,varargin{:}); %call target function +[fval,info,exit_flag,DLIK,Hess,SteadyState,trend_coeff] = feval(TargetFun,par_vector,varargin{:}); %call target function diff --git a/matlab/dsge_likelihood.m b/matlab/dsge_likelihood.m index 5b8c6f282..d5dfaefdf 100644 --- a/matlab/dsge_likelihood.m +++ b/matlab/dsge_likelihood.m @@ -1,4 +1,4 @@ -function [fval,DLIK,Hess,exit_flag,SteadyState,trend_coeff,info,Model,DynareOptions,BayesInfo,DynareResults] = dsge_likelihood(xparam1,DynareDataset,DatasetInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults,derivatives_info) +function [fval,info,exit_flag,DLIK,Hess,SteadyState,trend_coeff,Model,DynareOptions,BayesInfo,DynareResults] = dsge_likelihood(xparam1,DynareDataset,DatasetInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults,derivatives_info) % Evaluates the posterior kernel of a dsge model using the specified % kalman_algo; the resulting posterior includes the 2*pi constant of the % likelihood function @@ -35,14 +35,8 @@ function [fval,DLIK,Hess,exit_flag,SteadyState,trend_coeff,info,Model,DynareOpti %! @table @ @var %! @item fval %! Double scalar, value of (minus) the likelihood. -%! @item exit_flag -%! Integer scalar, equal to zero if the routine return with a penalty (one otherwise). -%! @item ys -%! Vector of doubles, steady state level for the endogenous variables. -%! @item trend_coeff -%! Matrix of doubles, coefficients of the deterministic trend in the measurement equation. %! @item info -%! Integer scalar, error code. +%! Double vector, second entry stores penalty, first entry the error code. %! @table @ @code %! @item info==0 %! No error. @@ -91,6 +85,16 @@ function [fval,DLIK,Hess,exit_flag,SteadyState,trend_coeff,info,Model,DynareOpti %! @item info==48 %! Posterior kernel is a complex valued number (logged prior density is complex). %! @end table +%! @item exit_flag +%! Integer scalar, equal to zero if the routine return with a penalty (one otherwise). +%! @item DLIK +%! Vector of doubles, score of the likelihood. +%! @item AHess +%! Matrix of doubles, asymptotic hessian matrix. +%! @item SteadyState +%! Vector of doubles, steady state level for the endogenous variables. +%! @item trend_coeff +%! Matrix of doubles, coefficients of the deterministic trend in the measurement equation. %! @item Model %! Matlab's structure describing the model (initialized by dynare, see @ref{M_}). %! @item DynareOptions @@ -99,10 +103,6 @@ function [fval,DLIK,Hess,exit_flag,SteadyState,trend_coeff,info,Model,DynareOpti %! Matlab's structure describing the priors (initialized by dynare, see @ref{bayesopt_}). %! @item DynareResults %! Matlab's structure gathering the results (initialized by dynare, see @ref{oo_}). -%! @item DLIK -%! Vector of doubles, score of the likelihood. -%! @item AHess -%! Matrix of doubles, asymptotic hessian matrix. %! @end table %! @sp 2 %! @strong{This function is called by:} diff --git a/matlab/dsge_var_likelihood.m b/matlab/dsge_var_likelihood.m index 87d09edb2..1317f7e6c 100644 --- a/matlab/dsge_var_likelihood.m +++ b/matlab/dsge_var_likelihood.m @@ -1,4 +1,4 @@ -function [fval,grad,hess,exit_flag,info,PHI,SIGMAu,iXX,prior] = dsge_var_likelihood(xparam1,DynareDataset,DynareInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults) +function [fval,info,exit_flag,grad,hess,SteadyState,trend_coeff,PHI,SIGMAu,iXX,prior] = dsge_var_likelihood(xparam1,DynareDataset,DynareInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults) % Evaluates the posterior kernel of the bvar-dsge model. % % INPUTS @@ -7,8 +7,16 @@ function [fval,grad,hess,exit_flag,info,PHI,SIGMAu,iXX,prior] = dsge_var_likelih % % OUTPUTS % o fval [double] Value of the posterior kernel at xparam1. -% o cost_flag [integer] Zero if the function returns a penalty, one otherwise. % o info [integer] Vector of informations about the penalty. +% o exit_flag [integer] Zero if the function returns a penalty, one otherwise. +% o grad [double] place holder for gradient of the likelihood +% currently not supported by dsge_var +% o hess [double] place holder for hessian matrix of the likelihood +% currently not supported by dsge_var +% o SteadyState [double] Steady state vector possibly recomputed +% by call to dynare_resolve() +% o trend_coeff [double] place holder for trend coefficients, +% currently not supported by dsge_var % o PHI [double] Stacked BVAR-DSGE autoregressive matrices (at the mode associated to xparam1). % o SIGMAu [double] Covariance matrix of the BVAR-DSGE (at the mode associated to xparam1). % o iXX [double] inv(X'X). @@ -45,6 +53,7 @@ PHI = []; SIGMAu = []; iXX = []; prior = []; +trend_coeff=[]; % Initialization of of the index for parameter dsge_prior_weight in Model.params. if isempty(dsge_prior_weight_idx) @@ -271,7 +280,7 @@ if imag(fval)~=0 return end -if (nargout == 8) +if (nargout == 10) if isinf(dsge_prior_weight) iXX = iGXX; else @@ -279,7 +288,7 @@ if (nargout == 8) end end -if (nargout==9) +if (nargout==11) if isinf(dsge_prior_weight) iXX = iGXX; else diff --git a/matlab/dynare_estimation_1.m b/matlab/dynare_estimation_1.m index a2be89596..6e71390b9 100644 --- a/matlab/dynare_estimation_1.m +++ b/matlab/dynare_estimation_1.m @@ -236,7 +236,7 @@ if ~isequal(options_.mode_compute,0) && ~options_.mh_posterior_mode_estimation if options_.analytic_derivation && strcmp(func2str(objective_function),'dsge_likelihood'), ana_deriv_old = options_.analytic_derivation; options_.analytic_derivation = 2; - [junk1, junk2, hh] = feval(objective_function,xparam1, ... + [junk1, junk2,junk3, junk4, hh] = feval(objective_function,xparam1, ... dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_); options_.analytic_derivation = ana_deriv_old; elseif ~isnumeric(options_.mode_compute) || ~(isequal(options_.mode_compute,5) && newratflag~=1), diff --git a/matlab/identification_analysis.m b/matlab/identification_analysis.m index 53fc6ad33..3e05e1639 100644 --- a/matlab/identification_analysis.m +++ b/matlab/identification_analysis.m @@ -156,7 +156,7 @@ if info(1)==0, dataset_ = dseries(oo_.endo_simul(options_.varobs_id,100+1:end)',dates('1Q1'), options_.varobs); derivatives_info.no_DLIK=1; bounds = prior_bounds(bayestopt_, options_.prior_trunc); - [fval,DLIK,AHess,cost_flag,ys,trend_coeff,info,M_,options_,bayestopt_,oo_] = dsge_likelihood(params',dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_,derivatives_info); + [fval,info,cost_flag,DLIK,AHess,ys,trend_coeff,M_,options_,bayestopt_,oo_] = dsge_likelihood(params',dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_,derivatives_info); % fval = DsgeLikelihood(xparam1,data_info,options_,M_,estim_params_,bayestopt_,oo_); options_.analytic_derivation = analytic_derivation; AHess=-AHess; diff --git a/matlab/initial_estimation_checks.m b/matlab/initial_estimation_checks.m index 84adbde0c..3e6430a82 100644 --- a/matlab/initial_estimation_checks.m +++ b/matlab/initial_estimation_checks.m @@ -116,9 +116,9 @@ ana_deriv = DynareOptions.analytic_derivation; DynareOptions.analytic_derivation=0; if ~isequal(DynareOptions.mode_compute,11) || ... (isequal(DynareOptions.mode_compute,11) && isequal(DynareOptions.order,1)) - [fval,junk1,junk2,a,b,c,d] = feval(objective_function,xparam1,DynareDataset,DatasetInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults); + [fval,info] = feval(objective_function,xparam1,DynareDataset,DatasetInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults); else - b=0; + info=0; fval = 0; end if DynareOptions.debug @@ -126,12 +126,6 @@ if DynareOptions.debug end DynareOptions.analytic_derivation=ana_deriv; -if DynareOptions.dsge_var || strcmp(func2str(objective_function),'non_linear_dsge_likelihood') - info = b; -else - info = d; -end - % if DynareOptions.mode_compute==5 % if ~strcmp(func2str(objective_function),'dsge_likelihood') % error('Options mode_compute=5 is not compatible with non linear filters or Dsge-VAR models!') diff --git a/matlab/mode_check.m b/matlab/mode_check.m index 40b675739..7a282af2e 100644 --- a/matlab/mode_check.m +++ b/matlab/mode_check.m @@ -140,7 +140,7 @@ for plt = 1:nbplt, end for i=1:length(z) xx(kk) = z(i); - [fval, junk1, junk2, exit_flag] = feval(fun,xx,DynareDataset,DatasetInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults); + [fval, info, exit_flag] = feval(fun,xx,DynareDataset,DatasetInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults); if exit_flag y(i,1) = fval; else diff --git a/matlab/non_linear_dsge_likelihood.m b/matlab/non_linear_dsge_likelihood.m index 94c46eee1..35685ed2a 100644 --- a/matlab/non_linear_dsge_likelihood.m +++ b/matlab/non_linear_dsge_likelihood.m @@ -1,4 +1,4 @@ -function [fval,ys,trend_coeff,exit_flag,info,Model,DynareOptions,BayesInfo,DynareResults] = non_linear_dsge_likelihood(xparam1,DynareDataset,DatasetInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults) +function [fval,info,exit_flag,DLIK,Hess,ys,trend_coeff,Model,DynareOptions,BayesInfo,DynareResults] = non_linear_dsge_likelihood(xparam1,DynareDataset,DatasetInfo,DynareOptions,Model,EstimatedParameters,BayesInfo,BoundsInfo,DynareResults) % Evaluates the posterior kernel of a dsge model using a non linear filter. %@info: @@ -31,14 +31,8 @@ function [fval,ys,trend_coeff,exit_flag,info,Model,DynareOptions,BayesInfo,Dynar %! @table @ @var %! @item fval %! Double scalar, value of (minus) the likelihood. -%! @item exit_flag -%! Integer scalar, equal to zero if the routine return with a penalty (one otherwise). -%! @item ys -%! Vector of doubles, steady state level for the endogenous variables. -%! @item trend_coeffs -%! Matrix of doubles, coefficients of the deterministic trend in the measurement equation. %! @item info -%! Integer scalar, error code. +%! Double vector, second entry stores penalty, first entry the error code. %! @table @ @code %! @item info==0 %! No error. @@ -81,6 +75,18 @@ function [fval,ys,trend_coeff,exit_flag,info,Model,DynareOptions,BayesInfo,Dynar %! @item info==45 %! Likelihood is a complex valued number. %! @end table +%! @item exit_flag +%! Integer scalar, equal to zero if the routine return with a penalty (one otherwise). +%! @item DLIK +%! Vector of doubles, placeholder for score of the likelihood, currently +%! not supported by non_linear_dsge_likelihood +%! @item AHess +%! Matrix of doubles, placeholder for asymptotic hessian matrix, currently +%! not supported by non_linear_dsge_likelihood +%! @item ys +%! Vector of doubles, steady state level for the endogenous variables. +%! @item trend_coeffs +%! Matrix of doubles, coefficients of the deterministic trend in the measurement equation. %! @item Model %! Matlab's structure describing the model (initialized by dynare, see @ref{M_}). %! @item DynareOptions @@ -132,6 +138,8 @@ fval = []; ys = []; trend_coeff = []; exit_flag = 1; +DLIK = []; +Hess = []; % Issue an error if loglinear option is used. if DynareOptions.loglinear diff --git a/matlab/osr1.m b/matlab/osr1.m index d5070cdbb..51fd5475f 100644 --- a/matlab/osr1.m +++ b/matlab/osr1.m @@ -77,7 +77,7 @@ inv_order_var = oo_.dr.inv_order_var; %extract unique entries of covariance i_var=unique(i_var); %% do initial checks -[loss,vx,info,exit_flag]=osr_obj(t0,i_params,inv_order_var(i_var),weights(i_var,i_var)); +[loss,info,exit_flag,vx]=osr_obj(t0,i_params,inv_order_var(i_var),weights(i_var,i_var)); if info~=0 print_info(info, options_.noprint, options_); else diff --git a/matlab/osr_obj.m b/matlab/osr_obj.m index 378d2b06e..7c3e70353 100644 --- a/matlab/osr_obj.m +++ b/matlab/osr_obj.m @@ -1,4 +1,4 @@ -function [loss,vx,info,exit_flag]=osr_obj(x,i_params,i_var,weights) +function [loss,info,exit_flag,vx,junk]=osr_obj(x,i_params,i_var,weights) % objective function for optimal simple rules (OSR) % INPUTS % x vector values of the parameters @@ -9,13 +9,15 @@ function [loss,vx,info,exit_flag]=osr_obj(x,i_params,i_var,weights) % % OUTPUTS % loss scalar loss function returned to solver -% vx vector variances of the endogenous variables % info vector info vector returned by resol % exit_flag scalar exit flag returned to solver +% vx vector variances of the endogenous variables +% junk empty dummy output for conformable +% header % % SPECIAL REQUIREMENTS % none -% Copyright (C) 2005-2013 Dynare Team +% Copyright (C) 2005-2016 Dynare Team % % This file is part of Dynare. %