# Errors in Bayesian estimation(matrix must be a positive definite)

**URL:** <https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583>\
**Category:** ML/Bayesian estimation\
**Created:** [31 March 2018 14:11 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583 "2018-03-31T14:11:24Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yu\_Lei](https://forum.dynare.org/letter_avatar_proxy/v4/letter/y/85e7bf/32.png) [@Yu\_Lei](https://forum.dynare.org/u/Yu_Lei)\
**Post date:** [31 March 2018 14:11 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/1 "2018-03-31T14:11:24Z")

</div>

Dear all,  
I am building a DSGE model about land use, however, my code have some problems with Bayestian estimation, I have tried mode\_compute=6,8 and 9, errors always exist in my estimation.But the code without estimation works. Here are my code files with an without estimation. Could you help me sovle may problem? Thanks a lot.  
[data.mat](https://forum.dynare.org/uploads/default/original/2X/4/42a9d379a14cf76713e595d54448f6b80fc156d8.mat) (3.9 KB)  
[paper\_without\_estimation.mod](https://forum.dynare.org/uploads/default/original/2X/4/4293edaa053107941eb586dff733d07203e89b6f.mod) (3.8 KB)  
[paper\_with\_estimation.mod](https://forum.dynare.org/uploads/default/original/2X/0/06b44ea398226a44f8603cc16e5bc20317870703.mod) (3.8 KB)

---

<div class="post-metadata">

**Author:** ![Yu\_Lei](https://forum.dynare.org/letter_avatar_proxy/v4/letter/y/85e7bf/32.png) [@Yu\_Lei](https://forum.dynare.org/u/Yu_Lei)\
**Post date:** [31 March 2018 14:13 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/2 "2018-03-31T14:13:55Z")

</div>

Errors in estimation file:  
Configuring Dynare …  
[mex] Generalized QZ.  
[mex] Sylvester equation solution.  
[mex] Kronecker products.  
[mex] Sparse kronecker products.  
[mex] Local state space iteration (second order).  
[mex] Bytecode evaluation.  
[mex] k-order perturbation solver.  
[mex] k-order solution simulation.  
[mex] Quasi Monte-Carlo sequence (Sobol).  
[mex] Markov Switching SBVAR.

Using 64-bit preprocessor  
Starting Dynare (version 4.5.3).  
Starting preprocessing of the model file …  
Found 22 equation(s).  
Evaluating expressions…done  
Computing static model derivatives:

- order 1  
Computing dynamic model derivatives:
- order 1
- order 2  
Processing outputs …  
done  
Preprocessing completed.

Residuals of the static equations:

Equation number 1 : 0  
Equation number 2 : 0  
Equation number 3 : 0  
Equation number 4 : 0  
Equation number 5 : 0  
Equation number 6 : 0  
Equation number 7 : 0  
Equation number 8 : 0  
Equation number 9 : 0  
Equation number 10 : 0  
Equation number 11 : 0  
Equation number 12 : 0  
Equation number 13 : 0  
Equation number 14 : 0  
Equation number 15 : 0  
Equation number 16 : 0  
Equation number 17 : 0  
Equation number 18 : 0  
Equation number 19 : 0  
Equation number 20 : 0  
Equation number 21 : 0  
Equation number 22 : 0

Warning: Some of the parameters have no value (sigma) when using steady. If these parameters are not  
initialized in a steadystate file or a steady\_state\_model-block, Dynare may not be able to solve the  
model…

STEADY-STATE RESULTS:

ch 0  
ql 0  
w 0  
n 0  
s 0  
lh 0  
r 0  
k 0  
le 0  
g 0  
ce 0  
b 0  
i 0  
y 0  
lamdae 0  
qk 0  
d 0  
lamdag 0  
theta 0  
a 0  
l 0  
c 0

EIGENVALUES:  
Modulus Real Imaginary

```
   1.078e-18 1.078e-18 0
   4.075e-16 -4.075e-16 0
   5.263e-14 5.263e-14 0
     0.07531 0.07531 0
        0.75 0.75 0
        0.75 0.75 0
        0.75 0.75 0
      0.8994 0.8994 0
       1.153 1.153 0.01166
       1.153 1.153 -0.01166
       3.356 3.356 0
   1.662e+14 1.662e+14 0
   8.313e+15 8.313e+15 0
         Inf Inf 0

```

There are 6 eigenvalue(s) larger than 1 in modulus  
for 6 forward-looking variable(s)

The rank condition is verified.

You did not declare endogenous variables after the estimation/calib\_smoother command.  
Warning: Some of the parameters have no value (sigma) when using initial\_estimation\_checks. If these  
parameters are not initialized in a steadystate file or a steady\_state\_model-block, Dynare may not be  
able to solve the model…  
Initial value of the log posterior (or likelihood): -60327451975825.97

# ========================================================== Change in the posterior covariance matrix = 0.01. Change in the posterior mean = 0.43758. Mode improvement = 60142816749032.44 New value of jscale = 1.1384e-12

# ========================================================== Change in the posterior covariance matrix = 8.9627e-14. Change in the posterior mean = 0.4222. Mode improvement = 476760259346.6788 New value of jscale = 4.6107e-14

Error using chol.  
The matrix must be a positive definite matrix.

Error gmhmaxlik\_core (line 194)  
dd = transpose(chol(CovJump));

Error gmhmaxlik (line 100)  
[PostMode, PostVariance, Scale, PostMean] = gmhmaxlik\_core(fun, OldPostMode, bounds,  
gmhmaxlikOptions, Scale, flag, MeanPar, OldPostVariance, varargin{:});

Error dynare\_minimize\_objective (line 258)  
[opt\_par\_values, hessian\_mat, Scale, fval] = gmhmaxlik(objective\_function, start\_par\_value, …

Error dynare\_estimation\_1 (line 220)  
[xparam1, fval, exitflag, hh, options\_, Scale, new\_rat\_hess\_info] =  
dynare\_minimize\_objective(objective\_function,xparam1,options\_.mode\_compute,options\_,[bounds.lb  
bounds.ub],bayestopt\_.name,bayestopt\_,hh,dataset\_,dataset\_info,options\_,M\_,estim\_params\_,bayestopt\_,bounds,oo\_);

Error dynare\_estimation (line 105)  
dynare\_estimation\_1(var\_list,dname);

Error paper\_with\_estimation (line 333)  
oo\_recursive\_=dynare\_estimation(var\_list\_);

Error dynare (line 223)  
evalin(‘base’,fname) ;

---

<div class="post-metadata">

**Author:** ![punnoosejacob](https://forum.dynare.org/user_avatar/forum.dynare.org/punnoosejacob/32/5976_2.png) [@punnoosejacob](https://forum.dynare.org/u/punnoosejacob)\
**Post date:** [1 April 2018 19:21 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/3 "2018-04-01T19:21:45Z")

</div>

Try the mode check command. This error message can sometimes come up, when the priors are tight and the optimisation hits the boundary…

It is better to use looser priors for the shock volatilities  
inv\_gamma2\_pdf,0.1,0.01;  
seems to be a tight prior for a stdev.

also, the model seems to be a log-linear one…should you not be using a model(linear) option…?

Reuben

---

<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 April 2018 17:23 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/4 "2018-04-03T17:23:00Z")

</div>

Your observation equation is wrong. You are trying to match trending data to stationary model variables. Please read 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:** ![Yu\_Lei](https://forum.dynare.org/letter_avatar_proxy/v4/letter/y/85e7bf/32.png) [@Yu\_Lei](https://forum.dynare.org/u/Yu_Lei)\
**Post date:** [4 April 2018 01:03 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/5 "2018-04-04T01:03:31Z")

</div>

Dear Reuben, thank you for your helping. I have realized that it is the data used in the estimation is wrong and I have solved the problem, thanks again.  
Sincerely  
Yu

---

<div class="post-metadata">

**Author:** ![Yu\_Lei](https://forum.dynare.org/letter_avatar_proxy/v4/letter/y/85e7bf/32.png) [@Yu\_Lei](https://forum.dynare.org/u/Yu_Lei)\
**Post date:** [4 April 2018 01:17 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/6 "2018-04-04T01:17:20Z")

</div>

Dear jpfeifer, thanks for your helping, the problem is the wrong data I used, and I have tried to solve it. Thanks for your help again.  
May I have another question? I am now in trouble with shocks. I have read your code in [https://github.com/JohannesPfeifer/DSGE\_mod/blob/master/RBC\_news\_shock\_model/RBC\_news\_shock\_model.mod](https://github.com/JohannesPfeifer/DSGE_mod/blob/master/RBC_news_shock_model/RBC_news_shock_model.mod)

```
shocks;

    var eps_z_news=1; //8 period anticipated TFP news shock

    var eps_z_surprise=1; //TFP surprise shock

end;

```

// set shocks for pure news

```
shock_matrix(1,strmatch('eps_z_news',M_.exo_names,'exact')) = 1; %set news shock to 1 (use any shock size you want)

shock_matrix(1+8,strmatch('eps_z_surprise',M_.exo_names,'exact')) = -1; %8 periods later use counteracting shock of -1

```

My problem is what the size of the shock means? Does ‘set news shock to 1’ mean that it is the same as ‘var eps\_z\_news=1’? And is it a feasible way to combine two shocks at the same time?  
If my code is  
shocks;  
var eps\_theta;stderr 0.01;  
var eps\_l;stderr 0.01;  
var eps\_a;stderr 0.01;  
end;  
how could I combine the eps\_theta (stderr 0.01) and eps\_l (stderr 0.01) shocks at the same time? Thanks a lot.  
Yours sincerely  
Yu

---

<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:** [4 April 2018 07:02 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/7 "2018-04-04T07:02:32Z")

</div>

What are you trying to do? The code you refer to is about simulating with a pre-specified shock size. In contrast, the `shocks`-block is about specifying the distribution of shocks.

---

<div class="post-metadata">

**Author:** ![Yu\_Lei](https://forum.dynare.org/letter_avatar_proxy/v4/letter/y/85e7bf/32.png) [@Yu\_Lei](https://forum.dynare.org/u/Yu_Lei)\
**Post date:** [4 April 2018 08:14 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/8 "2018-04-04T08:14:54Z")

</div>

Dear professor, thanks for your reply. Actually I just want to combine the shocks eps\_theta and eps\_l at the same time, and making the eps\_l shock an adverse way, I do not know if it is possible to realize it through your code I referred. Thanks a lot again.

---

<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:** [4 April 2018 09:34 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/9 "2018-04-04T09:34:59Z")

</div>

Combine them for what? For an IRF? If yes, the code applies.

---

<div class="post-metadata">

**Author:** ![Musadauda2022](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Musadauda2022](https://forum.dynare.org/u/Musadauda2022)\
**Post date:** [15 August 2022 08:39 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/10 "2022-08-15T08:39:52Z")

</div>

Good morning Prof.,  
I am currently having the below error message as below:

dynare musa.mod  
Using 64-bit preprocessor  
Starting Dynare (version 4.6.4).  
Calling Dynare with arguments: none  
Starting preprocessing of the model file …  
Found 28 equation(s).  
Evaluating expressions…done  
Computing static model derivatives (order 2).  
Computing static model derivatives w.r.t. parameters (order 2).  
Computing dynamic model derivatives (order 2).  
Computing dynamic model derivatives w.r.t. parameters (order 2).  
Processing outputs …  
done  
Preprocessing completed.

Residuals of the static equations:

Equation number 1 : 0 : CR  
Equation number 2 : 0 : CVR  
Equation number 3 : 0 : TP  
Equation number 4 : 0 : w  
Equation number 5 : 0 : mc  
Equation number 6 : 0 : r  
Equation number 7 : 0 : g  
Equation number 8 : 0 : d  
Equation number 9 : 0 : pii\_F  
Equation number 10 : 0 : z  
Equation number 11 : 0 : piistar  
Equation number 12 : 0 : exr  
Equation number 13 : 0 : C  
Equation number 14 : 0 : pii\_D  
Equation number 15 : 0 : y  
Equation number 16 : 0 : rstar  
Equation number 17 : 0 : a  
Equation number 18 : 0 : rev  
Equation number 19 : 0 : s\_d  
Equation number 20 : 0 : s\_g  
Equation number 21 : 0 : s\_ff  
Equation number 22 : 0 : s\_fy  
Equation number 23 : 0 : s\_b  
Equation number 24 : 0 : s\_r  
Equation number 25 : 0 : s\_y  
Equation number 26 : 0 : s\_df  
Equation number 27 : 0 : s\_t  
Equation number 28 : 0 : s\_rn

STEADY-STATE RESULTS:

CR 0  
CVR 0  
TP 0  
w 0  
mc 0  
r 0  
g 0  
d 0  
pii\_F 0  
z 0  
piistar 0  
exr 0  
C 0  
pii\_D 0  
y 0  
rstar 0  
a 0  
rev 0  
s\_d 0  
s\_g 0  
s\_ff 0  
s\_fy 0  
s\_b 0  
s\_r 0  
s\_y 0  
s\_df 0  
s\_t 0  
s\_rn 0

EIGENVALUES:  
Modulus Real Imaginary

```
   7.872e-17 7.872e-17 0
      0.3458 0.2832 0.1985
      0.3458 0.2832 -0.1985
      0.3802 -0.3802 0
        0.65 0.65 0
         0.7 0.7 0
         0.7 0.7 0
         0.7 0.7 0
         0.7 0.7 0
         0.7 0.7 0
         0.7 0.7 0
         0.7 0.7 0
         0.7 0.7 0
         0.7 0.7 0
         0.7 0.7 0
         0.7 0.7 0
         0.7 0.7 0
       1.127 1.127 0
       2.395 2.395 0
       13.28 12.27 5.074
       13.28 12.27 -5.074
   7.262e+15 7.262e+15 0
   4.328e+16 -4.328e+16 0

```

There are 6 eigenvalue(s) larger than 1 in modulus  
for 6 forward-looking variable(s)

The rank condition is verified.

Initial value of the log posterior (or likelihood): -1.549488895571599e+16

# ========================================================== Change in the posterior covariance matrix = 1. Change in the posterior mean = 1.8747. Mode improvement = 1.549488871149786e+16 New value of jscale = 2.4646e-08

# ========================================================== Change in the posterior covariance matrix = 2.9406e-11. Change in the posterior mean = 1.8086. Mode improvement = 255417643492.7751 New value of jscale = 4.7408e-18

Error using chol  
Matrix must be positive definite.

Error in gmhmaxlik\_core (line 194)  
dd = transpose(chol(CovJump));

Error in gmhmaxlik (line 100)  
[PostMode, PostVariance, Scale, PostMean] = gmhmaxlik\_core(fun,  
OldPostMode, bounds, gmhmaxlikOptions, Scale, flag, MeanPar,  
OldPostVariance, varargin{:});

Error in dynare\_minimize\_objective (line 323)  
[opt\_par\_values, hessian\_mat, Scale, fval] = gmhmaxlik(objective\_function,  
start\_par\_value, …

Error in dynare\_estimation\_1 (line 228)  
[xparam1, fval, exitflag, hh, options\_, Scale, new\_rat\_hess\_info] =  
dynare\_minimize\_objective(objective\_function,xparam1,options\_.mode\_compute,options\_,[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 musa.driver (line 590)  
oo\_recursive\_=dynare\_estimation(var\_list\_);

Error in dynare (line 293)  
evalin(‘base’,[fname ‘.driver’]) ;

[musa.mod](https://forum.dynare.org/uploads/short-url/xMVVGblA4aXwmjMcPliA3C6Dk6s.mod) (3.8 KB)  
[daudaquarter.xls](https://forum.dynare.org/uploads/short-url/qxYxp7Ii2otyRot6iIj1rRU91Yz.xls) (34 KB)

---

<div class="post-metadata">

**Author:** ![DoubleBass](https://forum.dynare.org/user_avatar/forum.dynare.org/doublebass/32/17420_2.png) [@DoubleBass](https://forum.dynare.org/u/DoubleBass)\
**Post date:** [15 August 2022 09:14 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/11 "2022-08-15T09:14:30Z")

</div>

Are you sure that your data-treatment is correct? Your data has to match the corresponding variables of your model. Since you have a log-linearised model this means that your data should also be in logs and have steady state of zero, your data clearly still has a trend and is not stationary.

Have you had a look at Prof. Pfeifers excellent [guide for how to use observation equations](https://drive.google.com/file/d/1r89OU5OE3CBa6tOlj6l3hNVWEaRH5Anv/view)? If not, you should definitely go through it.

---

<div class="post-metadata">

**Author:** ![Musadauda2022](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Musadauda2022](https://forum.dynare.org/u/Musadauda2022)\
**Post date:** [15 August 2022 09:35 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/12 "2022-08-15T09:35:57Z")

</div>

Thank You DoubleBass,

I will log-linearised the model and see the outcome of the result

---

<div class="post-metadata">

**Author:** ![DoubleBass](https://forum.dynare.org/user_avatar/forum.dynare.org/doublebass/32/17420_2.png) [@DoubleBass](https://forum.dynare.org/u/DoubleBass)\
**Post date:** [15 August 2022 11:17 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/13 "2022-08-15T11:17:11Z")

</div>

Your model is already log-lineraised, it is about the way you treat your data. It is not possible to just insert data that you download from some site. It has to match model variables. As an example, in your model output is given as percentage deviations from steady state of output per person, ie a stationary data series, but the data you (most likely) took is overall output which is non-stationary. Thus, you have to treat the data to match your variable.  
Read the file and look around the forum, there are plenty of posts about exactly just that 🙂

---

<div class="post-metadata">

**Author:** ![Musadauda2022](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Musadauda2022](https://forum.dynare.org/u/Musadauda2022)\
**Post date:** [15 August 2022 16:19 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/14 "2022-08-15T16:19:55Z")

</div>

Alright. Thank you, I really appreciate.

---

<div class="post-metadata">

**Author:** ![auserpallas](https://forum.dynare.org/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@auserpallas](https://forum.dynare.org/u/auserpallas)\
**Post date:** [6 March 2024 14:43 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/15 "2024-03-06T14:43:01Z")

</div>

Dear Prof. jpfeifer, there is nothing in this link, can you share it again?

---

<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 March 2024 15:02 UTC](https://forum.dynare.org/t/errors-in-bayesian-estimation-matrix-must-be-a-positive-definite/11583/16 "2024-03-06T15:02:58Z")

</div>

It is now located at [Pfeifer\_2013\_Observation\_Equations.pdf - Google&nbsp;Drive](https://drive.google.com/file/d/1r89OU5OE3CBa6tOlj6l3hNVWEaRH5Anv/view)
