2009-12-16 18:17:34 +01:00
function dynare_estimation_1 ( var_list_,dname)
% function dynare_estimation_1(var_list_,dname)
% runs the estimation of the model
2011-09-17 15:38:49 +02:00
%
2009-12-16 18:17:34 +01:00
% INPUTS
% var_list_: selected endogenous variables vector
% dname: alternative directory name
2011-09-17 15:38:49 +02:00
%
2009-12-16 18:17:34 +01:00
% OUTPUTS
% none
%
% SPECIAL REQUIREMENTS
% none
2021-09-15 18:35:30 +02:00
% Copyright (C) 2003-2021 Dynare Team
2009-12-16 18:17:34 +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/>.
2009-12-16 18:17:34 +01:00
2014-06-16 17:41:59 +02:00
global M_ options_ oo_ estim_params_ bayestopt_ dataset_ dataset_info
2009-12-16 18:17:34 +01:00
2021-07-22 17:46:08 +02:00
if ~ exist ( [ M_ . dname filesep ' Output' ] , ' dir' )
2021-09-15 18:35:30 +02:00
if isoctave && ~ exist ( M_ . dname )
% See https://savannah.gnu.org/bugs/index.php?61166
% This workaround is needed for recursive estimation.
mkdir ( M_ . dname )
end
2021-07-22 17:46:08 +02:00
mkdir ( M_ . dname , ' Output' ) ;
end
2017-03-16 15:45:08 +01:00
if isempty ( estim_params_ )
mode_compute_o = options_ . mode_compute ;
mh_replic_o = options_ . mh_replic ;
options_ . mode_compute = 0 ;
options_ . mh_replic = 0 ;
reset_options_related_to_estimation = true ;
else
reset_options_related_to_estimation = false ;
end
2016-06-16 22:40:54 +02:00
%store qz_criterium
qz_criterium_old = options_ . qz_criterium ;
2017-03-20 20:29:19 +01:00
if isnan ( options_ . first_obs )
first_obs_nan_indicator = true ;
else
first_obs_nan_indicator = false ;
end
2016-06-16 22:40:54 +02:00
2012-03-03 16:27:25 +01:00
% Set particle filter flag.
if options_ . order > 1
2019-12-19 22:52:39 +01:00
if options_ . particle . status
2013-07-10 16:16:32 +02:00
skipline ( )
2012-03-05 08:46:52 +01:00
disp ( ' Estimation using a non linear filter!' )
2013-07-10 16:16:32 +02:00
skipline ( )
2015-05-27 12:55:10 +02:00
if ~ options_ . nointeractive && ismember ( options_ . mode_compute , [ 1 , 3 , 4 ] ) && ~ strcmpi ( options_ . particle . filter_algorithm , ' gf' ) % Known gradient-based optimizers
2017-05-16 15:10:20 +02:00
disp ( ' You are using a gradient-based mode-finder. Particle filtering introduces discontinuities in the' )
2013-07-10 16:15:28 +02:00
disp ( ' objective function w.r.t the parameters. Thus, should use a non-gradient based optimizer.' )
2013-07-08 19:34:31 +02:00
fprintf ( ' \nPlease choose a mode-finder:\n' )
2013-07-10 16:03:49 +02:00
fprintf ( ' \t 0 - Continue using gradient-based method (it is most likely that you will no get any sensible result).\n' )
2013-07-08 19:34:31 +02:00
fprintf ( ' \t 6 - Monte Carlo based algorithm\n' )
2013-07-10 16:03:49 +02:00
fprintf ( ' \t 7 - Nelder-Mead simplex based optimization routine (Matlab optimization toolbox required)\n' )
fprintf ( ' \t 8 - Nelder-Mead simplex based optimization routine (Dynare' ' s implementation)\n' )
2013-07-08 19:34:31 +02:00
fprintf ( ' \t 9 - CMA-ES (Covariance Matrix Adaptation Evolution Strategy) algorithm\n' )
2013-07-10 16:03:49 +02:00
choice = [ ] ;
while isempty ( choice )
choice = input ( ' Please enter your choice: ' ) ;
if isnumeric ( choice ) && isint ( choice ) && ismember ( choice , [ 0 6 7 8 9 ] )
if choice
options_ . mode_compute = choice ;
end
else
fprintf ( ' \nThis is an invalid choice (you have to choose between 0, 6, 7, 8 and 9).\n' )
choice = [ ] ;
end
2013-07-08 19:34:31 +02:00
end
end
2012-03-05 08:46:52 +01:00
else
2019-12-19 22:52:39 +01:00
error ( ' For estimating the model with a second order approximation using a non linear filter, one should have options_.particle.status=true;' )
2012-03-05 08:46:52 +01:00
end
2012-03-03 16:27:25 +01:00
end
2011-09-19 16:38:38 +02:00
if ~ options_ . dsge_var
2012-03-05 08:46:52 +01:00
if options_ . particle . status
2012-03-03 16:27:25 +01:00
objective_function = str2func ( ' non_linear_dsge_likelihood' ) ;
2021-05-11 11:40:51 +02:00
[ options_ . particle ] = check_particle_filter_options ( options_ . particle ) ;
2012-03-03 16:27:25 +01:00
else
2021-05-17 21:24:25 +02:00
if options_ . occbin . likelihood . status && options_ . occbin . likelihood . inversion_filter
objective_function = str2func ( ' occbin.IVF_posterior' ) ;
else
objective_function = str2func ( ' dsge_likelihood' ) ;
end
2012-03-03 16:27:25 +01:00
end
2011-09-19 16:38:38 +02:00
else
2013-11-16 23:33:37 +01:00
objective_function = str2func ( ' dsge_var_likelihood' ) ;
2011-09-19 16:38:38 +02:00
end
2014-10-20 16:18:54 +02:00
[ dataset_ , dataset_info , xparam1 , hh , M_ , options_ , oo_ , estim_params_ , bayestopt_ , bounds ] = ...
2014-06-16 17:41:59 +02:00
dynare_estimation_init ( var_list_ , dname , [ ] , M_ , options_ , oo_ , estim_params_ , bayestopt_ ) ;
2011-09-17 15:38:49 +02:00
2013-11-16 23:26:19 +01:00
if options_ . dsge_var
check_dsge_var_model ( M_ , estim_params_ , bayestopt_ ) ;
2014-06-23 10:55:08 +02:00
if dataset_info . missing . state
error ( ' Estimation::DsgeVarLikelihood: I cannot estimate a DSGE-VAR model with missing observations!' )
end
if options_ . noconstant
var_sample_moments ( options_ . dsge_varlag , - 1 , dataset_ ) ;
else
% The steady state is non zero ==> a constant in the VAR is needed!
var_sample_moments ( options_ . dsge_varlag , 0 , dataset_ ) ;
end
2012-06-06 15:58:48 +02:00
end
2012-06-06 18:24:03 +02:00
% Set sigma_e_is_diagonal flag (needed if the shocks block is not declared in the mod file).
2019-03-19 14:26:16 +01:00
M_ . sigma_e_is_diagonal = true ;
2013-10-30 14:26:30 +01:00
if estim_params_ . ncx || any ( nnz ( tril ( M_ . Correlation_matrix , - 1 ) ) ) || isfield ( estim_params_ , ' calibrated_covariances' )
2019-03-19 14:26:16 +01:00
M_ . sigma_e_is_diagonal = false ;
2012-06-06 15:58:48 +02:00
end
2011-09-17 15:38:49 +02:00
data = dataset_ . data ;
2014-07-09 15:28:46 +02:00
rawdata = dataset_info . rawdata ;
2014-06-16 17:41:59 +02:00
data_index = dataset_info . missing . aindex ;
missing_value = dataset_info . missing . state ;
2009-12-16 18:17:34 +01:00
2011-09-17 15:38:49 +02:00
% Set number of observations
2014-06-16 17:41:59 +02:00
gend = dataset_ . nobs ;
2017-10-10 10:05:59 +02:00
2011-09-17 15:38:49 +02:00
% Set the number of observed variables.
2013-05-21 16:38:17 +02:00
n_varobs = length ( options_ . varobs ) ;
2017-10-10 10:05:59 +02:00
2011-09-17 15:38:49 +02:00
% Get the number of parameters to be estimated.
2009-12-16 18:17:34 +01:00
nvx = estim_params_ . nvx ; % Variance of the structural innovations (number of parameters).
nvn = estim_params_ . nvn ; % Variance of the measurement innovations (number of parameters).
ncx = estim_params_ . ncx ; % Covariance of the structural innovations (number of parameters).
ncn = estim_params_ . ncn ; % Covariance of the measurement innovations (number of parameters).
np = estim_params_ . np ; % Number of deep parameters.
nx = nvx + nvn + ncx + ncn + np ; % Total number of parameters to be estimated.
2017-10-10 10:05:59 +02:00
2011-02-21 11:20:26 +01:00
dr = oo_ . dr ;
2009-12-16 18:17:34 +01:00
if ~ isempty ( estim_params_ )
2013-10-30 14:26:30 +01:00
M_ = set_all_parameters ( xparam1 , estim_params_ , M_ ) ;
2009-12-16 18:17:34 +01:00
end
2011-10-12 21:46:50 +02:00
2009-12-16 18:17:34 +01:00
2013-10-30 21:43:33 +01:00
%% perform initial estimation checks;
try
2014-10-20 16:18:54 +02:00
oo_ = initial_estimation_checks ( objective_function , xparam1 , dataset_ , dataset_info , M_ , estim_params_ , options_ , bayestopt_ , bounds , oo_ ) ;
2013-11-08 15:02:30 +01:00
catch % if check fails, provide info on using calibration if present
e = lasterror ( ) ;
2014-07-20 12:46:14 +02:00
if estim_params_ . full_calibration_detected %calibrated model present and no explicit starting values
2013-10-30 21:43:33 +01:00
skipline ( 1 ) ;
fprintf ( ' ESTIMATION_CHECKS: There was an error in computing the likelihood for initial parameter values.\n' )
2016-06-19 17:03:48 +02:00
fprintf ( ' ESTIMATION_CHECKS: If this is not a problem with the setting of options (check the error message below),\n' )
fprintf ( ' ESTIMATION_CHECKS: you should try using the calibrated version of the model as starting values. To do\n' )
2017-05-16 15:10:20 +02:00
fprintf ( ' ESTIMATION_CHECKS: this, add an empty estimated_params_init-block with use_calibration option immediately before the estimation\n' )
2013-10-30 21:43:33 +01:00
fprintf ( ' ESTIMATION_CHECKS: command (and after the estimated_params-block so that it does not get overwritten):\n' ) ;
skipline ( 2 ) ;
end
2013-11-08 15:02:30 +01:00
rethrow ( e ) ;
2013-10-30 21:43:33 +01:00
end
2009-12-16 18:17:34 +01:00
2011-01-28 14:01:57 +01:00
if isequal ( options_ . mode_compute , 0 ) && isempty ( options_ . mode_file ) && options_ . mh_posterior_mode_estimation == 0
2021-01-17 16:39:10 +01:00
if options_ . order == 1 && ~ options_ . particle . status
if options_ . smoother
2021-05-17 21:24:25 +02:00
if options_ . occbin . smoother . status && options_ . occbin . smoother . inversion_filter
[ ~ , ~ , ~ , ~ , ~ , ~ , ~ , ~ , ~ , ~ , oo_ , atT , innov ] = occbin . IVF_posterior ( xparam1 , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , prior_bounds ( bayestopt_ , options_ . prior_trunc ) , oo_ ) ;
updated_variables = atT * nan ;
measurement_error = [ ] ;
ys = oo_ . dr . ys ;
trend_coeff = zeros ( length ( options_ . varobs_id ) , 1 ) ;
bayestopt_ . mf = bayestopt_ . smoother_var_list ( bayestopt_ . smoother_mf ) ;
[ oo_ ] = store_smoother_results ( M_ , oo_ , options_ , bayestopt_ , dataset_ , dataset_info , atT , innov , measurement_error , updated_variables , ys , trend_coeff ) ;
else
if options_ . occbin . smoother . status
[ atT , innov , measurement_error , updated_variables , ys , trend_coeff , aK , T , R , P , PK , decomp , Trend , state_uncertainty , M_ , oo_ , bayestopt_ ] = occbin . DSGE_smoother ( xparam1 , gend , transpose ( data ) , data_index , missing_value , M_ , oo_ , options_ , bayestopt_ , estim_params_ , dataset_ , dataset_info ) ;
else
[ atT , innov , measurement_error , updated_variables , ys , trend_coeff , aK , T , R , P , PK , decomp , Trend , state_uncertainty , M_ , oo_ , bayestopt_ ] = DsgeSmoother ( xparam1 , gend , transpose ( data ) , data_index , missing_value , M_ , oo_ , options_ , bayestopt_ , estim_params_ ) ;
end
[ oo_ ] = store_smoother_results ( M_ , oo_ , options_ , bayestopt_ , dataset_ , dataset_info , atT , innov , measurement_error , updated_variables , ys , trend_coeff , aK , P , PK , decomp , Trend , state_uncertainty ) ;
end
2021-01-17 16:39:10 +01:00
if options_ . forecast > 0
oo_ . forecast = dyn_forecast ( var_list_ , M_ , options_ , oo_ , ' smoother' , dataset_info ) ;
end
end
2021-01-17 20:06:49 +01:00
%reset qz_criterium
options_ . qz_criterium = qz_criterium_old ;
return
2021-01-17 16:39:10 +01:00
else %allow to continue, e.g. with MCMC_jumping_covariance
if options_ . smoother
error ( ' Estimation:: Particle Smoothers are not yet implemented.' )
2021-01-10 17:12:59 +01:00
end
2009-12-16 18:17:34 +01:00
end
end
2015-11-28 17:36:51 +01:00
%% Estimation of the posterior mode or likelihood mode
2011-01-28 14:01:57 +01:00
if ~ isequal ( options_ . mode_compute , 0 ) && ~ options_ . mh_posterior_mode_estimation
2017-05-16 15:10:20 +02:00
2016-06-06 18:28:45 +02:00
[ xparam1 , fval , exitflag , hh , options_ , Scale , new_rat_hess_info ] = dynare_minimize_objective ( objective_function , xparam1 , options_ . mode_compute , options_ , [ bounds . lb bounds . ub ] , bayestopt_ . name , bayestopt_ , hh , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , bounds , oo_ ) ;
2014-12-04 09:18:33 +01:00
fprintf ( ' \nFinal value of minus the log posterior (or likelihood):%f \n' , fval ) ;
2021-02-13 16:44:50 +01:00
if isnumeric ( options_ . mode_compute ) && options_ . mode_compute == 5
newratflag = new_rat_hess_info . newratflag ;
new_rat_hess_info = new_rat_hess_info . new_rat_hess_info ;
2015-04-06 11:24:42 +02:00
elseif isnumeric ( options_ . mode_compute ) && options_ . mode_compute == 6 %save scaling factor
2021-07-22 17:46:08 +02:00
save ( [ M_ . dname filesep ' Output' filesep M_ . fname ' _optimal_mh_scale_parameter.mat' ] , ' Scale' ) ;
2014-12-04 09:18:33 +01:00
options_ . mh_jscale = Scale ;
2018-05-16 16:15:42 +02:00
bayestopt_ . jscale ( : ) = options_ . mh_jscale ;
2017-05-16 15:10:20 +02:00
end
2015-04-06 11:24:42 +02:00
if ~ isnumeric ( options_ . mode_compute ) || ~ isequal ( options_ . mode_compute , 6 ) %always already computes covariance matrix
2013-10-27 10:59:04 +01:00
if options_ . cova_compute == 1 %user did not request covariance not to be computed
2017-05-16 12:42:01 +02:00
if options_ . analytic_derivation && strcmp ( func2str ( objective_function ) , ' dsge_likelihood' )
2014-12-04 09:18:33 +01:00
ana_deriv_old = options_ . analytic_derivation ;
2012-04-27 15:57:58 +02:00
options_ . analytic_derivation = 2 ;
2018-11-13 17:58:42 +01:00
[ ~ , ~ , ~ , ~ , hh ] = feval ( objective_function , xparam1 , ...
dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , bounds , oo_ ) ;
2014-12-04 09:18:33 +01:00
options_ . analytic_derivation = ana_deriv_old ;
2021-02-13 16:44:50 +01:00
elseif ~ isnumeric ( options_ . mode_compute ) || ~ ( isequal ( options_ . mode_compute , 5 ) && newratflag ~= 1 && strcmp ( func2str ( objective_function ) , ' dsge_likelihood' ) )
% with flag==0 or 2, we force to use the hessian from outer product gradient of optimizer 5
2017-02-08 12:41:56 +01:00
if options_ . hessian . use_penalized_objective
penalized_objective_function = str2func ( ' penalty_objective_function' ) ;
hh = hessian ( penalized_objective_function , xparam1 , options_ . gstep , objective_function , fval , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , bounds , oo_ ) ;
else
hh = hessian ( objective_function , xparam1 , options_ . gstep , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , bounds , oo_ ) ;
end
hh = reshape ( hh , nx , nx ) ;
2015-04-06 11:24:42 +02:00
elseif isnumeric ( options_ . mode_compute ) && isequal ( options_ . mode_compute , 5 )
2015-03-12 17:24:21 +01:00
% other numerical hessian options available with optimizer 5
%
% if newratflag == 0
% compute outer product gradient of optimizer 5
%
% if newratflag == 2
% compute 'mixed' outer product gradient of optimizer 5
% with diagonal elements computed with numerical second order derivatives
%
% uses univariate filters, so to get max # of available
2021-02-13 16:44:50 +01:00
% densities for outer product gradient
2015-03-12 17:24:21 +01:00
kalman_algo0 = options_ . kalman_algo ;
compute_hessian = 1 ;
2017-05-16 12:42:01 +02:00
if ~ ( ( options_ . kalman_algo == 2 ) || ( options_ . kalman_algo == 4 ) )
2015-03-12 17:24:21 +01:00
options_ . kalman_algo = 2 ;
2017-05-16 12:42:01 +02:00
if options_ . lik_init == 3
2015-03-12 17:24:21 +01:00
options_ . kalman_algo = 4 ;
end
2017-05-16 12:42:01 +02:00
elseif newratflag == 0 % hh already contains outer product gradient with univariate filter
2017-05-16 15:10:20 +02:00
compute_hessian = 0 ;
2015-03-12 17:24:21 +01:00
end
2017-05-16 12:42:01 +02:00
if compute_hessian
2015-03-12 17:24:21 +01:00
crit = options_ . newrat . tolerance . f ;
newratflag = newratflag > 0 ;
2021-01-21 21:26:06 +01:00
hh = reshape ( mr_hessian ( xparam1 , objective_function , fval , newratflag , crit , new_rat_hess_info , [ bounds . lb bounds . ub ] , bayestopt_ . p2 , 0 , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , bounds , oo_ ) , nx , nx ) ;
2015-03-12 17:24:21 +01:00
end
options_ . kalman_algo = kalman_algo0 ;
2012-04-27 15:57:58 +02:00
end
2009-12-16 18:17:34 +01:00
end
2011-02-04 17:17:48 +01:00
end
parameter_names = bayestopt_ . name ;
2014-12-04 09:18:33 +01:00
if options_ . cova_compute || options_ . mode_compute == 5 || options_ . mode_compute == 6
2021-07-22 17:46:08 +02:00
save ( [ M_ . dname filesep ' Output' filesep M_ . fname ' _mode.mat' ] , ' xparam1' , ' hh' , ' parameter_names' , ' fval' ) ;
2011-02-04 17:17:48 +01:00
else
2021-07-22 17:46:08 +02:00
save ( [ M_ . dname filesep ' Output' filesep M_ . fname ' _mode.mat' ] , ' xparam1' , ' parameter_names' , ' fval' ) ;
2011-02-04 17:17:48 +01:00
end
2009-12-16 18:17:34 +01:00
end
2011-05-13 12:32:23 +02:00
if ~ options_ . mh_posterior_mode_estimation && options_ . cova_compute
2010-09-01 22:15:47 +02:00
try
chol ( hh ) ;
catch
2013-07-10 16:16:32 +02:00
skipline ( )
2010-09-01 22:15:47 +02:00
disp ( ' POSTERIOR KERNEL OPTIMIZATION PROBLEM!' )
disp ( ' (minus) the hessian matrix at the "mode" is not positive definite!' )
disp ( ' => posterior variance of the estimated parameters are not positive.' )
2013-10-31 08:07:55 +01:00
disp ( ' You should try to change the initial values of the parameters using' )
2010-09-01 22:15:47 +02:00
disp ( ' the estimated_params_init block, or use another optimization routine.' )
2016-06-06 18:28:45 +02:00
params_at_bound = find ( abs ( xparam1 - bounds . ub ) < 1.e-10 | abs ( xparam1 - bounds . lb ) < 1.e-10 ) ;
2013-07-06 00:23:09 +02:00
if ~ isempty ( params_at_bound )
for ii = 1 : length ( params_at_bound )
2017-05-16 15:10:20 +02:00
params_at_bound_name { ii , 1 } = get_the_name ( params_at_bound ( ii ) , 0 , M_ , estim_params_ , options_ ) ;
2013-07-06 00:23:09 +02:00
end
disp_string = [ params_at_bound_name { 1 , : } ] ;
for ii = 2 : size ( params_at_bound_name , 1 )
disp_string = [ disp_string , ' , ' , params_at_bound_name { ii , : } ] ;
end
fprintf ( ' \nThe following parameters are at the prior bound: %s\n' , disp_string )
fprintf ( ' Some potential solutions are:\n' )
fprintf ( ' - Check your model for mistakes.\n' )
fprintf ( ' - Check whether model and data are consistent (correct observation equation).\n' )
fprintf ( ' - Shut off prior_trunc.\n' )
2014-10-20 16:18:54 +02:00
fprintf ( ' - Change the optimization bounds.\n' )
2013-07-06 00:23:09 +02:00
fprintf ( ' - Use a different mode_compute like 6 or 9.\n' )
fprintf ( ' - Check whether the parameters estimated are identified.\n' )
2016-06-06 18:28:45 +02:00
fprintf ( ' - Check prior shape (e.g. Inf density at bound(s)).\n' )
2013-07-06 00:23:09 +02:00
fprintf ( ' - Increase the informativeness of the prior.\n' )
end
2010-09-01 22:15:47 +02:00
warning ( ' The results below are most likely wrong!' ) ;
end
2009-12-16 18:17:34 +01:00
end
2019-03-19 14:26:16 +01:00
if options_ . mode_check . status && ~ options_ . mh_posterior_mode_estimation
2014-12-04 09:18:33 +01:00
ana_deriv_old = options_ . analytic_derivation ;
2012-07-05 10:22:36 +02:00
options_ . analytic_derivation = 0 ;
2014-10-20 16:18:54 +02:00
mode_check ( objective_function , xparam1 , hh , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , bounds , oo_ ) ;
2014-12-04 09:18:33 +01:00
options_ . analytic_derivation = ana_deriv_old ;
2009-12-16 18:17:34 +01:00
end
2016-06-13 11:03:41 +02:00
oo_ . posterior . optimization . mode = [ ] ;
2013-03-17 22:49:28 +01:00
oo_ . posterior . optimization . Variance = [ ] ;
2016-07-20 17:37:17 +02:00
oo_ . posterior . optimization . log_density = [ ] ;
2015-09-25 12:57:44 +02:00
invhess = [ ] ;
2011-05-13 14:34:53 +02:00
if ~ options_ . mh_posterior_mode_estimation
2016-06-13 11:03:41 +02:00
oo_ . posterior . optimization . mode = xparam1 ;
2016-07-20 17:37:17 +02:00
if exist ( ' fval' , ' var' )
oo_ . posterior . optimization . log_density = - fval ;
end
2011-05-13 14:34:53 +02:00
if options_ . cova_compute
2016-09-05 12:16:07 +02:00
hsd = sqrt ( diag ( hh ) ) ;
invhess = inv ( hh ./ ( hsd * hsd ' ) ) ./ ( hsd * hsd ' ) ;
2011-05-13 14:34:53 +02:00
stdh = sqrt ( diag ( invhess ) ) ;
2013-03-17 22:49:28 +01:00
oo_ . posterior . optimization . Variance = invhess ;
2011-05-13 14:34:53 +02:00
end
2009-12-16 18:17:34 +01:00
else
2010-09-01 22:15:47 +02:00
variances = bayestopt_ . p2 .* bayestopt_ . p2 ;
idInf = isinf ( variances ) ;
variances ( idInf ) = 1 ;
invhess = options_ . mh_posterior_mode_estimation * diag ( variances ) ;
xparam1 = bayestopt_ . p5 ;
idNaN = isnan ( xparam1 ) ;
xparam1 ( idNaN ) = bayestopt_ . p1 ( idNaN ) ;
2015-09-25 12:42:17 +02:00
outside_bound_pars = find ( xparam1 < bounds . lb | xparam1 > bounds . ub ) ;
xparam1 ( outside_bound_pars ) = bayestopt_ . p1 ( outside_bound_pars ) ;
2009-12-16 18:17:34 +01:00
end
2011-05-13 12:32:23 +02:00
if ~ options_ . cova_compute
stdh = NaN ( length ( xparam1 ) , 1 ) ;
end
2009-12-16 18:17:34 +01:00
2021-05-11 11:40:51 +02:00
if options_ . particle . status && isfield ( options_ . particle , ' posterior_sampler' )
if strcmpi ( options_ . particle . posterior_sampler , ' Herbst_Schorfheide' )
Herbst_Schorfheide_sampler ( objective_function , xparam1 , bounds , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , oo_ )
elseif strcmpi ( options_ . particle . posterior_sampler , ' DSMH' )
DSMH_sampler ( objective_function , xparam1 , bounds , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , oo_ )
end
end
2011-02-10 15:54:23 +01:00
if any ( bayestopt_ . pshape > 0 ) && ~ options_ . mh_posterior_mode_estimation
2013-05-21 16:38:17 +02:00
% display results table and store parameter estimates and standard errors in results
2020-06-26 12:06:32 +02:00
oo_ = display_estimation_results_table ( xparam1 , stdh , M_ , options_ , estim_params_ , bayestopt_ , oo_ , prior_dist_names , ' Posterior' , ' posterior' ) ;
2013-05-21 16:38:17 +02:00
% Laplace approximation to the marginal log density:
2011-05-13 14:55:10 +02:00
if options_ . cova_compute
estim_params_nbr = size ( xparam1 , 1 ) ;
2019-11-21 11:32:13 +01:00
if ispd ( invhess )
log_det_invhess = log ( det ( invhess ./ ( stdh * stdh ' ) ) ) + 2 * sum ( log ( stdh ) ) ;
likelihood = feval ( objective_function , xparam1 , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , bounds , oo_ ) ;
oo_ . MarginalDensity . LaplaceApproximation = . 5 * estim_params_nbr * log ( 2 * pi ) + . 5 * log_det_invhess - likelihood ;
else
oo_ . MarginalDensity . LaplaceApproximation = NaN ;
end
2013-07-10 16:16:32 +02:00
skipline ( )
2011-09-22 11:17:31 +02:00
disp ( sprintf ( ' Log data density [Laplace approximation] is %f.' , oo_ . MarginalDensity . LaplaceApproximation ) )
2013-07-10 16:16:32 +02:00
skipline ( )
2009-12-16 18:17:34 +01:00
end
2016-06-13 11:40:41 +02:00
if options_ . dsge_var
2018-11-13 17:58:42 +01:00
[ ~ , ~ , ~ , ~ , ~ , ~ , ~ , oo_ . dsge_var . posterior_mode . PHI_tilde , oo_ . dsge_var . posterior_mode . SIGMA_u_tilde , oo_ . dsge_var . posterior_mode . iXX , oo_ . dsge_var . posterior_mode . prior ] = ...
2016-06-13 11:40:41 +02:00
feval ( objective_function , xparam1 , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , bounds , oo_ ) ;
end
2012-03-15 15:32:31 +01:00
elseif ~ any ( bayestopt_ . pshape > 0 ) && ~ options_ . mh_posterior_mode_estimation
2020-06-26 12:06:32 +02:00
oo_ = display_estimation_results_table ( xparam1 , stdh , M_ , options_ , estim_params_ , bayestopt_ , oo_ , prior_dist_names , ' Maximum Likelihood' , ' mle' ) ;
2009-12-16 18:17:34 +01:00
end
if np > 0
pindx = estim_params_ . param_vals ( : , 1 ) ;
2021-07-22 17:46:08 +02:00
save ( [ M_ . dname filesep ' Output' filesep M_ . fname ' _params.mat' ] , ' pindx' ) ;
2009-12-16 18:17:34 +01:00
end
2016-12-27 12:07:22 +01:00
switch options_ . MCMC_jumping_covariance
2017-05-16 15:10:20 +02:00
case ' hessian' %Baseline
%do nothing and use hessian from mode_compute
case ' prior_variance' %Use prior variance
if any ( isinf ( bayestopt_ . p2 ) )
error ( ' Infinite prior variances detected. You cannot use the prior variances as the proposal density, if some variances are Inf.' )
else
hh = diag ( 1. / ( bayestopt_ . p2 .^ 2 ) ) ;
end
hsd = sqrt ( diag ( hh ) ) ;
invhess = inv ( hh ./ ( hsd * hsd ' ) ) ./ ( hsd * hsd ' ) ;
case ' identity_matrix' %Use identity
invhess = eye ( nx ) ;
otherwise %user specified matrix in file
try
load ( options_ . MCMC_jumping_covariance , ' jumping_covariance' )
hh = jumping_covariance ;
catch
error ( [ ' No matrix named ' ' jumping_covariance' ' could be found in ' , options_ . MCMC_jumping_covariance , ' .mat' ] )
end
[ nrow , ncol ] = size ( hh ) ;
if ~ isequal ( nrow , ncol ) && ~ isequal ( nrow , nx ) %check if square and right size
error ( [ ' jumping_covariance matrix must be square and have ' , num2str ( nx ) , ' rows and columns' ] )
end
try %check for positive definiteness
chol ( hh ) ;
2016-12-27 12:07:22 +01:00
hsd = sqrt ( diag ( hh ) ) ;
invhess = inv ( hh ./ ( hsd * hsd ' ) ) ./ ( hsd * hsd ' ) ;
2017-05-16 15:10:20 +02:00
catch
error ( [ ' Specified jumping_covariance is not positive definite' ] )
end
2016-12-27 12:07:22 +01:00
end
2011-02-10 15:54:23 +01:00
if ( any ( bayestopt_ . pshape > 0 ) && options_ . mh_replic ) || ...
( any ( bayestopt_ . pshape > 0 ) && options_ . load_mh_file ) %% not ML estimation
2018-01-31 23:25:03 +01:00
bounds = prior_bounds ( bayestopt_ , options_ . prior_trunc ) ; %reset bounds as lb and ub must only be operational during mode-finding
2014-10-20 16:18:54 +02:00
outside_bound_pars = find ( xparam1 < bounds . lb | xparam1 > bounds . ub ) ;
2013-07-06 00:20:07 +02:00
if ~ isempty ( outside_bound_pars )
for ii = 1 : length ( outside_bound_pars )
outside_bound_par_names { ii , 1 } = get_the_name ( ii , 0 , M_ , estim_params_ , options_ ) ;
end
disp_string = [ outside_bound_par_names { 1 , : } ] ;
for ii = 2 : size ( outside_bound_par_names , 1 )
disp_string = [ disp_string , ' , ' , outside_bound_par_names { ii , : } ] ;
2013-05-12 13:27:55 +02:00
end
2013-11-27 12:27:51 +01:00
if options_ . prior_trunc > 0
error ( [ ' Estimation:: Mode value(s) of ' , disp_string , ' are outside parameter bounds. Potentially, you should set prior_trunc=0.' ] )
else
error ( [ ' Estimation:: Mode value(s) of ' , disp_string , ' are outside parameter bounds.' ] )
end
2009-12-16 18:17:34 +01:00
end
2018-05-16 16:15:42 +02:00
% Tunes the jumping distribution's scale parameter
if options_ . mh_tune_jscale . status
2020-03-10 14:13:49 +01:00
if strcmp ( options_ . posterior_sampler_options . posterior_sampling_method , ' random_walk_metropolis_hastings' )
2021-01-20 10:33:12 +01:00
%get invhess in case of use_mh_covariance_matrix
posterior_sampler_options_temp = options_ . posterior_sampler_options . current_options ;
posterior_sampler_options_temp . invhess = invhess ;
posterior_sampler_options_temp = check_posterior_sampler_options ( posterior_sampler_options_temp , options_ ) ;
2018-05-16 16:15:42 +02:00
options = options_ . mh_tune_jscale ;
options . rwmh = options_ . posterior_sampler_options . rwmh ;
options_ . mh_jscale = calibrate_mh_scale_parameter ( objective_function , ...
2021-01-20 10:33:12 +01:00
posterior_sampler_options_temp . invhess , xparam1 , [ bounds . lb , bounds . ub ] , ...
2019-12-20 16:28:06 +01:00
options , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , bounds , oo_ ) ;
2021-01-20 10:33:12 +01:00
clear ( ' posterior_sampler_options_temp' , ' options' )
2019-12-20 16:28:06 +01:00
bayestopt_ . jscale ( : ) = options_ . mh_jscale ;
2021-01-20 10:33:12 +01:00
fprintf ( ' mh_jscale has been set equal to %s\n' , num2str ( options_ . mh_jscale ) )
2018-05-16 16:15:42 +02:00
else
warning ( ' mh_tune_jscale is only available with Random Walk Metropolis Hastings!' )
end
end
2009-12-16 18:17:34 +01:00
% runs MCMC
2017-01-17 19:23:27 +01:00
if options_ . mh_replic || options_ . load_mh_file
2016-05-13 21:35:59 +02:00
posterior_sampler_options = options_ . posterior_sampler_options . current_options ;
2015-09-25 12:57:44 +02:00
posterior_sampler_options . invhess = invhess ;
[ posterior_sampler_options , options_ ] = check_posterior_sampler_options ( posterior_sampler_options , options_ ) ;
2016-05-13 21:35:59 +02:00
% store current options in global
options_ . posterior_sampler_options . current_options = posterior_sampler_options ;
2017-01-17 19:23:27 +01:00
if options_ . mh_replic
ana_deriv_old = options_ . analytic_derivation ;
options_ . analytic_derivation = 0 ;
posterior_sampler ( objective_function , posterior_sampler_options . proposal_distribution , xparam1 , posterior_sampler_options , bounds , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , oo_ ) ;
options_ . analytic_derivation = ana_deriv_old ;
end
2009-12-16 18:17:34 +01:00
end
2015-09-25 12:39:05 +02:00
%% Here I discard first mh_drop percent of the draws:
CutSample ( M_ , options_ , estim_params_ ) ;
2010-09-01 22:15:47 +02:00
if options_ . mh_posterior_mode_estimation
2016-06-16 22:40:54 +02:00
%reset qz_criterium
options_ . qz_criterium = qz_criterium_old ;
2010-09-01 22:15:47 +02:00
return
2010-02-11 14:26:30 +01:00
else
2016-12-17 16:05:17 +01:00
%get stored results if required
if options_ . load_mh_file && options_ . load_results_after_load_mh
2021-07-22 17:46:08 +02:00
oo_load_mh = load ( [ M_ . dname filesep ' Output' filesep M_ . fname ' _results' ] , ' oo_' ) ;
2016-12-17 16:05:17 +01:00
end
2017-05-16 15:10:20 +02:00
if ~ options_ . nodiagnostic
2016-12-17 16:05:17 +01:00
if ( options_ . mh_replic > 0 || ( options_ . load_mh_file && ~ options_ . load_results_after_load_mh ) )
oo_ = McMCDiagnostics ( options_ , estim_params_ , M_ , oo_ ) ;
elseif options_ . load_mh_file && options_ . load_results_after_load_mh
if isfield ( oo_load_mh . oo_ , ' convergence' )
oo_ . convergence = oo_load_mh . oo_ . convergence ;
end
end
2009-12-16 18:17:34 +01:00
end
2010-09-01 22:15:47 +02:00
%% Estimation of the marginal density from the Mh draws:
2016-12-17 16:05:17 +01:00
if options_ . mh_replic || ( options_ . load_mh_file && ~ options_ . load_results_after_load_mh )
2015-09-25 18:23:21 +02:00
[ marginal , oo_ ] = marginal_density ( M_ , options_ , estim_params_ , oo_ , bayestopt_ ) ;
2013-11-03 11:40:36 +01:00
% Store posterior statistics by parameter name
2020-06-26 12:06:32 +02:00
oo_ = GetPosteriorParametersStatistics ( estim_params_ , M_ , options_ , bayestopt_ , oo_ , prior_dist_names ) ;
2012-11-29 14:26:39 +01:00
if ~ options_ . nograph
oo_ = PlotPosteriorDistributions ( estim_params_ , M_ , options_ , bayestopt_ , oo_ ) ;
end
2013-11-03 11:40:36 +01:00
% Store posterior mean in a vector and posterior variance in
% a matrix
2013-03-17 22:49:28 +01:00
[ oo_ . posterior . metropolis . mean , oo_ . posterior . metropolis . Variance ] ...
2012-04-21 21:28:03 +02:00
= GetPosteriorMeanVariance ( M_ , options_ . mh_drop ) ;
2016-12-17 16:05:17 +01:00
elseif options_ . load_mh_file && options_ . load_results_after_load_mh
2017-05-16 15:10:20 +02:00
%% load fields from previous MCMC run stored in results-file
2016-12-17 16:05:17 +01:00
field_names = { ' posterior_mode' , ' posterior_std_at_mode' , ...% fields set by marginal_density
2017-05-16 15:10:20 +02:00
' posterior_mean' , ' posterior_hpdinf' , ' posterior_hpdsup' , ' posterior_median' , ' posterior_variance' , ' posterior_std' , ' posterior_deciles' , ' posterior_density' , ...% fields set by GetPosteriorParametersStatistics
' prior_density' , ...%fields set by PlotPosteriorDistributions
} ;
2016-12-17 16:05:17 +01:00
for field_iter = 1 : size ( field_names , 2 )
if isfield ( oo_load_mh . oo_ , field_names { 1 , field_iter } )
oo_ . ( field_names { 1 , field_iter } ) = oo_load_mh . oo_ . ( field_names { 1 , field_iter } ) ;
end
2017-05-16 15:10:20 +02:00
end
2016-12-17 16:05:17 +01:00
% field set by marginal_density
if isfield ( oo_load_mh . oo_ , ' MarginalDensity' ) && isfield ( oo_load_mh . oo_ . MarginalDensity , ' ModifiedHarmonicMean' )
oo_ . MarginalDensity . ModifiedHarmonicMean = oo_load_mh . oo_ . MarginalDensity . ModifiedHarmonicMean ;
2017-05-16 15:10:20 +02:00
end
2016-12-17 16:05:17 +01:00
% field set by GetPosteriorMeanVariance
if isfield ( oo_load_mh . oo_ , ' posterior' ) && isfield ( oo_load_mh . oo_ . posterior , ' metropolis' )
oo_ . posterior . metropolis = oo_load_mh . oo_ . posterior . metropolis ;
end
2009-12-16 18:17:34 +01:00
end
2018-11-13 17:58:42 +01:00
[ error_flag , ~ , options_ ] = metropolis_draw ( 1 , options_ , estim_params_ , M_ ) ;
2020-01-23 17:55:55 +01:00
if ~ ( ~ isempty ( options_ . sub_draws ) && options_ . sub_draws == 0 )
if options_ . bayesian_irf
if error_flag
error ( ' Estimation::mcmc: I cannot compute the posterior IRFs!' )
end
PosteriorIRF ( ' posterior' ) ;
2013-12-18 16:39:41 +01:00
end
2020-01-23 17:55:55 +01:00
if options_ . moments_varendo
if error_flag
error ( ' Estimation::mcmc: I cannot compute the posterior moments for the endogenous variables!' )
end
2021-01-25 15:55:10 +01:00
if options_ . load_mh_file && options_ . mh_replic == 0 %user wants to recompute results
[ MetropolisFolder , info ] = CheckPath ( ' metropolis' , M_ . dname ) ;
if ~ info
generic_post_data_file_name = { ' Posterior2ndOrderMoments' , ' decomposition' , ' PosteriorVarianceDecomposition' , ' correlation' , ' PosteriorCorrelations' , ' conditional decomposition' , ' PosteriorConditionalVarianceDecomposition' } ;
for ii = 1 : length ( generic_post_data_file_name )
delete_stale_file ( [ MetropolisFolder filesep M_ . fname ' _' generic_post_data_file_name { 1 , ii } ' *' ] ) ;
end
end
end
2020-01-23 17:55:55 +01:00
oo_ = compute_moments_varendo ( ' posterior' , options_ , M_ , oo_ , var_list_ ) ;
2013-12-18 16:39:41 +01:00
end
2020-01-23 17:55:55 +01:00
if options_ . smoother || ~ isempty ( options_ . filter_step_ahead ) || options_ . forecast
if error_flag
error ( ' Estimation::mcmc: I cannot compute the posterior statistics!' )
end
2021-01-17 16:39:10 +01:00
if options_ . order == 1 && ~ options_ . particle . status
prior_posterior_statistics ( ' posterior' , dataset_ , dataset_info ) ; %get smoothed and filtered objects and forecasts
else
error ( ' Estimation::mcmc: Particle Smoothers are not yet implemented.' )
end
2013-12-18 16:39:41 +01:00
end
2020-01-23 17:55:55 +01:00
else
fprintf ( ' Estimation:mcmc: sub_draws was set to 0. Skipping posterior computations.' )
2010-09-01 22:15:47 +02:00
end
2017-10-03 15:32:17 +02:00
xparam1 = get_posterior_parameters ( ' mean' , M_ , estim_params_ , oo_ , options_ ) ;
2014-10-15 09:49:07 +02:00
M_ = set_all_parameters ( xparam1 , estim_params_ , M_ ) ;
2010-09-13 21:34:15 +02:00
end
end
2010-09-01 22:15:47 +02:00
2012-06-06 17:04:27 +02:00
if options_ . particle . status
2016-06-16 22:40:54 +02:00
%reset qz_criterium
options_ . qz_criterium = qz_criterium_old ;
2012-06-06 17:04:27 +02:00
return
end
2017-05-16 12:42:01 +02:00
if ( ~ ( ( any ( bayestopt_ . pshape > 0 ) && options_ . mh_replic ) || ( any ( bayestopt_ . pshape > 0 ) && options_ . load_mh_file ) ) ...
2019-03-19 14:26:16 +01:00
|| ~ options_ . smoother ) && ~ options_ . partial_information % to be fixed
2016-05-17 20:55:33 +02:00
%% ML estimation, or posterior mode without Metropolis-Hastings or Metropolis without Bayesian smoothes variables
2021-05-17 21:24:25 +02:00
if options_ . occbin . smoother . status && options_ . occbin . smoother . inversion_filter
[ ~ , ~ , ~ , ~ , ~ , ~ , ~ , ~ , ~ , ~ , oo_ , atT , innov ] = occbin . IVF_posterior ( xparam1 , dataset_ , dataset_info , options_ , M_ , estim_params_ , bayestopt_ , prior_bounds ( bayestopt_ , options_ . prior_trunc ) , oo_ ) ;
updated_variables = atT * nan ;
measurement_error = [ ] ;
ys = oo_ . dr . ys ;
trend_coeff = zeros ( length ( options_ . varobs_id ) , 1 ) ;
bayestopt_ . mf = bayestopt_ . smoother_var_list ( bayestopt_ . smoother_mf ) ;
[ oo_ , yf ] = store_smoother_results ( M_ , oo_ , options_ , bayestopt_ , dataset_ , dataset_info , atT , innov , measurement_error , updated_variables , ys , trend_coeff ) ;
else
if options_ . occbin . smoother . status
[ atT , innov , measurement_error , updated_variables , ys , trend_coeff , aK , T , R , P , PK , decomp , Trend , state_uncertainty , M_ , oo_ , bayestopt_ ] = occbin . DSGE_smoother ( xparam1 , dataset_ . nobs , transpose ( dataset_ . data ) , dataset_info . missing . aindex , dataset_info . missing . state , M_ , oo_ , options_ , bayestopt_ , estim_params_ , dataset_ , dataset_info ) ;
else
[ atT , innov , measurement_error , updated_variables , ys , trend_coeff , aK , T , R , P , PK , decomp , Trend , state_uncertainty , M_ , oo_ , bayestopt_ ] = DsgeSmoother ( xparam1 , dataset_ . nobs , transpose ( dataset_ . data ) , dataset_info . missing . aindex , dataset_info . missing . state , M_ , oo_ , options_ , bayestopt_ , estim_params_ ) ;
end
[ oo_ , yf ] = store_smoother_results ( M_ , oo_ , options_ , bayestopt_ , dataset_ , dataset_info , atT , innov , measurement_error , updated_variables , ys , trend_coeff , aK , P , PK , decomp , Trend , state_uncertainty ) ;
end
2017-05-16 12:42:01 +02:00
if ~ options_ . nograph
2012-02-13 11:58:28 +01:00
[ nbplt , nr , nc , lr , lc , nstar ] = pltorg ( M_ . exo_nbr ) ;
2020-12-16 22:38:22 +01:00
if ~ exist ( [ M_ . dname ' /graphs' ] , ' dir' )
mkdir ( M_ . dname , ' graphs' ) ;
2019-11-21 11:58:16 +01:00
end
2015-05-12 08:53:57 +02:00
if options_ . TeX && any ( strcmp ( ' eps' , cellstr ( options_ . graph_format ) ) )
2020-12-16 22:38:22 +01:00
fidTeX = fopen ( [ M_ . dname , ' /graphs/' M_ . fname ' _SmoothedShocks.tex' ] , ' w' ) ;
2015-05-12 08:35:41 +02:00
fprintf ( fidTeX , ' %% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n' ) ;
2012-02-13 11:58:28 +01:00
fprintf ( fidTeX , [ ' %% ' datestr ( now , 0 ) ' \n' ] ) ;
fprintf ( fidTeX , ' \n' ) ;
2010-09-13 21:34:15 +02:00
end
2017-05-16 12:42:01 +02:00
for plt = 1 : nbplt
2017-03-22 16:54:11 +01:00
fh = dyn_figure ( options_ . nodisplay , ' Name' , ' Smoothed shocks' ) ;
2010-09-13 21:34:15 +02:00
NAMES = [ ] ;
if options_ . TeX , TeXNAMES = [ ] ; end
2012-02-13 11:58:28 +01:00
nstar0 = min ( nstar , M_ . exo_nbr - ( plt - 1 ) * nstar ) ;
2013-08-15 16:35:01 +02:00
if gend == 1
marker_string { 1 , 1 } = ' -ro' ;
marker_string { 2 , 1 } = ' -ko' ;
else
marker_string { 1 , 1 } = ' -r' ;
marker_string { 2 , 1 } = ' -k' ;
end
2017-05-16 12:42:01 +02:00
for i = 1 : nstar0
2010-09-13 21:34:15 +02:00
k = ( plt - 1 ) * nstar + i ;
subplot ( nr , nc , i ) ;
2013-08-15 16:35:01 +02:00
plot ( [ 1 gend ] , [ 0 0 ] , marker_string { 1 , 1 } , ' linewidth' , . 5 )
2010-09-13 21:34:15 +02:00
hold on
2013-08-15 16:35:01 +02:00
plot ( 1 : gend , innov ( k , : ) , marker_string { 2 , 1 } , ' linewidth' , 1 )
2010-09-13 21:34:15 +02:00
hold off
2017-10-10 10:05:59 +02:00
name = M_ . exo_names { k } ;
2010-09-13 21:34:15 +02:00
if ~ isempty ( options_ . XTick )
set ( gca , ' XTick' , options_ . XTick )
set ( gca , ' XTickLabel' , options_ . XTickLabel )
2009-12-16 18:17:34 +01:00
end
2013-08-15 16:35:01 +02:00
if gend > 1
xlim ( [ 1 gend ] )
end
2010-09-13 21:34:15 +02:00
if options_ . TeX
2020-06-17 20:23:49 +02:00
title ( [ ' $' M_ . exo_names_tex { k } ' $' ] , ' Interpreter' , ' latex' )
else
title ( name , ' Interpreter' , ' none' )
2011-09-17 15:38:49 +02:00
end
2009-12-16 18:17:34 +01:00
end
2020-12-16 22:38:22 +01:00
dyn_saveas ( fh , [ M_ . dname , ' /graphs/' M_ . fname ' _SmoothedShocks' int2str ( plt ) ] , options_ . nodisplay , options_ . graph_format ) ;
2015-05-12 08:53:57 +02:00
if options_ . TeX && any ( strcmp ( ' eps' , cellstr ( options_ . graph_format ) ) )
2010-09-13 21:34:15 +02:00
fprintf ( fidTeX , ' \\begin{figure}[H]\n' ) ;
fprintf ( fidTeX , ' \\centering \n' ) ;
2020-12-16 22:38:22 +01:00
fprintf ( fidTeX , ' \\includegraphics[width=%2.2f\\textwidth]{%s_SmoothedShocks%s}\n' , options_ . figures . textwidth * min ( i / nc , 1 ) , [ M_ . dname , ' /graphs/' M_ . fname ] , int2str ( plt ) ) ;
2010-09-13 21:34:15 +02:00
fprintf ( fidTeX , ' \\caption{Smoothed shocks.}' ) ;
fprintf ( fidTeX , ' \\label{Fig:SmoothedShocks:%s}\n' , int2str ( plt ) ) ;
fprintf ( fidTeX , ' \\end{figure}\n' ) ;
fprintf ( fidTeX , ' \n' ) ;
2011-09-17 15:38:49 +02:00
end
2010-09-13 21:34:15 +02:00
end
2015-05-12 08:53:57 +02:00
if options_ . TeX && any ( strcmp ( ' eps' , cellstr ( options_ . graph_format ) ) )
2010-09-13 21:34:15 +02:00
fprintf ( fidTeX , ' \n' ) ;
fprintf ( fidTeX , ' %% End of TeX file.\n' ) ;
fclose ( fidTeX ) ;
2011-09-17 15:38:49 +02:00
end
2010-09-13 21:34:15 +02:00
end
if nvn
number_of_plots_to_draw = 0 ;
index = [ ] ;
2017-08-16 12:57:55 +02:00
for obs_iter = 1 : n_varobs
2017-08-16 13:11:35 +02:00
if max ( abs ( measurement_error ( obs_iter , : ) ) ) > options_ . ME_plot_tol ;
2010-09-13 21:34:15 +02:00
number_of_plots_to_draw = number_of_plots_to_draw + 1 ;
2017-08-16 12:57:55 +02:00
index = cat ( 1 , index , obs_iter ) ;
2010-09-13 21:34:15 +02:00
end
end
2012-02-13 11:58:28 +01:00
if ~ options_ . nograph
[ nbplt , nr , nc , lr , lc , nstar ] = pltorg ( number_of_plots_to_draw ) ;
2015-05-12 08:53:57 +02:00
if options_ . TeX && any ( strcmp ( ' eps' , cellstr ( options_ . graph_format ) ) )
2020-12-16 22:38:22 +01:00
fidTeX = fopen ( [ M_ . dname , ' /graphs/' M_ . fname ' _SmoothedObservationErrors.tex' ] , ' w' ) ;
2015-05-12 08:35:41 +02:00
fprintf ( fidTeX , ' %% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n' ) ;
2012-02-13 11:58:28 +01:00
fprintf ( fidTeX , [ ' %% ' datestr ( now , 0 ) ' \n' ] ) ;
fprintf ( fidTeX , ' \n' ) ;
2010-09-13 21:34:15 +02:00
end
2011-11-02 15:49:02 +01:00
for plt = 1 : nbplt
2017-03-22 16:54:11 +01:00
fh = dyn_figure ( options_ . nodisplay , ' Name' , ' Smoothed observation errors' ) ;
2017-08-16 12:57:55 +02:00
nstar0 = min ( nstar , number_of_plots_to_draw - ( plt - 1 ) * nstar ) ;
2013-08-15 16:35:01 +02:00
if gend == 1
marker_string { 1 , 1 } = ' -ro' ;
marker_string { 2 , 1 } = ' -ko' ;
else
marker_string { 1 , 1 } = ' -r' ;
marker_string { 2 , 1 } = ' -k' ;
end
2013-03-19 21:12:49 +01:00
for i = 1 : nstar0
2010-09-13 21:34:15 +02:00
k = ( plt - 1 ) * nstar + i ;
subplot ( nr , nc , i ) ;
2013-08-15 16:35:01 +02:00
plot ( [ 1 gend ] , [ 0 0 ] , marker_string { 1 , 1 } , ' linewidth' , . 5 )
2009-12-16 18:17:34 +01:00
hold on
2013-08-15 16:35:01 +02:00
plot ( 1 : gend , measurement_error ( index ( k ) , : ) , marker_string { 2 , 1 } , ' linewidth' , 1 )
2009-12-16 18:17:34 +01:00
hold off
2013-05-21 16:38:17 +02:00
name = options_ . varobs { index ( k ) } ;
2013-08-15 16:35:01 +02:00
if gend > 1
xlim ( [ 1 gend ] )
end
2009-12-16 18:17:34 +01:00
if ~ isempty ( options_ . XTick )
set ( gca , ' XTick' , options_ . XTick )
set ( gca , ' XTickLabel' , options_ . XTickLabel )
end
if options_ . TeX
2017-10-10 10:05:59 +02:00
idx = strmatch ( options_ . varobs { index ( k ) } , M_ . endo_names , ' exact' ) ;
2020-06-17 20:23:49 +02:00
title ( [ ' $' M_ . endo_names_tex { idx } ' $' ] , ' Interpreter' , ' latex' )
else
title ( name , ' Interpreter' , ' none' )
2010-09-26 13:42:41 +02:00
end
2009-12-16 18:17:34 +01:00
end
2020-12-16 22:38:22 +01:00
dyn_saveas ( fh , [ M_ . dname , ' /graphs/' M_ . fname ' _SmoothedObservationErrors' int2str ( plt ) ] , options_ . nodisplay , options_ . graph_format ) ;
2015-05-12 08:53:57 +02:00
if options_ . TeX && any ( strcmp ( ' eps' , cellstr ( options_ . graph_format ) ) )
2009-12-16 18:17:34 +01:00
fprintf ( fidTeX , ' \\begin{figure}[H]\n' ) ;
fprintf ( fidTeX , ' \\centering \n' ) ;
2020-12-16 22:38:22 +01:00
fprintf ( fidTeX , ' \\includegraphics[width=%2.2f\\textwidth]{%s_SmoothedObservationErrors%s}\n' , options_ . figures . textwidth * min ( i / nc , 1 ) , [ M_ . dname , ' /graphs/' M_ . fname ] , int2str ( plt ) ) ;
2010-09-13 21:34:15 +02:00
fprintf ( fidTeX , ' \\caption{Smoothed observation errors.}' ) ;
fprintf ( fidTeX , ' \\label{Fig:SmoothedObservationErrors:%s}\n' , int2str ( plt ) ) ;
2009-12-16 18:17:34 +01:00
fprintf ( fidTeX , ' \\end{figure}\n' ) ;
fprintf ( fidTeX , ' \n' ) ;
2011-09-17 15:38:49 +02:00
end
2009-12-16 18:17:34 +01:00
end
2015-05-12 08:53:57 +02:00
if options_ . TeX && any ( strcmp ( ' eps' , cellstr ( options_ . graph_format ) ) )
2012-02-13 11:58:28 +01:00
fprintf ( fidTeX , ' \n' ) ;
fprintf ( fidTeX , ' %% End of TeX file.\n' ) ;
fclose ( fidTeX ) ;
end
2010-09-13 21:34:15 +02:00
end
2011-09-17 15:38:49 +02:00
end
2010-09-13 21:34:15 +02:00
%%
%% Historical and smoothed variabes
%%
2012-02-13 11:58:28 +01:00
if ~ options_ . nograph
2017-05-16 15:10:20 +02:00
[ nbplt , nr , nc , lr , lc , nstar ] = pltorg ( n_varobs ) ;
if options_ . TeX && any ( strcmp ( ' eps' , cellstr ( options_ . graph_format ) ) )
2020-12-16 22:38:22 +01:00
fidTeX = fopen ( [ M_ . dname , ' /graphs/' M_ . fname ' _HistoricalAndSmoothedVariables.tex' ] , ' w' ) ;
2017-05-16 15:10:20 +02:00
fprintf ( fidTeX , ' %% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n' ) ;
fprintf ( fidTeX , [ ' %% ' datestr ( now , 0 ) ' \n' ] ) ;
fprintf ( fidTeX , ' \n' ) ;
2013-08-15 16:35:01 +02:00
end
2017-05-16 15:10:20 +02:00
for plt = 1 : nbplt
fh = dyn_figure ( options_ . nodisplay , ' Name' , ' Historical and smoothed variables' ) ;
NAMES = [ ] ;
nstar0 = min ( nstar , n_varobs - ( plt - 1 ) * nstar ) ;
if gend == 1
marker_string { 1 , 1 } = ' -ro' ;
marker_string { 2 , 1 } = ' --ko' ;
2010-09-26 13:42:41 +02:00
else
2017-05-16 15:10:20 +02:00
marker_string { 1 , 1 } = ' -r' ;
marker_string { 2 , 1 } = ' --k' ;
2013-08-15 16:35:01 +02:00
end
2017-05-16 15:10:20 +02:00
for i = 1 : nstar0
k = ( plt - 1 ) * nstar + i ;
subplot ( nr , nc , i ) ;
plot ( 1 : gend , yf ( k , : ) , marker_string { 1 , 1 } , ' linewidth' , 1 )
hold on
plot ( 1 : gend , rawdata ( : , k ) , marker_string { 2 , 1 } , ' linewidth' , 1 )
hold off
name = options_ . varobs { k } ;
if ~ isempty ( options_ . XTick )
set ( gca , ' XTick' , options_ . XTick )
set ( gca , ' XTickLabel' , options_ . XTickLabel )
end
if gend > 1
xlim ( [ 1 gend ] )
end
if options_ . TeX
2017-10-10 10:05:59 +02:00
idx = strmatch ( options_ . varobs { k } , M_ . endo_names , ' exact' ) ;
2020-06-17 20:23:49 +02:00
title ( [ ' $' M_ . endo_names_tex { idx } ' $' ] , ' Interpreter' , ' latex' )
else
title ( name , ' Interpreter' , ' none' )
2010-09-26 13:42:41 +02:00
end
2017-05-16 15:10:20 +02:00
end
2020-12-16 22:38:22 +01:00
dyn_saveas ( fh , [ M_ . dname , ' /graphs/' M_ . fname ' _HistoricalAndSmoothedVariables' int2str ( plt ) ] , options_ . nodisplay , options_ . graph_format ) ;
2017-05-16 15:10:20 +02:00
if options_ . TeX && any ( strcmp ( ' eps' , cellstr ( options_ . graph_format ) ) )
fprintf ( fidTeX , ' \\begin{figure}[H]\n' ) ;
fprintf ( fidTeX , ' \\centering \n' ) ;
2020-12-16 22:38:22 +01:00
fprintf ( fidTeX , ' \\includegraphics[width=%2.2f\\textwidth]{%s_HistoricalAndSmoothedVariables%s}\n' , options_ . figures . textwidth * min ( i / nc , 1 ) , [ M_ . dname , ' /graphs/' M_ . fname ] , int2str ( plt ) ) ;
2017-05-16 15:10:20 +02:00
fprintf ( fidTeX , ' \\caption{Historical and smoothed variables.}' ) ;
fprintf ( fidTeX , ' \\label{Fig:HistoricalAndSmoothedVariables:%s}\n' , int2str ( plt ) ) ;
fprintf ( fidTeX , ' \\end{figure}\n' ) ;
fprintf ( fidTeX , ' \n' ) ;
2010-09-13 21:34:15 +02:00
end
end
2015-05-12 08:53:57 +02:00
if options_ . TeX && any ( strcmp ( ' eps' , cellstr ( options_ . graph_format ) ) )
2010-09-13 21:34:15 +02:00
fprintf ( fidTeX , ' \n' ) ;
2017-05-16 15:10:20 +02:00
fprintf ( fidTeX , ' %% End of TeX file.\n' ) ;
fclose ( fidTeX ) ;
2009-12-16 18:17:34 +01:00
end
end
2010-09-01 22:15:47 +02:00
end
2010-09-13 21:34:15 +02:00
2011-09-17 15:38:49 +02:00
if options_ . forecast > 0 && options_ . mh_replic == 0 && ~ options_ . load_mh_file
2015-03-02 10:45:12 +01:00
oo_ . forecast = dyn_forecast ( var_list_ , M_ , options_ , oo_ , ' smoother' , dataset_info ) ;
2009-12-16 18:17:34 +01:00
end
if np > 0
pindx = estim_params_ . param_vals ( : , 1 ) ;
2021-07-22 17:46:08 +02:00
save ( [ M_ . dname filesep ' Output' filesep M_ . fname ' _pindx.mat' ] , ' pindx' ) ;
2009-12-16 18:17:34 +01:00
end
2016-06-16 22:40:54 +02:00
%reset qz_criterium
options_ . qz_criterium = qz_criterium_old ;
2017-03-16 15:45:08 +01:00
if reset_options_related_to_estimation
options_ . mode_compute = mode_compute_o ;
options_ . mh_replic = mh_replic_o ;
end
2017-03-20 20:29:19 +01:00
if first_obs_nan_indicator
options_ . first_obs = NaN ;
2019-03-19 14:26:16 +01:00
end