Hi all,

I was wondering if there is a way to have Dynare evaluate the likelihood and/or posterior density at a single parameter guess? I’m trying to build the likelihood and posterior density for the Smets Wouters 07 model but without the use of Dynare.

I’ve managed to solve the model (which I checked by comparing the second-moments of the endogenous variables in my hand-written code against dynare) but given the problems I’ve encountered, I’m a lot less certain that I haven’t screwed up my computation of posterior likelihood

The easiest way is to set

```
options_.debug=1
```

You will then have a field

```
oo_.likelihood_at_initial_parameters;
```

which stores the posterior (likelihood if you use ML).

Okay, I’ve managed to solve out the model moments in dynare and in my home-built code and they both match. However, the likelihood at the initial parameter guess is very slightly different between dynare and my own code:

Dynare reports a log likelihood of -2136.3977 while my own code gives a log likelihood of -2136.10010115258.

Is there anything special about how Dynare computes the model likelihood? Does it drop the first observation or perhaps initialize the prediction-variance matrix? Should I even be worried about such a seemingly small difference in likelihood?

The prediction variance matrix is initialized at the unconditional variance. Usually, such small differences are not a reason to worry. But when you are checking consistency, you usually want to be sure.

I have figured out why my likelihood was slightly different: the parameter vector in the dynare file was *very* slightly different from the parameter I asked my home-built code to evaluate. After fixing the dynare file, the likelihood was exactly the same between my own code and dynare’s code. Therefore I am more than confident that my likelihood function is correct, or at least consistent with Smets Wouters!

Having computed the likelihood function, I am now trying to compute the posterior by combining the prior and likelihood. I am now, however, getting somewhat larger discrepancies.

To diagnose the error, I changed all of the prior distributions in both my code and the Dynare code to normal densities. This resulted in identical posterior densities between the two programs. However, when I changed the prior on any one parameter in both programs to a beta pdf, inverse gamma pdf, or gamma pdf, then I got different values for the log posterior.

To obtain the beta and gamma pdfs I just use the betapdf(x,a,b) and gamma(x,a,b) matlab functions. To obtain the log of the inverse gamma function I use

y = log(2) - gammaln(b/2) + (b/2)*log(b*a^2/2) - ( (b+1)/2 )*log(x^2) - b*a^2/(2*x^2);

It’s always tricky to get the right mapping into a and b. See Chapter 4 and the associated codes in my Kobe lecture at https://sites.google.com/site/pfeiferecon/teaching

1 Like

I’ve tried to reproduce the inverse gamma prior by typing the following function, as specified on slide 30 of 66 in chapter 4.

function fig = invgam(y,a,b)

fig = (b^a)/(gamma(a)) * y^(a-1) * exp(-b*y);

end

I am trying to reproduce the prior distribution that dynare produces when given the following specification:

stderr ea,0.4618,0.01,3,INV_GAMMA_PDF,0.1,2;

However when graphing my attempted reproduction of the code presented on slide 30, it looks very dissimilar to the graph produced by Dynare when it graphs the priors.

Well this is embarrassing

I solved my problem (to my satisfaction at least, there are still differences between Dynare’s output and mine: at the initial parameter guess Dynare gives a log posterior density of -2166.4564778681 and my own code gives -2166.4564778687), but it turns out that I fundamentally did not understand how to evaluate gamma, inverse-gamma, or beta densities when only the mean and variance are known.

I wish I could explain how I worked around it as it involved using Dynare’s code directly.