Bug in ValueFnIter_Case1 with ExogShockFn

When solving an infinite horizon model and using the option ExogShockFn, the code returns with this error message:

Error using ValueFnIter_Case1 (line 150)
z_grid is not the correct shape (should be of size sum(n_z)-by-1)

The reason is the following: the code checks the size of z_grid and pi_z by default, but z_grid and pi_z are simply equal to empty arrays in this case (placeholders). The checks should be done only if the option ExogShockFn is not used.

Proposed change: include the checks on z_grid and pi_z in the following if condition

if isfield(vfoptions,'ExogShockFn')
   % OK no checks
else
   % DO CHECKS
end

UPDATE
After fixing the bug in the VF, the code does the VFI ok but then there is an error in StationaryDist_Case1:

Error using  * 
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the number of rows in the second matrix. To operate on each element of the matrix individually,
use TIMES (.*) for elementwise multiplication.

Error in StationaryDist_Case1 (line 197)
            z_stat=pi_z'*z_stat;
                   ^^^^^
1 Like

Fixed. Note, to use ExogShockFn you have to include Params as input to StationaryDist_Case1().

(ran Aiyagari1994 but with ExogShockFn, and that now works, so hopefully works everywhere)

1 Like

Hi Rob, thanks! The VFI and distribution run ok now, but there is an error at EvalFnOnAgentDist_Grid

Unrecognized function or variable 'z_gridvals'.

Error in EvalFnOnAgentDist_ValuesOnGrid_Case1 (line 83)
        Values=EvalFnOnAgentDist_Grid(FnsToEvaluate{ff}, FnToEvaluateParamsCell,PolicyValuesPermute,l_daprime,n_a,n_z,a_gridvals,z_gridvals);
                                                                                                                                 ^^^^^^^^^^
Error in solve_toolkit (line 291)
ValuesOnGrid=EvalFnOnAgentDist_ValuesOnGrid_Case1(Policy,FnsToEvaluate,Params,[],n_d,n_a,n_z,d_grid,a_grid,z_grid,[],simoptions);
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in main (line 56)

Fixed.

Eliminated “Parallel” from inputs to EvalFnOnAgentDist_ValuesOnGrid_Case1() while I was at it.

I rarely use this so didn’t think to check it :slight_smile:

1 Like

The bug is still there. I checked the code and the reason is as follows:

In line 26 of EvalFnOnAgentDist_ValuesOnGrid_Case1, the variable z_grid is an empty array. This is expected, since I chose the option ExogShockFn.

if all(size(z_grid)==[sum(n_z),1]) % stacked-column
    z_gridvals=CreateGridvals(n_z,z_grid,1);
elseif all(size(z_grid)==[prod(n_z),length(n_z)]) % joint grid 
    z_gridvals=z_grid;
end

This means that both conditions in the if statements are skipped and the variable z_gridvals is undefined when the program flow reaches line 83:

Values=EvalFnOnAgentDist_Grid(FnsToEvaluate{ff}, FnToEvaluateParamsCell,PolicyValuesPermute,l_daprime,n_a,n_z,a_gridvals,z_gridvals);

I think EvalFnOnAgentDist_ValuesOnGrid_Case1 should call somewhere the function simoptions.ExogShockFn, right?

By the way, the input argument Parallel in EvalFnOnAgentDist_ValuesOnGrid_Case1 is still there.

Oops, forgot to commit and push the update. Should be there now.

1 Like