# Construct likelihood given parameter vector

**URL:** <https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979>\
**Category:** ML/Bayesian estimation\
**Created:** [29 August 2022 01:31 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979 "2022-08-29T01:31:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay1](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jay1](https://forum.dynare.org/u/jay1)\
**Post date:** [29 August 2022 01:31 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979/1 "2022-08-29T01:31:46Z")

</div>

I’m wondering if it is possible to run a mod file once and then feed in parameter vectors and retrieve the likelihood using the dsge\_likelihood function. A previous post I found asking a similar question is: [https://forum.dynare.org/t/likelihood-only/3850](https://forum.dynare.org/t/likelihood-only/3850). The main difference between what I would like to do and what is recommended here is that I would like to use the dsge\_likelihood function outside of the mod file. I’ve tried this but receive a few errors which I think are related to the local nature of some of the variables.

If the above cannot be done, is my best approach to use the state space setup of the model produced by Dynare and run it through my own Kalman filter codes? Perhaps making use of the resol function for each parameter draw?

I’m ultimately interested in coding some of my own mcmc sampling routines for estimation. But for some of the models I am using, writing the state space of the model takes a significant amount of time. I’m hoping I can use Dynare to produce the state space (or even evaluate the likelihood) and then simply add it into my routines. Any suggestions are appreciated.

Thanks,  
Jay.

---

<div class="post-metadata">

**Author:** ![MichelJuillard](https://forum.dynare.org/user_avatar/forum.dynare.org/micheljuillard/32/5318_2.png) [@MichelJuillard](https://forum.dynare.org/u/MichelJuillard)\
**Post date:** [29 August 2022 07:34 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979/2 "2022-08-29T07:34:17Z")

</div>

Would you be interested in doing it in DynareJulia?

---

<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 August 2022 11:02 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979/3 "2022-08-29T11:02:07Z")

</div>

Can you provide your codes to see the error messages?

---

<div class="post-metadata">

**Author:** ![jay1](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jay1](https://forum.dynare.org/u/jay1)\
**Post date:** [29 August 2022 12:01 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979/4 "2022-08-29T12:01:40Z")

</div>

Codes are attached. Currently just trying with the version of Smets Wouters posted on your Github. The error I get is:

_Reference to non-existent field ‘analytic\_derivation’._

_Error in dsge\_likelihood (line 143)_  
_if DynareOptions.analytic\_derivation_

_Error in Main (line 6)_  
_[likelihood,junk1,junk2,exit\_flag,ys,trend\_coeff,info] = dsge\_likelihood(xparam1,dataset\_,options\_,M\_,estim\_params\_,bayestopt\_,oo\_);_

Perhaps this is an issue of trying to pass a structure through the function? In my Matlab workspace, options\_.analytic\_derivation does exist as an input.

[SmetsWouters\_2007.zip](https://forum.dynare.org/uploads/short-url/7Ihc062ahYktrmXeuISBRVqkVzj.zip) (26.5 KB)

---

<div class="post-metadata">

**Author:** ![jay1](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/9d8465/32.png) [@jay1](https://forum.dynare.org/u/jay1)\
**Post date:** [29 August 2022 12:03 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979/5 "2022-08-29T12:03:06Z")

</div>

If it can be done in DynareJulia, I would be open to doing it there.

---

<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 August 2022 12:36 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979/6 "2022-08-29T12:36:08Z")

</div>

Use Dynare 5 together with

```auto
dynare Smets_Wouters_2007_45.mod noclearall
%[dataset_,xparam1, hh, M_, options_, oo_, estim_params_,bayestopt_] = dynare_estimation_init(var_list_, M_.fname, [], M_, options_, oo_, estim_params_, bayestopt_);
load('usmodel_shock_decomp_mode.mat');
[dataset_, dataset_info, ~, ~, M_, options_, oo_, estim_params_,bayestopt_, bounds] = dynare_estimation_init({}, M_.dname, 0, M_, options_, oo_, estim_params_, bayestopt_);
[fval,info,exit_flag] = dsge_likelihood(xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_,[]);

```

---

<div class="post-metadata">

**Author:** ![MichelJuillard](https://forum.dynare.org/user_avatar/forum.dynare.org/micheljuillard/32/5318_2.png) [@MichelJuillard](https://forum.dynare.org/u/MichelJuillard)\
**Post date:** [29 August 2022 13:58 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979/7 "2022-08-29T13:58:25Z")

</div>

It can be done in Dynare Matlab or in DynareJulia. For Julia look at [GitHub - DynareJulia/Dynare.jl: A Julia rewrite of Dynare: solving, simulating and estimating DSGE models.](https://github.com/DynareJulia/Dynare.jl)  
The estimation code under development is in branch estimation\_3

---

<div class="post-metadata">

**Author:** ![hotz](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/c37758/32.png) [@hotz](https://forum.dynare.org/u/hotz)\
**Post date:** [19 July 2024 19:35 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979/8 "2024-07-19T19:35:16Z")

</div>

Dear Johannes,

What is the proper way to evaluate the `dsge_likelihood` function in Dynare v6? Using v6.1 on a Windows machine, I get the following error:

```matlab
>>[fval,info,exit_flag] = dsge_likelihood(xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,bounds,oo_,[]);
Not enough input arguments.

Error in dsge_likelihood (line 138)
    [T,R,SteadyState,info,dr, M_.params] = dynare_resolve(M_,options_,dr, endo_steady_state, exo_steady_state, exo_det_steady_state,'restrict');

```

To be clear, I also ran the preceding lines exactly as in your comment.

Best,  
Logan

---

<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 July 2024 09:37 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979/9 "2024-07-20T09:37:48Z")

</div>

I think it should be

```auto
[fval,info,exit_flag] = 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,[])

```

---

<div class="post-metadata">

**Author:** ![hotz](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/c37758/32.png) [@hotz](https://forum.dynare.org/u/hotz)\
**Post date:** [20 July 2024 14:44 UTC](https://forum.dynare.org/t/construct-likelihood-given-parameter-vector/20979/10 "2024-07-20T14:44:02Z")

</div>

Thank you Johannes!
