Get likelihood evaluated at parameter draw in posterior_function

Hello!

We can use the posterior_function to compute statistics on the prior/posterior draws. We can call each parameter inside that function by simply typing each parameter name.

I wanted to know if there was a quick command to get the data’s likelihood (or log-likelihood) evaluated at the current parameter draw.

Thank you for your time.

Do you want the likelihood for just one draw or all of them? And is it the likelihood or the posterior?

Thank you so much for the response!

I wanted to get the likelihood of the data at each parameter draw passed into the posterior_function (so in the posterior_function_demo, the likelihood at the draw xparam1), and I am interested in the likelihood of the data evaluated at that draw, not the posterior.

You need to try, but it should be

bounds = prior_bounds(bayestopt_, options_.prior_trunc);
options_=select_qz_criterium_value(options_);
[fval,info] = dsge_likelihood(xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_.dr,oo_.steady_state,oo_.exo_steady_state,oo_.exo_det_steady_state);
lnprior=priordens(xparam1,bayestopt_.pshape,bayestopt_.p6,bayestopt_.p7,bayestopt_.p3,bayestopt_.p4);
lik=-fval-lnprior;
1 Like

Hello professor!

I really appreciate the help!

My reading of the priordens function was that it returns the density of the prior. However, using the code you provided, I believe:

bounds = prior_bounds(bayestopt_, options_.prior_trunc);
[fval,info] = dsge_likelihood(xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_);
lik = fval

gives me the likelihood evaluated at each draw in the posterior_function! Thank you so much.

Related: I would also like to be able to get the likelihood at each draw in a prior_function. However, running the same code above in dynare 5.5 and Matlab R2023a gives me the error:

EXECUTE_POSTERIOR_FUNCTION: Execution of prior/posterior function led to an error. Execution cancelled.
Unrecognized field name "kstate".
Error in stochastic_solvers (line 196)
kstate = dr.kstate;
Error in resol (line 119)
    [dr,info] = stochastic_solvers(dr,check_flag,M,options,oo);
Error in compute_decision_rules (line 34)
    [dr,info,M_,oo_] = resol(0,M_,options_,oo_);
Error in dynare_resolve (line 36)
[dr,info,M_,oo_] =compute_decision_rules(M_,options_,oo_);
Error in dsge_likelihood (line 216)
    [T,R,SteadyState,info,Model,DynareResults] = dynare_resolve(Model,DynareOptions,DynareResults,'restrict');
Error in PC_slope_prior (line 61)
[fval,info,exit_flag,DLIK,Hess,SteadyState,trend_coeff,M_,options_,bayestopt_,oo_] = dsge_likelihood(xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_);
Error in execute_prior_posterior_function (line 90)
    junk=functionhandle(parameter_mat(1,:),M_,options_,oo_,estim_params_,bayestopt_,dataset_,dataset_info);
Error in Smets_Wouters_2007_45.driver (line 624)
oo_ = execute_prior_posterior_function('PC_slope', M_, options_, oo_, estim_params_, bayestopt_, dataset_, dataset_info, 'prior');
Error in dynare (line 281)
    evalin('base',[fname '.driver']);
Error in Smets_Wouters_2007 (line 2)
dynare Smets_Wouters_2007_45.mod 

Do you know of a way to get the likelihood evaluated at the draw for a function passed into the prior_function as well?

  1. You need to be careful. dsge_likelihood returns minus the posterior, not the likelihood:
    fval    = (likelihood-lnprior);

where likelihood is minus the actual likelihood. To get the actual likelihood, you therefore need

lik=-fval-lnprior;
  1. For running a prior function like that, you need to have an estimation-command first.
1 Like

Thank you so much professor!

Understood about dsge_likelihood. I wanted to confirm my understanding:

dsge_likelihood returns the likelihood minus the logged prior, so we need to add pack the logged prior (given by lnprior) to fval, which would be

lik = fval + lnprior

I have two lingering uncertainties: (1) I’m not sure why we’re multiplying the likelihood by a -1; and (2) running the lnprior function inside of a posterior_function I get the error:

EXECUTE_POSTERIOR_FUNCTION: Execution of prior/posterior function led to an error. Execution cancelled.
Index exceeds the number of array elements. Index must not exceed 16.
Error in lpdfgbeta (line 42)
    ldens(idx) = -betaln(a(idx),b(idx)) + (a(idx)-1).*log(x(idx)-aa(idx)) + (b(idx)-1).*log(bb(idx)-x(idx)) - (a(idx)+b(idx)-1).*log(bb(idx)-aa(idx));
Error in priordens (line 90)
    logged_prior_density = logged_prior_density + sum(lpdfgbeta(x(id1),p6(id1),p7(id1),p3(id1),p4(id1))) ;
Error in OOI_and_likelihood (line 62)
lnprior = priordens(xparam1,bayestopt_.pshape,bayestopt_.p6,bayestopt_.p7,bayestopt_.p3,bayestopt_.p4);
Error in execute_prior_posterior_function (line 90)
    junk=functionhandle(parameter_mat(1,:),M_,options_,oo_,estim_params_,bayestopt_,dataset_,dataset_info);
Error in Smets_Wouters_2007_45.driver (line 646)
oo_ = execute_prior_posterior_function('OOI_and_likelihood', M_, options_, oo_, estim_params_, bayestopt_, dataset_, dataset_info, 'posterior');
Error in dynare (line 281)
    evalin('base',[fname '.driver']);
Error in Smets_Wouters_2007 (line 2)
dynare Smets_Wouters_2007_45.mod 

About running the code after the estimation command, that worked!
I really appreciate your help and I apologize for all the trouble.

For clarity, I wrote:

lnprior = priordens(xparam1,bayestopt_.pshape,bayestopt_.p6,bayestopt_.p7,bayestopt_.p3,bayestopt_.p4);

in Matlab R2023a and Dynare 5.5 inside a posterior_function to produce the error.

  1. dsge_likelihood returns -1*likelihood not likelihood because numerical optimizers are generally minimizers, not maximizers. That’s why
  1. Without the actual code it is impossible to debug what causes the crash.

Hello professor!

Thank you so much for answering my question about the likelihood! I was able to get the error using Smets_Wouters_2007 code below:
issue.zip (66.1 KB)

It seems in Dynare 5.5 the code must be:
PC_slope.m (3.2 KB)

1 Like

Thank you so much for your help!

That gives exactly what I was after for the posterior function! Do you know why it breaks for the prior_function?

You forgot the line

PC_slope.m (3.3 KB)

1 Like