# Shock selection with inversion filter

**URL:** <https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255>\
**Category:** Occbin\
**Created:** [27 February 2024 10:51 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255 "2024-02-27T10:51:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![al1987](https://forum.dynare.org/letter_avatar_proxy/v4/letter/a/74df32/32.png) [@al1987](https://forum.dynare.org/u/al1987)\
**Post date:** [27 February 2024 10:51 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/1 "2024-02-27T10:51:38Z")

</div>

Hi all,

I am trying to understand how estimation works with Occbin, and I’m using Johannes’ NKM.mod file ([tests/occbin/filter · master · Dynare / dynare · GitLab](https://git.dynare.org/Dynare/dynare/-/tree/master/tests/occbin/filter)) as a toy example.

One issue I encounter is the way Occbin selects shocks that need to drop out during the periods when some observable is constant, say the interest rate when the ZLB binds.

In Johannes’ file, the command  
inx = strmatch(‘epsi’,M\_.exo\_names);  
if any(isnan(inom))  
M\_.heteroskedastic\_shocks.Qscale\_orig.periods=find(isnan(inom));  
M\_.heteroskedastic\_shocks.Qscale\_orig.exo\_id=inx;  
M\_.heteroskedastic\_shocks.Qscale\_orig.scale=0;  
instructs Dynare to drop the monetary shock when the interest rate drops out of observables.

When I run the file as it is written, and estimate the model using the inversion filter, I get the expected result: epsi is zero when the ZLB binds. See Fig 1 attached.

When I slightly modify the file to reorder the declaration of shocks, say to  
varexo  
epsi (long\_name=‘Notional interest rate shock’)  
epss (long\_name=‘Risk premium shock’)  
epsg {\varepsilon\_g} (long\_name=‘Productivity growth shock’)  
;  
the output I get signals that Dynare chooses to drop the risk shock instead of the monetary shock. This is in spite of the command instructing Dynare to drop the monetary shock. See Fig 2.

Even weirder, the smoothed shocks are the same as before, but the labels have changed - ie what Dynare calls epss, which now drops out during the ZLB, exactly corresponds to the smoothed epsi from the original version.

I ran into the issue by adding more shocks to the model and I would like to understand what is going on.  
 ![Fig1](https://forum.dynare.org/uploads/default/original/3X/c/f/cf7b68a0734f8abd0b675179ba09c69afa267f9d.png)  
 ![Fig2](https://forum.dynare.org/uploads/default/original/3X/6/7/67cbc7201c7a691c3c4b343d3b109abe834ddaa4.png)

---

<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:** [27 February 2024 18:36 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/2 "2024-02-27T18:36:58Z")

</div>

There is a one to one mapping between the declared observables `varobs` and the shocks `varexo`. In the example, `inom` is the second `varobs`, which has NaN values. Whenever that is the case, Dynare will drop the second declared shocks. By default, that was `epsi`. But in your second try, you moved `epss` to the second position, so that becomes the new shock to drop.

---

<div class="post-metadata">

**Author:** ![al1987](https://forum.dynare.org/letter_avatar_proxy/v4/letter/a/74df32/32.png) [@al1987](https://forum.dynare.org/u/al1987)\
**Post date:** [27 February 2024 20:18 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/3 "2024-02-27T20:18:09Z")

</div>

This makes sense. Thank you for the feedback Johannes.

I think it would be useful to clarify this link between the order of declaration of shocks and varobs in the Manual. It seems unlikely that people will choose the correct declaration order by chance. (Unless it is already written somewhere and I missed it.)

Also I wonder then about the role of the following command lines:  
M\_.heteroskedastic\_shocks.Qscale\_orig.periods=find(isnan(inom));  
M\_.heteroskedastic\_shocks.Qscale\_orig.exo\_id=inx;  
M\_.heteroskedastic\_shocks.Qscale\_orig.scale=0;

Does Dynare actually use these in estimation if the shock-varobs mapping is defined only by the declaration order? Or does it only play a role for the piecewise Kalman filter estimation?

---

<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:** [12 March 2024 14:45 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/4 "2024-03-12T14:45:13Z")

</div>

I finally found the time to push a clarification for the manual:

> **[manual: clarify treatment of missing data in Inversion Filter (!2284) · Merge...](https://git.dynare.org/Dynare/dynare/-/merge_requests/2284/diffs)**
>
> A platform for handling a wide class of economic models

The `heteroskedastic_shocks` is indeed only relevant for the PKF.

---

<div class="post-metadata">

**Author:** ![al1987](https://forum.dynare.org/letter_avatar_proxy/v4/letter/a/74df32/32.png) [@al1987](https://forum.dynare.org/u/al1987)\
**Post date:** [12 March 2024 15:06 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/5 "2024-03-12T15:06:05Z")

</div>

Thanks a lot. It’s super clear now.

---

<div class="post-metadata">

**Author:** ![mariale](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@mariale](https://forum.dynare.org/u/mariale)\
**Post date:** [1 November 2024 10:23 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/6 "2024-11-01T10:23:11Z")

</div>

Dear @jpfeifer  
while running the NKM.mod I encounter the following problem.  
Error using NKM.driver (line 1004)  
smoother redux does not recover full filtered variables results!

Error in dynare (line 308)  
evalin(‘base’,[fname ‘.driver’]); I am running the model in Dynare 6.2. Should I run in another version of Dynare? Best,

---

<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:** [1 November 2024 11:28 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/7 "2024-11-01T11:28:04Z")

</div>

Can you provide the mod-file to replicate the issue?

---

<div class="post-metadata">

**Author:** ![mariale](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@mariale](https://forum.dynare.org/u/mariale)\
**Post date:** [4 November 2024 02:37 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/8 "2024-11-04T02:37:48Z")

</div>

Dear @jpfeifer [NKM.mod](https://forum.dynare.org/uploads/short-url/KsKFdwyusVlRUDPlDnh6JPseUN.mod) (15.4 KB)

---

<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:** [5 November 2024 12:28 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/9 "2024-11-05T12:28:02Z")

</div>

Thanks, but you did not provide all the files required to run your code.

---

<div class="post-metadata">

**Author:** ![mariale](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@mariale](https://forum.dynare.org/u/mariale)\
**Post date:** [6 November 2024 00:45 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/10 "2024-11-06T00:45:17Z")

</div>

@jpfeifer, I am sorry, here:  
[dataobsfile.mat](https://forum.dynare.org/uploads/short-url/yXD3NgqD9GIwSvNBwZQ6xS3heyr.mat) (6.4 KB)  
[NKM.mod](https://forum.dynare.org/uploads/short-url/KsKFdwyusVlRUDPlDnh6JPseUN.mod) (15.4 KB)  
[NKM\_mh\_mode\_saved.mat](https://forum.dynare.org/uploads/short-url/vfXuSMWWyKnwNWmyKxOmsa4JLGT.mat) (972 Bytes)

---

<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:** [7 November 2024 07:52 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/11 "2024-11-07T07:52:30Z")

</div>

Can you please try the unstable version. There, I don’t get the message.

---

<div class="post-metadata">

**Author:** ![mariale](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@mariale](https://forum.dynare.org/u/mariale)\
**Post date:** [12 November 2024 00:57 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/12 "2024-11-12T00:57:39Z")

</div>

It works in the unstable version. Thank you!

---

<div class="post-metadata">

**Author:** ![mariale](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@mariale](https://forum.dynare.org/u/mariale)\
**Post date:** [12 November 2024 11:20 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/13 "2024-11-12T11:20:05Z")

</div>

@jpfeifer, sorry for any confusion. I have a question: Is the NKM\_mh\_mode\_saved.mat file generated as a result of the mode-finding procedure in Dynare? I’m currently working on estimating a similar model using IF.

---

<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:** [12 November 2024 12:34 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/14 "2024-11-12T12:34:42Z")

</div>

No, it is the mode found during the MCMC.

---

<div class="post-metadata">

**Author:** ![mariale](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@mariale](https://forum.dynare.org/u/mariale)\
**Post date:** [13 November 2024 05:30 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/15 "2024-11-13T05:30:40Z")

</div>

> [@mariale](#):
>
> sorry for any confusion. I have a question: Is the NKM\_mh\_mode\_saved.mat file generated as a result of the mode-finding procedure in Dynare? I’m currently working on estimating a similar model using IF.

In that case, do I need to first run the code with some mode\_compute option to find a mode and set cova\_compute=1 to store the Hessian, and then use its results later with the mode\_file option? I am trying to run the NKM.mod file without the NKM\_mh\_mode\_saved.mat file first, but it is taking a very long time now

---

<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:** [13 November 2024 19:33 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/16 "2024-11-13T19:33:48Z")

</div>

What exactly are you trying to achieve? Are you only interested in the mode? Or do you want to conduct Bayesian estimation?

---

<div class="post-metadata">

**Author:** ![mariale](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@mariale](https://forum.dynare.org/u/mariale)\
**Post date:** [14 November 2024 00:45 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/17 "2024-11-14T00:45:24Z")

</div>

Yes, at the end, I want to conduct Bayesian estimation and get posterior means/mode of parameters, and I was also wondering how I can obtain the NKM\_mh\_mode\_saved.mat file results first

---

<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:** [14 November 2024 07:10 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/18 "2024-11-14T07:10:38Z")

</div>

You cannot get it “first”. It stands at the end of estimation. If someone else provides you with the file you can use. Otherwise, you have to first run the estimation chain to obtain it.

---

<div class="post-metadata">

**Author:** ![mariale](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@mariale](https://forum.dynare.org/u/mariale)\
**Post date:** [14 November 2024 07:52 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/19 "2024-11-14T07:52:42Z")

</div>

@jpfeifer Thank you for your response. I understand that I need to run the estimation chain to obtain it. However, when I run the code with MCMC by specifying the estimation command as:  
// use inversion filter (note that IF provides smoother together with likelihood)  
occbin\_setup(likelihood\_inversion\_filter,smoother\_inversion\_filter);

estimation(datafile=dataobsfile2, mode\_compute=6, cova\_compute=1, nobs=120, first\_obs=1, mh\_replic=15000, plot\_priors=0, smoother, consider\_all\_endogenous, heteroskedastic\_filter, filter\_step\_ahead=[1:8], smoothed\_state\_uncertainty);  
It does not produce results even after 4-5 hours. Is it normal for IF to take such a long time? And more importantly, am I specifying the command correctly? I would greatly appreciate your help.

---

<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:** [14 November 2024 14:38 UTC](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255/20 "2024-11-14T14:38:40Z")

</div>

Yes, estimating a model with OccBin will take much longer than with a standard first order solution. It can take days.

[Next page](https://forum.dynare.org/t/shock-selection-with-inversion-filter/25255.md?page=2)
