Today I find myself looking at ValueFnIter_FHorz_RiskyAssetSemiExo_nod1_raw.m and in particular, the fact that EV changes shape during the iterations in ways I haven’t seen before.
At line 262 (or thereabouts) we see
EV=V(:,:,jj+1);
which is typical. A few lines later (inside the d4 loop) we see EV expanded by the transition matrix:
EV=EV.*shiftdim(pi_bothz',-1);
which is still typical (simpler expansions such as ValueFnIter_FHorz_raw.m use pi_z_J).
When we look at the V_Jplus1 case, we see what we expect: EV is derived from V_Jplus1.*shiftdim(pi_bothz',-1) on each iteration of the d4 loop, meaning that all sorts of reshaping of EV later in the loop doesn’t come back to bite us.
However, as we iterate backwards through j, EV starts as we expect and is then reshaped on the first iteration of the d4 loop to sometime quite larger that we then have to deal with on subsequent iterations. One thing that makes me suspicious are the tantalizing comments copy-pasted from the V_Jplus1 case that are irrelevant to loops not using V_Jplus1 at all. But if those comments have a shred of truth about how EV could be initialized or refined at the end of the loop so as to return to some smaller dimensions before going through its expansionary phase, it might indicate an optimization opportunity. If not, it would be helpful to fix the comments.