From 23f6019cdfa324618d03da920cc8740e732faefc Mon Sep 17 00:00:00 2001 From: Willi Mutschler Date: Fri, 1 Sep 2023 21:07:55 +0200 Subject: [PATCH] Factorize estimation: check prior stderr and corr parameters --- matlab/initial_estimation_checks.m | 23 ++------ .../estimation/check_prior_stderr_corr.m | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 matlab/utilities/estimation/check_prior_stderr_corr.m diff --git a/matlab/initial_estimation_checks.m b/matlab/initial_estimation_checks.m index 47adc09aa..06a064eef 100644 --- a/matlab/initial_estimation_checks.m +++ b/matlab/initial_estimation_checks.m @@ -184,26 +184,9 @@ if isfield(EstimatedParameters,'param_vals') && ~isempty(EstimatedParameters.par end end -if any(BayesInfo.pshape) % if Bayesian estimation - nvx=EstimatedParameters.nvx; - if nvx && any(BayesInfo.p3(1:nvx)<0) - warning('Your prior allows for negative standard deviations for structural shocks. Due to working with variances, Dynare will be able to continue, but it is recommended to change your prior.') - end - offset=nvx; - nvn=EstimatedParameters.nvn; - if nvn && any(BayesInfo.p3(1+offset:offset+nvn)<0) - warning('Your prior allows for negative standard deviations for measurement error. Due to working with variances, Dynare will be able to continue, but it is recommended to change your prior.') - end - offset = nvx+nvn; - ncx=EstimatedParameters.ncx; - if ncx && (any(BayesInfo.p3(1+offset:offset+ncx)<-1) || any(BayesInfo.p4(1+offset:offset+ncx)>1)) - warning('Your prior allows for correlations between structural shocks larger than +-1 and will not integrate to 1 due to truncation. Please change your prior') - end - offset = nvx+nvn+ncx; - ncn=EstimatedParameters.ncn; - if ncn && (any(BayesInfo.p3(1+offset:offset+ncn)<-1) || any(BayesInfo.p4(1+offset:offset+ncn)>1)) - warning('Your prior allows for correlations between measurement errors larger than +-1 and will not integrate to 1 due to truncation. Please change your prior') - end +% check and display warning if negative values of stderr or corr params are outside unit circle for Bayesian estimation +if any(BayesInfo.pshape) + check_prior_stderr_corr(EstimatedParameters,BayesInfo); end % display warning if some parameters are still NaN diff --git a/matlab/utilities/estimation/check_prior_stderr_corr.m b/matlab/utilities/estimation/check_prior_stderr_corr.m new file mode 100644 index 000000000..0cdc7b71c --- /dev/null +++ b/matlab/utilities/estimation/check_prior_stderr_corr.m @@ -0,0 +1,53 @@ +function check_prior_stderr_corr(estim_params_,bayestopt_) +% function check_prior_stderr_corr(estim_params_,bayestopt_) +% ------------------------------------------------------------------------- +% Check if the prior allows for negative standard deviations and +% correlations larger than +-1. If so, issue a warning. +% ========================================================================= +% INPUTS +% o estim_params_: [struct] information on estimated parameters +% o bayestopt_: [struct] information on priors +% ------------------------------------------------------------------------- +% OUTPUTS +% none, but issues a warning if the prior allows for negative standard +% ------------------------------------------------------------------------- +% This function is called by +% o initial_estimation_checks.m +% ------------------------------------------------------------------------- +% Copyright © 2023 Dynare Team +% +% 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 +% along with Dynare. If not, see . +% ========================================================================= + +nvx = estim_params_.nvx; % number of stderr parameters for structural shocks +if nvx && any(bayestopt_.p3(1:nvx)<0) + warning('Your prior allows for negative standard deviations for structural shocks. Due to working with variances, Dynare will be able to continue, but it is recommended to change your prior.') +end +offset = nvx; +nvn = estim_params_.nvn; % number of stderr parameters for measurement errors +if nvn && any(bayestopt_.p3(1+offset:offset+nvn)<0) + warning('Your prior allows for negative standard deviations for measurement error. Due to working with variances, Dynare will be able to continue, but it is recommended to change your prior.') +end +offset = nvx+nvn; +ncx = estim_params_.ncx; % number of corr parameters for structural shocks +if ncx && (any(bayestopt_.p3(1+offset:offset+ncx)<-1) || any(bayestopt_.p4(1+offset:offset+ncx)>1)) + warning('Your prior allows for correlations between structural shocks larger than +-1 and will not integrate to 1 due to truncation. Please change your prior') +end +offset = nvx+nvn+ncx; +ncn = estim_params_.ncn; % number of corr parameters for measurement errors +if ncn && (any(bayestopt_.p3(1+offset:offset+ncn)<-1) || any(bayestopt_.p4(1+offset:offset+ncn)>1)) + warning('Your prior allows for correlations between measurement errors larger than +-1 and will not integrate to 1 due to truncation. Please change your prior') +end \ No newline at end of file