diff --git a/matlab/+occbin/IVF_core.m b/matlab/+occbin/IVF_core.m index 1ffe0a153515810cdc992906a6b0cae5fc17b1de..b3415d3129fcb3097db0106279f07eb71b0b44be 100644 --- a/matlab/+occbin/IVF_core.m +++ b/matlab/+occbin/IVF_core.m @@ -1,5 +1,5 @@ function [filtered_errs, resids, Emat, stateval, error_code] = IVF_core(M_,oo_,options_,err_index,filtered_errs_init,my_obs_list,obs,init_val) -% function [filtered_errs, resids, Emat, stateval] = IVF_core(M_,oo_,options_,err_index,filtered_errs_init,my_obs_list,obs,init_val) +% function [filtered_errs, resids, Emat, stateval, error_code] = IVF_core(M_,oo_,options_,err_index,filtered_errs_init,my_obs_list,obs,init_val) % Computes thre % % Outputs: diff --git a/matlab/+occbin/IVF_posterior.m b/matlab/+occbin/IVF_posterior.m index 533f5f6692f5b02e70e083426b4fbf66c347eb20..b00da8c56f4da6d932ece294df72b6d1044841b1 100644 --- a/matlab/+occbin/IVF_posterior.m +++ b/matlab/+occbin/IVF_posterior.m @@ -106,7 +106,13 @@ filtered_errs_init = zeros(sample_length,sum(err_index)); if info(1) fval = Inf; exit_flag = 0; + atT=NaN(size(stateval(:,DynareResults.dr.order_var)')); + innov=NaN(Model.exo_nbr,sample_length); return +else + atT = stateval(:,DynareResults.dr.order_var)'; + innov = zeros(Model.exo_nbr,sample_length); + innov(diag(Model.Sigma_e)~=0,:)=filtered_errs'; end nobs=size(filtered_errs,1); @@ -184,15 +190,4 @@ end % remember that the likelihood has already been multiplied by -1 % hence, posterior is -1 times the log of the prior -fval = like+prior; -atT = stateval(:,DynareResults.dr.order_var)'; -innov = zeros(Model.exo_nbr,sample_length); -innov(diag(Model.Sigma_e)~=0,:)=filtered_errs'; -updated_variables = atT*nan; -BayesInfo.mf = BayesInfo.smoother_var_list(BayesInfo.smoother_mf); - - -initDynareOptions=DynareOptions; -DynareOptions.nk=[]; %unset options_.nk and reset it later -[DynareResults]=store_smoother_results(Model,DynareResults,DynareOptions,BayesInfo,dataset_,obs_info,atT,innov,[],updated_variables,DynareResults.dr.ys,zeros(length(DynareOptions.varobs_id),1)); -DynareOptions=initDynareOptions; +fval = like+prior; \ No newline at end of file diff --git a/matlab/dynare_estimation_1.m b/matlab/dynare_estimation_1.m index 44a77ede2697d5b75f57b9cab722c6f4e678b942..b22b30fee0fa2ca39877971914f4873c46f25911 100644 --- a/matlab/dynare_estimation_1.m +++ b/matlab/dynare_estimation_1.m @@ -175,13 +175,20 @@ if isequal(options_.mode_compute,0) && isempty(options_.mode_file) && ~options_. if options_.order==1 && ~options_.particle.status if options_.smoother 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); + [~, info, ~, ~, ~, ~, ~, ~, ~, ~, oo_, atT, innov] = occbin.IVF_posterior(xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,prior_bounds(bayestopt_,options_.prior_trunc),oo_); + if ismember(info(1),[303,304,306]) + fprintf('\nIVF: smoother did not succeed. No results will be written to oo_.\n') + else + 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); + options_nk=options_.nk; + options_.nk=[]; %unset options_.nk and reset it later + [oo_]=store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff); + options_.nk=options_nk; + end 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); @@ -590,13 +597,20 @@ if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.psha || ~options_.smoother ) && ~options_.partial_information % to be fixed %% ML estimation, or posterior mode without Metropolis-Hastings or Metropolis without Bayesian smoothed variables 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); + [~, info, ~, ~, ~, ~, ~, ~, ~, ~, oo_, atT, innov] = occbin.IVF_posterior(xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,prior_bounds(bayestopt_,options_.prior_trunc),oo_); + if ismember(info(1),[303,304,306]) + fprintf('\nIVF: smoother did not succeed. No results will be written to oo_.\n') + else + 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); + options_nk=options_.nk; + options_.nk=[]; %unset options_.nk and reset it later + [oo_, yf]=store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff); + options_.nk=options_nk; + end 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); diff --git a/matlab/evaluate_smoother.m b/matlab/evaluate_smoother.m index fd94f8027bb5814b51397bd4e6c7ccf1f6d3460d..783976244d75cec1864b05770f0d8e7523211f3a 100644 --- a/matlab/evaluate_smoother.m +++ b/matlab/evaluate_smoother.m @@ -105,12 +105,17 @@ end if options_.occbin.smoother.status if options_.occbin.smoother.inversion_filter - [~, ~, ~, ~, ~, ~, ~, ~, ~, ~, oo_, atT, innov] = occbin.IVF_posterior(parameters,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); + [~, info, ~, ~, ~, ~, ~, ~, ~, ~, oo_, atT, innov] = occbin.IVF_posterior(parameters,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,prior_bounds(bayestopt_,options_.prior_trunc),oo_); + if ismember(info(1),[303,304,306]) + oo_.occbin.smoother.error_flag=1; + else + oo_.occbin.smoother.error_flag=0; + 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); + end else [atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,T,R,P,PK,decomp,Trend,state_uncertainty,M_,oo_,bayestopt_] = ... occbin.DSGE_smoother(parameters,dataset_.nobs,transpose(dataset_.data),dataset_info.missing.aindex,dataset_info.missing.state,M_,oo_,options_,bayestopt_,estim_params_,dataset_,dataset_info); @@ -124,7 +129,14 @@ if ~(options_.occbin.smoother.status && options_.occbin.smoother.inversion_filte [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 else - [oo_]=store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff); + if ~oo_.occbin.smoother.error_flag + options_nk=options_.nk; + options_.nk=[]; %unset options_.nk and reset it later + [oo_]=store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff); + options_.nk=options_nk; + else + fprintf('\nIVF: smoother did not succeed. No results will be written to oo_.\n') + end end if nargout>4 Smoothed_variables_declaration_order_deviation_form=atT(oo_.dr.inv_order_var(bayestopt_.smoother_var_list),:); diff --git a/matlab/perfect-foresight-models/sim1.m b/matlab/perfect-foresight-models/sim1.m index d6382a0fbb16771a2a7ad8f9a9860378c1b2775d..f5cd589a44d797bb636790c8f7b186059233cc64 100644 --- a/matlab/perfect-foresight-models/sim1.m +++ b/matlab/perfect-foresight-models/sim1.m @@ -68,8 +68,9 @@ for iter = 1:options.simul.maxit % A is the stacked Jacobian with period x equations alongs the rows and % periods times variables (in declaration order) along the columns if options.debug && iter==1 - row=find(all(A==0,2)); - column=find(all(A==0,1)); + [row,col]=find(A); + row=setdiff(1:periods*ny,row); + column=setdiff(1:periods*ny,col); if ~isempty(row) || ~isempty(column) fprintf('The stacked Jacobian is singular. The problem derives from:\n') if ~isempty(row) diff --git a/tests/occbin/filter/NKM.mod b/tests/occbin/filter/NKM.mod index cd871d250a839b3071884e5fd2f4048f2f00ab19..4eae3535ce942b03e82b87a02c4e312a4d0c1f90 100644 --- a/tests/occbin/filter/NKM.mod +++ b/tests/occbin/filter/NKM.mod @@ -11,10 +11,6 @@ @#define small_model = 0 @#endif -// if ~exist('run_ivf','var') -run_ivf=0; -// end - // ----------------- Defintions -----------------------------------------// var c //1 Consumption @@ -278,8 +274,6 @@ check; varobs yg inom pi; estimated_params; - // PARAM NAME, INITVAL, LB, UB, PRIOR_SHAPE, PRIOR_P1, PRIOR_P2, PRIOR_P3, PRIOR_P4, JSCALE - // PRIOR_SHAPE: BETA_PDF, GAMMA_PDF, NORMAL_PDF, INV_GAMMA_PDF varphip,,0,inf,NORMAL_PDF,100,25; phipi,,,,NORMAL_PDF,2,0.25; phiy,,0,inf,NORMAL_PDF,0.5,0.25; @@ -291,8 +285,6 @@ varobs yg inom pi; sigi,,,,INV_GAMMA_PDF,0.002,0.002; end; - -// dataloading_jme_beta(1,'sims.txt',30); load('dataobsfile','inom') // check if inom is at lb and remove data + associated shock verbatim; @@ -338,4 +330,12 @@ varobs yg inom pi; subplot(223) plot([oo0.SmoothedShocks.epss oo_.SmoothedShocks.epss]), title('epss') legend('PKF','IF') + figure, + subplot(221) + plot([oo0.SmoothedVariables.inom oo_.SmoothedVariables.inom]), title('inom') + subplot(222) + plot([oo0.SmoothedVariables.yg oo_.SmoothedVariables.yg]), title('yg') + subplot(223) + plot([oo0.SmoothedVariables.pi oo_.SmoothedVariables.pi]), title('pi') + legend('PKF','IF') occbin_write_regimes(smoother);