Bug in EvalFnOnAgentDist_ValuesOnGrid_FHorz_Case1_PType

I think there is a bug in

EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_ValuesOnGrid_FHorz_Case1_PType.m

when it is used in a finite-horizon PType model with:

  • no ordinary exogenous state (n_z = 0)

  • a semi-exogenous state semiz

Symptom

Calling


ValuesOnGrid = EvalFnOnAgentDist_ValuesOnGrid_FHorz_Case1_PType( ...

Policy,FnsToEvaluate,Params,n_d,n_a,n_z,N_j,N_i,d_grid,a_grid,z_grid,simoptions);

fails with this error message:


Error using gpuArray/reshape

Number of elements must not change.

Error in EvalFnOnAgentDist_ValuesOnGrid_FHorz_Case1_PType (line 253)

ValuesOnGrid.(FnNames{kk}).(Names_i{ii})=gather(reshape(ValuesOnGrid_ii.(FnNames{kk}),[n_a_temp,N_j_temp]));

Why this looks like a bug

In this setup, n_z = 0, but simoptions.n_semiz is nonzero. So the evaluated objects should still live on the grid


(a, semiz, j)

not just


(a, j)

The non-PType helper

EvaluateFnOnAgentDist/FHorz/EvalFnOnAgentDist_ValuesOnGrid_FHorz_Case1.m

handles this by folding semiz into the effective exogenous grid before reshaping. So with semiz, the output should have size like


[n_a_temp, n_semiz_temp, N_j_temp]

or, in the more general notation used in the wrapper,


[n_a_temp, n_ze_temp, N_j_temp]

However, in the PType wrapper, the code falls into the branch


if prod(n_ze_temp)==0

and then reshapes to


[n_a_temp, N_j_temp]

which is only valid when there are truly no exogenous or semi-exogenous states.

But with semiz, ValuesOnGrid_ii.(FnNames{kk}) contains values over (a, semiz, j), so it has more than n_a_temp * N_j_temp elements. That is why the reshape fails.

Suspected issue

It looks like the PType wrapper is incorrectly treating the case

  • n_z = 0

  • n_semiz > 0

as if there were no exogenous-state dimensions at all.

Likely fix

The wrapper should preserve the semiz dimension in the same way as the non-PType helper does, so that this case reshapes to something like


[n_a_temp, n_ze_temp, N_j_temp]

rather than


[n_a_temp, N_j_temp]

when semiz is present.

Fixed in the merged pull request

1 Like

Now Values on Grid for Ptypes in Finite horizon works, but it prints the parameter structure to the Matlab command window

The culprit is this part

if simoptions_temp.verboseparams==1
    fprintf('Parameter values for the current permanent type \n')
    Parameters_temp
end

In my example simoptions_temp.verboseparams=100, which is weird.
If you want to replicate the bug, please run my code at:

Script main.m in the shared repo

Cleaned up the defaults to be simoptions.verboseparams=0

1 Like

ps. the point of setting simoptions.verboseparams=1 is so the user has a way to easily look at how the parameters are being parsed for each ptype, so you can check that it is all being handled in the way you expect

1 Like