I think there is a bug in the way this function indexes the finite-horizon semi-exogenous transition matrix. This is the function:
Relevant object
The input pi_semiz_J is a 4-dimensional array with size
[N_semiz, N_semiz, N_dsemiz, N_j]
where the dimensions are:
-
current semi-exogenous state
-
next semi-exogenous state
-
decision affecting the semi-exogenous transition
-
age
So in a finite-horizon model, the transition matrix is allowed to vary with age.
Relevant code
In StationaryDist_FHorz_Iteration_SemiExo_noz_raw.m:
Policy_dsemiexo=reshape(Policy_dsemiexo,[N_a*N_semiz,1,N_j]);
semizindex=repelem(gpuArray(1:1:N_semiz)',N_a,1) ...
+ N_semiz*gpuArray(0:1:N_semiz-1) ...
+ (N_semiz*N_semiz)*(Policy_dsemiexo-1);
and later:
semiztransitions=gather(pi_semiz_J(semizindex(:,:,jj)));
Why this looks wrong
The array semizindex only encodes linear indexing for the first 3 dimensions of pi_semiz_J:
-
current semiz
-
next semiz
-
decision
It does not include any offset for the 4th dimension, age.
In particular, there is no term of the form
(N_semiz*N_semiz*N_dsemiz)*(jj-1)
in the construction of semizindex.
Therefore, semizindex(:,:,jj) is an index for a 3-dimensional array of size
[N_semiz, N_semiz, N_dsemiz]
but it is then being used to index the 4-dimensional array pi_semiz_J.
Why this implies a bug
If pi_semiz_J varies with age, then the code should select the age-jj slice of the transition matrix before applying the linear index over (semiz, semiz', d).
But the current code does:
pi_semiz_J(semizindex(:,:,jj))
instead of first restricting to age jj.
So the age dimension is not being indexed correctly.
What I think the code should do instead
It seems the safe way is:
pi_semiz_jj = pi_semiz_J(:,:,:,jj);
semiztransitions = gather(pi_semiz_jj(semizindex(:,:,jj)));
because semizindex(:,:,jj) is exactly an index for the 3-dimensional object
pi_semiz_jj(semiz, semizprime, dsemiz).
Bottom line
So the issue is:
-
pi_semiz_Jis 4-dimensional -
semizindexonly indexes 3 dimensions -
the current code applies the 3-dimensional linear index directly to the 4-dimensional array
This appears to ignore the finite-horizon age dimension of the semi-exogenous transition matrix.
Note: I generated this post with the help of AI.
@robertdkirkby, @MichaelTiemann Please let me know what you think about this. I’m having strange results in a project of mine that uses the toolkit and with my proposed fix everything looks fine.
@robertdkirkby I created a PR here: Fix FHorz semiexo PType distribution and lifecycle profile helpers by aledinola · Pull Request #79 · vfitoolkit/VFIToolkit-matlab · GitHub This contains two fixes: the distribution command and the lifecycle command.