Bad Gradient by adding more variables

Dear all, I am working on IRF-matchig approach. when u added more variables in get-empirical-irfs file. I got my error as bad gradient. what is reason?

Where did this error appear?

it appears once estimation starts. code is attachedget_empirical_IRFs.m (6.3 KB) IRF_matching_objective.m (1.4 KB) finaldata.xlsx (85.4 KB) IRFNEW.mod (6.4 KB) build_companionform.m (608 Bytes)

You need to adjust

%get pointwise variance of IRFs across draws and compute weighting matrix
IRF_variances=var(IRFs([y_pos,pie_pos,rm_pos,rl_pos,c_pos, invest_pos],:,:),0,3)';
IRF_weighting=inv(diag(IRF_variances(2:end)));

Currently, the weighting matrix is Inf

Are these variances are related to number of variables like

%get pointwise variance of IRFs across draws and compute weighting matrix
IRF_variances=var(IRFs([y_pos,pie_pos,rm_pos,rl_pos,c_pos,invest_pos],:,:),0,7)';
IRF_weighting=inv(diag(IRF_variances(2:end)))

Yes, something must be wrong here.

on the basis of number of variables it can be defined like this?

I defined on number of variables but didn’t get what is wrong here. if I reduce number of variables, I didn’t got error

You need to check

  1. whether you are computing the variance across the IRFs correctly (first line)
  2. whether you are selecting the variances correctly (second line)