Page 1 of 4

Likelihood only!

PostPosted: Tue Apr 15, 2014 3:56 pm
by Glm
Let's say that I want the value of the likelihood only not more nor less, cause I want to loop over a parameter and want to see how the value of the likelihood changes for each of the paramaterer value in my grid? And since I am looping, I want each run to be as quick as possible.

Last but not least where do I find the value of the likelihood after each run, what's the short cut command for it in Matlab?

Thank you!

Glm

Re: Likelihood only!

PostPosted: Wed Apr 16, 2014 10:14 am
by jpfeifer
With the most recent snapshot, the following should be possible.
1. Run a short MCMC using the estimation command with say mh_replic=10 to set up all variables for likelihood computation.
2. After the estimation command, add to your mod-file
Code: Select all
global dataset_  bayestopt_       
[likelihood,junk1,junk2,exit_flag,ys,trend_coeff,info] = dsge_likelihood(xparam1,dataset_,options_,M_,estim_params_,bayestopt_,oo_);

The first line makes the required additional variables global, while the second line computes the likelihood for a given parameter vector xparam1 (which you need to define or set). This second line can be embedded into a loop.
I quickly tested this code by adding the following lines to the fs2000.mod in Dynare's examples folder:

Code: Select all
load fs2000_mode xparam1
global dataset_  bayestopt_       
[likelihood,junk1,junk2,exit_flag,ys,trend_coeff,info] = dsge_likelihood(xparam1,dataset_,options_,M_,estim_params_,bayestopt_,oo_);

Re: Likelihood only!

PostPosted: Wed Apr 16, 2014 1:10 pm
by Glm
Why do I need the Metropolis-Hastings algorithm at all? Because I am only interested in the value of the likelihood function for a specific set of parameters, I am not interested in the shape of the distribution. Can't I use "Initial value of the log posterior (or likelihood): 1026.1859" instead? Because this value is shown in the very beginning of the estimation and it only takes about 1-2 seconds for dynare to calculate it compare to the whole estimation which takes about 10 seconds.

Re: Likelihood only!

PostPosted: Wed Apr 16, 2014 1:25 pm
by jpfeifer
You need the MCMC once at the beginning to initialize the data set and the priors. After that you can get the likelihood for any given parameter vector by just calling the dsge_likelihood function as shown. That should take less than a second. I am not suggesting to run the MCMC for every set of parameters. Rather I am telling you to directly change xparam1 and compute its likelihood without calling Dynare again.

Re: Likelihood only!

PostPosted: Wed Apr 16, 2014 2:36 pm
by Glm
Oki, I see. Thank you very much :)

Re: Likelihood only!

PostPosted: Thu Apr 17, 2014 11:44 am
by Glm
I tried your trick on Smets and Wouters (2007) model and I got positive values in the "likelihood". Could that indicate a problem?

Re: Likelihood only!

PostPosted: Thu Apr 17, 2014 12:27 pm
by jpfeifer
It depends. When you load xparam1 from the mode-file and use it, is the result the same as shown in "Initial value of the log posterior"? If not, post the files.

Re: Likelihood only!

PostPosted: Thu Apr 17, 2014 1:00 pm
by Glm
Oki, it doesn't seem like they are the same so I've attached the files. I have disabled all parameters in the estimation block except for "csadjcost" in order to save time. If you wanna try with more or all parameters, just take away the % sign in front of them. The modfile also calls the parameters by using the funktion "parametrar_funktion" but that's nothing to be concerned about, that shouldn't change anything.
Ok, that's it I think.

Thank you!

Re: Likelihood only!

PostPosted: Thu Apr 17, 2014 2:08 pm
by jpfeifer
There was some problem with global variables. Using
Code: Select all
global dataset_  bayestopt_
[dataset_,xparam1, hh, M_, options_, oo_, estim_params_,bayestopt_] = dynare_estimation_init(var_list_, M_.fname, [], M_, options_, oo_, estim_params_, bayestopt_);
load sw_check_mode xparam1
[likelihood,junk1,junk2,exit_flag,ys,trend_coeff,info] = dsge_likelihood(xparam1,dataset_,options_,M_,estim_params_,bayestopt_,oo_);

should yield identical results.

Re: Likelihood only!

PostPosted: Thu Apr 17, 2014 2:29 pm
by Glm
Thank you! It seems to work now :)

Re: Likelihood only!

PostPosted: Thu Apr 17, 2014 4:03 pm
by Glm
So, nor the initial parameter values in the parameter block nor the initial parameter values in the estimation block should effect the likelihood value that I get later on when changing the vector xparam1 ?

Re: Likelihood only!

PostPosted: Thu Apr 17, 2014 4:39 pm
by jpfeifer
For the estimated parameters, only the values in xparam1 should matter. For the calibrated ones, of course the previously set values matter.

Re: Likelihood only!

PostPosted: Tue May 20, 2014 5:18 pm
by Glm
I have a strange problem when doing this method and I've traced the problem to
dsge_likelihood=>dynare_resolve=>resol=>stochastic_solvers=>dyn_first_order_solver=>dyn_first_order_solver

Then, I run Matlab in debug mode and I set a break point just at row 220 where the following code line is
[err, ss, tt, w, sdim, dr.eigval, info1] = mjdgges(E, D, DynareOptions.qz_criterium, DynareOptions.qz_zero_threshold);
And then when I click "step in" in Matlab but it just passes it without stepping in but I still get values for [err, ss, tt, w, sdim, dr.eigval, info1]. And when I try to open
mjdgges it says that it doesn't exist (all this I do in debug mode when the courser is stopped just at the line after (221)). I also noticed that when I run the code over and over again with different paramater vectors in dsge_likelihood (I change xparam1) it gives the same value for [err, ss, tt, w, sdim, dr.eigval, info1].

So, what I do next is that I once again stop at [err, ss, tt, w, sdim, dr.eigval, info1] = mjdgges(E, D, DynareOptions.qz_criterium, DynareOptions.qz_zero_threshold); (row 220 in dyn_first_order_solver) and before I click "step in" I manually add the folder where mjdgges is located by clicking
addpath C:\dynare\4.4.1\matlab\qz. Then I am able to "step in" and what happens then is that I get complex values for w in the output vector [err, ss, tt, w, sdim, dr.eigval, info1]. And when I run Matlab normally after that it always gives negative values for w which in turn always gives me the likelihood value 1.0000e+8 regardless of what parameter i put in.

So can anybody make sense of this? What can I do?

Re: Likelihood only!

PostPosted: Tue May 20, 2014 7:58 pm
by jpfeifer
mjdgges is a mex-file (dll-file) and hence cannot be debugged by Matlab. For people who cannot run/compile mex-files, there is a backup m-file version in the missing/qz-folder that is slightly different. When you set the path to missing/qz you force Matlab to use this m-file and consequently, you can debug it.

Note that 1.0000e+8 is an error code.

Does this answer your question?

Re: Likelihood only!

PostPosted: Wed May 21, 2014 2:19 pm
by Glm
Yes, that made things a lot clearer. Thank you :)