2023-10-24 22:21:15 +02:00
function [fval,info,exit_flag,DLIK,Hess,ys,trend_coeff,M_,options_,bayestopt_,dr] = non_linear_dsge_likelihood ( xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,BoundsInfo,dr, endo_steady_state, exo_steady_state, exo_det_steady_state)
% [fval,info,exit_flag,DLIK,Hess,ys,trend_coeff,M_,options_,bayestopt_,dr] = non_linear_dsge_likelihood(xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,BoundsInfo,dr, endo_steady_state, exo_steady_state, exo_det_steady_state)
2019-12-14 17:23:47 +01:00
% Evaluates the posterior kernel of a dsge model using a non linear filter.
%
% INPUTS
% - xparam1 [double] n× 1 vector, estimated parameters.
2023-10-02 11:12:31 +02:00
% - dataset_ [struct] Matlab's structure containing the dataset
% - dataset_info [struct] Matlab's structure describing the dataset
2023-09-15 09:26:48 +02:00
% - options_ [struct] Matlab's structure describing the options
% - M_ [struct] Matlab's structure describing the M_
2023-10-24 22:21:15 +02:00
% - estim_params_ [struct] Matlab's structure describing the estimated_parameters
2023-09-15 09:26:48 +02:00
% - bayestopt_ [struct] Matlab's structure describing the priors
% - BoundsInfo [struct] Matlab's structure specifying the bounds on the paramater values
2023-09-27 08:06:37 +02:00
% - dr [structure] Reduced form model.
% - endo_steady_state [vector] steady state value for endogenous variables
% - exo_steady_state [vector] steady state value for exogenous variables
% - exo_det_steady_state [vector] steady state value for exogenous deterministic variables
2019-12-14 17:23:47 +01:00
%
% OUTPUTS
% - fval [double] scalar, value of the likelihood or posterior kernel.
% - info [integer] 4× 1 vector, informations resolution of the model and evaluation of the likelihood.
% - exit_flag [integer] scalar, equal to 1 (no issues when evaluating the likelihood) or 0 (not able to evaluate the likelihood).
% - DLIK [double] Empty array.
% - Hess [double] Empty array.
% - ys [double] Empty array.
% - trend_coeff [double] Empty array.
2023-09-15 09:26:48 +02:00
% - M_ [struct] Updated M_ structure described in INPUTS section.
% - options_ [struct] Updated options_ structure described in INPUTS section.
2023-10-02 11:12:31 +02:00
% - bayestopt_ [struct] See INPUTS section.
2023-09-27 08:06:37 +02:00
% - dr [struct] decision rule structure described in INPUTS section.
2011-12-26 17:44:41 +01:00
2023-09-15 09:26:48 +02:00
% Copyright © 2010-2023 Dynare Team
2011-12-26 17:44:41 +01:00
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
2021-06-09 17:33:48 +02:00
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
2011-12-26 17:44:41 +01:00
2015-10-09 14:18:31 +02:00
% Initialization of the returned arguments.
fval = [ ] ;
ys = [ ] ;
trend_coeff = [ ] ;
exit_flag = 1 ;
2016-06-01 15:51:13 +02:00
DLIK = [ ] ;
Hess = [ ] ;
2015-10-09 14:18:31 +02:00
2017-02-08 12:35:08 +01:00
% Ensure that xparam1 is a column vector.
2021-01-27 15:41:24 +01:00
% (Don't do the transformation if xparam1 is empty, otherwise it would become a
% 0× 1 matrix, which create issues with older MATLABs when comparing with [] in
% check_bounds_and_definiteness_estimation)
if ~ isempty ( xparam1 )
xparam1 = xparam1 ( : ) ;
end
2017-02-08 12:35:08 +01:00
2015-10-09 14:18:31 +02:00
% Issue an error if loglinear option is used.
2023-09-15 09:26:48 +02:00
if options_ . loglinear
2015-10-09 14:18:31 +02:00
error ( ' non_linear_dsge_likelihood: It is not possible to use a non linear filter with the option loglinear!' )
end
%------------------------------------------------------------------------------
% 1. Get the structural parameters & define penalties
%------------------------------------------------------------------------------
2023-10-24 22:21:15 +02:00
M_ = set_all_parameters ( xparam1 , estim_params_ , M_ ) ;
2020-07-17 13:18:39 +02:00
2023-10-24 22:21:15 +02:00
[ fval , info , exit_flag , Q , H ] = check_bounds_and_definiteness_estimation ( xparam1 , M_ , estim_params_ , BoundsInfo ) ;
2020-06-22 18:45:13 +02:00
if info ( 1 )
2015-10-09 14:18:31 +02:00
return
end
%------------------------------------------------------------------------------
% 2. call model setup & reduction program
%------------------------------------------------------------------------------
2023-09-15 13:40:10 +02:00
% Linearize the model around the deterministic steadystate and extract the matrices of the state equation (T and R).
2023-09-27 08:06:37 +02:00
[ dr , info , M_ . params ] = resol ( 0 , M_ , options_ , dr , endo_steady_state , exo_steady_state , exo_det_steady_state ) ;
2015-10-09 14:18:31 +02:00
2016-06-01 18:22:51 +02:00
if info ( 1 )
2016-07-18 11:11:43 +02:00
if info ( 1 ) == 3 || info ( 1 ) == 4 || info ( 1 ) == 5 || info ( 1 ) == 6 || info ( 1 ) == 19 || ...
2017-05-16 15:10:20 +02:00
info ( 1 ) == 20 || info ( 1 ) == 21 || info ( 1 ) == 23 || info ( 1 ) == 26 || ...
info ( 1 ) == 81 || info ( 1 ) == 84 || info ( 1 ) == 85
2016-06-01 18:22:51 +02:00
%meaningful second entry of output that can be used
fval = Inf ;
info ( 4 ) = info ( 2 ) ;
exit_flag = 0 ;
return
else
fval = Inf ;
info ( 4 ) = 0.1 ;
exit_flag = 0 ;
return
end
2015-10-09 14:18:31 +02:00
end
% Define a vector of indices for the observed variables. Is this really usefull?...
2023-09-15 09:26:48 +02:00
bayestopt_ . mf = bayestopt_ . mf1 ;
2015-10-09 14:18:31 +02:00
% Get needed informations for kalman filter routines.
2023-09-15 09:26:48 +02:00
start = options_ . presample + 1 ;
2023-10-02 11:12:31 +02:00
Y = transpose ( dataset_ . data ) ;
2015-10-09 14:18:31 +02:00
%------------------------------------------------------------------------------
% 3. Initial condition of the Kalman filter
%------------------------------------------------------------------------------
2023-09-15 09:26:48 +02:00
mf0 = bayestopt_ . mf0 ;
mf1 = bayestopt_ . mf1 ;
2019-12-20 11:26:35 +01:00
restrict_variables_idx = dr . restrict_var_list ;
state_variables_idx = restrict_variables_idx ( mf0 ) ;
2019-12-14 17:30:30 +01:00
number_of_state_variables = length ( mf0 ) ;
2015-10-09 14:18:31 +02:00
ReducedForm . steadystate = dr . ys ( dr . order_var ( restrict_variables_idx ) ) ;
ReducedForm . constant = ReducedForm . steadystate + . 5 * dr . ghs2 ( restrict_variables_idx ) ;
ReducedForm . state_variables_steady_state = dr . ys ( dr . order_var ( state_variables_idx ) ) ;
ReducedForm . Q = Q ;
ReducedForm . H = H ;
ReducedForm . mf0 = mf0 ;
ReducedForm . mf1 = mf1 ;
2023-09-15 09:26:48 +02:00
if options_ . order > 3
2019-12-20 11:26:35 +01:00
ReducedForm . use_k_order_solver = true ;
ReducedForm . dr = dr ;
2023-09-15 09:26:48 +02:00
ReducedForm . udr = folded_to_unfolded_dr ( dr , M_ , options_ ) ;
2022-08-30 14:16:14 +02:00
if pruning
error ( ' Pruning is not available for orders > 3' ) ;
end
2019-12-20 11:26:35 +01:00
else
ReducedForm . use_k_order_solver = false ;
ReducedForm . ghx = dr . ghx ( restrict_variables_idx , : ) ;
ReducedForm . ghu = dr . ghu ( restrict_variables_idx , : ) ;
ReducedForm . ghxx = dr . ghxx ( restrict_variables_idx , : ) ;
ReducedForm . ghuu = dr . ghuu ( restrict_variables_idx , : ) ;
ReducedForm . ghxu = dr . ghxu ( restrict_variables_idx , : ) ;
2023-06-30 15:50:21 +02:00
ReducedForm . ghs2 = dr . ghs2 ( restrict_variables_idx , : ) ;
2023-09-15 09:26:48 +02:00
if options_ . order == 3
2022-08-30 14:16:14 +02:00
ReducedForm . ghxxx = dr . ghxxx ( restrict_variables_idx , : ) ;
ReducedForm . ghuuu = dr . ghuuu ( restrict_variables_idx , : ) ;
ReducedForm . ghxxu = dr . ghxxu ( restrict_variables_idx , : ) ;
ReducedForm . ghxuu = dr . ghxuu ( restrict_variables_idx , : ) ;
ReducedForm . ghxss = dr . ghxss ( restrict_variables_idx , : ) ;
ReducedForm . ghuss = dr . ghuss ( restrict_variables_idx , : ) ;
end
2019-12-20 11:26:35 +01:00
end
2015-10-09 14:18:31 +02:00
% Set initial condition.
2023-09-15 09:26:48 +02:00
switch options_ . particle . initialization
2015-10-09 14:18:31 +02:00
case 1 % Initial state vector covariance is the ergodic variance associated to the first order Taylor-approximation of the model.
StateVectorMean = ReducedForm . constant ( mf0 ) ;
2023-09-08 08:03:18 +02:00
[ A , B ] = kalman_transition_matrix ( dr , dr . restrict_var_list , dr . restrict_columns ) ;
2023-09-15 09:26:48 +02:00
StateVectorVariance = lyapunov_symm ( A , B * Q * B ' , options_ . lyapunov_fixed_point_tol , ...
options_ . qz_criterium , options_ . lyapunov_complex_threshold , [ ] , options_ . debug ) ;
2020-10-08 12:15:26 +02:00
StateVectorVariance = StateVectorVariance ( mf0 , mf0 ) ;
2015-10-09 14:18:31 +02:00
case 2 % Initial state vector covariance is a monte-carlo based estimate of the ergodic variance (consistent with a k-order Taylor-approximation of the model).
StateVectorMean = ReducedForm . constant ( mf0 ) ;
2023-09-15 09:26:48 +02:00
old_DynareOptionsperiods = options_ . periods ;
options_ . periods = 5000 ;
old_DynareOptionspruning = options_ . pruning ;
options_ . pruning = options_ . particle . pruning ;
2023-09-27 08:06:37 +02:00
y_ = simult ( endo_steady_state , dr , M_ , options_ ) ;
2020-10-08 12:15:26 +02:00
y_ = y_ ( dr . order_var ( state_variables_idx ) , 2001 : 5000 ) ; %state_variables_idx is in dr-order while simult_ is in declaration order
2023-09-15 09:26:48 +02:00
if any ( any ( isnan ( y_ ) ) ) || any ( any ( isinf ( y_ ) ) ) && ~ options_ . pruning
2021-05-27 21:04:35 +02:00
fval = Inf ;
info ( 1 ) = 202 ;
info ( 4 ) = 0.1 ;
exit_flag = 0 ;
return ;
end
StateVectorVariance = cov ( y_ ' ) ;
2023-09-15 09:26:48 +02:00
options_ . periods = old_DynareOptionsperiods ;
options_ . pruning = old_DynareOptionspruning ;
2015-10-09 14:18:31 +02:00
clear ( ' old_DynareOptionsperiods' , ' y_' ) ;
2017-04-02 18:37:45 +02:00
case 3 % Initial state vector covariance is a diagonal matrix (to be used
% if model has stochastic trends).
2015-10-09 14:18:31 +02:00
StateVectorMean = ReducedForm . constant ( mf0 ) ;
2023-09-15 09:26:48 +02:00
StateVectorVariance = options_ . particle . initial_state_prior_std * eye ( number_of_state_variables ) ;
2015-10-09 14:18:31 +02:00
otherwise
error ( ' Unknown initialization option!' )
end
ReducedForm . StateVectorMean = StateVectorMean ;
ReducedForm . StateVectorVariance = StateVectorVariance ;
2020-12-08 22:49:41 +01:00
[ ~ , flag ] = chol ( ReducedForm . StateVectorVariance ) ; %reduced_rank_cholesky(ReducedForm.StateVectorVariance)';
if flag
fval = Inf ;
info ( 1 ) = 201 ;
info ( 4 ) = 0.1 ;
exit_flag = 0 ;
return ;
end
2015-10-09 14:18:31 +02:00
%------------------------------------------------------------------------------
% 4. Likelihood evaluation
%------------------------------------------------------------------------------
2023-09-15 09:26:48 +02:00
options_ . warning_for_steadystate = 0 ;
2015-10-09 14:18:31 +02:00
[ s1 , s2 ] = get_dynare_random_generator_state ( ) ;
2023-09-15 09:26:48 +02:00
LIK = feval ( options_ . particle . algorithm , ReducedForm , Y , start , options_ . particle , options_ . threads , options_ , M_ ) ;
2015-10-09 14:18:31 +02:00
set_dynare_random_generator_state ( s1 , s2 ) ;
if imag ( LIK )
2020-12-08 22:25:47 +01:00
fval = Inf ;
2016-06-01 18:22:51 +02:00
info ( 1 ) = 46 ;
info ( 4 ) = 0.1 ;
exit_flag = 0 ;
2020-12-08 22:25:47 +01:00
return
2015-10-09 14:18:31 +02:00
elseif isnan ( LIK )
2020-12-08 22:25:47 +01:00
fval = Inf ;
2016-06-01 18:22:51 +02:00
info ( 1 ) = 45 ;
info ( 4 ) = 0.1 ;
exit_flag = 0 ;
2020-12-08 22:25:47 +01:00
return
2015-10-09 14:18:31 +02:00
else
likelihood = LIK ;
end
2023-09-15 09:26:48 +02:00
options_ . warning_for_steadystate = 1 ;
2015-10-09 14:18:31 +02:00
% ------------------------------------------------------------------------------
% Adds prior if necessary
% ------------------------------------------------------------------------------
2023-09-15 09:26:48 +02:00
lnprior = priordens ( xparam1 ( : ) , bayestopt_ . pshape , bayestopt_ . p6 , bayestopt_ . p7 , bayestopt_ . p3 , bayestopt_ . p4 ) ;
2019-12-14 17:24:34 +01:00
fval = ( likelihood - lnprior ) ;
2015-10-09 14:18:31 +02:00
if isnan ( fval )
2016-06-01 18:22:51 +02:00
fval = Inf ;
info ( 1 ) = 47 ;
info ( 4 ) = 0.1 ;
2015-10-09 14:18:31 +02:00
exit_flag = 0 ;
return
end
2019-12-14 17:24:34 +01:00
if ~ isreal ( fval )
2016-06-01 18:22:51 +02:00
fval = Inf ;
info ( 1 ) = 48 ;
info ( 4 ) = 0.1 ;
2015-10-09 14:18:31 +02:00
exit_flag = 0 ;
return
end
2016-06-14 17:28:52 +02:00
2019-12-14 17:24:34 +01:00
if isinf ( LIK )
2016-06-14 17:28:52 +02:00
fval = Inf ;
info ( 1 ) = 50 ;
info ( 4 ) = 0.1 ;
exit_flag = 0 ;
return
end