# Bayesian estimation \_ mode\_computation

**URL:** <https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857>\
**Category:** Dynare help (legacy posts)\
**Created:** [2 November 2016 15:14 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857 "2016-11-02T15:14:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [2 November 2016 15:14 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/1 "2016-11-02T15:14:39Z")

</div>

Hi Prof. Pfeifer an to all,

I got a question related to bayesian estimation of DSGE:

I estimate the mode using differnet algorithms through ‘‘mode\_compute’’ option and I try different algorithms sequentially with mode\_compute=6 and then mode\_compute=8 being (sequentially) the last two ones.

When I compare the Log data density (Laplace approximation) :

the estimation with '‘mode\_compute=6 ‘’’ has a higher LOG DATA DENSITY = 4161  
…than the estimation with '‘mode\_compute=8 ‘’’ finds the mode over the ‘‘FILE\_mode.mat’’ estimated with mode\_compute=6.  
the estimation with '‘mode\_compute=8 ‘’’ has a LOG DATA DENSITY = 4153 … which is lower.

everything else the same.

Now given equal odds the rule is to choose the one with the higher Log data density. But in my case mode\_compute=8 already finds the global mode after the mode\_compute=6 has been run so I am tempted to choose the last one.

My question is :  
in order to run the mh\_replications should I stick to the previous estimation (the one before the last with mode\_compute=6) which has a higher Log data density  
or  
should I select the last one (with mode\_compute=8) which has already optimized over the previous one, ?

thanks

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [2 November 2016 18:07 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/2 "2016-11-02T18:07:55Z")

</div>

You are looking at the wrong statistic. The marginal data density is the likelihood of the data, given the model. In principle, it has nothing to to with the posterior mode (except that the Laplace approximation approximates around the mode), because the parameters are integrated out. You need to compare the posterior density, i.e. the density of the parameters given the model and the data:

[quote]Final value of minus the log posterior (or likelihood):-569.338740  
[/quote]

---

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [3 November 2016 14:06 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/3 "2016-11-03T14:06:17Z")

</div>

thanks for the quick reply Prof. Pfeifer:

A bit confused though. I am citing below one of your comments which can be found under the following link:

> [@Model Comparison Bayesian Estimation (again)](https://forum.dynare.org/t/model-comparison-bayesian-estimation-again/3804/1):
>
> Hello, I read the relevant forum topics on this already, still not 100% sure I am doing it right, so some short questions are remaining: I am basically comparing one model in 2 different versions, the second version differs only in one equation of the model block. I am using the same data for both models. I am using the same priors for the two models (althought the second model does contain additional parameters that didnt have to be estimated in the first - (1) does that bias the posterior od…

> [@](#):
>
> Re: Model Comparison Bayesian Estimation (again)  
> Postby Peter Zar » Thu Sep 03, 2015 9:00 am
> 
> Hi together,  
> just one more question in the same context: If I estimate a model M1 and get  
> Log data density [Laplace approximation] is -480, then I estimate a different version M2 (change priors but same data) and get  
> Log data density [Laplace approximation] is -490  
> then M1 is preferred by the data by a Bayes factor of exp(10), correct?
> 
> Best, Peter  
> Peter Zar Posts: 10Joined: Tue Jan 21, 2014 12:02 pm
> 
> ## Top
> 
> Re: Model Comparison Bayesian Estimation (again)  
> Postby jpfeifer » Thu Sep 03, 2015 6:58 pm
> 
> Exactly.

In the cited text above one asks you regarding model comparison using

> [@](#):
>
> Log data density [Laplace approximation]

and you approve. Is that right?  
I am confused : How can I calculate the statistics you mention: IIs it already provided by dynare (I do not seem to find such a statistic)

> [@](#):
>
> Final value of minus the log posterior (or likelihood):-569.338740

in my mode computation.

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [3 November 2016 16:56 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/4 "2016-11-03T16:56:17Z")

</div>

Again, you are not doing model comparison, but mode-finding. Your model is still the same. Model comparison is done via the marginal data density as indicated in the post you reference. Mode-finding proceeds by finding the highest posterior density. After mode-finding, Dynare will tell you this value. In the unstable version, you will get in the output window:

> [@](#):
>
> Final value of minus the log posterior (or likelihood):

---

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [3 November 2016 18:01 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/5 "2016-11-03T18:01:58Z")

</div>

Thanks a lot Prof. for your patience.

I have done all computations in Dynare 4.4.3  
any chance I can get the value somewhere with 4.4.3 ?  
or should I LOAD the different modes with (mode\_file =MODENAME.mat ) and run (with mode\_compute=0) in the unstable version and get the value of minus the log posterior (or likelihood) ?

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [4 November 2016 08:26 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/6 "2016-11-04T08:26:29Z")

</div>

In your log-file, you should have

```auto

```

---

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [4 November 2016 11:16 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/7 "2016-11-04T11:16:45Z")

</div>

that is already in the main output window as well.

Thank you very much for your help Prof. Pfeifer.

---

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [19 November 2016 13:37 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/8 "2016-11-19T13:37:29Z")

</div>

Hi again Prof. Pfeifer and to all,

I am doing the Bayesian estimation of NK framework with some financial frictions.

I first did the mode\_computation and got the same results with mode=6,8,9

But when I run the MCMC (replications) I get good disgnostics and reasonable acceptance rate (22-23%)  
BUT I get two problems:  
…

1. the posterior distribution looks odd : the green vertical line (the mode) does not intersect at the peak of the black line (distribution) for a couple of parameters. in some cases it is way apart from it.
2. in another case I also have the problem that the second set of replications (first set is mh\_replic=30000; second set mh\_replic=30000) gives a much lower or higher acceptance\_rate compared to the first although the ''mh\_jscale=did not change.

I wonder what could be the underlying problem, and how I could fix it (no identification problem: ident.test ok) ?  
I appreciate if someone run accross same problems and share the opinion.

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [20 November 2016 09:53 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/9 "2016-11-20T09:53:45Z")

</div>

Please do a trace plot of the parameters you think are problematic and post the results. See Pfeifer (2014): An Introduction to Graphs in Dynare at [sites.google.com/site/pfeiferecon/dynare](https://sites.google.com/site/pfeiferecon/dynare) for the syntax.

---

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [22 November 2016 13:44 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/10 "2016-11-22T13:44:37Z")

</div>

Thanks a lot Prof. Pfeifer for agreeing to look further into my question.

I get different acceptance ratio depending on the number of MCMC replicatations I run so It took me a while to make sure I get acceptance ratio of 24 % on the 2 blocks.  
I am attaching the Posterior distributions and the TRACE plots in the ZIP file below as you asked Professor.

Ps. in addition, I got the following warning .

> [@](#):
>
> Log data density [Laplace approximation] is 2962.476056.
> 
> Estimation::mcmc: Multiple chains mode.  
> Estimation::mcmc: Old mh-files successfully erased!  
> Estimation::mcmc: Old metropolis.log file successfully erased!  
> Estimation::mcmc: Creation of a new metropolis.log file.  
> Estimation::mcmc: Searching for initial values…  
> Estimation::mcmc: Initial values found!
> 
> Estimation::mcmc: Write details about the MCMC… Ok!  
> Estimation::mcmc: Details about the MCMC are available in BggGKlinear/metropolis\BggGKlinear\_mh\_history\_0.mat
> 
> > In dyn\_first\_order\_solver (line 311)  
> > In stochastic\_solvers (line 217)  
> > In resol (line 137)  
> > In dynare\_resolve (line 69)  
> > In dsge\_likelihood (line 256)  
> > In random\_walk\_metropolis\_hastings\_core (line 167)  
> > In random\_walk\_metropolis\_hastings (line 117)  
> > In dynare\_estimation\_1 (line 782)  
> > In dynare\_estimation (line 89)  
> > In BggGKlinear (line 1194)  
> > In dynare (line 180)  
> > Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =  
> > 4.997928e-17.
> 
> Estimation::mcmc: Number of mh files: 3 per block.  
> Estimation::mcmc: Total number of generated files: 6.  
> Estimation::mcmc: Total number of iterations: 10000.  
> Estimation::mcmc: Current acceptance ratio per chain:  
> Chain 1: 24.1876%  
> Chain 2: 24.4576%  
> Estimation::mcmc::diagnostics: Univariate convergence diagnostic, Brooks and Gelman (1998):

Very much appreciate that you agreed to look into it.

Best  
[diag.zip](https://forum.dynare.org/uploads/default/original/2X/8/8f21c9bd5eecf20e0ea4336f9eae991558e3a267.zip) (945 KB)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [22 November 2016 14:08 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/11 "2016-11-22T14:08:55Z")

</div>

You clearly need much more draws. At least 200,000.

---

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [22 November 2016 14:27 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/12 "2016-11-22T14:27:38Z")

</div>

Thanks a lot for the quick reply Prof. Pfeifer,  
In a separate estimation with twice as many observations I get a similar problem, though no warning as before (in the attached ZIP folder).

Is that the only reason?

(Obviously that’s something can be done easily) …  
[diag2.zip](https://forum.dynare.org/uploads/default/original/2X/a/aee94942ad20db8b7b2990cb089ca6da074e8373.zip) (1 MB)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [22 November 2016 20:41 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/13 "2016-11-22T20:41:56Z")

</div>

The warning you can ignore. It happens for one draw, which is nothing to worry about. But you really need more draws.

---

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [23 November 2016 14:42 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/14 "2016-11-23T14:42:39Z")

</div>

Many Thanks Professor.

---

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [28 November 2016 14:37 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/15 "2016-11-28T14:37:31Z")

</div>

HI again to all and to Prof. Pfeifer,

Following up with the conversation with Prof. Pfeifer,  
I have reached 300 replications on my Bayesian estimation.

The initial problem that I had seems to persist, namely, that while the diagnostics seem fine, the mode (grren line) does not intersect at the peak of the posterior distribution even after 300,000 replications .

I wonder how I could possibly fix that ?  
I have attached the posteriors in a zip file.

Ps. the mode has been computed with 8,9,6 and got the same results.  
[diag\_300.zip](https://forum.dynare.org/uploads/default/original/2X/2/27641a3338b6db34efc61f9c6b3a631b1098cf22.zip) (1.01 MB)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [28 November 2016 14:43 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/16 "2016-11-28T14:43:04Z")

</div>

What exactly is your problem? Looking at the prior posterior plots and the trace plots, the estimation results look quite good.

---

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [28 November 2016 14:54 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/17 "2016-11-28T14:54:41Z")

</div>

Looking at the graphs which I am citing below :

> [@](#):
>
> 0 posterior1.jpg Posterior for ‘‘eps\_IS’’  
> 0 posterior2.jpg Posterior for ‘’ phiX ‘’ zzeta, gammP, thetP, thetY,  
> 0 posterior3.jpg Posterior for ‘‘rhoBU’’

**the green line does not intersect at the peak of the posterior distribution.**

Isn’t it supposed to be that way ? that is the vertical (green ) line cut the posterior )black) at the peak ??  
Alternatively how would I interpret that the posterior mode (greeen line) is not at the peak of the distribution ???

thanks a lot for your time Prof. Pfeifer !

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [29 November 2016 10:25 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/18 "2016-11-29T10:25:15Z")

</div>

This can sometimes happen, but given the rather small differences this is not a reason to worry in your case (the distributions are made using a kernel density estimate and the peak can be somewhat off in that case). Usually when there is a serious problem, the green line is far away and the trace plots show serious drift. None of this is the case here.

---

<div class="post-metadata">

**Author:** ![KKLS](https://forum.dynare.org/letter_avatar_proxy/v4/letter/k/ecd19e/32.png) [@KKLS](https://forum.dynare.org/u/KKLS)\
**Post date:** [29 November 2016 13:17 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/19 "2016-11-29T13:17:03Z")

</div>

Many thanks for the comments again Prof.

I think i might have the situation similar to what you are mentioning: that is a drift in the trace plots.

> [@](#):
>
> by jpfeifer » Tue Nov 29, 2016 10:25 am  
> This can sometimes happen, but given the rather small differences this is not a reason to worry in your case (the distributions are made using a kernel density estimate and the peak can be somewhat off in that case). Usually when there is a serious problem **, the green line is far away and the trace plots show serious drift.** None of this is the case here.

I have simultaneously estimated the \*\*same model \*\*with a shorter sample of the **same observables**.  
It seems there is a problem with the traceplots (i.e parameter ‘‘thetP’’, and ‘‘epsIS’’ )

Is there much I can do about it ?  
probably raise the replications even further (currently 550) ?  
[diag\_SUBsample\_550.zip](https://forum.dynare.org/uploads/default/original/2X/1/1c0d8fad6bc10363237d7a1795517dd1a3c3053d.zip) (1.11 MB)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [29 November 2016 20:53 UTC](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857/20 "2016-11-29T20:53:49Z")

</div>

Not exactly. What you see in those two graphs is an example of bimodality, where the MCMC correctly explores both regions. You still might want to use a longer chain to properly sample from both modes. But there is nothing here to suggest that the chain has not yet converged to its ergodic distribution.  
Have you looked at a trace\_plot of the posterior density?

[Next page](https://forum.dynare.org/t/bayesian-estimation---mode-computation/5857.md?page=2)
