# Particle filter dynare

**URL:** <https://forum.dynare.org/t/particle-filter-dynare/5829>\
**Category:** ML/Bayesian estimation\
**Created:** [19 October 2016 11:56 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829 "2016-10-19T11:56:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![HouseC](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/f475e1/32.png) [@HouseC](https://forum.dynare.org/u/HouseC)\
**Post date:** [19 October 2016 11:56 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/1 "2016-10-19T11:56:15Z")

</div>

Dears,

I search the internet and realized that Dynare could use particle filter when using Bayesian estimation. Is there any example code to show how to call it. I understand that the we need to set “order =2” within the options of estimation. But how about the rest of model?

Best,  
House

---

<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:** [19 October 2016 15:10 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/2 "2016-10-19T15:10:05Z")

</div>

The current test files are at [github.com/DynareTeam/dynare/tree/master/tests/particle](https://github.com/DynareTeam/dynare/tree/master/tests/particle)

---

<div class="post-metadata">

**Author:** ![HouseC](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/f475e1/32.png) [@HouseC](https://forum.dynare.org/u/HouseC)\
**Post date:** [22 October 2016 17:59 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/3 "2016-10-22T17:59:38Z")

</div>

> [@jpfeifer](#):
>
> The current test files are at [github.com/DynareTeam/dynare/tree/master/tests/particle](https://github.com/DynareTeam/dynare/tree/master/tests/particle)

Dear Johannes,  
Many thanks for the link. While I trying to run the linear\_model.mod file. It seems that the datafile “linear\_model\_data” is missing. Is it possible to share this datafile so that the code could be runned.  
Besides, dynare 4.4.3 fails to understand “filter\_algorithm=sis” when estimation command tries to call it, could you please suggest which version of dynare could be useful?

Best,  
House

---

<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:** [22 October 2016 18:05 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/4 "2016-10-22T18:05:14Z")

</div>

1. You need to use the unstable version.
2. The `linear_model_data.mat` is generated by first running [github.com/DynareTeam/dynare/blob/master/tests/particle/linear\_model.mod](https://github.com/DynareTeam/dynare/blob/master/tests/particle/linear_model.mod)

---

<div class="post-metadata">

**Author:** ![HouseC](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/f475e1/32.png) [@HouseC](https://forum.dynare.org/u/HouseC)\
**Post date:** [22 October 2016 18:26 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/5 "2016-10-22T18:26:25Z")

</div>

[quote=“jpfeifer”]1. You need to use the unstable version.  
2. The `linear_model_data.mat` is generated by first running [github.com/DynareTeam/dynare/blob/master/tests/particle/linear\_model.mod](https://github.com/DynareTeam/dynare/blob/master/tests/particle/linear_model.mod)[/quote]

Thanks for patient reply. The first question is silly.  
When I use the unstable version of 20160802 and choose either 0 or 6 for mode-finder, the error message appears:  
\*Error using print\_info (line 110)  
Likelihood is not a number (NaN) or a complex number  
Error in print\_info (line 110)  
error(‘Likelihood is not a number (NaN) or a complex number’);  
Error in initial\_estimation\_checks (line 142)  
print\_info(info, DynareOptions.noprint, DynareOptions)  
Error in dynare\_estimation\_1 (line 159)  
oo\_ =  
initial\_estimation\_checks(objective\_function,xparam1,dataset\_,dataset\_info,M\_,estim\_params\_,options\_,bayestopt\_,bounds,oo\_);  
Error in dynare\_estimation (line 105)  
dynare\_estimation\_1(var\_list,dname);  
Error in linear\_model (line 182)  
oo\_recursive\_=dynare\_estimation(var\_list\_);  
Error in dynare (line 223)  
evalin(‘base’,fname) ; \*

Should I try the most updated version of Dynare? Or it’s a bug to fix?

---

<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:** [24 October 2016 09:22 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/6 "2016-10-24T09:22:08Z")

</div>

The `linear_model.mod`  
file seems to be very outdated. I will try to look into it.

---

<div class="post-metadata">

**Author:** ![HouseC](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/f475e1/32.png) [@HouseC](https://forum.dynare.org/u/HouseC)\
**Post date:** [24 October 2016 13:21 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/7 "2016-10-24T13:21:03Z")

</div>

[quote=“jpfeifer”]The `linear_model.mod`  
file seems to be very outdated. I will try to look into it.[/quote]

Thanks. Please keep me updated.

---

<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:** [24 October 2016 14:06 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/8 "2016-10-24T14:06:21Z")

</div>

Attached is a working version of the model.  
[linear\_model.mod](https://forum.dynare.org/uploads/default/original/2X/9/91265b94f627850d4008358e7566fbe8f4cf94a9.mod) (1.16 KB)

---

<div class="post-metadata">

**Author:** ![HouseC](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/f475e1/32.png) [@HouseC](https://forum.dynare.org/u/HouseC)\
**Post date:** [25 October 2016 14:16 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/9 "2016-10-25T14:16:36Z")

</div>

> [@jpfeifer](#):
>
> Attached is a working version of the model.

Dear Johannes,

Thanks for the code. It works.  
Yet I try to understand the code structure and hope to hear your comments.

1. on prior distribution. In the specified estimated\_params; All parameters are not specified the distribution, is it proper to do that?

`estimated_params;
a1, .50;
a2, .00;
a3, .70;
a4, .40;
b1, .90;
b2, .00;
b3, .80;
c1, .95;
stderr u, .05;
stderr v, .05;
stderr y, 0.01;
stderr z, 0.01;
end;
`

1. on the option choice. I understands that they are to do with particle filter. Is there any user manual to deal with these options? I just to understand the meaning of each options behind.

[code]options\_.particle.status = 1;  
options\_.particle.initialization = 1;  
options\_.particle.pruning = 0;  
options\_.particle.number\_of\_particles = 20000;  
particle.resampling.status.systematic = 1;  
options\_.particle.resampling.neff\_threshold = .1;

options\_.gstep(1) = 1e-4;  
options\_.gstep(2) = .1;

options\_.mode\_check\_neighbourhood\_size = 0.05;

set\_dynare\_threads(‘local\_state\_space\_iteration\_2’,3);  
[/code]

Best,  
House

---

<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:** [25 October 2016 17:54 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/10 "2016-10-25T17:54:29Z")

</div>

1. The first part just means we are doing maximum likelihood instead of Bayesian estimation. That can easily be changed.
2. Please have a look at the manual of the unstable version where most of the options should be (implicitly) defined. The `gstep` sets the gradient step size for numerical gradients and should usually not be altered.

---

<div class="post-metadata">

**Author:** ![HouseC](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/f475e1/32.png) [@HouseC](https://forum.dynare.org/u/HouseC)\
**Post date:** [26 October 2016 01:16 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/11 "2016-10-26T01:16:22Z")

</div>

[quote=“jpfeifer”]1. The first part just means we are doing maximum likelihood instead of Bayesian estimation. That can easily be changed.  
2. Please have a look at the manual of the unstable version where most of the options should be (implicitly) defined. The `gstep` sets the gradient step size for numerical gradients and should usually not be altered.[/quote]

1. I change your shared code in the block of estimated\_params; by specifying the distribution AND keep other commands unchanged. In this case, I guess we are doing bayesian estimation.

[code]estimated\_params;

a1, , , , normal\_pdf, 0.5, 0.05;  
a2, , , , normal\_pdf, 0.0, 0.05;  
a3, , , ,normal\_pdf, 0.7, 0.05;  
a4, , , ,normal\_pdf, 0.4, 0.05;

b1, , , , normal\_pdf, 0.9, 0.05;  
b2, , , ,normal\_pdf, 0.0, 0.05;  
b3, , , ,normal\_pdf, 0.8, 0.05;  
c1, , , , normal\_pdf, 0.95, 0.05;

stderr u, , , , gamma\_pdf, 0.05, 0.05;  
stderr v, , , , gamma\_pdf, 0.05, 0.05;  
stderr y, , , ,gamma\_pdf, 0.05, 0.05;  
stderr z, , , ,gamma\_pdf, 0.05, 0.05;  
end;[/code]

Running this mode file results in the following error message:  
\*Error using chol  
Matrix must be positive definite.  
Error in sequential\_importance\_particle\_filter (line 74)  
StateVectorVarianceSquareRoot =  
chol(ReducedForm.StateVectorVariance)’;%reduced\_rank\_cholesky(ReducedForm.StateVectorVariance)’;  
Error in non\_linear\_dsge\_likelihood (line 333)  
LIK =  
feval(DynareOptions.particle.algorithm,ReducedForm,Y,start,DynareOptions.particle,DynareOptions.threads);  
Error in cmaes (line 948)  
fitness.raw(k) = feval(fitfun, arxvalid(:,k), varargin{:});  
Error in dynare\_minimize\_objective (line 361)  
[x, fval, COUNTEVAL, STOPFLAG, OUT, BESTEVER] =  
cmaes(func2str(objective\_function),start\_par\_value,H0,cmaesOptions,varargin{:});  
Error in dynare\_estimation\_1 (line 199)  
[xparam1, fval, exitflag, hh, options\_, Scale, new\_rat\_hess\_info] =  
dynare\_minimize\_objective(objective\_function,xparam1,options\_.mode\_compute,options\_,[[bounds.lb](http://bounds.lb)  
bounds.ub],bayestopt\_.name,bayestopt\_,hh,dataset\_,dataset\_info,options\_,M\_,estim\_params\_,bayestopt\_,bounds,oo\_);  
Error in dynare\_estimation (line 105)  
dynare\_estimation\_1(var\_list,dname);  
Error in linear\_model (line 185)  
oo\_recursive\_=dynare\_estimation(var\_list\_);  
Error in dynare (line 223)  
evalin(‘base’,fname) ; \*

I checked the user manual and it says “mode\_compute=9” call an evolutionary algorithm for difficult non-linear non-convex optimization and “mode\_compute=6” uses a Monte-Carlo based optimization routine. In this case, it seems using “mode\_compute=6” is still acceptable. Then I change the estimation options and use `mode_compute=6` instead of `mode_compute=9`. The modified mod file could be called without error message. But the system is ineffective and updated the mode after several hours. Is it proper to modify the code in this way?

1. I search the user manual for unstable version using key words “options” and “particle”. It seems that the manual mentions these key words in the estimation command. Yet, it does not mention “options\_.particle”. Yet I guess the following matlab file helps to understand why we need to specify the options. [dynare.org/dynare-matlab-m2html/matlab/global\_initialization.html](http://www.dynare.org/dynare-matlab-m2html/matlab/global_initialization.html)

---

<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:** [26 October 2016 18:06 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/12 "2016-10-26T18:06:57Z")

</div>

1. Yes, using a non-uniform prior is fine and running a different mode-finder is also OK.
2. In the manual to the unstable version you will see the estimation options for the particle filter after

```auto

```

What the interface does is setting the respective fields of

```auto

```

As you found out, these are initialized in

```auto

```

Only some of these options are accessible via the estimation interface, because they are not intended for basic users to change. Instead of setting options via the interface, you can always directly set the options before the command if you know the name of these

---

<div class="post-metadata">

**Author:** ![HouseC](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/f475e1/32.png) [@HouseC](https://forum.dynare.org/u/HouseC)\
**Post date:** [28 October 2016 05:41 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/13 "2016-10-28T05:41:21Z")

</div>

[quote=“jpfeifer”]1. Yes, using a non-uniform prior is fine and running a different mode-finder is also OK.  
2. In the manual to the unstable version you will see the estimation options for the particle filter after

```auto

```

What the interface does is setting the respective fields of

```auto

```

As you found out, these are initialized in

```auto

```

Only some of these options are accessible via the estimation interface, because they are not intended for basic users to change. Instead of setting options via the interface, you can always directly set the options before the command if you know the name of these[/quote]

Dear Johannes,  
Sorry for late reply and thanks for your comments.  
I try to run the linear model with bayesian estimation. However, my labtop cannot find the mode after days of searching. It seems that running particle filter is extremely time-inefficient and demands lots of computational power.

Best,  
House

---

<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:** [28 October 2016 16:57 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/14 "2016-10-28T16:57:07Z")

</div>

Yes, indeed the particle filter is computationally expensive. That is why we rely on the Kalman filter for linear models.

---

<div class="post-metadata">

**Author:** ![shengan28](https://forum.dynare.org/user_avatar/forum.dynare.org/shengan28/32/7453_2.png) [@shengan28](https://forum.dynare.org/u/shengan28)\
**Post date:** [29 October 2016 21:13 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/15 "2016-10-29T21:13:45Z")

</div>

Where is this user manual for the unstable version. Specifically for the particle filter syntax?

---

<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:** [30 October 2016 08:07 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/16 "2016-10-30T08:07:25Z")

</div>

When downloading and installing the unstable version, you can find the up-to-date manual in the doc-folder. It’s called dynare.pdf

---

<div class="post-metadata">

**Author:** ![shengan28](https://forum.dynare.org/user_avatar/forum.dynare.org/shengan28/32/7453_2.png) [@shengan28](https://forum.dynare.org/u/shengan28)\
**Post date:** [2 November 2016 00:12 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/17 "2016-11-02T00:12:13Z")

</div>

By including mode\_check, the mode check plots are created. How can we get the parameter trajectories and parameter densities plots to be created? I am able to create them with other estimation programs, but not this one.

---

<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:** [2 November 2016 08:49 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/18 "2016-11-02T08:49:59Z")

</div>

Which estimation command exactly did you use?

---

<div class="post-metadata">

**Author:** ![shengan28](https://forum.dynare.org/user_avatar/forum.dynare.org/shengan28/32/7453_2.png) [@shengan28](https://forum.dynare.org/u/shengan28)\
**Post date:** [27 December 2016 01:55 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/19 "2016-12-27T01:55:59Z")

</div>

I have been playing around with the linear\_model.mod program.

See [github.com/DynareTeam/dynare/blob/master/tests/particle/linear\_model.mod](https://github.com/DynareTeam/dynare/blob/master/tests/particle/linear_model.mod)

Are the option-options the new, or the old approaches to programming SMC.  
For example,

`options_.particle.number_of_particles = 20000;
options_.particle.resampling.status = 'systematic';
options_.particle.resampling.neff_threshold = .1;
`

can also be programmed within the estimation command:

```auto

```

or more specifically, my question is which approach is preferable?

Also, is there documentation regarding these options?\>

[code]  
options\_.gstep(1) = 1e-4;  
options\_.gstep(2) = .1;

options\_.mode\_check\_neighbourhood\_size = 0.05;

set\_dynare\_threads(‘local\_state\_space\_iteration\_2’,3);[/code]

---

<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 December 2016 06:37 UTC](https://forum.dynare.org/t/particle-filter-dynare/5829/20 "2016-12-27T06:37:39Z")

</div>

In general, you can set any Dynare option manually the way you see it above. If you use the preprocessor interface, all it does is translate the commands to statements like the one above. The preprocessor interface is less prone to mistakes and therefore usually more preferable.

The gstep part is size of the gradient step used in numerical solvers.

`mode_check_neighbourhood_size` is in the manual.

set\_dynare\_threads is a command to

[quote]set the number of threads used by some MEX files when compiled  
with OpenMP support (i.e with --enable-openmp is given to configure) or any  
other parallel library.  
[/quote]

[Next page](https://forum.dynare.org/t/particle-filter-dynare/5829.md?page=2)
