european-central-bank / BEAR-toolbox

The Bayesian Estimation, Analysis and Regression toolbox (BEAR) is a comprehensive (Bayesian Panel) VAR toolbox for forecasting and policy analysis.
101 stars 38 forks source link

Bug in the historical decomposition codes #27

Open keremtuzcuoglu opened 2 years ago

keremtuzcuoglu commented 2 years ago

Hello,

First of all, thank you for creating such a powerful toolbox. To improve it, I'd like to point out a bug in the historical decomposition functions. I am using BVAR option with sign restrictions and HD. But I believe the issue will be present with any model as long as HD computation scripts are used.

When there are more than 1 exogenous variable, the variable "contributors" in the hd-related scripts includes the number of exogenous variables. However when it comes to calculating and plotting HD, it uses only the total contribution from the exogenous variables altogether, not individually. Thus the code gives an error since there need more contributors than computed.

For example this the error message:

Index in position 1 exceeds array bounds (must not exceed 11).

Error in bear.hdecomp_inc_exo (line 78) hd_record{kk,ll}(ii,:) = HDstorage{ii,1}{kk,ll}(2,:);

Error in BEARmain (line 1071) [hd_record,favar]=bear.hdecomp_inc_exo(beta_gibbs,D_record,opts.It,opts.Bu,Y,X,n,m,p,k,T,data_exo,exo,endo,const,IRFt,strctident,favar);

To fix this, one might simply change the following command

contributors = n + 1 + length(exo) + 1; %variables + constant + exogenous + initial conditions

to

contributors = n + 1 + double(length(exo)>0) + 1; %variables + constant + exogenous + initial conditions or contributors = n + 1 + double(~isempty(exo)) + 1; %variables + constant + exogenous + initial conditions

I verified that the new command fixes the bug. But the change has to be made consistently in each hd-related script.

I hope this helps.

Cheers, K.

ebenetce commented 2 years ago

Thanks for reporting that, could check if in your models you where using a constant? i.e. opts.const? I believe a similar error occurs when there is 1 exovar + constant