From da159038e6130f10dd9f7a0f4b548fe56af80e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Fri, 21 Sep 2012 22:59:03 +0200 Subject: [PATCH] Added check for BK and steady state in the optimization of the prior density. --- matlab/get_prior_info.m | 9 ++- matlab/maximize_prior_density.m | 6 +- matlab/minus_logged_prior_density.m | 96 ++++++++++++++++++++++++++++- 3 files changed, 103 insertions(+), 8 deletions(-) diff --git a/matlab/get_prior_info.m b/matlab/get_prior_info.m index 4498caeed..b355d4fc0 100644 --- a/matlab/get_prior_info.m +++ b/matlab/get_prior_info.m @@ -130,6 +130,7 @@ end if info==2% Prior optimization. % Initialize to the prior mode if possible + oo_.dr=set_state_space(oo_.dr,M_,options_); k = find(~isnan(bayestopt_.p5)); xparam1(k) = bayestopt_.p5(k); % Pertubation of the initial condition. @@ -139,7 +140,11 @@ if info==2% Prior optimization. while look_for_admissible_initial_condition xinit = xparam1+scale*randn(size(xparam1)); if all(xinit(:)>bayestopt_.p3) && all(xinit(:). + +global objective_function_penalty_base + fake_1 = 1; fake_2 = 1; -fake_3 = 1; -f = - priordens(xparams,pshape,p6,p7,p3,p4); \ No newline at end of file +exit_flag = 1; +info = 0; + +%------------------------------------------------------------------------------ +% 1. Get the structural parameters & define penalties +%------------------------------------------------------------------------------ + +% Return, with endogenous penalty, if some parameters are smaller than the lower bound of the prior domain. +if ~isequal(DynareOptions.mode_compute,1) && any(xparamsp4) + k = find(xparams>p4); + fval = objective_function_penalty_base+sum((xparams(k)-p4(k)).^2); + exit_flag = 0; + info = 42; + return +end + +% Get the diagonal elements of the covariance matrices for the structural innovations (Q) and the measurement error (H). +DynareModel = set_all_parameters(xparams,EstimatedParams,DynareModel); + +Q = DynareModel.Sigma_e; +H = DynareModel.H; + +% Test if Q is positive definite. +if EstimatedParams.ncx + % Try to compute the cholesky decomposition of Q (possible iff Q is positive definite) + [CholQ,testQ] = chol(Q); + if testQ + % The variance-covariance matrix of the structural innovations is not definite positive. We have to compute the eigenvalues of this matrix in order to build the endogenous penalty. + a = diag(eig(Q)); + k = find(a < 0); + if k > 0 + fval = objective_function_penalty_base+sum(-a(k)); + exit_flag = 0; + info = 43; + return + end + end +end + +% Test if H is positive definite. +if EstimatedParams.ncn + % Try to compute the cholesky decomposition of H (possible iff H is positive definite) + [CholH,testH] = chol(H); + if testH + % The variance-covariance matrix of the measurement errors is not definite positive. We have to compute the eigenvalues of this matrix in order to build the endogenous penalty. + a = diag(eig(H)); + k = find(a < 0); + if k > 0 + fval = objective_function_penalty_base+sum(-a(k)); + exit_flag = 0; + info = 44; + return + end + end +end + + +%----------------------------- +% 2. Check BK and steady state +%----------------------------- + +M_ = set_all_parameters(xparams,EstimatedParams,DynareModel); +[dr,info,DynareModel,DynareOptions,DynareResults] = resol(0,DynareModel,DynareOptions,DynareResults); + +% Return, with endogenous penalty when possible, if dynare_resolve issues an error code (defined in resol). +if info(1) == 1 || info(1) == 2 || info(1) == 5 || info(1) == 7 || info(1) ... + == 8 || info(1) == 22 || info(1) == 24 || info(1) == 19 + fval = objective_function_penalty_base+1; + info = info(1); + exit_flag = 0; + return +elseif info(1) == 3 || info(1) == 4 || info(1)==6 || info(1) == 20 || info(1) == 21 || info(1) == 23 + fval = objective_function_penalty_base+info(2); + info = info(1); + exit_flag = 0; + return +end + + +fval = - priordens(xparams,pshape,p6,p7,p3,p4); \ No newline at end of file