From e9871d7d476ea5fcfd2ef8e24fb0219c12698185 Mon Sep 17 00:00:00 2001 From: Willi Mutschler <willi@mutschler.eu> Date: Mon, 9 Oct 2023 22:31:26 +0200 Subject: [PATCH] method_of_moments: cosmetical changes --- matlab/+mom/objective_function.m | 12 ++++-------- matlab/+mom/set_correct_bounds_for_stderr_corr.m | 2 +- matlab/+mom/transform_prior_to_laplace_prior.m | 5 ++--- matlab/estimation/check_posterior_sampler_options.m | 2 +- matlab/estimation/posterior_sampler.m | 4 ++-- matlab/estimation/posterior_sampler_initialization.m | 4 ++-- .../estimation/tune_mcmc_mh_jscale_wrapper.m | 1 + 7 files changed, 13 insertions(+), 17 deletions(-) diff --git a/matlab/+mom/objective_function.m b/matlab/+mom/objective_function.m index 6562f96628..1821cc566d 100644 --- a/matlab/+mom/objective_function.m +++ b/matlab/+mom/objective_function.m @@ -221,14 +221,10 @@ if strcmp(options_mom_.mom.mom_method,'SMM') y_sim = simult_(M_, options_mom_, dr.ys, dr, scaled_shock_series, options_mom_.order); % provide meaningful penalty if data is nan or inf if any(any(isnan(y_sim))) || any(any(isinf(y_sim))) - if options_mom_.mom.vector_output == 1 % lsqnonlin requires vector output - fval = Inf(size(weighting_info.Sw,1),1); - else - fval = Inf; - end info(1)=180; info(4) = 0.1; exit_flag = 0; + fval = Inf; if options_mom_.mom.vector_output == 1 % lsqnonlin requires vector output fval = ones(options_mom_.mom.mom_nbr,1)*options_mom_.huge_number; end @@ -272,21 +268,21 @@ if strcmp(options_mom_.mom.mom_method,'GMM') || strcmp(options_mom_.mom.mom_meth end if options_mom_.mom.compute_derivs && options_mom_.mom.analytic_jacobian if options_mom_.mom.penalized_estimator - dxparam1 = eye(length(xparam)); + dxparam = eye(length(xparam)); end for jp=1:length(xparam) dmoments_difference = - model_moments_params_derivs(:,jp); dresiduals = sqrt(options_mom_.mom.weighting_matrix_scaling_factor)*weighting_info.Sw*dmoments_difference; if options_mom_.mom.vector_output == 1 % lsqnonlin requires vector output if options_mom_.mom.penalized_estimator - df(:,jp)=[dresiduals;dxparam1(:,jp)./sqrt(diag(diag(bayestopt_.p2.^2)))]; + df(:,jp)=[dresiduals;dxparam(:,jp)./sqrt(diag(diag(bayestopt_.p2.^2)))]; else df(:,jp) = dresiduals; end else df(jp,1) = dresiduals'*residuals + residuals'*dresiduals; if options_mom_.mom.penalized_estimator - df(jp,1)=df(jp,1)+(dxparam1(:,jp))'/(diag(bayestopt_.p2.^2))*(xparam-bayestopt_.p1)+(xparam-bayestopt_.p1)'/(diag(bayestopt_.p2.^2))*(dxparam1(:,jp)); + df(jp,1)=df(jp,1)+(dxparam(:,jp))'/(diag(bayestopt_.p2.^2))*(xparam-bayestopt_.p1)+(xparam-bayestopt_.p1)'/(diag(bayestopt_.p2.^2))*(dxparam(:,jp)); end end end diff --git a/matlab/+mom/set_correct_bounds_for_stderr_corr.m b/matlab/+mom/set_correct_bounds_for_stderr_corr.m index c8299c3cf7..3499cc3993 100644 --- a/matlab/+mom/set_correct_bounds_for_stderr_corr.m +++ b/matlab/+mom/set_correct_bounds_for_stderr_corr.m @@ -8,7 +8,7 @@ function BoundsInfo = set_correct_bounds_for_stderr_corr(estim_params_,BoundsInf % o BoundsInfo [struct] information on bounds % ------------------------------------------------------------------------- % OUTPUT -% o BoundsInfo [struct] updated information on bounds +% o BoundsInfo [struct] updated bounds % ------------------------------------------------------------------------- % This function is called by % o mom.run diff --git a/matlab/+mom/transform_prior_to_laplace_prior.m b/matlab/+mom/transform_prior_to_laplace_prior.m index db6be256aa..358f9fd1e6 100644 --- a/matlab/+mom/transform_prior_to_laplace_prior.m +++ b/matlab/+mom/transform_prior_to_laplace_prior.m @@ -1,10 +1,10 @@ function bayestopt_ = transform_prior_to_laplace_prior(bayestopt_) -% function bayestopt_ = transform_prior_to_laplace_prior(bayestopt_) +% bayestopt_ = transform_prior_to_laplace_prior(bayestopt_) % ------------------------------------------------------------------------- % Transforms the prior specification to a Laplace type of approximation: % only the prior mean and standard deviation are relevant, all other shape % information, except for the parameter bounds, is ignored. -% ========================================================================= +% ------------------------------------------------------------------------- % INPUTS % bayestopt_ [structure] prior information % ------------------------------------------------------------------------- @@ -32,7 +32,6 @@ function bayestopt_ = transform_prior_to_laplace_prior(bayestopt_) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <https://www.gnu.org/licenses/>. - if any(setdiff([0;bayestopt_.pshape],[0,3])) fprintf('\nNon-normal priors specified. Penalized estimation uses a Laplace type of approximation:'); fprintf('\nOnly the prior mean and standard deviation are relevant, all other shape information, except for the parameter bounds, is ignored.\n\n'); diff --git a/matlab/estimation/check_posterior_sampler_options.m b/matlab/estimation/check_posterior_sampler_options.m index c0d4235ff1..29de7cf689 100644 --- a/matlab/estimation/check_posterior_sampler_options.m +++ b/matlab/estimation/check_posterior_sampler_options.m @@ -311,7 +311,7 @@ if init end % slice posterior sampler does not require mode or hessian to run - % needs to be set to 1 to skip parts in dynare_estimation_1.m + % needs to be set to 1 to skip parts in dynare_estimation_1.m or mom.run.m % requiring posterior maximization/calibrated smoother before MCMC options_.mh_posterior_mode_estimation=true; diff --git a/matlab/estimation/posterior_sampler.m b/matlab/estimation/posterior_sampler.m index eb2b55ea8d..f1939042ba 100644 --- a/matlab/estimation/posterior_sampler.m +++ b/matlab/estimation/posterior_sampler.m @@ -10,8 +10,8 @@ function posterior_sampler(TargetFun,ProposalFun,xparam1,sampler_options,mh_boun % o xparam1 [double] (p*1) vector of parameters to be estimated (initial values). % o sampler_options structure % o mh_bounds [double] (p*2) matrix defining lower and upper bounds for the parameters. -% o dataset_ [structure] data structure -% o dataset_info [structure] dataset info structure +% o dataset_ [structure] data structure (likelihood-based) or data moments (method of moments) +% o dataset_info [structure] dataset info structure (likelihood-based) or info on weighting matrix (method of moments) % o options_ [structure] options structure % o M_ [structure] model structure % o estim_params_ [structure] estimated parameters structure diff --git a/matlab/estimation/posterior_sampler_initialization.m b/matlab/estimation/posterior_sampler_initialization.m index 1307588241..194f4c3c30 100644 --- a/matlab/estimation/posterior_sampler_initialization.m +++ b/matlab/estimation/posterior_sampler_initialization.m @@ -9,8 +9,8 @@ function [ ix2, ilogpo2, ModelName, MetropolisFolder, FirstBlock, FirstLine, npa % o xparam1 [double] (p*1) vector of parameters to be estimated (initial values). % o vv [double] (p*p) matrix, posterior covariance matrix (at the mode). % o mh_bounds [double] (p*2) matrix defining lower and upper bounds for the parameters. -% o dataset_ data structure -% o dataset_info dataset info structure +% o dataset_ data structure (likelihood based) or data moments/irfs (method of moments) +% o dataset_info dataset info structure (likelihood based) or info on weighting matrix (method of moments) % o options_ options structure % o M_ model structure % o estim_params_ estimated parameters structure diff --git a/matlab/utilities/estimation/tune_mcmc_mh_jscale_wrapper.m b/matlab/utilities/estimation/tune_mcmc_mh_jscale_wrapper.m index 0da796f513..82548cd584 100644 --- a/matlab/utilities/estimation/tune_mcmc_mh_jscale_wrapper.m +++ b/matlab/utilities/estimation/tune_mcmc_mh_jscale_wrapper.m @@ -18,6 +18,7 @@ function mh_jscale = tune_mcmc_mh_jscale_wrapper(invhess, options_, M_, objectiv % ------------------------------------------------------------------------- % This function is called by % o dynare_estimation_1 +% o mom.run % ------------------------------------------------------------------------- % Copyright © 2023 Dynare Team -- GitLab