diff --git a/doc/dynare.texi b/doc/dynare.texi
index eccf5096d..aee146dd2 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -6189,6 +6189,21 @@ Percentage of MCMC draws at the beginning and end of the MCMC chain taken
to compute the @cite{Geweke (1992,1999)} convergence diagnostics (requires @ref{mh_nblocks}=1)
after discarding the first @ref{mh_drop} percent of draws as a burnin. Default: @code{[0.2 0.5]}.
+@item raftery_lewis_diagnostics
+@anchor{raftery_lewis_diagnostics}
+Triggers the computation of the @cite{Raftery and Lewis (1992)} convergence diagnostics. The goal is deliver the number of draws
+required to estimate a particular quantile of the CDF @code{q} with precision @code{r} with a probability @code{s}. Typically, one wants to estimate
+the @code{q=0.025} percentile (corresponding to a 95 percent HPDI) with a precision of 0.5 percent (@code{r=0.005}) with 95 percent
+certainty (@code{s=0.95}). The defaults can be changed via @ref{raftery_lewis_qrs}. Based on the
+theory of first order Markov Chains, the diagnostics will provide a required burn-in (@code{M}), the number of draws after the burnin (@code{N})
+as well as a thinning factor that would deliver a first order chain (@code{k}). The last line of the table will also deliver the maximum over
+all parameters for the respective values.
+
+@item raftery_lewis_qrs = [@var{DOUBLE} @var{DOUBLE} @var{DOUBLE}]
+@anchor{raftery_lewis_qrs}
+Sets the quantile of the CDF @code{q} that is estimated with precision @code{r} with a probability @code{s} in the
+@cite{Raftery and Lewis (1992)} convergence diagnostics. Default: @code{[0.025 0.005 0.95]}.
+
@item consider_all_endogenous
Compute the posterior moments, smoothed variables, k-step ahead
filtered variables and forecasts (when requested) on all the
@@ -14276,6 +14291,10 @@ Rabanal, Pau and Juan Rubio-Ramirez (2003): ``Comparing New Keynesian
Models of the Business Cycle: A Bayesian Approach,'' Federal Reserve
of Atlanta, @i{Working Paper Series}, 2003-30.
+@item
+Raftery, Adrien E. and Steven Lewis (1992): ``How many iterations in the Gibbs sampler?,'' in @i{Bayesian Statistics, Vol. 4},
+ed. J.O. Berger, J.M. Bernardo, A.P. Dawid, and A.F.M. Smith, Clarendon Press: Oxford, pp. 763-773.
+
@item
Ratto, Marco (2008): ``Analysing DSGE models with global sensitivity
analysis'', @i{Computational Economics}, 31, 115--139
diff --git a/license.txt b/license.txt
index f14eaf9f6..f06d329d0 100644
--- a/license.txt
+++ b/license.txt
@@ -81,6 +81,11 @@ Copyright: 2010-2015 Alexander Meyer-Gohde
2015 Dynare Team
License: GPL-3+
+Files: matlab/convergence_diagnostics/raftery_lewis.m
+Copyright: 2016 Benjamin Born and Johannes Pfeifer
+ 2016 Dynare Team
+License: GPL-3+
+
Files: matlab/optimization/simpsa.m matlab/optimization/simpsaget.m matlab/optimization/simpsaset.m
Copyright: 2005 Henning Schmidt, FCC, henning@fcc.chalmers.se
2006 Brecht Donckels, BIOMATH, brecht.donckels@ugent.be
diff --git a/matlab/convergence_diagnostics/McMCDiagnostics.m b/matlab/convergence_diagnostics/McMCDiagnostics.m
index cb84ff438..fb4f41262 100644
--- a/matlab/convergence_diagnostics/McMCDiagnostics.m
+++ b/matlab/convergence_diagnostics/McMCDiagnostics.m
@@ -123,7 +123,7 @@ LastLineNumber = record.MhDraws(end,3);
NumberOfDraws = PastDraws(1);
if NumberOfDraws<=2000
- warning(['estimation:: MCMC convergence diagnostics are not computed because the total number of iterations is less than 2000!'])
+ warning(['estimation:: MCMC convergence diagnostics are not computed because the total number of iterations is not bigger than 2000!'])
return
end
@@ -193,6 +193,33 @@ if nblck == 1 % Brooks and Gelman tests need more than one block
dyn_latex_table(M_,options_,my_title,'geweke',headers,param_name_tex,datamat,lh,12,4,additional_header);
end
skipline(2);
+
+ if options_.convergence.rafterylewis.indicator
+ if any(options_.convergence.rafterylewis.qrs<0) || any(options_.convergence.rafterylewis.qrs>1) || length(options_.convergence.rafterylewis.qrs)~=3 ...
+ || (options_.convergence.rafterylewis.qrs(1)-options_.convergence.rafterylewis.qrs(2)<=0)
+ fprintf('\nCONVERGENCE DIAGNOSTICS: Invalid option for raftery_lewis_qrs. Using the default of [0.025 0.005 0.95].\n')
+ options_.convergence.rafterylewis.qrs=[0.025 0.005 0.95];
+ end
+ Raftery_Lewis_q=options_.convergence.rafterylewis.qrs(1);
+ Raftery_Lewis_r=options_.convergence.rafterylewis.qrs(2);
+ Raftery_Lewis_s=options_.convergence.rafterylewis.qrs(3);
+ oo_.Raftery_Lewis = raftery_lewis(x2,Raftery_Lewis_q,Raftery_Lewis_r,Raftery_Lewis_s);
+ oo_.Raftery_Lewis.parameter_names=param_name;
+ my_title=sprintf('Raftery/Lewis (1992) Convergence Diagnostics, based on quantile q=%4.3f with precision r=%4.3f with probability s=%4.3f.',Raftery_Lewis_q,Raftery_Lewis_r,Raftery_Lewis_s);
+ headers = char('Variables','M (burn-in)','N (req. draws)','N+M (total draws)','k (thinning)');
+
+ raftery_data_mat=[oo_.Raftery_Lewis.M_burn,oo_.Raftery_Lewis.N_prec,oo_.Raftery_Lewis.N_total,oo_.Raftery_Lewis.k_thin];
+ raftery_data_mat=[raftery_data_mat;max(raftery_data_mat)];
+ labels_Raftery_Lewis=char(param_name,'Maximum');
+ lh = size(labels_Raftery_Lewis,2)+2;
+ dyntable(options_,my_title,headers,labels_Raftery_Lewis,raftery_data_mat,lh,10,0);
+ if options_.TeX
+ labels_Raftery_Lewis_tex=char(param_name_tex,'Maximum');
+ lh = size(labels_Raftery_Lewis_tex,2)+2;
+ dyn_latex_table(M_,options_,my_title,'raftery_lewis',headers,labels_Raftery_Lewis_tex,raftery_data_mat,lh,10,0);
+ end
+ end
+
return;
end
diff --git a/matlab/convergence_diagnostics/raftery_lewis.m b/matlab/convergence_diagnostics/raftery_lewis.m
new file mode 100644
index 000000000..c2a162d87
--- /dev/null
+++ b/matlab/convergence_diagnostics/raftery_lewis.m
@@ -0,0 +1,179 @@
+function [raftery_lewis] = raftery_lewis(runs,q,r,s)
+% function raftery_lewis = raftery_lewis(runs,q,r,s)
+% Computes the convergence diagnostics of Raftery and Lewis (1992), i.e. the
+% number of draws needed in MCMC to estimate the posterior cdf of the q-quantile
+% within an accuracy r with probability s
+%
+% Inputs:
+% - draws [n_draws by n_var] double matrix of draws from the sampler
+% - q [scalar] quantile of the quantity of interest
+% - r [scalar] level of desired precision
+% - s [scalar] probability associated with r
+%
+% Output:
+% raftery_lewis [structure] containing the fields:
+% - M_burn [n_draws by 1] number of draws required for burn-in
+% - N_prec [n_draws by 1] number of draws required to achieve desired precision r
+% - k_thin [n_draws by 1] thinning required to get 1st order MC
+% - k_ind [n_draws by 1] thinning required to get independence
+% - I_stat [n_draws by 1] I-statistic of Raftery/Lewis (1992b)
+% measures increase in required
+% iterations due to dependence in chain
+% - N_min [scalar] # draws if the chain is white noise
+% - N_total [n_draws by 1] nburn + nprec
+%
+
+% ---------------------------------------------------------------------
+% NOTES: Example values of q, r, s:
+% 0.025, 0.005, 0.95 (for a long-tailed distribution)
+% 0.025, 0.0125, 0.95 (for a short-tailed distribution);
+%
+% - The result is quite sensitive to r, being proportional to the
+% inverse of r^2.
+% - For epsilon (closeness of probabilities to equilibrium values),
+% Raftery/Lewis use 0.001 and argue that the results
+% are quite robust to changes in this value
+%
+% ---------------------------------------------------------------------
+% REFERENCES:
+% Raftery, Adrien E./Lewis, Steven (1992a): "How many iterations in the Gibbs sampler?"
+% in: Bernardo/Berger/Dawid/Smith (eds.): Bayesian Statistics, Vol. 4, Clarendon Press: Oxford,
+% pp. 763-773.
+% Raftery, Adrien E./Lewis, Steven (1992b): "Comment: One long run with diagnostics:
+% Implementation strategies for Markov chain Monte Carlo." Statistical Science,
+% 7(4), pp. 493-497.
+%
+% ----------------------------------------------------
+
+% Copyright (C) 2016 Benjamin Born and Johannes Pfeifer
+% Copyright (C) 2016 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 .
+
+
+
+[n_runs, n_vars] = size(runs);
+
+raftery_lewis.M_burn=NaN(n_vars,1);
+raftery_lewis.N_prec=NaN(n_vars,1);
+raftery_lewis.k_thin=NaN(n_vars,1);
+raftery_lewis.k_ind=NaN(n_vars,1);
+raftery_lewis.I_stat=NaN(n_vars,1);
+raftery_lewis.N_total=NaN(n_vars,1);
+
+
+thinned_chain = zeros(n_runs,1);
+%quantities that can be precomputed as they are independent of variable
+Phi = norminv((s+1)/2); %note the missing ^{-1} at the Phi in equation top page 5, see RL (1995)
+raftery_lewis.N_min = fix(Phi^2*(1-q)*q/r^2+1);
+
+for nv = 1:n_vars % big loop over variables
+ if q > 0 && q < 1
+ work = (runs(:,nv) <= quantile(runs(:,nv),q));
+ else
+ error('Quantile must be between 0 and 1');
+ end;
+
+ k_thin_current_var = 1;
+ bic = 1;
+ epss = 0.001;
+ % Find thinning factor for which first-order Markov Chain is preferred to second-order one
+ while(bic > 0)
+ thinned_chain=work(1:k_thin_current_var:n_runs,1);
+ [g2, bic] = first_vs_second_order_MC_test(thinned_chain);
+ k_thin_current_var = k_thin_current_var+1;
+ end;
+
+ k_thin_current_var = k_thin_current_var-1; %undo last step
+
+ %compute transition probabilities
+ transition_matrix = zeros(2,2);
+ for i1 = 2:size(thinned_chain,1)
+ transition_matrix(thinned_chain(i1-1)+1,thinned_chain(i1)+1) = transition_matrix(thinned_chain(i1-1)+1,thinned_chain(i1)+1)+1;
+ end;
+ alpha = transition_matrix(1,2)/(transition_matrix(1,1)+transition_matrix(1,2)); %prob of going from 1 to 2
+ beta = transition_matrix(2,1)/(transition_matrix(2,1)+transition_matrix(2,2)); %prob of going from 2 to 1
+
+ kmind=k_thin_current_var;
+ [g2, bic]=independence_chain_test(thinned_chain);
+
+ while(bic > 0)
+ thinned_chain=work(1:kmind:n_runs,1);
+ [g2, bic] = independence_chain_test(thinned_chain);
+ kmind = kmind+1;
+ end;
+
+ m_star = log((alpha + beta)*epss/max(alpha,beta))/log(abs(1 - alpha - beta)); %equation bottom page 4
+ raftery_lewis.M_burn(nv) = fix((m_star+1)*k_thin_current_var);
+ n_star = (2 - (alpha + beta))*alpha*beta*(Phi^2)/((alpha + beta)^3 * r^2); %equation top page 5
+ raftery_lewis.N_prec(nv) = fix(n_star+1)*k_thin_current_var;
+ raftery_lewis.I_stat(nv) = (raftery_lewis.M_burn(nv) + raftery_lewis.N_prec(nv))/raftery_lewis.N_min;
+ raftery_lewis.k_ind(nv) = max(fix(raftery_lewis.I_stat(nv)+1),kmind);
+ raftery_lewis.k_thin(nv) = k_thin_current_var;
+ raftery_lewis.N_total(nv)= raftery_lewis.M_burn(nv)+raftery_lewis.N_prec(nv);
+end;
+
+end
+
+function [g2, bic] = first_vs_second_order_MC_test(d)
+%conducts a test of first vs. second order Markov Chain via BIC criterion
+n_obs=size(d,1);
+g2 = 0;
+tran=zeros(2,2,2);
+for t_iter=3:n_obs % count state transitions
+ tran(d(t_iter-2,1)+1,d(t_iter-1,1)+1,d(t_iter,1)+1)=tran(d(t_iter-2,1)+1,d(t_iter-1,1)+1,d(t_iter,1)+1)+1;
+end;
+% Compute the log likelihood ratio statistic for second-order MC vs first-order MC. G2 statistic of Bishop, Fienberg and Holland (1975)
+for ind_1 = 1:2
+ for ind_2 = 1:2
+ for ind_3 = 1:2
+ if tran(ind_1,ind_2,ind_3) ~= 0
+ fitted = (tran(ind_1,ind_2,1) + tran(ind_1,ind_2,2))*(tran(1,ind_2,ind_3) + tran(2,ind_2,ind_3))/...
+ (tran(1,ind_2,1) + tran(1,ind_2,2) + tran(2,ind_2,1) + tran(2,ind_2,2));
+ focus = tran(ind_1,ind_2,ind_3);
+ g2 = g2 + log(focus/fitted)*focus;
+ end
+ end; % end of for i3
+ end; % end of for i2
+end; % end of for i1
+g2 = g2*2;
+bic = g2 - log(n_obs-2)*2;
+
+end
+
+
+function [g2, bic] = independence_chain_test(d)
+%conducts a test of independence Chain via BIC criterion
+n_obs=size(d,1);
+trans = zeros(2,2);
+for ind_1 = 2:n_obs
+ trans(d(ind_1-1)+1,d(ind_1)+1)=trans(d(ind_1-1)+1,d(ind_1)+1)+1;
+end;
+dcm1 = n_obs - 1;
+g2 = 0;
+% Compute the log likelihood ratio statistic for second-order MC vs first-order MC. G2 statistic of Bishop, Fienberg and Holland (1975)
+for ind_1 = 1:2
+ for ind_2 = 1:2
+ if trans(ind_1,ind_2) ~= 0
+ fitted = ((trans(ind_1,1) + trans(ind_1,2))*(trans(1,ind_2) + trans(2,ind_2)))/dcm1;
+ focus = trans(ind_1,ind_2);
+ g2 = g2 + log(focus/fitted)*focus;
+ end;
+ end;
+end;
+g2 = g2*2;
+bic = g2 - log(dcm1);
+end
diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m
index 12273dd17..e62b59065 100644
--- a/matlab/global_initialization.m
+++ b/matlab/global_initialization.m
@@ -767,6 +767,9 @@ options_.gpu = 0;
%Geweke convergence diagnostics
options_.convergence.geweke.taper_steps=[4 8 15];
options_.convergence.geweke.geweke_interval=[0.2 0.5];
+%Raftery/Lewis convergence diagnostics;
+options_.convergence.rafterylewis.indicator=0;
+options_.convergence.rafterylewis.qrs=[0.025 0.005 0.95];
% Options for lmmcp solver
options_.lmmcp.status = 0;
diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy
index 61230cb3e..6aaa25707 100644
--- a/preprocessor/DynareBison.yy
+++ b/preprocessor/DynareBison.yy
@@ -108,7 +108,7 @@ class ParsingDriver;
%token LYAPUNOV_FIXED_POINT_TOL LYAPUNOV_DOUBLING_TOL LYAPUNOV_SQUARE_ROOT_SOLVER_TOL LOG_DEFLATOR LOG_TREND_VAR LOG_GROWTH_FACTOR MARKOWITZ MARGINAL_DENSITY MAX MAXIT
%token MFS MH_CONF_SIG MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER POSTERIOR_MAX_SUBSAMPLE_DRAWS MIN MINIMAL_SOLVING_PERIODS
%token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN
-%token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO CONTEMPORANEOUS_CORRELATION DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL MCMC_JUMPING_COVARIANCE MOMENT_CALIBRATION
+%token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO CONTEMPORANEOUS_CORRELATION DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL RAFTERY_LEWIS_QRS RAFTERY_LEWIS_DIAGNOSTICS MCMC_JUMPING_COVARIANCE MOMENT_CALIBRATION
%token NUMBER_OF_PARTICLES RESAMPLING SYSTEMATIC GENERIC RESAMPLING_THRESHOLD RESAMPLING_METHOD KITAGAWA STRATIFIED SMOOTH
%token CPF_WEIGHTS AMISANOTRISTANI MURRAYJONESPARSLOW
%token FILTER_ALGORITHM PROPOSAL_APPROXIMATION CUBATURE UNSCENTED MONTECARLO DISTRIBUTION_APPROXIMATION
@@ -1800,6 +1800,8 @@ estimation_options : o_datafile
| o_qz_zero_threshold
| o_taper_steps
| o_geweke_interval
+ | o_raftery_lewis_qrs
+ | o_raftery_lewis_diagnostics
| o_mcmc_jumping_covariance
| o_irf_plot_threshold
| o_posterior_max_subsample_draws
@@ -2898,6 +2900,8 @@ o_xls_range : XLS_RANGE EQUAL range { driver.option_str("xls_range", $3); };
o_filter_step_ahead : FILTER_STEP_AHEAD EQUAL vec_int { driver.option_vec_int("filter_step_ahead", $3); };
o_taper_steps : TAPER_STEPS EQUAL vec_int { driver.option_vec_int("convergence.geweke.taper_steps", $3); };
o_geweke_interval : GEWEKE_INTERVAL EQUAL vec_value { driver.option_num("convergence.geweke.geweke_interval",$3); };
+o_raftery_lewis_diagnostics : RAFTERY_LEWIS_DIAGNOSTICS { driver.option_num("convergence.rafterylewis.indicator", "1"); };
+o_raftery_lewis_qrs : RAFTERY_LEWIS_QRS EQUAL vec_value { driver.option_num("convergence.rafterylewis.qrs",$3); };
o_constant : CONSTANT { driver.option_num("noconstant", "0"); };
o_noconstant : NOCONSTANT { driver.option_num("noconstant", "1"); };
o_mh_recover : MH_RECOVER { driver.option_num("mh_recover", "1"); };
diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll
index 33be4c8d9..a5ca4ac04 100644
--- a/preprocessor/DynareFlex.ll
+++ b/preprocessor/DynareFlex.ll
@@ -272,6 +272,8 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2
lik_init {return token::LIK_INIT;}
taper_steps {return token::TAPER_STEPS;}
geweke_interval {return token::GEWEKE_INTERVAL;}
+raftery_lewis_qrs {return token::RAFTERY_LEWIS_QRS;}
+raftery_lewis_diagnostics {return token::RAFTERY_LEWIS_DIAGNOSTICS;}
graph {return token::GRAPH;}
nograph {return token::NOGRAPH;}
nodisplay {return token::NODISPLAY;}
diff --git a/tests/TeX/fs2000_corr_ME.mod b/tests/TeX/fs2000_corr_ME.mod
index fe432c944..dfb933ee1 100644
--- a/tests/TeX/fs2000_corr_ME.mod
+++ b/tests/TeX/fs2000_corr_ME.mod
@@ -169,7 +169,7 @@ end;
write_latex_prior_table;
estimation(mode_compute=8,order=1,datafile='../fs2000/fsdat_simul',mode_check,smoother,filter_decomposition,mh_replic=4000, mh_nblocks=1, mh_jscale=0.8,forecast = 8,bayesian_irf,filtered_vars,filter_step_ahead=[1,3],irf=20,
- moments_varendo,contemporaneous_correlation,conditional_variance_decomposition=[1 2 4],smoothed_state_uncertainty) m P c e W R k d y gy_obs;
+ moments_varendo,contemporaneous_correlation,conditional_variance_decomposition=[1 2 4],smoothed_state_uncertainty,raftery_lewis_diagnostics) m P c e W R k d y gy_obs;
trace_plot(options_,M_,estim_params_,'PosteriorDensity',1);
trace_plot(options_,M_,estim_params_,'StructuralShock',1,'e_a')
diff --git a/tests/estimation/fs2000.mod b/tests/estimation/fs2000.mod
index 1ef75d559..0fa67c46b 100644
--- a/tests/estimation/fs2000.mod
+++ b/tests/estimation/fs2000.mod
@@ -84,12 +84,19 @@ options_.solve_tolf = 1e-12;
estimation(order=1,datafile=fsdat_simul,nobs=192,loglinear,mh_replic=3000,mh_nblocks=1,mh_jscale=0.8,moments_varendo,selected_variables_only,contemporaneous_correlation,smoother,forecast=8,
geweke_interval = [0.19 0.49],
- taper_steps = [4 7 15]
+ taper_steps = [4 7 15],
+ raftery_lewis_diagnostics,
+ raftery_lewis_qrs=[0.025 0.01 0.95]
) y m;
+
if ~isequal(options_.convergence.geweke.taper_steps,[4 7 15]') || ~isequal(options_.convergence.geweke.geweke_interval,[0.19 0.49])
error('Interface for Geweke diagnostics not working')
end
+if ~isequal(options_.convergence.rafterylewis.qrs,[0.025 0.01 0.95]) || ~isequal(options_.convergence.rafterylewis.indicator,1)
+ error('Interface for Raftery/Lewis diagnostics not working')
+end
+
%test load_mh_file option
options_.smoother=0;
options_.moments_varendo=0;