# Problem with BVAR and Metropolis-Hastings

**URL:** <https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105>\
**Category:** ML/Bayesian estimation\
**Created:** [12 December 2017 10:12 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105 "2017-12-12T10:12:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [12 December 2017 10:12 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/1 "2017-12-12T10:12:13Z")

</div>

Dear Members,

I have some issues for two mod files that I cannot solve:

**- The bvar\_us.mod does not work. I get error messages and I can’t find why:**

_Warning: Range cannot be used in ‘basic’ mode. The entire sheet will be loaded. \_  
 \> In xlsread (line 211)  
\_ In load\_xls\_file\_data (line 77)_  
\_ In read\_variables (line 92)\_  
\_ In bvar\_toolbox (line 65)\_  
\_ In bvar\_density (line 37)\_  
\_ In bvar\_us (line 75)\_  
\_ In dynare (line 223) \_  
_Error using error_  
_The message must be specified as either a character vector or a message structure._  
_Error in read\_variables (line 104)_  
\_ error([strtrim(var\_names\_01(dyn\_i\_01,:)) ’ not found in ’ fullname])\_  
_Error in bvar\_toolbox (line 65)_  
_dataset = read\_variables(options_.datafile, options\_.varobs, [],\_  
_options_.xls\_sheet, options\_.xls\_range);\_  
_Error in bvar\_density (line 37)_  
\_ [ny, nx, posterior, prior] = bvar\_toolbox(nlags);\_  
_Error in bvar\_us (line 75)_  
_bvar\_density(4);_  
_Error in dynare (line 223)_  
_evalin(‘base’,fname) ;_

**- The NK3eq\_estim does not work as well, especially for the Metropolis-Hastings.**

_Warning: Range cannot be used in ‘basic’ mode. The entire sheet will be loaded. \_  
 \> In xlsread (line 211)  
\_ In load\_xls\_file\_data (line 77)_  
\_ In dseries (line 130)\_  
\_ In makedataset (line 121)\_  
\_ In dynare\_estimation\_init (line 538)\_  
\_ In dynare\_estimation\_1 (line 116)\_  
\_ In dynare\_estimation (line 105)\_  
\_ In NK3eq\_estim (line 208)\_  
\_ In dynare (line 223) \_

_Warning: The results below are most likely wrong! \_  
 \> In dynare\_estimation\_1 (line 316)  
\_ In dynare\_estimation (line 105)_  
\_ In NK3eq\_estim (line 208)\_  
\_ In dynare (line 223) \_  
_Warning: Matrix is singular, close to singular or badly scaled. Results may be_  
_inaccurate. RCOND = NaN. \_  
 \> In dynare\_estimation\_1 (line 339)  
\_ In dynare\_estimation (line 105)_  
\_ In NK3eq\_estim (line 208)\_  
\_ In dynare (line 223) \_

_Error using chol_  
_Matrix must be positive definite._  
_Error in posterior\_sampler\_initialization (line 84)_  
_d = chol(vv);_  
_Error in posterior\_sampler (line 60)_  
\_ posterior\_sampler\_initialization(TargetFun, xparam1, vv,\_  
\_ mh\_bounds,dataset\_,dataset\_info,options\_,M\_,estim\_params\_,bayestopt\_,oo\_);\_  
_Error in dynare\_estimation\_1 (line 447)_  
\_ posterior\_sampler(objective\_function,posterior\_sampler\_options.proposal\_distribution,xparam1,posterior\_sampler\_options,bounds,dataset\_,dataset\_info,options\_,M\_,estim\_params\_,bayestopt\_,oo\_);\_  
\_ Error in dynare\_estimation (line 105)\_  
\_ dynare\_estimation\_1(var\_list,dname);\_  
_Error in NK3eq\_estim (line 208)_  
_oo\_recursive_=dynare\_estimation(var\_list\_);\_  
_Error in dynare (line 223)_  
_evalin(‘base’,fname) ;_

[bvar\_us.mod](https://forum.dynare.org/uploads/default/original/2X/6/6846f5acaf77960a08bce642c2f73a511600b733.mod) (226 Bytes)  
[NK3eq\_estim.mod](https://forum.dynare.org/uploads/default/original/2X/2/2771e997b0f527ab6c282a48d21899b563efacf3.mod) (3.2 KB)  
[data\_us\_nk.xls.zip](https://forum.dynare.org/uploads/default/original/2X/2/2237dad8cde5837491b537948e40706a639a3b10.zip) (23.7 KB)

Thank you in advance,

Best regards

---

<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 December 2017 15:39 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/2 "2017-12-20T15:39:45Z")

</div>

This is a problem with Excel compatibility on your machine. Which operating system are you using? And do you have Excel installed?

---

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [27 December 2017 16:19 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/3 "2017-12-27T16:19:36Z")

</div>

Dear Jpfeifer,

Thank you for your answer. I am using the latest version of Excel (2017). Excel is installed. What do you mean by operating system?

How do I solve this problem?

Thank you,

Best regards

---

<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 December 2017 08:37 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/4 "2017-12-28T08:37:17Z")

</div>

Try saving the file as an `xlsx` instead of `xls`-file.

---

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [28 December 2017 10:29 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/5 "2017-12-28T10:29:01Z")

</div>

Thank you, it almost works. I still have a problem with the bayesian estimation, the Metropolis-Hastings does not appear when running the model:

> POSTERIOR KERNEL OPTIMIZATION PROBLEM!  
> (minus) the hessian matrix at the “mode” is not positive definite!  
> =\> posterior variance of the estimated parameters are not positive.  
> You should try to change the initial values of the parameters using  
> the estimated\_params\_init block, or use another optimization routine.  
> Warning: The results below are most likely wrong!
> 
> > In dynare\_estimation\_1 (line 316)  
> > In dynare\_estimation (line 105)  
> > In NK3eq\_estim (line 206)  
> > In dynare (line 223)  
> > Warning: Matrix is singular, close to singular or badly scaled. Results may be  
> > inaccurate. RCOND = NaN.  
> > In dynare\_estimation\_1 (line 339)  
> > In dynare\_estimation (line 105)  
> > In NK3eq\_estim (line 206)  
> > In dynare (line 223)
> 
> RESULTS FROM POSTERIOR ESTIMATION  
> parameters  
> prior mean mode s.d. prior pstdev
> 
> rho\_D 0.500 0.9738 NaN beta 0.2000  
> rho\_S 0.500 0.9996 NaN beta 0.2000  
> rho\_R 0.500 0.3463 NaN beta 0.2000  
> sigma 1.000 3.2743 NaN gamm 0.4000  
> varphi 2.000 1.3990 NaN gamm 0.7500  
> phi\_pi 1.500 1.5913 NaN norm 0.1000  
> phi\_y 0.050 -0.0002 NaN norm 0.0500  
> rho 0.750 0.6801 NaN beta 0.0500  
> theta 0.500 0.8077 NaN beta 0.1000
> 
> standard deviation of shocks  
> prior mean mode s.d. prior pstdev
> 
> eta\_D 0.100 0.0399 NaN invg 2.0000  
> eta\_S 0.100 0.2169 NaN invg 2.0000  
> eta\_R 0.100 0.2621 NaN invg 2.0000
> 
> Log data density [Laplace approximation] is NaN.
> 
> Error using chol  
> Matrix must be positive definite.  
> Error in posterior\_sampler\_initialization (line 84)  
> d = chol(vv);  
> Error in posterior\_sampler (line 60)  
> posterior\_sampler\_initialization(TargetFun, xparam1, vv,  
> mh\_bounds,dataset\_,dataset\_info,options\_,M\_,estim\_params\_,bayestopt\_,oo\_);  
> Error in dynare\_estimation\_1 (line 447)  
> posterior\_sampler(objective\_function,posterior\_sampler\_options.proposal\_distribution,xparam1,posterior\_sampler\_options,bounds,dataset\_,dataset\_info,options\_,M\_,estim\_params\_,bayestopt\_,oo\_);  
> Error in dynare\_estimation (line 105)  
> dynare\_estimation\_1(var\_list,dname);  
> Error in NK3eq\_estim (line 206)  
> oo\_recursive\_=dynare\_estimation(var\_list\_);  
> Error in dynare (line 223)  
> evalin(‘base’,fname) ;

Thank you in advance,

Best regards

DC

---

<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 December 2017 18:51 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/6 "2017-12-29T18:51:05Z")

</div>

What do the `mode_check` plots say?

---

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [3 January 2018 12:17 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/7 "2018-01-03T12:17:19Z")

</div>

I only get plots with the priors… nothing about the posteriors or the mean. Here is attached the plots.

Furthermore, when I run the bar\_us, I get this error message

"Error using error  
The message must be specified as either a character vector or a message structure.  
Error in read\_variables (line 104)  
error([strtrim(var\_names\_01(dyn\_i\_01,:)) ’ not found in ’ fullname])  
Error in bvar\_toolbox (line 65)  
dataset = read\_variables(options\_.datafile, options\_.varobs, ,  
options\_.xls\_sheet, options\_.xls\_range);  
Error in bvar\_density (line 37)  
[ny, nx, posterior, prior] = bvar\_toolbox(nlags);  
Error in bvar\_us (line 74)  
bvar\_density(4);  
Error in dynare (line 223)  
evalin(‘base’,fname) ;

> > "

[plots.fig](https://forum.dynare.org/uploads/default/original/2X/4/464254636b21890ce7d9c677494c93a192851825.fig) (146.5 KB)

Thank you in advance,

Best regards

---

<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:** [3 January 2018 14:43 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/8 "2018-01-03T14:43:00Z")

</div>

You need to enable `mode_check`. To fix the problem you are now reporting, please fix the `read_variables`-file as in [https://github.com/DynareTeam/dynare/pull/1568/commits/7059cef909e1f248d61fd91ca2aca2316e1ed32b](https://github.com/DynareTeam/dynare/pull/1568/commits/7059cef909e1f248d61fd91ca2aca2316e1ed32b)

---

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [4 January 2018 15:12 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/9 "2018-01-04T15:12:33Z")

</div>

Dear Jpfeifer,

Thank you, my bvar file works just fine now as it gives me the posterior BVAR impulse responses.

Nonetheless, I still have issues with my NK3eq\_estim file as it gives me the following errors, and no posterior or means on the bayesian plots and no metropolis hastings:

Warning: The results below are most likely wrong!

> In dynare\_estimation\_1 (line 316)  
> In dynare\_estimation (line 105)  
> In NK3eq\_estim (line 206)  
> In dynare (line 223)  
> Warning: Matrix is singular, close to singular or badly scaled. Results may be  
> inaccurate. RCOND = NaN.  
> In dynare\_estimation\_1 (line 339)  
> In dynare\_estimation (line 105)  
> In NK3eq\_estim (line 206)  
> In dynare (line 223)

RESULTS FROM POSTERIOR ESTIMATION  
parameters  
prior mean mode s.d. prior pstdev

rho\_D 0.500 0.9738 NaN beta 0.2000  
rho\_S 0.500 0.9996 NaN beta 0.2000  
rho\_R 0.500 0.3463 NaN beta 0.2000  
sigma 1.000 3.2743 NaN gamm 0.4000  
varphi 2.000 1.3990 NaN gamm 0.7500  
phi\_pi 1.500 1.5913 NaN norm 0.1000  
phi\_y 0.050 -0.0002 NaN norm 0.0500  
rho 0.750 0.6801 NaN beta 0.0500  
theta 0.500 0.8077 NaN beta 0.1000

standard deviation of shocks  
prior mean mode s.d. prior pstdev

eta\_D 0.100 0.0399 NaN invg 2.0000  
eta\_S 0.100 0.2169 NaN invg 2.0000  
eta\_R 0.100 0.2621 NaN invg 2.0000

Log data density [Laplace approximation] is NaN.

Error using chol  
Matrix must be positive definite.  
Error in posterior\_sampler\_initialization (line 84)  
d = chol(vv);  
Error in posterior\_sampler (line 60)  
posterior\_sampler\_initialization(TargetFun, xparam1, vv,  
mh\_bounds,dataset\_,dataset\_info,options\_,M\_,estim\_params\_,bayestopt\_,oo\_);  
Error in dynare\_estimation\_1 (line 447)  
posterior\_sampler(objective\_function,posterior\_sampler\_options.proposal\_distribution,xparam1,posterior\_sampler\_options,bounds,dataset\_,dataset\_info,options\_,M\_,estim\_params\_,bayestopt\_,oo\_);  
Error in dynare\_estimation (line 105)  
dynare\_estimation\_1(var\_list,dname);  
Error in NK3eq\_estim (line 206)  
oo\_recursive\_=dynare\_estimation(var\_list\_);  
Error in dynare (line 223)  
evalin(‘base’,fname) ;

> >

Thank you,

Best regards

---

<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:** [6 January 2018 08:56 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/10 "2018-01-06T08:56:19Z")

</div>

As I said several times now, you should investigate the `mode_check` plots from estimation to find out where the problem is.

---

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [8 January 2018 14:31 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/11 "2018-01-08T14:31:37Z")

</div>

Sorry, I did not get your point last time. I get quite standard plots… I do not find them useful to see why the Metropolis-Hastings algorithm is not running… and why my posterior variances are not positive.

[untitled.fig](https://forum.dynare.org/uploads/default/original/2X/b/b3a63cf2c8a9c13e118a0858163f67a9f235ebf3.fig) (116.7 KB)  
[untitled1.fig](https://forum.dynare.org/uploads/default/original/2X/9/9adcc1ebc0b2a3448a7005c945e976f0268ccec1.fig) (46.4 KB)

Sorry for the inconveniences,

Best regards

---

<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:** [9 January 2018 11:35 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/12 "2018-01-09T11:35:03Z")

</div>

The plots indeed look good. Have you tried `mode_compute=5`?

---

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [9 January 2018 12:26 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/13 "2018-01-09T12:26:15Z")

</div>

It still doesn’t work… Really strange. I get the same error messages. The M-H box should appear, but nothing is happening. And it is not a problem with my machine as other codes work just fine.

A mystery

Thank you for your help,

DC

---

<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:** [9 January 2018 12:40 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/14 "2018-01-09T12:40:31Z")

</div>

The reason is that your Hessian at the mode is not positive definite. But the question is why that is the case. Could you please upload the mode-file.

---

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [9 January 2018 12:43 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/15 "2018-01-09T12:43:04Z")

</div>

Here are the mod and excel file.

Much thanks

[NK3eq\_estim.mod](https://forum.dynare.org/uploads/default/original/2X/2/27858903a95f0c9045fce9ddc6515191a616fe34.mod) (3.0 KB)

[https://forum.dynare.org/uploads/default/original/2X/2/2237dad8cde5837491b537948e40706a639a3b10.zip](https://forum.dynare.org/uploads/default/original/2X/2/2237dad8cde5837491b537948e40706a639a3b10.zip)

---

<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:** [9 January 2018 12:50 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/16 "2018-01-09T12:50:43Z")

</div>

Your observation equations are wrong. The model is linearized, but the data are not mean 0. Please take a look at Pfeifer(2013): [“A Guide to Specifying Observation Equations for the Estimation of DSGE Models”](http://sites.google.com/site/pfeiferecon/Pfeifer_2013_Observation_Equations.pdf)

---

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [10 January 2018 09:32 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/17 "2018-01-10T09:32:33Z")

</div>

Those damn observation equation. But I thought my data were demeaned as you can see in the ‘obs’ sheet of excel…

Thank you

---

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [10 January 2018 10:37 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/18 "2018-01-10T10:37:02Z")

</div>

I have demeaned my data an other way by writing a Matlab file. However, when running the mod file, I still get the same error messages…

---

<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:** [11 January 2018 09:29 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/19 "2018-01-11T09:29:17Z")

</div>

`r_obs` was clearly not mean 0. With your old dataset, you had one autocorrelation close to a unit root. How do the parameter estimates look like now?

---

<div class="post-metadata">

**Author:** ![dacorb](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dacorb](https://forum.dynare.org/u/dacorb)\
**Post date:** [15 January 2018 08:29 UTC](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105/21 "2018-01-15T08:29:51Z")

</div>

Dear Jpfeifer,

Sorry, I’m very very new to coding on Dynare and I am trying to solve some exercices to practice. This exercise is tiring me, I think I’m going to give up on that one.

**I get this** :

RESULTS FROM POSTERIOR ESTIMATION  
parameters  
prior mean mode s.d. prior pstdev

rho\_D 0.500 0.8688 NaN beta 0.2000  
rho\_S 0.500 0.9987 NaN beta 0.2000  
rho\_R 0.500 0.4099 NaN beta 0.2000  
sigma 1.000 0.9908 NaN gamm 0.4000  
varphi 2.000 0.9952 NaN gamm 0.7500  
phi\_pi 1.500 1.5026 NaN norm 0.1000  
phi\_y 0.050 -0.0011 NaN norm 0.0500  
rho 0.750 0.7647 NaN beta 0.0500  
theta 0.500 0.5595 NaN beta 0.1000

standard deviation of shocks  
prior mean mode s.d. prior pstdev

eta\_D 0.100 0.0118 NaN invg 2.0000  
eta\_S 0.100 0.1811 NaN invg 2.0000  
eta\_R 0.100 0.0969 NaN invg 2.0000

I do not even know if my Matlab file is ok.

[datatest.m](https://forum.dynare.org/uploads/default/original/2X/9/9db4d9f7baf5883bd83060b9f458ce8ac8ed944f.m) (5.5 KB)

Thank you,

Best regards

[Next page](https://forum.dynare.org/t/problem-with-bvar-and-metropolis-hastings/11105.md?page=2)
