New Bug in StationaryDist_FHorz_Iteration_SemiExo_noz_raw

I got this error:

Error using sparse
Vectors must be the same length.

Error in StationaryDist_FHorz_Iteration_SemiExo_noz_raw (line 46)
    Gammatranspose=sparse(Policy_aprimesemiz(:,:,jj),II2,semiztransitions,N_a*N_semiz,N_a*N_semiz); % From (a,semiz) to (a',semiz')
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in StationaryDist_FHorz_SemiExo (line 78)
        StationaryDist=StationaryDist_FHorz_Iteration_SemiExo_noz_raw(jequaloneDist,AgeWeightParamNames,Policy_dsemiexo,Policy_aprime,N_dsemiz,N_a,N_semiz,N_j,pi_semiz_J,Parameters);
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in StationaryDist_FHorz_Case1 (line 201)
    StationaryDist=StationaryDist_FHorz_SemiExo(jequaloneDist,AgeWeightParamNames,Policy,n_d,n_a,simoptions.n_semiz,n_z,N_j,simoptions.pi_semiz_J,pi_z_J,Parameters,simoptions);
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in StationaryDist_Case1_FHorz_PType (line 234)
        StationaryDist_ii=StationaryDist_FHorz_Case1(jequaloneDist_temp,AgeWeightParamNames_temp,Policy_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,pi_z_temp,Parameters_temp,simoptions_temp);
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in fun_solve1 (line 124)
StatDist=StationaryDist_Case1_FHorz_PType(jequaloneDist,AgeWeightsParamNames,PTypeDistName,Policy,n_d,n_a,n_z,N_j,N_i,pi_z,Params,simoptions);
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in main (line 122)
[V, pol_s,pol_aprime,StatDist,ValuesOnGrid,AllStats,AgeStats,SimPanelValues,AgeStatsSim] = fun_solve1(Params,a_grid,s_grid,l_grid,g_grid,pi_l,pi_g,N_j,N_i);
                                                       

You can reproduce the error by running the code/main.m in this repo: GitHub - aledinola/life-cycle-search · GitHub

I got the error reported above today when I updated my vfi-toolkit repo.

@robertdkirkby: Did you modify something that broke the codes? It was working so well :smiley:

I’m working on a paper that uses this code… I did update from upstream/master and I guess it synced the local code on my computer to the official toolkit repo online. Is there a way I can revert back to the previous version? I apologize in advance for my limited knowledge of Github.

UPDATE
All good, I managed to revert back to the version before the commit " improve semiz iteration"
I definitely have to improve my knowledge of git and github :slight_smile:
The good thing is that now we isolated the problem.

Would be this change

But not obvious to me what the error was caused by as I tried a few examples that all worked and covered the obvious alternatives. Anyway, I’ll run your example on Monday and see if I can figure it out.

Fixed. Was a trivial error (in one place I accidentally left it saying N_semiz where it should have said N_semizshort).

Should work now. And should be faster than it used to be.

Also, this model will be better performing if you turn on grid interpolation layer.

1 Like

Thanks a lot, I will test it right now

All good. I use interpolation in my real project. This is just a demo to test the toolkit with a toy model and make sure the toolkit functions give correct results. You can notice that I have double coded everything with custom-made functions that run on cpu (with the exception of the panel simulation). The results are identical across toolkit and my own cpu implementation.

Since I don’t have time to code the interpolation method on the cpu, I am not using it in this test. I just trust the VFI-Toolkit :smiley:

P.S. With this method I found a previous bug in the distribution code.

2 Likes

That makes a lot of sense! Great to have you stress testing everything to the limit :smiley:

1 Like

The same error encountered before reappeared. I had fixed the error myself. So when I write above that the error was fixed, I was using my patched code, not the official code in the toolkit repo. Today I synced again my local repo with the upstream main and the error reappeared (to be precise, the error message is almost the same, just the line number shifted).

I can easily revert back again, but it would be a good idea to fix this issue once and for all.

Error using sparse
Vectors must be the same length.

Error in StationaryDist_FHorz_Iteration_SemiExo_nProbs_noz_raw (line 47)
    Gammatranspose=sparse(Policy_aprimesemiz(:,:,jj),II2,PolicyProbs(:,:,jj),N_a*N_semiz,N_a*N_semiz); % Note: sparse() will accumulate at repeated indices [only relevant at grid end points]
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in StationaryDist_FHorz_SemiExo (line 99)
        StationaryDist=StationaryDist_FHorz_Iteration_SemiExo_nProbs_noz_raw(jequaloneDist,AgeWeightParamNames,Policy_dsemiexo,Policy_aprime,PolicyProbs,2,N_dsemiz,N_a,N_semiz,N_j,pi_semiz_J,Parameters);
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in StationaryDist_FHorz_Case1 (line 201)
    StationaryDist=StationaryDist_FHorz_SemiExo(jequaloneDist,AgeWeightParamNames,Policy,n_d,n_a,simoptions.n_semiz,n_z,N_j,simoptions.pi_semiz_J,pi_z_J,Parameters,simoptions);
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in StationaryDist_Case1_FHorz_PType (line 234)
        StationaryDist_ii=StationaryDist_FHorz_Case1(jequaloneDist_temp,AgeWeightParamNames_temp,Policy_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,pi_z_temp,Parameters_temp,simoptions_temp);
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in solve_model (line 110)
StatDist = StationaryDist_Case1_FHorz_PType(jequaloneDist,AgeWeightsParamNames,PTypeDistParamNames,Policy,n_d,n_a,n_z,N_j,Names_i,pi_z,Params,simoptions);
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in fun_obj (line 20)
results = solve_model(Params, grids, ptypes, pi_semiz, semiz_grid, jequaloneDist, ReturnFn, FnsToEval, vfoptions, simoptions);
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in main_estimation (line 199)
    [~,results]=fun_obj(guess_vec,Params,inputs,calibNames,targetNames,data_mom,calibWeights);
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I think I understood what is going on: @robertdkirkby you fixed the no interpolation case (gridinterplayer=0) but did not update the interpolation part.
I created a new post to explain better the entire issue. I’ve changed the code to make it work and sent a PR to the upstream toolkit repository. Please let me know if you agree with the change (my toy example now runs without errors but you never know).

I would be happy to donate my toy model on GitHub - aledinola/life-cycle-search · GitHub to be included as a toolkit example. The subfolder docs contains a short description of the model (with attached tex file in case you like to shorten it further). The lines after 137 in main.m can be eliminated or commented out since they re-solve the same model using custom-made cpu code, to check the correctness of the toolkit.