1function [fval,info,exit_flag,fake_1,fake_2] = minus_logged_prior_density(xparams,pshape,p6,p7,p3,p4,DynareOptions,DynareModel,EstimatedParams,DynareResults)
2% Evaluates minus the logged prior density.
3%
4% INPUTS
5%   xparams    [double]   vector of parameters.
6%   pshape     [integer]  vector specifying prior densities shapes.
7%   p6         [double]   vector, first hyperparameter.
8%   p7         [double]   vector, second hyperparameter.
9%   p3         [double]   vector, prior's lower bound.
10%   p4         [double]   vector, prior's upper bound.
11%
12% OUTPUTS
13%   f          [double]  value of minus the logged prior density.
14%   info       [double]  vector: second entry stores penalty, first entry the error code.
15%
16% Copyright (C) 2009-2017 Dynare Team
17%
18% This file is part of Dynare.
19%
20% Dynare is free software: you can redistribute it and/or modify
21% it under the terms of the GNU General Public License as published by
22% the Free Software Foundation, either version 3 of the License, or
23% (at your option) any later version.
24%
25% Dynare is distributed in the hope that it will be useful,
26% but WITHOUT ANY WARRANTY; without even the implied warranty of
27% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28% GNU General Public License for more details.
29%
30% You should have received a copy of the GNU General Public License
31% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
32
33fake_1 = 1;
34fake_2 = 1;
35
36exit_flag = 1;
37info = zeros(4,1);
38
39%------------------------------------------------------------------------------
40% 1. Get the structural parameters & define penalties
41%------------------------------------------------------------------------------
42
43% Return, with endogenous penalty, if some parameters are smaller than the lower bound of the prior domain.
44if ~isequal(DynareOptions.mode_compute,1) && any(xparams<p3)
45    k = find(xparams<p3);
46    fval = Inf;
47    exit_flag = 0;
48    info(1) = 41;
49    info(4) = sum((p3(k)-xparams(k)).^2);
50    return
51end
52
53% Return, with endogenous penalty, if some parameters are greater than the upper bound of the prior domain.
54if ~isequal(DynareOptions.mode_compute,1) && any(xparams>p4)
55    k = find(xparams>p4);
56    fval = Inf;
57    exit_flag = 0;
58    info(1) = 42;
59    info(4) = sum((xparams(k)-p4(k)).^2);
60    return
61end
62
63% Get the diagonal elements of the covariance matrices for the structural innovations (Q) and the measurement error (H).
64DynareModel = set_all_parameters(xparams,EstimatedParams,DynareModel);
65
66Q = DynareModel.Sigma_e;
67H = DynareModel.H;
68
69% Test if Q is positive definite.
70if ~issquare(Q) || EstimatedParams.ncx || isfield(EstimatedParams,'calibrated_covariances')
71    % Try to compute the cholesky decomposition of Q (possible iff Q is positive definite)
72    [Q_is_positive_definite, penalty] = ispd(Q);
73    if ~Q_is_positive_definite
74        % 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.
75        fval = Inf;
76        exit_flag = 0;
77        info(1) = 43;
78        info(4) = penalty;
79        return
80    end
81    if isfield(EstimatedParams,'calibrated_covariances')
82        correct_flag=check_consistency_covariances(Q);
83        if ~correct_flag
84            penalty = sum(Q(EstimatedParams.calibrated_covariances.position).^2);
85            fval = Inf;
86            exit_flag = 0;
87            info(1) = 71;
88            info(4) = penalty;
89            return
90        end
91    end
92
93end
94
95% Test if H is positive definite.
96if ~issquare(H) || EstimatedParams.ncn || isfield(EstimatedParams,'calibrated_covariances_ME')
97    [H_is_positive_definite, penalty] = ispd(H);
98    if ~H_is_positive_definite
99        % 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.
100        fval = Inf;
101        exit_flag = 0;
102        info(1) = 44;
103        info(4) = penalty;
104        return
105    end
106    if isfield(EstimatedParams,'calibrated_covariances_ME')
107        correct_flag=check_consistency_covariances(H);
108        if ~correct_flag
109            penalty = sum(H(EstimatedParams.calibrated_covariances_ME.position).^2);
110            fval = Inf;
111            exit_flag = 0;
112            info(1) = 72;
113            info(4) = penalty;
114            return
115        end
116    end
117end
118
119
120%-----------------------------
121% 2. Check BK and steady state
122%-----------------------------
123
124M_ = set_all_parameters(xparams,EstimatedParams,DynareModel);
125[dr,info,DynareModel,DynareOptions,DynareResults] = resol(0,DynareModel,DynareOptions,DynareResults);
126
127% Return, with endogenous penalty when possible, if dynare_resolve issues an error code (defined in resol).
128if info(1)
129    if info(1) == 3 || info(1) == 4 || info(1) == 5 || info(1)==6 ||info(1) == 19 ...
130                info(1) == 20 || info(1) == 21 || info(1) == 23 || info(1) == 26 || ...
131                info(1) == 81 || info(1) == 84 ||  info(1) == 85
132        %meaningful second entry of output that can be used
133        fval = Inf;
134        info(4) = info(2);
135        exit_flag = 0;
136        return
137    else
138        fval = Inf;
139        info(4) = 0.1;
140        exit_flag = 0;
141        return
142    end
143end
144
145
146
147fval = - priordens(xparams,pshape,p6,p7,p3,p4);