2023-10-25 10:14:12 +02:00
function [ info , M_ , options_ , oo_ , ReducedForm ] = ...
solve_model_for_online_filter ( setinitialcondition , xparam1 , dataset_ , options_ , M_ , estim_params_ , bayestopt_ , bounds , oo_ )
2014-11-10 19:00:16 +01:00
2019-07-11 12:45:26 +02:00
% Solves the dsge model for an particular parameters set.
%
% INPUTS
% - setinitialcondition [logical] return initial condition if true.
% - xparam1 [double] n× 1 vector, parameter values.
2023-10-25 10:14:12 +02:00
% - dataset_ [struct] Dataset for estimation.
% - options_ [struct] Dynare options.
% - M_ [struct] Model description.
% - estim_params_ [struct] Estimated parameters.
% - bayestopt_ [struct] Prior definition.
% - oo_ [struct] Dynare results.
2019-07-11 12:45:26 +02:00
%
% OUTPUTS
% - info [integer] scalar, nonzero if any problem occur when computing the reduced form.
2023-10-25 10:14:12 +02:00
% - M_ [struct] M_ description.
% - options_ [struct] Dynare options.
% - oo_ [struct] Dynare results.
2019-07-11 12:45:26 +02:00
% - ReducedForm [struct] Reduced form model.
2023-09-08 08:05:48 +02:00
% Copyright © 2013-2023 Dynare Team
2014-11-10 19:00:16 +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:21:49 +02:00
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
2014-11-10 19:00:16 +01:00
2019-07-11 12:45:26 +02:00
persistent init_flag restrict_variables_idx state_variables_idx mf0 mf1 number_of_state_variables
2014-11-10 19:00:16 +01:00
2019-07-11 12:45:26 +02:00
info = 0 ;
2014-11-10 19:00:16 +01:00
2019-07-11 12:45:26 +02:00
%----------------------------------------------------
2014-11-10 19:00:16 +01:00
% 1. Get the structural parameters & define penalties
2019-07-11 12:45:26 +02:00
%----------------------------------------------------
2014-11-10 19:00:16 +01:00
2019-07-11 12:45:26 +02:00
% Test if some parameters are smaller than the lower bound of the prior domain.
if any ( xparam1 < bounds . lb )
info = 41 ;
return
end
2014-11-10 19:00:16 +01:00
2019-07-11 12:45:26 +02:00
% Test if some parameters are greater than the upper bound of the prior domain.
if any ( xparam1 > bounds . ub )
info = 42 ;
return
end
2014-11-10 19:00:16 +01:00
% Get the diagonal elements of the covariance matrices for the structural innovations (Q) and the measurement error (H).
2023-10-25 10:14:12 +02:00
Q = M_ . Sigma_e ;
H = M_ . H ;
for i = 1 : estim_params_ . nvx
k = estim_params_ . var_exo ( i , 1 ) ;
2014-11-10 19:00:16 +01:00
Q ( k , k ) = xparam1 ( i ) * xparam1 ( i ) ;
end
2023-10-25 10:14:12 +02:00
offset = estim_params_ . nvx ;
if estim_params_ . nvn
for i = 1 : estim_params_ . nvn
2014-11-10 19:00:16 +01:00
H ( i , i ) = xparam1 ( i + offset ) * xparam1 ( i + offset ) ;
end
2023-10-25 10:14:12 +02:00
offset = offset + estim_params_ . nvn ;
2014-11-10 19:00:16 +01:00
else
2023-10-25 10:14:12 +02:00
H = zeros ( size ( dataset_ . data , 2 ) ) ;
2014-11-10 19:00:16 +01:00
end
% Get the off-diagonal elements of the covariance matrix for the structural innovations. Test if Q is positive definite.
2023-10-25 10:14:12 +02:00
if estim_params_ . ncx
for i = 1 : estim_params_ . ncx
k1 = estim_params_ . corrx ( i , 1 ) ;
k2 = estim_params_ . corrx ( i , 2 ) ;
2014-11-10 19:00:16 +01:00
Q ( k1 , k2 ) = xparam1 ( i + offset ) * sqrt ( Q ( k1 , k1 ) * Q ( k2 , k2 ) ) ;
Q ( k2 , k1 ) = Q ( k1 , k2 ) ;
end
% Try to compute the cholesky decomposition of Q (possible iff Q is positive definite)
2019-07-11 12:45:26 +02:00
[ ~ , testQ ] = chol ( Q ) ;
if testQ
% The variance-covariance matrix of the structural innovations is not definite positive.
info = 43 ;
return
end
2023-10-25 10:14:12 +02:00
offset = offset + estim_params_ . ncx ;
2014-11-10 19:00:16 +01:00
end
% Get the off-diagonal elements of the covariance matrix for the measurement errors. Test if H is positive definite.
2023-10-25 10:14:12 +02:00
if estim_params_ . ncn
corrn_observable_correspondence = estim_params_ . corrn_observable_correspondence ;
for i = 1 : estim_params_ . ncn
2014-11-10 19:00:16 +01:00
k1 = corrn_observable_correspondence ( i , 1 ) ;
k2 = corrn_observable_correspondence ( i , 2 ) ;
H ( k1 , k2 ) = xparam1 ( i + offset ) * sqrt ( H ( k1 , k1 ) * H ( k2 , k2 ) ) ;
H ( k2 , k1 ) = H ( k1 , k2 ) ;
end
% Try to compute the cholesky decomposition of H (possible iff H is positive definite)
2019-07-11 12:45:26 +02:00
[ ~ , testH ] = chol ( H ) ;
if testH
% The variance-covariance matrix of the measurement errors is not definite positive.
info = 44 ;
return
end
2023-10-25 10:14:12 +02:00
offset = offset + estim_params_ . ncn ;
2014-11-10 19:00:16 +01:00
end
% Update estimated structural parameters in Mode.params.
2023-10-25 10:14:12 +02:00
if estim_params_ . np > 0
M_ . params ( estim_params_ . param_vals ( : , 1 ) ) = xparam1 ( offset + 1 : end ) ;
2014-11-10 19:00:16 +01:00
end
2023-10-25 10:14:12 +02:00
% Update M_.Sigma_e and M_.H.
M_ . Sigma_e = Q ;
M_ . H = H ;
2014-11-10 19:00:16 +01:00
%------------------------------------------------------------------------------
% 2. call model setup & reduction program
%------------------------------------------------------------------------------
2019-07-11 12:45:26 +02:00
warning ( ' off' , ' MATLAB:nearlySingularMatrix' )
2023-10-25 10:14:12 +02:00
[ ~ , ~ , ~ , info , oo_ . dr , M_ . params ] = ...
dynare_resolve ( M_ , options_ , oo_ . dr , oo_ . steady_state , oo_ . exo_steady_state , oo_ . exo_det_steady_state , ' restrict' ) ;
2019-07-11 12:45:26 +02:00
warning ( ' on' , ' MATLAB:nearlySingularMatrix' )
2014-11-14 18:16:11 +01:00
2019-07-11 12:45:26 +02:00
if info ( 1 ) ~= 0
if nargout == 5
ReducedForm = 0 ;
2014-11-10 19:00:16 +01:00
end
2019-07-11 12:45:26 +02:00
return
2014-11-10 19:00:16 +01:00
end
% Get decision rules and transition equations.
2023-10-25 10:14:12 +02:00
dr = oo_ . dr ;
2014-11-10 19:00:16 +01:00
% Set persistent variables (first call).
if isempty ( init_flag )
2023-10-25 10:14:12 +02:00
mf0 = bayestopt_ . mf0 ;
mf1 = bayestopt_ . mf1 ;
2016-06-01 18:24:27 +02:00
restrict_variables_idx = dr . restrict_var_list ;
2019-07-11 12:45:26 +02:00
state_variables_idx = restrict_variables_idx ( mf0 ) ;
2014-11-10 19:00:16 +01:00
number_of_state_variables = length ( mf0 ) ;
2019-07-11 12:45:26 +02:00
init_flag = true ;
2014-11-10 19:00:16 +01:00
end
2019-07-11 12:45:26 +02:00
% Return reduced form model.
if nargout > 4
ReducedForm . ghx = dr . ghx ( restrict_variables_idx , : ) ;
ReducedForm . ghu = dr . ghu ( restrict_variables_idx , : ) ;
ReducedForm . steadystate = dr . ys ( dr . order_var ( restrict_variables_idx ) ) ;
2023-10-25 10:14:12 +02:00
if options_ . order == 2
2020-02-15 16:50:32 +01:00
ReducedForm . use_k_order_solver = false ;
2019-07-11 12:45:26 +02:00
ReducedForm . ghxx = dr . ghxx ( restrict_variables_idx , : ) ;
ReducedForm . ghuu = dr . ghuu ( restrict_variables_idx , : ) ;
ReducedForm . ghxu = dr . ghxu ( restrict_variables_idx , : ) ;
ReducedForm . constant = ReducedForm . steadystate + . 5 * dr . ghs2 ( restrict_variables_idx ) ;
2023-07-01 16:43:48 +02:00
ReducedForm . ghs2 = dr . ghs2 ( restrict_variables_idx , : ) ;
2023-10-25 10:14:12 +02:00
elseif options_ . order > = 3
2020-02-15 16:50:32 +01:00
ReducedForm . use_k_order_solver = true ;
ReducedForm . dr = dr ;
2023-10-25 10:14:12 +02:00
ReducedForm . udr = folded_to_unfolded_dr ( dr , M_ , options_ ) ;
2019-07-11 12:45:26 +02:00
else
2021-01-26 09:10:55 +01:00
n_states = size ( dr . ghx , 2 ) ;
n_shocks = size ( dr . ghu , 2 ) ;
2020-02-15 16:50:32 +01:00
ReducedForm . use_k_order_solver = false ;
2021-01-26 09:10:55 +01:00
ReducedForm . ghxx = zeros ( size ( restrict_variables_idx , 1 ) , n_states ^2 ) ;
ReducedForm . ghuu = zeros ( size ( restrict_variables_idx , 1 ) , n_shocks ^2 ) ;
ReducedForm . ghxu = zeros ( size ( restrict_variables_idx , 1 ) , n_states * n_shocks ) ;
ReducedForm . constant = ReducedForm . steadystate ;
2019-07-11 12:45:26 +02:00
end
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 ;
2014-11-14 18:16:11 +01:00
end
2014-11-10 19:00:16 +01:00
2019-07-11 12:45:26 +02:00
% Set initial condition
if setinitialcondition
2023-10-25 10:14:12 +02:00
switch options_ . particle . initialization
2014-11-10 19:00:16 +01:00
case 1 % Initial state vector covariance is the ergodic variance associated to the first order Taylor-approximation of the model.
2020-02-15 16:50:32 +01:00
StateVectorMean = ReducedForm . state_variables_steady_state ; %.constant(mf0);
2023-09-08 08:03:18 +02:00
[ A , B ] = kalman_transition_matrix ( dr , dr . restrict_var_list , dr . restrict_columns ) ;
2023-10-25 10:14:12 +02:00
StateVectorVariance = lyapunov_symm ( A , B * ReducedForm . Q * B ' , options_ . lyapunov_fixed_point_tol , ...
options_ . qz_criterium , options_ . lyapunov_complex_threshold , [ ] , options_ . debug ) ;
2021-01-26 09:14:34 +01:00
StateVectorVariance = StateVectorVariance ( mf0 , mf0 ) ;
2014-11-10 19:00:16 +01: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).
2020-02-15 16:50:32 +01:00
StateVectorMean = ReducedForm . state_variables_steady_state ; %.constant(mf0);
2023-10-25 10:14:12 +02:00
old_DynareOptionsperiods = options_ . periods ;
options_ . periods = 5000 ;
old_DynareOptionspruning = options_ . pruning ;
options_ . pruning = options_ . particle . pruning ;
y_ = simult ( dr . ys , dr , M_ , options_ ) ;
y_ = y_ ( dr . order_var ( state_variables_idx ) , 2001 : options_ . periods ) ;
2014-11-10 19:00:16 +01:00
StateVectorVariance = cov ( y_ ' ) ;
2023-10-25 10:14:12 +02:00
options_ . periods = old_DynareOptionsperiods ;
options_ . pruning = old_DynareOptionspruning ;
2014-11-10 19:00:16 +01:00
clear ( ' old_DynareOptionsperiods' , ' y_' ) ;
case 3 % Initial state vector covariance is a diagonal matrix.
2020-02-15 16:50:32 +01:00
StateVectorMean = ReducedForm . state_variables_steady_state ; %.constant(mf0);
2023-10-25 10:14:12 +02:00
StateVectorVariance = options_ . particle . initial_state_prior_std * eye ( number_of_state_variables ) ;
2014-11-10 19:00:16 +01:00
otherwise
error ( ' Unknown initialization option!' )
end
ReducedForm . StateVectorMean = StateVectorMean ;
ReducedForm . StateVectorVariance = StateVectorVariance ;
2022-02-04 11:29:31 +01:00
end