How to obtain trace plots after a Metropolis-Hastings
It can be useful to plot iterations vs. sampled values for each variable (parameter) in a chain. If the Metropolis-Hastings converges to a stable distribution, the trace plot has to be stable (e.g. no trend in the sampled values).
After a metropolis (that is after the estimation command in the mod file) you just have to write:
trace_plot(options_,M_,estim_params_,type,blck,name1,name2)
where:
- options_, M_ and estim_params_ are Dynare's global structures describing the model,
type is a string equal to 'DeepParameter' (for a parameter of the structural model), 'MeasurementError' (std. or correlation in the measurement equation error) or 'StructuralShock' (for the standard deviation of a structural shock or the correlation between two shocks),
blck<mh_nblck is an integer specifying the mh chain to be considered,
- name1 is a string specifying for instance the name of a deep parameter,
- name2 is an optional second string needed if you are interested in the correlation between two structural shocks or two measurement errors.
After the estimation of the model in fs2000.mod if we want to build a trace plot for parameter alp, we write:
trace_plot(options_,M_,estim_params_,'DeepParameter',2,'alp')
and the following plot will pop up:
where the black curve is a moving average of the sampled values. That is, if the sampled values are
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.133486 2021] [wsgi:error] [pid 2583:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latexwhere
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.168808 2021] [wsgi:error] [pid 2584:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latexis the number of simulations in the Metropolis Hastings chain, the moving average curve is defined as:
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.203730 2021] [wsgi:error] [pid 2585:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latex
for all
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.250688 2021] [wsgi:error] [pid 2586:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latexbetween
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.293438 2021] [wsgi:error] [pid 2587:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latexand
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.339234 2021] [wsgi:error] [pid 2588:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latex. The default value of
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.387493 2021] [wsgi:error] [pid 2589:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latexis 200. The value of
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.431827 2021] [wsgi:error] [pid 2590:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latexmay be changed by writing:
options_.trace_plot_ma = 2222;
before the call to trace_plot. A value of
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.480275 2021] [wsgi:error] [pid 2591:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latexroughly equal to
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.520667 2021] [wsgi:error] [pid 2592:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latexof
latex error! exitcode was 2 (signal 0), transscript follows: [Tue Sep 07 12:56:32.553828 2021] [wsgi:error] [pid 2593:tid 139902259758848] [client 192.168.1.1:44074] failed to exec() latexseems to be reasonable. In our example, we see that the moving average curve is slightly downward slopping, meaning that the Metropolis Hastings did not reach the ergodic distribution after 22000 draws.