From dc7c0fa74da0837092bbf35a4a7f9dbf5e0813cf Mon Sep 17 00:00:00 2001
From: Marco Ratto <marco.ratto@jrc.ec.europa.eu>
Date: Thu, 31 May 2012 14:44:16 +0200
Subject: [PATCH] Include prior info in penalty with Bayesian estimation.

This can improve a bit optimization routines when parameter go beyond prior bounds during line search algorithms or when numerical gradient is computed.
---
 matlab/dsge_likelihood.m | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/matlab/dsge_likelihood.m b/matlab/dsge_likelihood.m
index 08c985a758..81cf44592e 100644
--- a/matlab/dsge_likelihood.m
+++ b/matlab/dsge_likelihood.m
@@ -141,7 +141,7 @@ function [fval,DLIK,Hess,exit_flag,ys,trend_coeff,info,Model,DynareOptions,Bayes
 % In summary, an initial call to the present function, without triggering
 % any condition, guarantees that 'penalty' is properly initialized when needed.
 
-persistent penalty
+persistent penalty prior_penalty
 
 % Initialization of the returned variables and others...
 fval        = [];
@@ -180,6 +180,10 @@ end
 if ~isequal(DynareOptions.mode_compute,1) && any(xparam1<BayesInfo.lb)
     k = find(xparam1<BayesInfo.lb);
     fval = penalty+sum((BayesInfo.lb(k)-xparam1(k)).^2);
+    xtemp=xparam1;
+    xtemp(k)=BayesInfo.lb(k);
+    prior_correction = max(0,-prior_penalty-priordens(xtemp,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4));
+    fval = fval+prior_correction;
     exit_flag = 0;
     info = 41;
     if analytic_derivation,
@@ -192,6 +196,10 @@ end
 if ~isequal(DynareOptions.mode_compute,1) && any(xparam1>BayesInfo.ub)
     k = find(xparam1>BayesInfo.ub);
     fval = penalty+sum((xparam1(k)-BayesInfo.ub(k)).^2);
+    xtemp=xparam1;
+    xtemp(k)=BayesInfo.ub(k);
+    prior_correction = max(0,-prior_penalty -priordens(xtemp,BayesInfo.pshape,BayesInfo.p6,BayesInfo.p7,BayesInfo.p3,BayesInfo.p4));
+    fval = fval+prior_correction;
     exit_flag = 0;
     info = 42;
     if analytic_derivation,
@@ -749,6 +757,7 @@ DynareOptions.kalman_algo = kalman_algo;
 
 % Update the penalty.
 penalty = fval;
+prior_penalty = -lnprior;
 
 if analytic_derivation==0 && nargout==2,
     lik=lik(start:end,:);
-- 
GitLab