Likelihood only!

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

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
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:

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_);
1 Like

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.

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.

Oki, I see. Thank you very much :slight_smile:

I tried your trick on Smets and Wouters (2007) model and I got positive values in the “likelihood”. Could that indicate a problem?

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.

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!
ML only.zip (14.6 KB)

There was some problem with global variables. Using

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.

Thank you! It seems to work now :slight_smile:

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 ?

For the estimated parameters, only the values in xparam1 should matter. For the calibrated ones, of course the previously set values matter.

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?

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?

Yes, that made things a lot clearer. Thank you :slight_smile:

I use this method for Smets and Wouters (2007) model and then I grid the parameters one at the time between wide intervals. The parameter value is on the x-axis and the likelihood is on the y-axis.

The interval for the shock parameters are 0.01-5. The strange thing however is that many of the shocks, technology for instance, is just a horisontal line as if it doesn’t affect the likelihood at all. Does this make sense? What could this mean?

Please post or send the whole code. It might have to do with the covariance matrix not correctly updating.

The files are about 10 mb because of all the big arrays that I saved from Dynare. Therefore I sent the files here instead
sprend.com/download.htm?C=beff9a … 09348fd8e1
under the name ‘all_code.zip’

You open the m-file ‘submit_code.m’ and click run, then Matlab loads the matrices you need from the mat-file ‘a_sw.mat’. Next, Matlab, grids all of the parameters one at the time around the parameter vector ‘param_vec_fcon’. When this is done the function ‘subplot_funktion’ is used to plot all of the parameters with the likelihood on the y-axis, where the black vertikal line is the paramater value from the initial parameter vector ‘param_vec_fcon’.

All the shocks are named sigma_x. So for instance the technology shock is called “sigma_a”, these symbols you’ll see in the subplots.

Let me know if you are missing any files :slight_smile:

numSubplots.m is missing.

Sorry about that. Here it is

\\Glenn
numSubplots.m (1.08 KB)