From 212e49bcbf8a6686579faeecf3a4c6efb557bccc Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Thu, 22 Jan 2026 22:34:22 +1300 Subject: [PATCH 01/90] Support mixed finite/infinite horizon problems OLGModel14 attempts to solve a GE for households (with finite horizons) and firms (with infinite horizons). While there are plenty of functions for solving for one type of horizon or another, we must make some accommodations for mixtures of the two. Given there is already some code in the FHorz cases for the occasional infinite horizon, I attempted to flesh that out until the GE could be solved without throwing errors. I took the liberty of keeping InfHorz grids named without the _J suffix, just so we'd get an error if we sent the wrong parameters to the wrong function. I also saw what appeared to be minor drifts in the implementations of the two types of functions, and I copy/pasted what appeared to be the more elaborated code to the side that seemed to be missing it. --- .../PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m | 10 +++++++--- .../PType/HeteroAgentStationaryEqm_InfHorz_PType.m | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m index f4e4dad6..f3e18ffa 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m @@ -443,9 +443,8 @@ end end - %% Set up exogenous shock grids now (so they can then just be reused every time) - + % CHECK IF THE FOLLOWING IS REDUNDANT/OBSOLETE down to next %% if isstruct(z_grid) PTypeStructure.(iistr).z_grid=z_grid.(Names_i{ii}); else @@ -473,6 +472,10 @@ % Check if using ExogShockFn or EiidShockFn, and if so, do these use a parameter that is being determined in general eqm heteroagentoptions.gridsinGE(ii)=0; + + %% Set up exogenous shock grids now (so they can then just be reused every time) + % Check if using ExogShockFn or EiidShockFn, and if so, do these use a + % parameter that is being determined in general eqm if isfield(PTypeStructure.(iistr).vfoptions,'ExogShockFn') tempExogShockFnParamNames=getAnonymousFnInputNames(PTypeStructure.(iistr).vfoptions.ExogShockFn); % can just leave action space in here as we only use it to see if GEPriceParamNames is part of it @@ -565,7 +568,7 @@ end % Regardless of whether they are done here of in _subfn, they will be precomputed by the time we get to the value fn, staty dist, etc. So PTypeStructure.(iistr).vfoptions.alreadygridvals_semiexo=1; - PTypeStructure.(iistr).simoptions.alreadygridvals_semiexo=1; + PTypeStructure.(iistr).simoptions.alreadygridvals_semiexo=1; end @@ -581,6 +584,7 @@ if isa(ReturnFn,'struct') PTypeStructure.(iistr).ReturnFn=ReturnFn.(Names_i{ii}); end + PTypeStructure.(iistr).ReturnFnParamNames=ReturnFnParamNamesFn(PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).Parameters); %% jequaloneDist and AgeWeightsParamNames diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m index 48b0dd89..51a22f89 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m @@ -481,6 +481,7 @@ PTypeStructure.(iistr).FnsToEvaluate=FnsToEvaluate_temp; PTypeStructure.(iistr).FnsToEvaluateParamNames=FnsToEvaluateParamNames_temp; PTypeStructure.(iistr).WhichFnsForCurrentPType=WhichFnsForCurrentPType; + PTypeStructure.(iistr).FnsAndPTypeIndicator_ii=FnsAndPTypeIndicator_ii; %% PType masses if isa(PTypeDistParamNames, 'array') @@ -488,8 +489,9 @@ else PTypeStructure.(iistr).PTypeWeight=PTypeStructure.(iistr).Parameters.(PTypeDistParamNames{1}); % Don't need '.(Names_i{ii}' as this was already done when putting it into PTypeStrucutre, and here I take it straing from PTypeStructure.(iistr).Parameters rather than from Parameters itself. end + % Ptype masses - PTypeStructure.ptweights(ii,1)=PTypeStructure.(iistr).Parameters.(PTypeDistParamNames{1}); + PTypeStructure.ptweights(1,ii)=PTypeStructure.(iistr).Parameters.(PTypeDistParamNames{1}); end From 228f86bdee0b30f8b5c32b31a1d1681ab6e28b5e Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Thu, 29 Jan 2026 15:52:52 +1300 Subject: [PATCH 02/90] Re-merge master for take 2 Return to resolving OLGModel14 changes needed to run. To wit: HeteroAgentStationaryEqm_Case1_FHorz_PType: add some clarifying comments about how we have moved code around to reduce duplication between FHorz and InfHorz cases that can be mixed together (as OLGModel14 does). HeteroAgentStationaryEqm_Case1_PType: Mostly clean and comment. Also remove error message related to no implemented functionality. Is it now implemented? ValueFnIter_nod_HowardGreedy_raw: if we should call this function without ever evaluating the return function (because `maxiter` was zero or some such, then `Ftemp` would never have been initialized. I restored the test for non-finite values inside the loop because testing a matrix that's `n_a` by `n_z` is relatively cheap, and it's very valuable for people to see the moment the bad value pops up (for easier debugging). --- .../InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m index 51a22f89..26899e30 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m @@ -360,7 +360,7 @@ else PTypeStructure.(iistr).l_e=0; end - + if isstruct(d_grid) PTypeStructure.(iistr).d_grid=d_grid.(Names_i{ii}); else @@ -481,6 +481,7 @@ PTypeStructure.(iistr).FnsToEvaluate=FnsToEvaluate_temp; PTypeStructure.(iistr).FnsToEvaluateParamNames=FnsToEvaluateParamNames_temp; PTypeStructure.(iistr).WhichFnsForCurrentPType=WhichFnsForCurrentPType; + % Copied from FHorz/PType version for consistency PTypeStructure.(iistr).FnsAndPTypeIndicator_ii=FnsAndPTypeIndicator_ii; %% PType masses @@ -490,7 +491,7 @@ PTypeStructure.(iistr).PTypeWeight=PTypeStructure.(iistr).Parameters.(PTypeDistParamNames{1}); % Don't need '.(Names_i{ii}' as this was already done when putting it into PTypeStrucutre, and here I take it straing from PTypeStructure.(iistr).Parameters rather than from Parameters itself. end - % Ptype masses + % Ptype masses; Copied from FHorz/PType version for consistency PTypeStructure.ptweights(1,ii)=PTypeStructure.(iistr).Parameters.(PTypeDistParamNames{1}); end From b7ecf491b11bd18a6ae1d7ee392b5ce9be3968d0 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Thu, 12 Feb 2026 18:04:03 +1300 Subject: [PATCH 03/90] Improve lowmemory==2 for ExpAssets In my particular case, the toolkit was trying to allocate overly large arrays when `lowmemory==2` was selected. Specifically: ``` DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); ``` Replicated a large `EV` N_d1 times N_a1 times is unnecessary, as the loop for `lowmemory==2` doesn't use all of EV, but iterates across the `z`s (as well as the `e`s. I noticed that other ExpAsset codes already had the fixes I am proposing herein (namely, replicate based on `EV(:,:,z_c)` rather than all of EV and then selecting the `z` elements from that). I also noticed that other codes had optimizations like lifting some array constructions out of loops, etc. I attempted to therefore coalesce the best of all across all in this directory. There's obviously more work to be done for more code cases, but I thought I'd start with this bite-sized chunk. Normally I'd create a fresh branch for these changes and a clean pull request. But I need these changes in the OLGModel14-mixed-horizon-fixes branch, which might need a while before its merged. So I'm leaving it to you to decide whether you want to `git cherry-pick` this commit and merge into the main branch ahead of that decision. --- .../ValueFnIter_FHorz_ExpAsset_e_raw.m | 12 ++++-------- .../ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m | 13 +++++-------- .../ValueFnIter_FHorz_ExpAsset_noa1_noz_e_raw.m | 6 +++--- .../ValueFnIter_FHorz_ExpAsset_noa1_raw.m | 6 ++---- .../ValueFnIter_FHorz_ExpAsset_nod1_noa1_e_raw.m | 3 ++- ...ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_e_raw.m | 6 +++--- .../ValueFnIter_FHorz_ExpAsset_nod1_noz_e_raw.m | 5 +++-- .../ValueFnIter_FHorz_ExpAsset_nod1_raw.m | 6 ++---- .../ValueFnIter_FHorz_ExpAsset_raw.m | 8 ++------ 9 files changed, 26 insertions(+), 39 deletions(-) diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m index 3cc76f83..fc4c1330 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m @@ -103,13 +103,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); % should autofill e dimension %Calc the max and its index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -118,7 +116,7 @@ Policy(:,:,:,N_j)=shiftdim(maxindex,1); elseif vfoptions.lowmemory==1 - + DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_val, ReturnFnParamsVec,0,0); @@ -210,13 +208,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); % should autofill e dimension % Calc the max and its index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -225,7 +221,7 @@ Policy(:,:,:,jj)=shiftdim(maxindex,1); elseif vfoptions.lowmemory==1 - + DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec,0,0); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m index 6b87d831..dec6b4e6 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m @@ -93,12 +93,12 @@ V(:,:,:,N_j)=shiftdim(Vtemp,1); Policy(:,:,:,N_j)=shiftdim(maxindex,1); elseif vfoptions.lowmemory==1 - + DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case2_Disc_Par2e(ReturnFn,[n_d1,n_d2], n_a2, n_z, special_n_e, d_gridvals, a2_grid, z_gridvals_J(:,:,N_j), e_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*repelem(EV,N_d1,1); + entireRHS=ReturnMatrix_e+DiscountedEV; %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -109,14 +109,13 @@ elseif vfoptions.lowmemory==2 for z_c=1:N_z z_gridvals_J(z_c,:,N_j); - EV_z=EV(:,:,z_c); - + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_ze=CreateReturnFnMatrix_Case2_Disc_Par2e(ReturnFn,[n_d1,n_d2], n_a2, special_n_z, special_n_e, d_gridvals, a2_grid, z_val, e_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix_ze+DiscountFactorParamsVec*repelem(EV_z,N_d1,1); + entireRHS=ReturnMatrix_ze+DiscountedEV_z; %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -191,14 +190,12 @@ elseif vfoptions.lowmemory==2 for z_c=1:N_z z_gridvals_J(z_c,:,jj); - EV_z=EV(:,:,z_c); - for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); ReturnMatrix_ze=CreateReturnFnMatrix_Case2_Disc_Par2e(ReturnFn,[n_d1,n_d2], n_a2, special_n_z, special_n_e, d_gridvals, a2_grid, z_val, e_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix_ze+DiscountFactorParamsVec*repelem(EV_z,N_d1,1); + entireRHS=ReturnMatrix_ze+DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_noz_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_noz_e_raw.m index ef888832..d8e436b8 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_noz_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_noz_e_raw.m @@ -58,11 +58,11 @@ % Switch EV from being in terps of a2prime to being in terms of d2 and a2 EV=a2primeProbs.*Vlower+(1-a2primeProbs).*Vupper; % (d2,a1prime,a2,u,zprime) - + DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,1,1); if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,[n_d1,n_d2], n_a2, n_e, d_gridvals, a2_grid, e_gridvals_J(:,:,N_j), ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,1,1); % Should autofill e dimension + entireRHS=ReturnMatrix+DiscountedEV; % Should autofill e dimension %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -75,7 +75,7 @@ ReturnMatrix_e=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,[n_d1,n_d2], n_a2, special_n_e, d_gridvals, a2_grid, e_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*repelem(EV,N_d1,1); + entireRHS=ReturnMatrix_e+DiscountedEV; %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_raw.m index b137e598..8b72125e 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_raw.m @@ -64,13 +64,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,[n_d1,n_d2], n_a2, n_z, d_gridvals, a2_grid, z_gridvals_J(:,:,N_j), ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -81,7 +79,7 @@ elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,1); ReturnMatrix=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,[n_d1,n_d2], n_a2, special_n_z, d_gridvals, a2_grid, z_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_e_raw.m index c698fd52..43d2d6b3 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_e_raw.m @@ -90,12 +90,13 @@ V(:,:,:,N_j)=shiftdim(Vtemp,1); Policy(:,:,:,N_j)=shiftdim(maxindex,1); elseif vfoptions.lowmemory==1 + DiscountedEV=DiscountFactorParamsVec*EV; for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case2_Disc_Par2e(ReturnFn,n_d2, n_a2, n_z, special_n_e, d2_gridvals, a2_grid, z_gridvals_J(:,:,N_j), e_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*EV; + entireRHS=ReturnMatrix_e+DiscountedEV; %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_e_raw.m index b5594da4..7fcf9a49 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_e_raw.m @@ -110,11 +110,11 @@ % Switch EV from being in terps of a2prime to being in terms of d2 and a2 EV=a2primeProbs.*Vlower+(1-a2primeProbs).*Vupper; % (d2,a1prime,a2,u,zprime) - + DiscountedEV=DiscountFactorParamsVec*EV; if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,n_d2, n_a2, n_e, d2_gridvals, a2_grid, e_gridvals_J(:,:,jj), ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix+DiscountFactorParamsVec*EV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -125,7 +125,7 @@ for e_c=1:N_e ReturnMatrix_e=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,n_d2, n_a2, special_n_e, d2_gridvals, a2_grid, e_gridvals_J(e_c,:,jj), ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*EV; + entireRHS=ReturnMatrix_e+DiscountedEV; %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noz_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noz_e_raw.m index ef8264a7..857652cf 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noz_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noz_e_raw.m @@ -62,10 +62,11 @@ EV=aprimeProbs.*Vlower+(1-aprimeProbs).*Vupper; % (d2,a1prime,a2,u,zprime) % Already applied the probabilities from interpolating onto grid + DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2,n_a1,n_a1,n_a2,n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, e_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); % should autofill e dimension + entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -77,7 +78,7 @@ e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2,n_a1,n_a1,n_a2,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, e_val, ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); % should autofill e dimension + entireRHS=ReturnMatrix_e+DiscountedEV; % should autofill e dimension %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m index 01f2bd53..9fcb0625 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m @@ -68,12 +68,10 @@ EV(isnan(EV))=0; %multiplications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) EV=squeeze(sum(EV,3)); % sum over z', leaving a singular third dimension - DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_jj), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -85,7 +83,7 @@ for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),1,N_a1,1); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m index 2f53ff2b..041961dc 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m @@ -85,13 +85,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2,n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); % (d,a1prime,a) - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); %Calc the max and its index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -102,11 +100,9 @@ elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); - ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); - entireRHS_z=ReturnMatrix_z+DiscountedEV_z; + entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1); %Calc the max and its index [Vtemp,maxindex]=max(entireRHS_z,[],1); From 681e0c13055e26dbec84ef128d23845a713a2014 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sun, 1 Mar 2026 14:59:28 +1300 Subject: [PATCH 04/90] Support CustomModelStat variables for PTypes This change implements support for CustomModelStats when `heteroagentoptions` has PType specifiers (i.e., `heteroagentoptions.household.CustomModelStats`). In this implementation, we can support CustomModelStats for each different PType, separately. This is a simple case because each PType is only either FHorz or InfHorz. It does not yet support a overarching PType implementation whereby a single CustomModelStats can see all PTypes at once. This case is more difficult, as different PTypes can have different horizons, thus a universal CustomModelStats function would have to unpack that before calling magical subfunctions. --- ...roAgentStationaryEqm_Case1_FHorz_PType_subfn.m | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m index 4837b3df..e763231b 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m @@ -18,6 +18,7 @@ %% +Ptype_cells=cell(1,PTypeStructure.N_i); % Hold results in case needed for CustomStats AggVars_ConditionalOnPType=zeros(PTypeStructure.numFnsToEvaluate,PTypeStructure.N_i,'gpuArray'); % Create AggVars conditional on ptype. for ii=1:PTypeStructure.N_i @@ -69,7 +70,9 @@ % PTypeStructure.(iistr).simoptions.outputasstructure=0; % Want AggVars_ii as matrix to make it easier to add them across the PTypes (is set outside this script) AggVars_ii=EvalFnOnAgentDist_AggVars_Case1(StationaryDist_ii, Policy_ii, PTypeStructure.(iistr).FnsToEvaluate, PTypeStructure.(iistr).Parameters, PTypeStructure.(iistr).FnsToEvaluateParamNames, PTypeStructure.(iistr).n_d, PTypeStructure.(iistr).n_a, PTypeStructure.(iistr).n_z, PTypeStructure.(iistr).d_grid, PTypeStructure.(iistr).a_grid, PTypeStructure.(iistr).z_gridvals, PTypeStructure.(iistr).simoptions); end - + if heteroagentoptions.useCustomModelStats==1 + Ptype_cells{ii}={V_ii,Policy_ii,StationaryDist_ii}; + end AggVars_ConditionalOnPType(PTypeStructure.(iistr).FnsAndPTypeIndicator_ii,ii)=AggVars_ii; if heteroagentoptions.useCustomModelStats==1 @@ -121,6 +124,7 @@ end end + %% Evaluate General Eqm Eqns % use of real() is a hack that could disguise errors, but I couldn't find why matlab was treating output as complex GeneralEqmConditionsVec=zeros(1,length(GEeqnNames)); @@ -172,8 +176,13 @@ end if heteroagentoptions.useCustomModelStats==1 fprintf('Current CustomModelStats variables: \n') - for ii=1:length(customstatnames) - fprintf(heteroagentoptions.verboseaccuracy1,customstatnames{ii},CustomStats.(customstatnames{ii})) + for ii=1:PTypeStructure.N_i + iistr=PTypeStructure.iistr{ii}; + if isfield(customstatnames, iistr) + for pp=1:length(customstatnames.(iistr)) + fprintf(heteroagentoptions.verboseaccuracy1,customstatnames.(iistr){pp},CustomStats.(iistr).(customstatnames.(iistr){pp})) + end + end end end fprintf('Current GeneralEqmEqns: \n') From add94554c87bb2e6075ffa7924f63cdd45603391 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 3 Mar 2026 21:37:38 +1300 Subject: [PATCH 05/90] Create a working OLGModel14 with mixed horizon transition paths These are relatively minimal changes needed to graph the transition paths across OLGModel14. There's one innocuous change related to handling `experienceasset` in `vfoptions`, but these changes have none of the work I've tried to do to make experienceassets work with transition paths. That work will come later. There are a few cases where I could not help but reformat some code (isa(X,'struct') -> isstruct(X)), and also Names_i{ii}->iistr where appropriate. But otherwise they are about as minimal as I could make them. I have created `TransitionPath_InfHorz_PType_singlepath.m` which could easily be used to simplify the guts of `TransitionPath_InfHorz_PType_shooting.m`, but that, too, will wait for a later day. --- ...FnOnAgentDist_AllStats_FHorz_Case1_PType.m | 76 +++++++--- .../PType/TransitionPath_Case1_FHorz_PType.m | 136 ++++++++++++------ .../TransitionPath_InfHorz_PType_shooting.m | 1 + .../TransitionPath_InfHorz_PType_singlepath.m | 89 ++++++++++++ .../AgentDist_InfHorz_TPath_SingleStep_raw.m | 6 +- .../PricePathParamPath_FHorz_StructToMatrix.m | 63 +++----- .../Subcodes/inputsFindtplus1tminus1.m | 11 +- 7 files changed, 269 insertions(+), 113 deletions(-) create mode 100644 TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m diff --git a/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m b/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m index 517b042a..e776ae7a 100644 --- a/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m +++ b/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m @@ -335,18 +335,35 @@ N_a_temp=prod(n_a_temp); a_gridvals_temp=CreateGridvals(n_a_temp,a_grid_temp,1); - % Turn (semiz,z,e) into z_gridvals_J_temp as FnsToEvalute do not distinguish them - [n_z_temp,z_gridvals_J_temp,N_z_temp,l_z_temp,simoptions_temp]=CreateGridvals_FnsToEvaluate_FHorz(n_z_temp,z_grid_temp,N_j_temp,simoptions_temp,Parameters_temp); + if isfinite(N_j_temp) + % Turn (semiz,z,e) into z_gridvals_J_temp as FnsToEvalute do not distinguish them + [n_z_temp,z_gridvals_J_temp,N_z_temp,l_z_temp,simoptions_temp]=CreateGridvals_FnsToEvaluate_FHorz(n_z_temp,z_grid_temp,N_j_temp,simoptions_temp,Parameters_temp); + else + l_z_temp=length(n_z_temp); + N_z_temp=prod(n_z_temp); + z_gridvals_temp=CreateGridvals(n_z_temp,z_grid_temp,1); + end if N_z_temp==0 N_z_temp=1; % Just makes things easier below end % Switch to PolicyVals - PolicyValues_temp=PolicyInd2Val_FHorz(PolicyIndexes_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,d_grid_temp,a_grid_temp,simoptions_temp,1); - if l_z_temp==0 - PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,3,1]); % (N_a,N_j,l_daprime) + if isfinite(N_j_temp) + PolicyValues_temp=PolicyInd2Val_FHorz(PolicyIndexes_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,d_grid_temp,a_grid_temp,simoptions_temp,1); + if l_z_temp==0 + PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,3,1]); % (N_a,N_j,l_daprime) + else + PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,3,4,1]); % (N_a,N_z,N_j,l_daprime) + end + StationaryDist_ii=reshape(StationaryDist.(Names_i{ii}),[N_a_temp*N_z_temp*N_j_temp,1]); % Note: does not impose *StationaryDist.ptweights(ii) else - PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,3,4,1]); % (N_a,N_z,N_j,l_daprime) + PolicyValues_temp=PolicyInd2Val_Case1(PolicyIndexes_temp,n_d_temp,n_a_temp,n_z_temp,d_grid_temp,a_grid_temp,simoptions_temp,1); + if l_z_temp==0 + PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,1]); % (N_a,l_daprime) + else + PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,3,1]); % (N_a,N_z,l_daprime) + end + StationaryDist_ii=reshape(StationaryDist.(Names_i{ii}),[N_a_temp*N_z_temp,1]); % Note: does not impose *StationaryDist.ptweights(ii) end l_daprime_temp=size(PolicyValues_temp,1); @@ -354,7 +371,6 @@ FnsAndPTypeIndicator(:,ii)=FnsAndPTypeIndicator_ii; %% Some things that don't need to go in the loop over FnsToEvalaute - StationaryDist_ii=reshape(StationaryDist.(Names_i{ii}),[N_a_temp*N_z_temp*N_j_temp,1]); % Note: does not impose *StationaryDist.ptweights(ii) % Eliminate all the zero-weighted points (this doesn't really save runtime for the exact calculation and often can increase it, but % for the createDigest it slashes the runtime. So since we want it then we may as well do it now.) temp=logical(StationaryDist_ii~=0); @@ -376,14 +392,21 @@ CondlRestnFnParamNames={}; end - if l_z_temp==0 - CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,CondlRestnFnParamNames,N_j_temp,2); % j in 2nd dimension: (a,j,l_d+l_a), so we want j to be after N_a - RestrictionValues=logical(EvalFnOnAgentDist_Grid_J(CondlRestnFn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,0,a_gridvals_temp,[])); + if isfinite(N_j_temp) + if l_z_temp==0 + CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,CondlRestnFnParamNames,N_j_temp,2); % j in 2nd dimension: (a,j,l_d+l_a), so we want j to be after N_a + RestrictionValues=logical(EvalFnOnAgentDist_Grid_J(CondlRestnFn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,0,a_gridvals_temp,[])); + else + CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,CondlRestnFnParamNames,N_j_temp,3); % j in 3rd dimension: (a,z,j,l_d+l_a), so we want j to be after N_a and N_z + RestrictionValues=logical(EvalFnOnAgentDist_Grid_J(CondlRestnFn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_J_temp)); + end + RestrictionValues=reshape(RestrictionValues,[N_a_temp*N_z_temp*N_j_temp,1]); else - CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,CondlRestnFnParamNames,N_j_temp,3); % j in 3rd dimension: (a,z,j,l_d+l_a), so we want j to be after N_a and N_z - RestrictionValues=logical(EvalFnOnAgentDist_Grid_J(CondlRestnFn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_J_temp)); + CondlRestnFnParamsCell=CreateCellFromParams(Parameters_temp,CondlRestnFnParamNames); + + RestrictionValues=logical(EvalFnOnAgentDist_Grid(CondlRestnFn, CondlRestnFnParamsCell,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_temp)); + RestrictionValues=reshape(RestrictionValues,[N_a_temp*N_z_temp,1]); end - RestrictionValues=reshape(RestrictionValues,[N_a_temp*N_z_temp*N_j_temp,1]); RestrictedStationaryDistVec=StationaryDist_ii; RestrictedStationaryDistVec(~RestrictionValues)=0; % zero mass on all points that do not meet the restriction @@ -411,16 +434,35 @@ if FnsAndPTypeIndicator_ii(ff)==1 % If this function is relevant to this ptype % Get parameter names for current FnsToEvaluate functions - tempnames=getAnonymousFnInputNames(FnsToEvaluate.(FnsToEvalNames{ff})); + % Get parameter names for current FnsToEvaluate functions + if isstruct(FnsToEvaluate.(FnsToEvalNames{ff})) + tempfn=FnsToEvaluate.(FnsToEvalNames{ff}).(Names_i{ii}); + else + tempfn=FnsToEvaluate.(FnsToEvalNames{ff}); + end + tempnames=getAnonymousFnInputNames(tempfn); if length(tempnames)>(l_daprime_temp+l_a_temp+l_z_temp) FnsToEvaluateParamNames={tempnames{l_daprime_temp+l_a_temp+l_z_temp+1:end}}; % the first inputs will always be (d,aprime,a,z) else FnsToEvaluateParamNames={}; end - if l_z_temp==0 - CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,FnsToEvaluateParamNames,N_j_temp,2); + if isfinite(N_j_temp) + if l_z_temp==0 + CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,FnsToEvaluateParamNames,N_j_temp,2); + else + CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,FnsToEvaluateParamNames,N_j_temp,3); + end + + %% We have set up the current PType, now do some calculations for it. + simoptions_temp.keepoutputasmatrix=1; + ValuesOnGrid_ii=EvalFnOnAgentDist_Grid_J(tempfn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_J_temp); + ValuesOnGrid_ii=reshape(ValuesOnGrid_ii,[N_a_temp*N_z_temp*N_j_temp,1]); + + % StationaryDist_ii=reshape(StationaryDist.(Names_i{ii}),[N_a_temp*N_z_temp*N_j_temp,1]); % Note: does not impose *StationaryDist.ptweights(ii) else - CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,FnsToEvaluateParamNames,N_j_temp,3); + FnToEvaluateParamsCell=CreateCellFromParams(Parameters_temp,FnsToEvaluateParamNames); + ValuesOnGrid_ii=EvalFnOnAgentDist_Grid(tempfn, FnToEvaluateParamsCell,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_temp); + ValuesOnGrid_ii=reshape(ValuesOnGrid_ii,[N_a_temp*N_z_temp,1]); end %% We have set up the current PType, now do some calculations for it. diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m index ef2caced..2898342d 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m @@ -133,15 +133,43 @@ %% Some internal commands require a few vfoptions and simoptions to be set vfoptions.EVpre=0; % Not actually an option that can be used here -if isfield(vfoptions,'lowmemory')==0 - vfoptions.lowmemory=0; +if ~isfield(vfoptions, 'experienceasset') + vfoptions.experienceasset=0; +end +if ~isfield(vfoptions,'lowmemory') + lowmemory_in_PType=0; + for ii=1:N_i + if isfield(vfoptions.Name_i{ii}, 'lowmemory') + lowmemory_in_PType=1; + elseif lowmemory_in_PType==1 + vfoptions.Name_i{ii}.lowmemory=0; + end + end + if lowmemory_in_PType==0 + vfoptions.lowmemory=0; + end end %% Get AgeWeights from Parameters -try - AgeWeights=Parameters.(AgeWeightsParamNames{1}); -catch - error(['Failed to find parameter ', AgeWeightsParamNames{1}]) +if isstruct(AgeWeightsParamNames) + for ii=1:N_i + try + AgeWeights=Parameters.(AgeWeightsParamNames.(Names_i{ii}){1}); + if length(AgeWeights)~=N_j.(Names_i{ii}) + error('Ageweights does not have age-like length') + else + break + end + catch + error(['Failed to find parameter ', AgeWeightsParamNames.(Names_i{ii}){1}]) + end + end +else + try + AgeWeights=Parameters.(AgeWeightsParamNames{1}); + catch + error(['Failed to find parameter ', AgeWeightsParamNames{1}]) + end end % Later, when creating PTypeStructure, we get the ptype-specific versions out of this and create an AgeWeights_T structure. @@ -223,7 +251,7 @@ PTypeStructure.(iistr).vfoptions.divideandconquer=0; %default else if PTypeStructure.(iistr).vfoptions.divideandconquer==1 - PTypeStructure.(iistr).vfoptions.level1n=ceil(n_a/50); % default + PTypeStructure.(iistr).vfoptions.level1n=ceil(n_a.(iistr)/50); % default end end if ~isfield(PTypeStructure.(iistr).vfoptions,'gridinterplayer') @@ -261,7 +289,7 @@ % Horizon is determined via N_j if isstruct(N_j) - PTypeStructure.(iistr).N_j=N_j.(Names_i{ii}); + PTypeStructure.(iistr).N_j=N_j.(iistr); elseif isscalar(N_j) PTypeStructure.(iistr).N_j=N_j; else @@ -269,7 +297,7 @@ end if isa(n_d,'struct') - PTypeStructure.(iistr).n_d=n_d.(Names_i{ii}); + PTypeStructure.(iistr).n_d=n_d.(iistr); else PTypeStructure.(iistr).n_d=n_d; end @@ -281,7 +309,7 @@ PTypeStructure.(iistr).l_d=length(n_d); end if isa(n_a,'struct') - PTypeStructure.(iistr).n_a=n_a.(Names_i{ii}); + PTypeStructure.(iistr).n_a=n_a.(iistr); else PTypeStructure.(iistr).n_a=n_a; end @@ -293,7 +321,7 @@ PTypeStructure.(iistr).l_aprime=PTypeStructure.(iistr).l_aprime-1; end if isa(n_z,'struct') - PTypeStructure.(iistr).n_z=n_z.(Names_i{ii}); + PTypeStructure.(iistr).n_z=n_z.(iistr); else PTypeStructure.(iistr).n_z=n_z; end @@ -313,17 +341,17 @@ end if isa(d_grid,'struct') - PTypeStructure.(iistr).d_grid=gpuArray(d_grid.(Names_i{ii})); + PTypeStructure.(iistr).d_grid=gpuArray(d_grid.(iistr)); else PTypeStructure.(iistr).d_grid=gpuArray(d_grid); end if isa(a_grid,'struct') - PTypeStructure.(iistr).a_grid=gpuArray(a_grid.(Names_i{ii})); + PTypeStructure.(iistr).a_grid=gpuArray(a_grid.(iistr)); else PTypeStructure.(iistr).a_grid=gpuArray(a_grid); end if isa(z_grid,'struct') - PTypeStructure.(iistr).z_grid=gpuArray(z_grid.(Names_i{ii})); + PTypeStructure.(iistr).z_grid=gpuArray(z_grid.(iistr)); else PTypeStructure.(iistr).z_grid=gpuArray(z_grid); end @@ -354,14 +382,14 @@ % end if isa(pi_z,'struct') - PTypeStructure.(iistr).pi_z=pi_z.(Names_i{ii}); % Different grids by permanent type, but not depending on age. (same as the case just above; this case can occour with or without the existence of vfoptions, as long as there is no vfoptions.agedependentgrids) + PTypeStructure.(iistr).pi_z=pi_z.(iistr); % Different grids by permanent type, but not depending on age. (same as the case just above; this case can occour with or without the existence of vfoptions, as long as there is no vfoptions.agedependentgrids) else PTypeStructure.(iistr).pi_z=pi_z; end PTypeStructure.(iistr).ReturnFn=ReturnFn; if isa(ReturnFn,'struct') - PTypeStructure.(iistr).ReturnFn=ReturnFn.(Names_i{ii}); + PTypeStructure.(iistr).ReturnFn=ReturnFn.(iistr); end % Parameters are allowed to be given as structure, or as vector/matrix (in terms of their dependence on permanent type). @@ -373,8 +401,8 @@ for kField=1:nFields if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) - if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); + if isfield(Parameters.(FullParamNames{kField}),iistr) + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(iistr); end elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. temp=Parameters.(FullParamNames{kField}); @@ -398,7 +426,7 @@ % The parameter names can be made to depend on the permanent-type PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames; if isa(DiscountFactorParamNames,'struct') - PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(Names_i{ii}); + PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(iistr); end % Implement new way of handling ReturnFn inputs (note l_d, l_a, l_z are just created for this and then not used for anything else later) @@ -440,7 +468,7 @@ PTypeStructure.(iistr).WhichFnsForCurrentPType=zeros(PTypeStructure.numFnsToEvaluate,1); jj=1; % jj indexes the FnsToEvaluate that are relevant to the current PType for kk=1:PTypeStructure.numFnsToEvaluate - if isa(FnsToEvaluate.(FnNames{kk}),'struct') + if isstruct(FnsToEvaluate.(FnNames{kk})) if isfield(FnsToEvaluate.(FnNames{kk}), Names_i{ii}) PTypeStructure.(iistr).FnsToEvaluate.(FnNames{kk})=FnsToEvaluate.(FnNames{kk}).(Names_i{ii}); % % Figure out FnsToEvaluateParamNames @@ -486,32 +514,48 @@ %% Organise V_final and AgentDist_initial % Reshape V_final - if transpathoptions.fastOLG==0 + if ~isfinite(PTypeStructure.(iistr).N_j) if N_z==0 if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j]); + V_final.(iistr)=reshape(V_final.(iistr),[N_a,1]); else - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_e,N_j]); + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_e]); end else if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_j]); + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z]); else - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_e,N_j]); + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_e]); end end - else + elseif transpathoptions.fastOLG==0 + N_j_temp=PTypeStructure.(iistr).N_j; + if N_z==0 + if N_e==0 + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); + else + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_e,N_j_temp]); + end + else + if N_e==0 + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_j_temp]); + else + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_e,N_j_temp]); + end + end + else % transpathoptions.fastOLG==1 + N_j_temp=PTypeStructure.(iistr).N_j; if N_z==0 if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j]); + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); else - V_final.(iistr)=reshape(permute(V_final.(iistr),[1,3,2]),[N_a*N_j,N_e]); + V_final.(iistr)=reshape(permute(reshape(V_final.(iistr),[N_a,N_e,N_j_temp]),[1,3,2]),[N_a*N_j_temp,N_e]); end else if N_e==0 - V_final.(iistr)=reshape(permute(V_final.(iistr),[1,3,2]),[N_a*N_j,N_z]); + V_final.(iistr)=reshape(permute(reshape(V_final.(iistr),[N_a,N_z,N_j_temp]),[1,3,2]),[N_a*N_j_temp,N_z]); else - V_final.(iistr)=reshape(permute(V_final.(iistr),[1,4,2,3]),[N_a*N_j,N_z,N_e]); + V_final.(iistr)=reshape(permute(reshape(V_final.(iistr),[N_a,N_z,N_e,N_j_temp]),[1,4,2,3]),[N_a*N_j_temp,N_z,N_e]); end end end @@ -608,13 +652,18 @@ if N_e==0 jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a,T]); else - jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_e,T]); % simoptions.fastOLG==1 + fprintf('Following error applies to agent permanent type: %s \n',iistr) + error('The age weights parameter seems to be the wrong size') end - else - if N_e==0 - jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z,T]); % simoptions.fastOLG==1 + else % not a structure, so must apply to all permanent types + if all(size(AgeWeights)==[N_j_temp,1]) + % Does not depend on transition path period + AgeWeights_T.(iistr)=gather(AgeWeights.*ones(1,T)); + elseif all(size(AgeWeights)==[1,N_j_temp]) + % Does not depend on transition path period + AgeWeights_T.(iistr)=gather(AgeWeights'.*ones(1,T)); else - jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z*N_e,T]); % simoptions.fastOLG==1 + error('The age weights parameter seems to be the wrong size') end end PTypeStructure.(iistr).jequalOneDist_T=jequalOneDist_temp; @@ -622,15 +671,18 @@ transpathoptions.(iistr).trivialjequalonedist=1; if N_z==0 if N_e==0 - jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a,1]); - else - jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_e,1]); % simoptions.fastOLG==1 + AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a,1]); % if simoptions.fastOLG==0 + if PTypeStructure.(iistr).simoptions.fastOLG==1 + AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a,1]); + end end else if N_e==0 - jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z,1]); % simoptions.fastOLG==1 + AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z,1]); + elseif simoptions.fastOLG==0 + AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z*N_e,1]); else - jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z*N_e,1]); % simoptions.fastOLG==1 + AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z,N_e]); end end PTypeStructure.(iistr).jequalOneDist=jequalOneDist_temp; @@ -726,7 +778,7 @@ %% Check if using _tminus1 and/or _tplus1 variables. if isstruct(FnsToEvaluate) && isstruct(GeneralEqmEqns) - [tplus1priceNames,tminus1priceNames,tminus1AggVarsNames,tminus1paramNames,tplus1pricePathkk]=inputsFindtplus1tminus1(FnsToEvaluate,GeneralEqmEqns,PricePathNames,ParamPathNames); + [tplus1priceNames,tminus1priceNames,tminus1AggVarsNames,tminus1paramNames,tplus1pricePathkk]=inputsFindtplus1tminus1(FnsToEvaluate,GeneralEqmEqns,PricePathNames,ParamPathNames,Names_i); else tplus1priceNames=[]; tminus1priceNames=[]; @@ -783,7 +835,7 @@ %% Shooting algorithm if transpathoptions.GEnewprice~=2 - % For permanent type, there is just one shooting command, + % For permanent types, there is just one shooting command, % because things like z,e, and fastOLG are handled on a per-PType basis (to permit that they differ across ptype) [PricePath,GEcondnPathmatrix]=TransitionPath_Case1_FHorz_PType_shooting(PricePath0, PricePathNames, ParamPath, ParamPathNames, T, V_final, AgentDist_initial, FnsToEvaluate, GeneralEqmEqns, PricePathSizeVec, ParamPathSizeVec, PricePathSizeVec_ii, ParamPathSizeVec_ii, GEeqnNames,nGeneralEqmEqns,nGeneralEqmEqns_acrossptypes,GeneralEqmEqnsCell,GeneralEqmEqnParamNames, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, transpathoptions, PTypeStructure); diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m index 9d445e40..5099bcc0 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m @@ -31,6 +31,7 @@ AgentDist_initial.(iistr)=reshape(StationaryDist_init.(iistr),[N_a*N_z,1]); end PricePathNew=zeros(size(PricePathOld),'gpuArray'); PricePathNew(T,:)=PricePathOld(T,:); +GEeqnNames=fieldnames(GeneralEqmEqns); GEcondnPath=zeros(T-1,length(GEeqnNames),'gpuArray'); diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m new file mode 100644 index 00000000..6fc25d28 --- /dev/null +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m @@ -0,0 +1,89 @@ +function AggVarsPath=TransitionPath_InfHorz_PType_singlepath(PricePathOld, ParamPath, PricePathNames, ParamPathNames, T, V_final, AgentDist_initial, ... + l_d,N_d,n_d,N_a,n_a,N_z,n_z,d_grid,a_grid,d_gridvals,aprime_gridvals,a_gridvals,z_grid,pi_z,ReturnFn, ... + FnsToEvaluate, ... + Parameters, DiscountFactorParamNames, ReturnFnParamNames, FnsToEvaluateParamNames, AggVarNames, ... + ... % use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, ... + transpathoptions, vfoptions, simoptions) +% PricePathOld is matrix of size T-by-'number of prices' +% ParamPath is matrix of size T-by-'number of parameters that change over path' + +% Remark to self: No real need for T as input, as this is anyway the length of PricePathOld + +% For this agent type, first go back through the value & policy fns. +% Then forwards through agent dist and agg vars. +if N_d>0 + PolicyIndexesPath=zeros(2,N_a,N_z,T-1,'gpuArray'); %Periods 1 to T-1 +else + PolicyIndexesPath=zeros(N_a,N_z,T-1,'gpuArray'); %Periods 1 to T-1 +end + +%First, go from T-1 to 1 calculating the Value function and Optimal +%policy function at each step. Since we won't need to keep the value +%functions for anything later we just store the next period one in +%Vnext, and the current period one to be calculated in V +Vnext=V_final; +for tt=1:T-1 %so t=T-i + % The following digs deeper into PricePathOld and ParamPath in + % FHorz case--check it + for kk=1:length(PricePathNames) + Parameters.(PricePathNames{kk})=PricePathOld(T-tt,kk); + end + for kk=1:length(ParamPathNames) + Parameters.(ParamPathNames{kk})=ParamPath(T-tt,kk); + end + + [V, Policy]=ValueFnIter_InfHorz_TPath_SingleStep(Vnext,n_d,n_a,n_z,d_grid, a_grid, z_grid, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + % The VKron input is next period value fn, the VKron output is this period. + % Policy is kept in the form where it is just a single-value in (d,a') + + if N_d>0 + PolicyIndexesPath(:,:,:,T-tt)=Policy; + else + PolicyIndexesPath(:,:,T-tt)=Policy; + end + Vnext=V; + +end +% Free up space on GPU by deleting things no longer needed +clear V Vnext + +%Now we have the full PolicyIndexesPath, we go forward in time from 1 +%to T using the policies to update the agents distribution generating a +%new price path +%Call AgentDist the current periods distn +AgentDist=AgentDist_initial; +AggVarsPath=zeros(length(FnsToEvaluate),T-1); + +% Precompute for later +II1=gpuArray(1:1:N_a*N_z); +IIones=ones(N_a*N_z,1,'gpuArray'); + +for tt=1:T-1 + + %Get the current optimal policy + if N_d>0 + Policy=PolicyIndexesPath(:,:,:,tt); + else + Policy=PolicyIndexesPath(:,:,tt); + end + + GEprices=PricePathOld(tt,:); + + % Again, since we don't do tminus1, don't dig as deep as FHorz + for nn=1:length(ParamPathNames) + Parameters.(ParamPathNames{nn})=ParamPath(tt,nn); + end + for nn=1:length(PricePathNames) + Parameters.(PricePathNames{nn})=PricePathOld(tt,nn); + end + + PolicyUnKron=UnKronPolicyIndexes_Case1(Policy, n_d, n_a, n_z,vfoptions); + AggVars=EvalFnOnAgentDist_AggVars_Case1(AgentDist, PolicyUnKron, FnsToEvaluate, Parameters, FnsToEvaluateParamNames, n_d, n_a, n_z, d_grid, a_grid, z_grid, simoptions); % The 2 is for Parallel (use GPU) + + AgentDist=AgentDist_InfHorz_TPath_SingleStep(AgentDist,Policy,II1,IIones,N_a,N_z,sparse(pi_z)); + + AggVarsPath(:,tt)=AggVars; +end + + +end diff --git a/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m b/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m index 9cee56c8..dfc7fbd7 100644 --- a/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m +++ b/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m @@ -8,7 +8,11 @@ % AgentDist is already sparse and on cpu -Gammatranspose=sparse(gather(Policy_aprimez),II1,II2,N_a*N_z,N_a*N_z); +if length(size(Policy_aprimez))>2 + II1=repmat(II1,2,1); + IIones=repmat(IIones,2,1); +end +Gammatranspose=sparse(gather(Policy_aprimez),II1,IIones,N_a*N_z,N_a*N_z); % Two steps of the Tan improvement AgentDist=reshape(Gammatranspose*AgentDist,[N_a,N_z]); diff --git a/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m b/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m index 723a6f3a..204c0052 100644 --- a/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m +++ b/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m @@ -71,54 +71,29 @@ % Note: Internally PricePathOld is matrix of size T-by-'number of prices'. % ParamPath is matrix of size T-by-'number of parameters that change over the transition path'. % Actually, some of those prices are 1-by-N_j or N_i or both, so is more subtle than this. + Names_i=fieldnames(N_j); PricePathNames=fieldnames(PricePathStruct); PricePathSizeVec=zeros(1,length(PricePathNames)); % Allows for a given price param to depend on age (or permanent type) - if isstruct(N_j) - Names_i=fieldnames(N_j); - for pp=1:length(PricePathNames) - if isstruct(PricePathStruct.(PricePathNames{pp})) - tempptypenames=fieldnames(PricePathStruct.(PricePathNames{pp})); - temp=PricePathStruct.(PricePathNames{pp}).(tempptypenames{1}); - tempsize=size(temp); - PricePathSizeVec(pp)=length(tempptypenames)*tempsize(tempsize~=T); % Get the dimension which is not T - for ii=1:N_i - N_j_temp=N_j.(Names_i{ii}); - if ~any(PricePathSizeVec(pp)==[1,N_i,N_j_temp,N_i*N_j_temp]) - error(['PricePath for ', PricePathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) - end - end - else - temp=PricePathStruct.(PricePathNames{pp}); - tempsize=size(temp); - PricePathSizeVec(pp)=tempsize(tempsize~=T); % Get the dimension which is not T - for ii=1:N_i - N_j_temp=N_j.(Names_i{ii}); - if ~any(PricePathSizeVec(pp)==[1,N_i,N_j_temp]) - error(['PricePath for ', PricePathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) - end + for pp=1:length(PricePathNames) + if isstruct(PricePathStruct.(PricePathNames{pp})) + tempptypenames=fieldnames(PricePathStruct.(PricePathNames{pp})); + temp=PricePathStruct.(PricePathNames{pp}).(tempptypenames{1}); + tempsize=size(temp); + PricePathSizeVec(pp)=length(tempptypenames)*tempsize(tempsize~=T); % Get the dimension which is not T + for ii=1:N_i + N_j_temp=N_j.(Names_i{ii}); + if isfinite(N_j_temp) && ~any(PricePathSizeVec(pp)==[1,N_i,N_j_temp,N_i*N_j_temp]) + error(['PricePath for ', PricePathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) end end - end - else - for pp=1:length(PricePathNames) - if isstruct(PricePathStruct.(PricePathNames{pp})) - tempptypenames=fieldnames(PricePathStruct.(PricePathNames{pp})); - temp=PricePathStruct.(PricePathNames{pp}).(tempptypenames{1}); - tempsize=size(temp); - PricePathSizeVec(pp)=length(tempptypenames)*tempsize(tempsize~=T); % Get the dimension which is not T - for ii=1:N_i - if ~any(PricePathSizeVec(pp)==[1,N_i,N_j,N_i*N_j]) - error(['PricePath for ', PricePathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) - end - end - else - temp=PricePathStruct.(PricePathNames{pp}); - tempsize=size(temp); - PricePathSizeVec(pp)=tempsize(tempsize~=T); % Get the dimension which is not T - for ii=1:N_i - if ~any(PricePathSizeVec(pp)==[1,N_i,N_j]) - error(['PricePath for ', PricePathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) - end + else + temp=PricePathStruct.(PricePathNames{pp}); + tempsize=size(temp); + PricePathSizeVec(pp)=tempsize(tempsize~=T); % Get the dimension which is not T + for ii=1:N_i + N_j_temp=N_j.(Names_i{ii}); + if isfinite(N_j_temp) && ~any(PricePathSizeVec(pp)==[1,N_i,N_j_temp]) + error(['PricePath for ', PricePathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) end end end diff --git a/TransitionPaths/Subcodes/inputsFindtplus1tminus1.m b/TransitionPaths/Subcodes/inputsFindtplus1tminus1.m index 6ba76f04..90d5ddd6 100644 --- a/TransitionPaths/Subcodes/inputsFindtplus1tminus1.m +++ b/TransitionPaths/Subcodes/inputsFindtplus1tminus1.m @@ -16,26 +16,19 @@ AggVarNames=fieldnames(FnsToEvaluate); PTypeAggVarNames=struct(); for ff=1:length(AggVarNames) - if ~isempty(Names_i) + if exist('Names_i','var') temp={}; for ii=1:length(Names_i) if isfield(FnsToEvaluate.(AggVarNames{ff}), Names_i{ii}) - if ~isfield(PTypeAggVarNames, Names_i{ii}) - PTypeAggVarNames.(Names_i{ii})={}; - PTypeFnInputNames.(Names_i{ii})={}; - end - PTypeAggVarNames.(Names_i{ii})=[PTypeAggVarNames.(Names_i{ii});AggVarNames{ff}]; temp=getAnonymousFnInputNames(FnsToEvaluate.(AggVarNames{ff}).(Names_i{ii})); - PTypeFnInputNames.(Names_i{ii})=[PTypeFnInputNames.(Names_i{ii});temp]; % Note, this will include the (d,aprime,a,z), but that is irrelevant to our current purposes break end end - if isempty(temp) + if length(temp)==0 error(['Could not find', AggVarNames{ff}, 'with PType in {', Names_i, '}']) end else temp=getAnonymousFnInputNames(FnsToEvaluate.(AggVarNames{ff})); - FnInputNames={FnInputNames{:},temp{:}}; % Note, this will include the (d,aprime,a,z), but that is irrelevant to our current purposes end tempninputs=length(temp); ninputs=ninputs+tempninputs; From 007dc9862858be36f38f6981dd8d3c024961f026 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 4 Mar 2026 10:07:29 +1300 Subject: [PATCH 06/90] Fix II1, IIone calcs for Gammatranspose Previous version got us our first graph, but then failed in later iterations. This seems to work more robustly. Now we need to figure out why our transition doesn't converge. Likely because I haven't let the GE solver do its job. (Fast was better than good while working out the code paths. Now we need to see what good really looks like...) --- .../PType/TransitionPath_InfHorz_PType_singlepath.m | 9 +++++++-- .../AgentDist_InfHorz_TPath_SingleStep_raw.m | 4 ---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m index 6fc25d28..ded09b03 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m @@ -55,8 +55,13 @@ AggVarsPath=zeros(length(FnsToEvaluate),T-1); % Precompute for later -II1=gpuArray(1:1:N_a*N_z); -IIones=ones(N_a*N_z,1,'gpuArray'); +if N_d>0 + II1=gpuArray(repmat(1:1:N_a*N_z,size(PolicyIndexesPath,1),1)); + IIones=ones(size(PolicyIndexesPath,1),N_a*N_z,1,'gpuArray'); +else + II1=gpuArray(1:1:N_a*N_z); + IIones=ones(N_a*N_z,1,'gpuArray'); +end for tt=1:T-1 diff --git a/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m b/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m index dfc7fbd7..66a1c395 100644 --- a/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m +++ b/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m @@ -8,10 +8,6 @@ % AgentDist is already sparse and on cpu -if length(size(Policy_aprimez))>2 - II1=repmat(II1,2,1); - IIones=repmat(IIones,2,1); -end Gammatranspose=sparse(gather(Policy_aprimez),II1,IIones,N_a*N_z,N_a*N_z); % Two steps of the Tan improvement From efeb2232ff844e39e8e3e884daba7d57b6a8a09c Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 4 Mar 2026 11:44:50 +1300 Subject: [PATCH 07/90] More mixed horizon fixes First we fixed everything through `TransitionPath_Case1_FHorz_PType` This change fixes everything through `AgentDistOnTransPath_Case1_FHorz_PType` and `EvalFnOnTransPath_AggVars_Case1_FHorz_PType`. --- .../EvalFnOnTransPath_AggVars_InfHorz.m | 12 +++++++++ ...lFnOnTransPath_AggVars_Case1_FHorz_PType.m | 7 +++-- .../AgentDistOnTransPath_Case1_FHorz_PType.m | 26 ++++++++++++------- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m b/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m index e280a1f6..e620407d 100644 --- a/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m +++ b/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m @@ -1,6 +1,18 @@ function AggVarsPath=EvalFnOnTransPath_AggVars_InfHorz(FnsToEvaluate,AgentDistPath,PolicyPath,PricePath,ParamPath, Parameters, T, n_d, n_a, n_z, d_grid, a_grid,z_grid,simoptions) % AggVarsPath is T periods long (periods 0 (before the reforms are announced) & T are the initial and final values). +%% Check which transpathoptions have been used, set all others to defaults +if exist('transpathoptions','var')==0 + disp('No transpathoptions given, using defaults') + %If transpathoptions is not given, just use all the defaults + transpathoptions.verbose=0; +else + %Check transpathoptions for missing fields, if there are some fill them with the defaults + if ~isfield(transpathoptions,'verbose') + transpathoptions.verbose=0; + end +end + if ~exist('simoptions','var') % If simoptions is not given, just use all the defaults simoptions.experienceasset=0; diff --git a/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m b/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m index 73201f17..a760775b 100644 --- a/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m +++ b/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m @@ -184,8 +184,11 @@ [FnsToEvaluate_temp,~, ~,~]=PType_FnsToEvaluate(FnsToEvaluate,Names_i,ii,l_d_temp,l_a_temp,l_z_temp,0); - AggVarsPath_ii=EvalFnOnTransPath_AggVars_Case1_FHorz(FnsToEvaluate_temp, AgentDistPath_temp, PolicyPath_temp, PricePath_temp, ParamPath_temp, Parameters_temp, T, n_d_temp, n_a_temp, n_z_temp, N_j_temp, d_grid_temp, a_grid_temp,z_grid_temp, transpathoptions_temp, simoptions_temp); - + if isfinite(N_j_temp) + AggVarsPath_ii=EvalFnOnTransPath_AggVars_Case1_FHorz(FnsToEvaluate_temp, AgentDistPath_temp, PolicyPath_temp, PricePath_temp, ParamPath_temp, Parameters_temp, T, n_d_temp, n_a_temp, n_z_temp, N_j_temp, d_grid_temp, a_grid_temp,z_grid_temp, transpathoptions_temp, simoptions_temp); + else + AggVarsPath_ii=EvalFnOnTransPath_AggVars_Case1(FnsToEvaluate_temp, AgentDistPath_temp, PolicyPath_temp, PricePath_temp, ParamPath_temp, Parameters_temp, T, n_d_temp, n_a_temp, n_z_temp, d_grid_temp, a_grid_temp,z_grid_temp, transpathoptions_temp, simoptions_temp); + end FnNames_temp=fieldnames(FnsToEvaluate_temp); for ff=1:length(FnNames_temp) diff --git a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m index d1851e16..37708531 100644 --- a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m +++ b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m @@ -129,15 +129,17 @@ else AgentDist_initial_temp=AgentDist_initial; % NEED TO DEAL WITH THIS PROPERLY end - if isstruct(AgeWeightsParamNames) - AgeWeightsParamNames_temp=AgeWeightsParamNames.(Names_i{ii}); - else - AgeWeightsParamNames_temp=AgeWeightsParamNames; - end - if isstruct(jequalOneDist) - jequalOneDist_temp=jequalOneDist.(iistr); - else - jequalOneDist_temp=jequalOneDist; + if isfinite(N_j_temp) + if isstruct(AgeWeightsParamNames) + AgeWeightsParamNames_temp=AgeWeightsParamNames.(Names_i{ii}); + else + AgeWeightsParamNames_temp=AgeWeightsParamNames; + end + if isstruct(jequalOneDist) + jequalOneDist_temp=jequalOneDist.(Names_i{ii}); + else + jequalOneDist_temp=jequalOneDist; + end end @@ -177,7 +179,11 @@ % Compute the agent distribution path for permanent type ii - AgentDistPath_ii=AgentDistOnTransPath_Case1_FHorz(AgentDist_initial_temp, jequalOneDist_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, AgeWeightsParamNames_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,pi_z_temp, T,Parameters_temp, transpathoptions_temp, simoptions_temp); + if isfinite(N_j_temp) + AgentDistPath_ii=AgentDistOnTransPath_Case1_FHorz(AgentDist_initial_temp, jequalOneDist_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, AgeWeightsParamNames_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,pi_z_temp, T,Parameters_temp, transpathoptions_temp, simoptions_temp); + else + AgentDistPath_ii=AgentDistOnTransPath_Case1(AgentDist_initial_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, n_d_temp,n_a_temp,n_z_temp,pi_z_temp,T,Parameters_temp,simoptions_temp); + end % Note: T cannot depend on ptype, nor can PricePath depend on ptype AgentDistPath.(Names_i{ii})=AgentDistPath_ii; From dd8f4663c44e7fc1e608a05f4d13ee857cfafa76 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Thu, 5 Mar 2026 21:49:16 +1300 Subject: [PATCH 08/90] Don't try to look for age-shaped vectors when InfHorz Because this file handles general PType structures (even though it's name doesn't say so), we should not look for age-like shapes when `N_j_temp` is `Inf`. --- .../Subcodes/PricePathParamPath_FHorz_StructToMatrix.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m b/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m index 204c0052..ca780736 100644 --- a/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m +++ b/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m @@ -146,7 +146,7 @@ ParamPathSizeVec(pp)=length(tempptypenames)*tempsize(tempsize~=T); % Get the dimension which is not T for ii=1:N_i N_j_temp=N_j.(Names_i{ii}); - if ~any(ParamPathSizeVec(pp)==[1,N_i,N_j_temp,N_i*N_j_temp]) + if isfinite(N_j_temp) && ~any(ParamPathSizeVec(pp)==[1,N_i,N_j_temp,N_i*N_j_temp]) error(['ParamPath for ', ParamPathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) end end @@ -156,7 +156,7 @@ ParamPathSizeVec(pp)=tempsize(tempsize~=T); % Get the dimension which is not T for ii=1:N_i N_j_temp=N_j.(Names_i{ii}); - if ~any(ParamPathSizeVec(pp)==[1,N_i,N_j_temp]) + if isfinite(N_j_temp) && ~any(ParamPathSizeVec(pp)==[1,N_i,N_j_temp]) error(['ParamPath for ', ParamPathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) end end From 92143bb74d8ce48890e82e12803cc9eb82d315db Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 6 Mar 2026 22:31:19 +1300 Subject: [PATCH 09/90] Update vestigial `transpathoptions.maxiterations` to maxiter There were a few vestigial uses of `transpathoptions.maxiterations` where most of the code uses `transpathoptions.maxiter`. All are now consistent. --- .../StockVar/TransitionPath_Case1_FHorz_StockVar_shooting.m | 2 +- .../TransitionPath_Case1_FHorz_StockVar_shooting_fastOLG.m | 2 +- .../InfHorz/PType/TransitionPath_InfHorz_PType.m | 6 +++--- .../InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/TransitionPaths/FHorz/StockVar/TransitionPath_Case1_FHorz_StockVar_shooting.m b/TransitionPaths/FHorz/StockVar/TransitionPath_Case1_FHorz_StockVar_shooting.m index 9cca4f9e..37a19c9b 100644 --- a/TransitionPaths/FHorz/StockVar/TransitionPath_Case1_FHorz_StockVar_shooting.m +++ b/TransitionPaths/FHorz/StockVar/TransitionPath_Case1_FHorz_StockVar_shooting.m @@ -130,7 +130,7 @@ StockVarsPathNew=zeros(size(StockVarsPathOld)); %% -while max(PricePathDist,StockVarsPathDist)>transpathoptions.tolerance && pathcountertranspathoptions.tolerance && pathcounter0 PolicyIndexesPath=zeros(2,N_a,N_z,N_j,T-1,'gpuArray'); %Periods 1 to T-1 else diff --git a/TransitionPaths/FHorz/StockVar/TransitionPath_Case1_FHorz_StockVar_shooting_fastOLG.m b/TransitionPaths/FHorz/StockVar/TransitionPath_Case1_FHorz_StockVar_shooting_fastOLG.m index 3587f047..567b8fb3 100644 --- a/TransitionPaths/FHorz/StockVar/TransitionPath_Case1_FHorz_StockVar_shooting_fastOLG.m +++ b/TransitionPaths/FHorz/StockVar/TransitionPath_Case1_FHorz_StockVar_shooting_fastOLG.m @@ -141,7 +141,7 @@ StockVarsPathNew=zeros(size(StockVarsPathOld)); %% -while max(PricePathDist,StockVarsPathDist)>transpathoptions.tolerance && pathcountertranspathoptions.tolerance && pathcounter0 PolicyIndexesPath=zeros(2,N_a,N_z,N_j,T-1,'gpuArray'); %Periods 1 to T-1 else diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m index 7104b382..6393b470 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m @@ -15,7 +15,7 @@ transpathoptions.oldpathweight=0.9; % default =0.9 transpathoptions.weightscheme=1; % default =1 transpathoptions.Ttheta=1; - transpathoptions.maxiterations=500; % Based on personal experience anything that hasn't converged well before this is just hung-up on trying to get the 4th decimal place (typically because the number of grid points was not large enough to allow this level of accuracy). + transpathoptions.maxiter=500; % Based on personal experience anything that hasn't converged well before this is just hung-up on trying to get the 4th decimal place (typically because the number of grid points was not large enough to allow this level of accuracy). transpathoptions.verbose=0; transpathoptions.verbosegraphs=0; transpathoptions.graphpricepath=0; % 1: creates a graph of the 'current' price path which updates each iteration. @@ -42,8 +42,8 @@ if isfield(transpathoptions,'Ttheta')==0 transpathoptions.Ttheta=1; end - if isfield(transpathoptions,'maxiterations')==0 - transpathoptions.maxiterations=500; + if isfield(transpathoptions,'maxiter')==0 + transpathoptions.maxiter=500; end if isfield(transpathoptions,'verbose')==0 transpathoptions.verbose=0; diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m index 5099bcc0..b971a43c 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m @@ -38,7 +38,7 @@ %% Iterate on the transition path PricePathDist=Inf; pathcounter=1; -while PricePathDist>transpathoptions.tolerance && pathcountertranspathoptions.tolerance && pathcounter Date: Sun, 8 Mar 2026 11:53:12 +1300 Subject: [PATCH 10/90] Rework guts of TransitionPath_InfHorz_PType_singlepath; PType support for _tminus1 AggVars Replaced the previous DNA transplant with better DNA transplant. In addition to sorting out the correct shapes of the Policies so that AgentDist did not immediately diverge, also added PType support for AggVars using _tminus1 functionality. --- .../PType/TransitionPath_Case1_FHorz_PType.m | 44 ++--- ...ransitionPath_Case1_FHorz_PType_shooting.m | 43 ++++- .../TransitionPath_InfHorz_PType_singlepath.m | 152 ++++++++++++++---- .../Subcodes/inputsFindtplus1tminus1.m | 11 +- 4 files changed, 189 insertions(+), 61 deletions(-) diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m index 2898342d..bf9e5f88 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m @@ -186,11 +186,11 @@ if isstruct(GeneralEqmEqns) if length(PricePathNames)~=length(fieldnames(GeneralEqmEqns)) fprintf('length(PricePathNames)=%i and length(fieldnames(GeneralEqmEqns))=%i (relates to following error) \n', length(PricePathNames), length(fieldnames(GeneralEqmEqns))) - error('Initial PricePath contains less variables than GeneralEqmEqns (structure) \n') + error('Initial PricePath contains fewer variables than GeneralEqmEqns (structure) \n') end else if length(PricePathNames)~=length(GeneralEqmEqns) - error('Initial PricePath contains less variables than GeneralEqmEqns') + error('Initial PricePath contains fewer variables than GeneralEqmEqns') end end @@ -515,19 +515,8 @@ %% Organise V_final and AgentDist_initial % Reshape V_final if ~isfinite(PTypeStructure.(iistr).N_j) - if N_z==0 - if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,1]); - else - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_e]); - end - else - if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z]); - else - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_e]); - end - end + % If no z, then N_z=1 here + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z]); elseif transpathoptions.fastOLG==0 N_j_temp=PTypeStructure.(iistr).N_j; if N_z==0 @@ -776,9 +765,15 @@ %% If using a shooting algorithm, set that up transpathoptions=setupGEnewprice3_shooting(transpathoptions,GeneralEqmEqns,PricePathNames,N_i,PricePathSizeVec); -%% Check if using _tminus1 and/or _tplus1 variables. +%% Check if using _tminus1 and/or _tplus1 variables, and update PTypeStructure if isstruct(FnsToEvaluate) && isstruct(GeneralEqmEqns) [tplus1priceNames,tminus1priceNames,tminus1AggVarsNames,tminus1paramNames,tplus1pricePathkk]=inputsFindtplus1tminus1(FnsToEvaluate,GeneralEqmEqns,PricePathNames,ParamPathNames,Names_i); + if isstruct(tminus1AggVarsNames) + AggVarsPTypes=fieldnames(tminus1AggVarsNames); + for ii=1:length(AggVarsPTypes) + PTypeStructure.(AggVarsPTypes{ii}).tminus1AggVarsNames=tminus1AggVarsNames.(AggVarsPTypes{ii}); + end + end else tplus1priceNames=[]; tminus1priceNames=[]; @@ -812,9 +807,20 @@ use_tminus1AggVars=0; if ~isempty(tminus1AggVarsNames) use_tminus1AggVars=1; - for ii=1:length(tminus1AggVarsNames) - if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames{ii}) - error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames{ii}) + if isstruct(tminus1AggVarsNames) + AggVarsPTypes=fieldnames(tminus1AggVarsNames); + for nn=1:length(AggVarsPTypes) + for ii=1:length(tminus1AggVarsNames.(AggVarsPTypes{nn})) + if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames.(AggVarsPTypes{nn}){ii}) + error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames{ii}) + end + end + end + else + for ii=1:length(tminus1AggVarsNames) + if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames{ii}) + error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames{ii}) + end end end end diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m index 81c3b64a..f8ba00bc 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m @@ -128,10 +128,11 @@ % After that is finished we can put the AggVars together, evaluate GE conditions, and update price path AggVarsFullPath=zeros(PTypeStructure.numFnsToEvaluate,T-1,N_i); % Does not include period T for ii=1:N_i - iistr=PTypeStructure.Names_i{ii}; - + iistr=PTypeStructure.Names_i{ii}; + % Following few lines I would normally do outside of the while loop, but have to set them for each ptype % AgentDist=AgentDist_initial.(iistr); + % WARNING: The following would overwrite themselves next iteration % V_final=V_final.(iistr); % AgeWeights_T=AgeWeights_T.(iistr); % jequalOneDist_T=jequalOneDist_T.(iistr); @@ -253,8 +254,24 @@ %% Do the general eqm conditions and create PricePathNew based on these if all(transpathoptions.GEptype==0) - GEcondnPath=zeros(T,length(GEeqnNames)); - + GECondnPath=zeros(T,length(GEeqnNames)); + + warning("do we need to do this for AggVars?") + % Restore all AggVarNames and tminus1AggVarsNames for GEeqns + AggVarNames=cell(1,N_i); + tminus1AggVarsNames=cell(1,N_i); + use_tminus1AggVars=0; + for ii=1:N_i + iistr=PTypeStructure.Names_i{ii}; + AggVarNames{ii}=PTypeStructure.(iistr).AggVarNames; + if isfield(PTypeStructure.(iistr), 'tminus1AggVarsNames') + use_tminus1AggVars=1; + tminus1AggVarsNames{ii}=PTypeStructure.(iistr).tminus1AggVarsNames; + end + end + AggVarNames=vertcat(AggVarNames{:}); + tminus1AggVarsNames=vertcat(tminus1AggVarsNames{:}); + % Parameters that may be relevant to General Eqm Parameters=PTypeStructure.ParametersRaw; @@ -332,7 +349,23 @@ end % Done loop over tt, evaluating the GE conditions else % Some GE conditions depend on PType - GEcondnPath=zeros(T,nGeneralEqmEqns_acrossptypes); + GECondnPath=zeros(T,nGeneralEqmEqns_acrossptypes); + error("need to fit these loops together") + + % Restore AggVarNames and tminus1AggVarsNames by PType for GEeqns + AggVarNames=cell(1,N_i); + tminus1AggVarsNames=cell(1,N_i); + use_tminus1AggVars=0; + for ii=1:N_i + iistr=PTypeStructure.Names_i{ii}; + AggVarNames{ii}=PTypeStructure.(iistr).AggVarNames; + tminus1AggVarsNames{ii}=PTypeStructure.(iistr).tminus1AggVarsNames; + if ~isempty(tminus1AggVarsNames{ii}) + use_tminus1AggVars=1; + end + end + AggVarNames=vertcat(AggVarNames{:}); + tminus1AggVarsNames=vertcat(tminus1AggVarsNames{:}); % Parameters that may be relevant to General Eqm Parameters=PTypeStructure.ParametersRaw; diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m index ded09b03..d8899f78 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m @@ -1,8 +1,8 @@ function AggVarsPath=TransitionPath_InfHorz_PType_singlepath(PricePathOld, ParamPath, PricePathNames, ParamPathNames, T, V_final, AgentDist_initial, ... l_d,N_d,n_d,N_a,n_a,N_z,n_z,d_grid,a_grid,d_gridvals,aprime_gridvals,a_gridvals,z_grid,pi_z,ReturnFn, ... FnsToEvaluate, ... - Parameters, DiscountFactorParamNames, ReturnFnParamNames, FnsToEvaluateParamNames, AggVarNames, ... - ... % use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, ... + Parameters, DiscountFactorParamNames, ReturnFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, ... + use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, ... transpathoptions, vfoptions, simoptions) % PricePathOld is matrix of size T-by-'number of prices' % ParamPath is matrix of size T-by-'number of parameters that change over path' @@ -12,24 +12,40 @@ % For this agent type, first go back through the value & policy fns. % Then forwards through agent dist and agg vars. if N_d>0 + l_d=length(n_d); PolicyIndexesPath=zeros(2,N_a,N_z,T-1,'gpuArray'); %Periods 1 to T-1 else + l_d=0; PolicyIndexesPath=zeros(N_a,N_z,T-1,'gpuArray'); %Periods 1 to T-1 end +% This and much of the rest of this code borrowed from TransitionPath_InfHorz_shooting.m +if simoptions.gridinterplayer==0 + II1=(1:1:N_a*N_z); % Index for this period (a,z) + IIones=ones(N_a*N_z,1); % Next period 'probabilities' +elseif simoptions.gridinterplayer==1 + PolicyProbsPath=zeros(N_a*N_z,2,T-1,'gpuArray'); % preallocate + II2=([1:1:N_a*N_z; 1:1:N_a*N_z]'); % Index for this period (a,z), note the 2 copies +end + +if size(V_final)~=[N_a,N_z] + error("V_final wrong shape") + V_final=reshape(V_final,[N_a,N_z]); +end + %First, go from T-1 to 1 calculating the Value function and Optimal %policy function at each step. Since we won't need to keep the value %functions for anything later we just store the next period one in %Vnext, and the current period one to be calculated in V Vnext=V_final; -for tt=1:T-1 %so t=T-i +for ttr=1:T-1 %so t=T-i % The following digs deeper into PricePathOld and ParamPath in % FHorz case--check it for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(T-tt,kk); + Parameters.(PricePathNames{kk})=PricePathOld(T-ttr,kk); end for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(T-tt,kk); + Parameters.(ParamPathNames{kk})=ParamPath(T-ttr,kk); end [V, Policy]=ValueFnIter_InfHorz_TPath_SingleStep(Vnext,n_d,n_a,n_z,d_grid, a_grid, z_grid, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); @@ -37,9 +53,9 @@ % Policy is kept in the form where it is just a single-value in (d,a') if N_d>0 - PolicyIndexesPath(:,:,:,T-tt)=Policy; + PolicyIndexesPath(:,:,:,T-ttr)=Policy; else - PolicyIndexesPath(:,:,T-tt)=Policy; + PolicyIndexesPath(:,:,T-ttr)=Policy; end Vnext=V; @@ -47,45 +63,111 @@ % Free up space on GPU by deleting things no longer needed clear V Vnext +%% Modify PolicyIndexesPath into forms needed for forward iteration +% Create version of PolicyIndexesPath in form we want for the agent distribution iteration +% Creates PolicyaprimezPath, and when using grid interpolation layer also PolicyProbsPath +if isscalar(n_a) + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:),[N_a*N_z,T-1]); % aprime index +elseif length(n_a)==2 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1),[N_a*N_z,T-1]); +elseif length(n_a)==3 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+n_a(1)*n_a(2)*(PolicyIndexesPath(l_d+3,:,:,:)-1),[N_a*N_z,T-1]); +elseif length(n_a)==4 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+n_a(1)*n_a(2)*(PolicyIndexesPath(l_d+3,:,:,:)-1)+n_a(1)*n_a(2)*n_a(3)*(PolicyIndexesPath(l_d+4,:,:,:)-1),[N_a*N_z,T-1]); +end +PolicyaprimezPath=PolicyaprimePath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1); +if simoptions.gridinterplayer==1 + PolicyaprimezPath=reshape(PolicyaprimezPath,[N_a*N_z,1,T-1]); % reinterpret this as lower grid index + PolicyaprimezPath=repelem(PolicyaprimezPath,1,2,1); % create copy that will be the upper grid index + PolicyaprimezPath(:,2,:)=PolicyaprimezPath(:,2,:)+1; % upper grid index + PolicyProbsPath(:,2,:)=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,:),[N_a*N_z,1,T-1]); % L2 index + PolicyProbsPath(:,2,:)=(PolicyProbsPath(:,2,:)-1)/(1+simoptions.ngridinterp); % probability of upper grid point + PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point +end +% Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats +PolicyValuesPath=PolicyInd2Val_InfHorz_TPath(PolicyIndexesPath,n_d,n_a,n_z,T-1,d_grid,a_grid,vfoptions,1); +PolicyValuesPath=permute(reshape(PolicyValuesPath,[size(PolicyValuesPath,1),N_a,N_z,T-1]),[2,3,1,4]); %[N_a,N_z,l_d+l_a,T-1] + %Now we have the full PolicyIndexesPath, we go forward in time from 1 %to T using the policies to update the agents distribution generating a %new price path %Call AgentDist the current periods distn -AgentDist=AgentDist_initial; -AggVarsPath=zeros(length(FnsToEvaluate),T-1); -% Precompute for later -if N_d>0 - II1=gpuArray(repmat(1:1:N_a*N_z,size(PolicyIndexesPath,1),1)); - IIones=ones(size(PolicyIndexesPath,1),N_a*N_z,1,'gpuArray'); -else - II1=gpuArray(1:1:N_a*N_z); - IIones=ones(N_a*N_z,1,'gpuArray'); -end +AgentDist=sparse(gather(reshape(AgentDist_initial,[N_a*N_z,1]))); +AggVarsPath=zeros(length(FnsToEvaluate),T-1); +pi_z_sparse=sparse(gather(pi_z)); % Need full pi_z for value fn, and sparse for agent dist for tt=1:T-1 - - %Get the current optimal policy - if N_d>0 - Policy=PolicyIndexesPath(:,:,:,tt); - else - Policy=PolicyIndexesPath(:,:,tt); + %% Setup the Parameters for period tt + + % Get t-1 PricePath and ParamPath before we update them + if use_tminus1price==1 + for pp=1:length(tminus1priceNames) + if tt>1 + Parameters.([tminus1priceNames{pp},'_tminus1'])=Parameters.(tminus1priceNames{pp}); + else + Parameters.([tminus1priceNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1priceNames{pp}); + end + end end - - GEprices=PricePathOld(tt,:); - - % Again, since we don't do tminus1, don't dig as deep as FHorz - for nn=1:length(ParamPathNames) - Parameters.(ParamPathNames{nn})=ParamPath(tt,nn); + if use_tminus1params==1 + for pp=1:length(tminus1paramNames) + if tt>1 + Parameters.([tminus1paramNames{pp},'_tminus1'])=Parameters.(tminus1paramNames{pp}); + else + Parameters.([tminus1paramNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1paramNames{pp}); + end + end end - for nn=1:length(PricePathNames) - Parameters.(PricePathNames{nn})=PricePathOld(tt,nn); + % Get t-1 AggVars before we update them + if use_tminus1AggVars==1 + for pp=1:length(tminus1AggVarsNames) + if tt>1 + % The AggVars have not yet been updated, so they still contain previous period values + Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); + else + Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); + end + end + end + + % Update current PricePath and ParamPath + for kk=1:length(PricePathNames) + Parameters.(PricePathNames{kk})=PricePathOld(tt,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); + end + for kk=1:length(ParamPathNames) + Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); + end + + % Get t+1 PricePath + if use_tplus1price==1 + for pp=1:length(tplus1priceNames) + kk=tplus1pricePathkk(pp); + Parameters.([tplus1priceNames{pp},'_tplus1'])=PricePathOld(tt+1,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); % Make is so that the time t+1 variables can be used + end end - PolicyUnKron=UnKronPolicyIndexes_Case1(Policy, n_d, n_a, n_z,vfoptions); - AggVars=EvalFnOnAgentDist_AggVars_Case1(AgentDist, PolicyUnKron, FnsToEvaluate, Parameters, FnsToEvaluateParamNames, n_d, n_a, n_z, d_grid, a_grid, z_grid, simoptions); % The 2 is for Parallel (use GPU) - - AgentDist=AgentDist_InfHorz_TPath_SingleStep(AgentDist,Policy,II1,IIones,N_a,N_z,sparse(pi_z)); + %% Get the current optimal policy, and iterate the agent dist + if simoptions.gridinterplayer==0 + AgentDistnext=AgentDist_InfHorz_TPath_SingleStep(AgentDist,PolicyaprimezPath(:,tt),II1,IIones,N_a,N_z,pi_z_sparse); + elseif simoptions.gridinterplayer==1 + AgentDistnext=AgentDist_InfHorz_TPath_SingleStep_nProbs(AgentDist,PolicyaprimezPath(:,:,tt),II2,PolicyProbsPath(:,:,tt),N_a,N_z,pi_z_sparse); + end + + %% AggVars + AggVars_Means=EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars(gpuArray(full(AgentDist)), PolicyValuesPath(:,:,:,tt), FnsToEvaluate, Parameters, FnsToEvaluateParamNames, AggVarNames, n_a, n_z, a_gridvals, z_grid,1); + AggVars=zeros(length(AggVars_Means),1); + if length(fieldnames(AggVars_Means))~=length(AggVarNames) + error(["AggVar length disparity:";"---------";AggVarNames;"--- vs ---";fieldnames(AggVars_Means)]); + end + for ii=1:length(AggVarNames) + AggVars(ii)=AggVars_Means.(AggVarNames{ii}).Mean; + Parameters.(AggVarNames{ii})=AggVars(ii); + end + + % Do nothing with IntermediateEqns and GeneralEqmEqns as they are outside PType scope (they are where all the PTypes meet). + + AgentDist=AgentDistnext; AggVarsPath(:,tt)=AggVars; end diff --git a/TransitionPaths/Subcodes/inputsFindtplus1tminus1.m b/TransitionPaths/Subcodes/inputsFindtplus1tminus1.m index 90d5ddd6..6ba76f04 100644 --- a/TransitionPaths/Subcodes/inputsFindtplus1tminus1.m +++ b/TransitionPaths/Subcodes/inputsFindtplus1tminus1.m @@ -16,19 +16,26 @@ AggVarNames=fieldnames(FnsToEvaluate); PTypeAggVarNames=struct(); for ff=1:length(AggVarNames) - if exist('Names_i','var') + if ~isempty(Names_i) temp={}; for ii=1:length(Names_i) if isfield(FnsToEvaluate.(AggVarNames{ff}), Names_i{ii}) + if ~isfield(PTypeAggVarNames, Names_i{ii}) + PTypeAggVarNames.(Names_i{ii})={}; + PTypeFnInputNames.(Names_i{ii})={}; + end + PTypeAggVarNames.(Names_i{ii})=[PTypeAggVarNames.(Names_i{ii});AggVarNames{ff}]; temp=getAnonymousFnInputNames(FnsToEvaluate.(AggVarNames{ff}).(Names_i{ii})); + PTypeFnInputNames.(Names_i{ii})=[PTypeFnInputNames.(Names_i{ii});temp]; % Note, this will include the (d,aprime,a,z), but that is irrelevant to our current purposes break end end - if length(temp)==0 + if isempty(temp) error(['Could not find', AggVarNames{ff}, 'with PType in {', Names_i, '}']) end else temp=getAnonymousFnInputNames(FnsToEvaluate.(AggVarNames{ff})); + FnInputNames={FnInputNames{:},temp{:}}; % Note, this will include the (d,aprime,a,z), but that is irrelevant to our current purposes end tempninputs=length(temp); ninputs=ninputs+tempninputs; From 43cb440d2bc838f10222da0a517a8be4b1aa1416 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sun, 8 Mar 2026 13:03:04 +1300 Subject: [PATCH 11/90] Fix code drift in setupGEnewprice3_shooting.m Fix the for loop structure attempting to align `GeneralEqmEqns` (using iterator `gg`) with `GEPriceParamNames` (using iterator `pp`) via the table join `options.GEnewprice3.howtoupdate` (using iterator `gg2`). I don't quite understand how the `PricePathSizeVec` is constructed, so don't know how to make a test case that creates the expected `N_i` spacing. But even without the test case, I think this loop works as intended, whereas the previous one did not. --- Optimization/setupGEnewprice3_shooting.m | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Optimization/setupGEnewprice3_shooting.m b/Optimization/setupGEnewprice3_shooting.m index 8cd3929a..c3ca2ea7 100644 --- a/Optimization/setupGEnewprice3_shooting.m +++ b/Optimization/setupGEnewprice3_shooting.m @@ -93,13 +93,14 @@ for gg2=1:size(options.GEnewprice3.howtoupdate,1) if strcmp(options.GEnewprice3.howtoupdate{gg2,1},GEeqnNames{gg}) pricename_gg=options.GEnewprice3.howtoupdate{gg2,2}; - end - end - for pp=1:length(GEPriceParamNames) - if strcmp(pricename_gg,GEPriceParamNames{pp}) - if PricePathSizeVec(2,pp)-PricePathSizeVec(1,pp)+1~=N_i - fprintf('Following error relates to GE condition %s and to price %s \n',GEeqnNames{gg},GEPriceParamNames{pp}) - error('You declared a GE condition to depend on permenent type, but the price that relates to it (in options.GEnewprice3.howtoupdate) does not depend on ptype') + for pp=1:length(GEPriceParamNames) + if strcmp(pricename_gg,GEPriceParamNames{pp}) + if PricePathSizeVec(2,pp)-PricePathSizeVec(1,pp)+1~=N_i + fprintf('Following error relates to GE condition %s and to price %s \n',GEeqnNames{gg},GEPriceParamNames{pp}) + error('You declared a GE condition to depend on permenent type, but the price that relates to it (in options.GEnewprice3.howtoupdate) does not depend on ptype') + end + break + end end end end From c174d23951d1a5c2341a2c87dcbe993c0703aa0b Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Mon, 9 Mar 2026 10:19:22 +1300 Subject: [PATCH 12/90] Fixed AgeWeights merge and other fixes When I attempted to merge TransitionPath_Case1_FHorz, I misunderstood a detail about reshaping arrays and the way that AgentDist_initial interacted with AgeWeights_initial. Now sorted. Also fixed some other small but obvious mistakes (my own and others'). --- .../FHorz/PType/TransitionPath_Case1_FHorz_PType.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m index bf9e5f88..c3be75cb 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m @@ -512,7 +512,7 @@ % Convert z and e to age-dependent joint-grids and transtion matrix % output: z_gridvals_J, pi_z_J, e_gridvals_J, pi_e_J, transpathoptions,vfoptions,simoptions - %% Organise V_final and AgentDist_initial + %% Organise V_final, AgeWeights and AgentDist_init % Reshape V_final if ~isfinite(PTypeStructure.(iistr).N_j) % If no z, then N_z=1 here @@ -676,7 +676,9 @@ end PTypeStructure.(iistr).jequalOneDist=jequalOneDist_temp; end - + AgentDist_initial.(iistr)=AgentDist_init; + clear AgentDist_init + %% Which parts of ParamPath and PricePath relate to ptype ii % Some ParamPath and PricePath parameters may depend on ptype PTypeStructure.(iistr).RelevantPricePath=ones(1,size(PricePath0,2)); % start will all relevant From 85c48549b0a195b4aa983f0d2c9c4f8b9601cb5d Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Mon, 9 Mar 2026 12:41:46 +1300 Subject: [PATCH 13/90] Use ttr as varname walking backward from V_final Make these three files consistent with all others in terms of using `ttr` as the iterator name calculating the value function and optimal policy at each step from T-1 to 1. Later, when we change this to actually stepping backwards, all the `ttr`s should be changed at the same time to start at T-1, walking backwards, with `ttr` instead of `T-ttr` used within the loop. --- .../PType/TransitionPath_InfHorz_PType_shooting.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m index b971a43c..5a2c905b 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m @@ -75,14 +75,14 @@ %functions for anything later we just store the next period one in %Vnext, and the current period one to be calculated in V Vnext=V_final.(iistr); - for tt=1:T-1 %so t=T-i + for ttr=1:T-1 %so t=T-i for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(T-tt,kk); + Parameters.(PricePathNames{kk})=PricePathOld(T-ttr,kk); end for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(T-tt,kk); + Parameters.(ParamPathNames{kk})=ParamPath(T-ttr,kk); end [V, Policy]=ValueFnIter_Case1_TPath_SingleStep(Vnext,n_d,n_a,n_z,d_grid, a_grid, z_grid, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); @@ -90,9 +90,9 @@ % Policy is kept in the form where it is just a single-value in (d,a') if N_d>0 - PolicyIndexesPath(:,:,:,T-tt)=Policy; + PolicyIndexesPath(:,:,:,T-ttr)=Policy; else - PolicyIndexesPath(:,:,T-tt)=Policy; + PolicyIndexesPath(:,:,T-ttr)=Policy; end Vnext=V; From 78abb8733273e85ba6e0d6a476225800066d6ba2 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 13 Mar 2026 06:12:03 +1300 Subject: [PATCH 14/90] Fix typos Fix some typos in comments. --- .../ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_raw.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_raw.m index 90b9999e..bcff857e 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_raw.m @@ -79,7 +79,7 @@ skipinterp=(Vlower==Vupper); a2primeProbs(skipinterp)=0; % effectively skips interpolation - % Switch EV from being in terps of a2prime to being in terms of d2 and a2 + % Switch EV from being in terms of a2prime to being in terms of d2 and a2 EV=a2primeProbs.*Vlower+(1-a2primeProbs).*Vupper; % (d2,a1prime,a2,u,zprime) ReturnMatrix=CreateReturnFnMatrix_Case2_Disc_noz_Par2(ReturnFn,n_d2, n_a2, d2_gridvals, a2_grid, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command From bd87f3e8b8b74915fdadf409578d3ad3041e84cd Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 13 Mar 2026 06:14:18 +1300 Subject: [PATCH 15/90] Efficient z reshape There are many places where Z is tested against 0 for reshaping purposes. It could equivalently be testing against <2, because the reshape for z==1 is the same as for z==0. If this is accepted, there are many other cases that could be similarly changed. --- aprimeFnMatrix/CreateaprimePolicyExperienceAsset.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aprimeFnMatrix/CreateaprimePolicyExperienceAsset.m b/aprimeFnMatrix/CreateaprimePolicyExperienceAsset.m index 0c1b2dfa..5b779ce1 100644 --- a/aprimeFnMatrix/CreateaprimePolicyExperienceAsset.m +++ b/aprimeFnMatrix/CreateaprimePolicyExperienceAsset.m @@ -114,7 +114,7 @@ %% Calcuate grid indexes and probs from the values -if N_z==0 +if N_z<2 a2primeVals=reshape(a2primeVals,[1,N_a]); else a2primeVals=reshape(a2primeVals,[1,N_a*N_z]); @@ -131,7 +131,7 @@ % Those points which tried to leave the top of the grid have probability 1 of the 'upper' point (0 of lower point) offTopOfGrid=(a2primeVals>=a2_grid(end)); a2primeIndexes(offTopOfGrid)=n_a2-1; % lower grid point is the one before the end point -if N_z==0 +if N_z<2 a2primeIndexes=reshape(a2primeIndexes,[N_a,1]); else a2primeIndexes=reshape(a2primeIndexes,[N_a*N_z,1]); @@ -145,7 +145,7 @@ a2primeProbs(offBottomOfGrid)=1; a2primeProbs(offTopOfGrid)=0; -if N_z==0 +if N_z<2 a2primeIndexes=reshape(a2primeIndexes,[N_a,1]); % Index of lower grid point a2primeProbs=reshape(a2primeProbs,[N_a,1]); % Probability of lower grid point else From 73b5fe1d9dac075b2a9b9f29c3966694ea3a7a57 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 13 Mar 2026 06:18:45 +1300 Subject: [PATCH 16/90] Enable reshaping StationaryDist with nans MATLAB 2025a supports reshaping sparse gpuarray, but not isnan(sparse(gpuarray)). So make full before allocating to gpuArray. --- .../InfHorz/EvalFnOnAgentDist_AggVars_InfHorz.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_AggVars_InfHorz.m b/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_AggVars_InfHorz.m index 916d7968..97dadf43 100644 --- a/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_AggVars_InfHorz.m +++ b/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_AggVars_InfHorz.m @@ -150,7 +150,7 @@ %% if simoptions.parallel==2 - StationaryDist=gpuArray(StationaryDist); + StationaryDist=gpuArray(full(StationaryDist)); % MATLAB 2025a supports reshaping sparse gpuarray, but not isnan(sparse(gpuarray)) Policy=gpuArray(Policy); n_d=gpuArray(n_d); n_a=gpuArray(n_a); From bcb2d554d49ab229e47ce9306d3f055ec27621e7 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 13 Mar 2026 06:20:02 +1300 Subject: [PATCH 17/90] Don't error early when searching for parameters in PTypes Don't give error about not finding a parameter until we've exhausted the PTypes looking for it. --- .../FHorz/PType/TransitionPath_Case1_FHorz_PType.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m index c3be75cb..16382bad 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m @@ -161,7 +161,9 @@ break end catch - error(['Failed to find parameter ', AgeWeightsParamNames.(Names_i{ii}){1}]) + if ii==N_i + error(['Failed to find parameter ', AgeWeightsParamNames.(Names_i{ii}){1}]) + end end end else From 9760f18cb0d9ce63a44b0576b7592ac16a9de355 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 13 Mar 2026 06:23:43 +1300 Subject: [PATCH 18/90] Fix case where n_z==1 When testing code with a trivial Z grid (only 1 single element), bugs were revealed in both the comments and the code. These changes fix that. They also take the liberty of unifying some common code. --- .../DC1/ValueFnIter_FHorz_DC1_raw.m | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/ValueFnIter/FHorz/DivideConquer/DC1/ValueFnIter_FHorz_DC1_raw.m b/ValueFnIter/FHorz/DivideConquer/DC1/ValueFnIter_FHorz_DC1_raw.m index 77f13291..d31fbb41 100644 --- a/ValueFnIter/FHorz/DivideConquer/DC1/ValueFnIter_FHorz_DC1_raw.m +++ b/ValueFnIter/FHorz/DivideConquer/DC1/ValueFnIter_FHorz_DC1_raw.m @@ -55,19 +55,20 @@ aprimeindexes=loweredge+(0:1:maxgap(ii)); % aprime possibilities are n_d-by-maxgap(ii)+1-by-1-by-n_z ReturnMatrix_ii=CreateReturnFnMatrix_Case1_Disc_DC1_Par2(ReturnFn, n_d, n_z, d_gridvals, a_grid(aprimeindexes), a_grid(level1ii(ii)+1:level1ii(ii+1)-1), z_gridvals_J(:,:,N_j), ReturnFnParamsVec,2); - [Vtempii,maxindex]=max(ReturnMatrix_ii,[],1); - V(curraindex,:,N_j)=shiftdim(Vtempii,1); - dind=(rem(maxindex-1,N_d)+1); - allind=dind+N_d*zind; % loweredge is n_d-by-1-by-1-by-n_z - Policy(curraindex,:,N_j)=shiftdim(maxindex+N_d*(loweredge(allind)-1),1); else loweredge=maxindex1(:,1,ii,:); % Just use aprime(ii) for everything ReturnMatrix_ii=CreateReturnFnMatrix_Case1_Disc_DC1_Par2(ReturnFn, n_d, n_z, d_gridvals, a_grid(loweredge), a_grid(level1ii(ii)+1:level1ii(ii+1)-1), z_gridvals_J(:,:,N_j), ReturnFnParamsVec,2); - [Vtempii,maxindex]=max(ReturnMatrix_ii,[],1); - V(curraindex,:,N_j)=shiftdim(Vtempii,1); - dind=(rem(maxindex-1,N_d)+1); - allind=dind+N_d*zind; % loweredge is n_d-by-1-by-1-by-n_z + end + % Common code in either case... + [Vtempii,maxindex]=max(ReturnMatrix_ii,[],1); % size(ReturnMatrix_ii)=[n_d*(maxgap(ii)+1),vfoptions.level1n-2,n_z] + V(curraindex,:,N_j)=shiftdim(Vtempii,1); + dind=(rem(maxindex-1,N_d)+1); + allind=dind+N_d*zind; % loweredge is n_d-by-1-by-1-by-n_z + if n_z==1 + % Avoid MATLAB's singleton expansion; alternatively, could just transpose maxindex, further simplifying code paths + Policy(curraindex,:,N_j)=shiftdim(maxindex+N_d*(loweredge(allind)'-1),1); + else Policy(curraindex,:,N_j)=shiftdim(maxindex+N_d*(loweredge(allind)-1),1); end end @@ -273,21 +274,21 @@ ReturnMatrix_ii=CreateReturnFnMatrix_Case1_Disc_DC1_Par2(ReturnFn, n_d, n_z, d_gridvals, a_grid(aprimeindexes), a_grid(level1ii(ii)+1:level1ii(ii+1)-1), z_gridvals_J(:,:,jj), ReturnFnParamsVec,2); aprimez=aprimeindexes+N_a*zBind; entireRHS_ii=ReturnMatrix_ii+DiscountFactorParamsVec*reshape(EV(aprimez),[N_d*(maxgap(ii)+1),1,N_z]); % autoexpand level1iidiff(ii) in 2nd-dim - [Vtempii,maxindex]=max(entireRHS_ii,[],1); - V(curraindex,:,jj)=shiftdim(Vtempii,1); - dind=(rem(maxindex-1,N_d)+1); - allind=dind+N_d*zind; % loweredge is n_d-by-1-by-1-by-n_z - Policy(curraindex,:,jj)=shiftdim(maxindex+N_d*(loweredge(allind)-1),1); else loweredge=maxindex1(:,1,ii,:); % Just use aprime(ii) for everything ReturnMatrix_ii=CreateReturnFnMatrix_Case1_Disc_DC1_Par2(ReturnFn, n_d, n_z, d_gridvals, a_grid(loweredge), a_grid(level1ii(ii)+1:level1ii(ii+1)-1), z_gridvals_J(:,:,jj), ReturnFnParamsVec,2); aprimez=loweredge+N_a*zBind; entireRHS_ii=ReturnMatrix_ii+DiscountFactorParamsVec*reshape(EV(aprimez),[N_d*1,1,N_z]); % autoexpand level1iidiff(ii) in 2nd-dim - [Vtempii,maxindex]=max(entireRHS_ii,[],1); - V(curraindex,:,jj)=shiftdim(Vtempii,1); - dind=(rem(maxindex-1,N_d)+1); - allind=dind+N_d*zind; % loweredge is n_d-by-1-by-1-by-n_z + end + [Vtempii,maxindex]=max(entireRHS_ii,[],1); + V(curraindex,:,jj)=shiftdim(Vtempii,1); + dind=(rem(maxindex-1,N_d)+1); + allind=dind+N_d*zind; % loweredge is n_d-by-1-by-1-by-n_z + if n_z==1 + % Avoid MATLAB's singleton expansion; alternatively, could just transpose maxindex, further simplifying code paths + Policy(curraindex,:,jj)=shiftdim(maxindex+N_d*(loweredge(allind)'-1),1); + else Policy(curraindex,:,jj)=shiftdim(maxindex+N_d*(loweredge(allind)-1),1); end end From 107643742ac8597d0b9dd3563ac9a2d0080e833a Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 13 Mar 2026 07:21:38 +1300 Subject: [PATCH 19/90] Incrementally push AggVars into PType parameter lists When evaluating agents in PType structures (such as firms and households), it happens that we can inject just-calculated AggVars into the Parameter lists of to-be evaluated PType structures, increasing the power and generality of using PTypes. Also update `HeteroAgentStationaryEqm_Case1_PType_subfn` to handle CustomModelStats. --- ...onaryEqm_Case1_FHorz_PType_GEptype_subfn.m | 46 ++++++++++++++++++- ...entStationaryEqm_Case1_FHorz_PType_subfn.m | 18 ++++++-- ...roAgentStationaryEqm_InfHorz_PType_subfn.m | 35 +++++++++++++- 3 files changed, 93 insertions(+), 6 deletions(-) diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m index 7ffcceac..67d940ab 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m @@ -31,7 +31,8 @@ Parameters.(GEPriceParamNames{pp})=GEpricesvec(GEpriceindexes(pp,1):GEpriceindexes(pp,2)); end -AggVars_ConditionalOnPType=zeros(PTypeStructure.numFnsToEvaluate,PTypeStructure.N_i,'gpuArray'); % Create AggVars conditional on ptype. +Ptype_cells=cell(1,PTypeStructure.N_i); % Hold results in case needed for CustomStats +AggVars_ConditionalOnPType=zeros(PTypeStructure.numFnsToEvaluate,PTypeStructure.N_i); % Create AggVars conditional on ptype. for ii=1:PTypeStructure.N_i @@ -76,6 +77,9 @@ AggVars_ii=EvalFnOnAgentDist_AggVars_Case1(StationaryDist_ii, Policy_ii, PTypeStructure.(iistr).FnsToEvaluate, PTypeStructure.(iistr).Parameters, PTypeStructure.(iistr).FnsToEvaluateParamNames, PTypeStructure.(iistr).n_d, PTypeStructure.(iistr).n_a, PTypeStructure.(iistr).n_z, PTypeStructure.(iistr).d_grid, PTypeStructure.(iistr).a_grid, PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).simoptions); end + if heteroagentoptions.useCustomModelStats==1 + Ptype_cells{ii}={V_ii,Policy_ii,StationaryDist_ii}; + end AggVars_ConditionalOnPType(PTypeStructure.(iistr).FnsAndPTypeIndicator_ii,ii)=AggVars_ii; if heteroagentoptions.useCustomModelStats==1 @@ -83,17 +87,29 @@ Policy.(iistr)=Policy_ii; StationaryDist.(iistr)=StationaryDist_ii; end + + warning("do we still need this?") + % Put updated AggVars into subsequent PTypeStructure Parameters, so they can be used for subsequent PType evaluations + FnsToEvaluate_aa=fieldnames(PTypeStructure.(iistr).FnsToEvaluate); + for jj=ii+1:PTypeStructure.N_i + jjstr=PTypeStructure.iistr{jj}; + for aa=1:length(AggVars_ii) + PTypeStructure.(jjstr).Parameters.(FnsToEvaluate_aa{aa})=AggVars_ii(aa); + end + end end AggVars=sum(AggVars_ConditionalOnPType.*PTypeStructure.ptweights,2); % Note: AggVars is a vector -%% Put GE parameters and AggVars in structure, so they can be used for intermediateEqns and GeneralEqmEqns +%% Put GE parameters and AggVars in structure, so they can be used for intermediateEqns and GeneralEqmEqns % already did the basic GE params % for pp=1:nGEprices % Parameters.(GEPriceParamNames{pp})=GEprices(GEpriceindexes(pp,1):GEpriceindexes(pp,2)); % end + +% We pushed AggVars down into the PTypeStructure parameters; this puts them into the unified Parameter structure for aa=1:length(AggVarNames) Parameters.(AggVarNames{aa})=AggVars(aa); end @@ -172,6 +188,32 @@ end end +%% Custom Model Stats +customstatnames=struct(); +if heteroagentoptions.useCustomModelStats==1 + if isfield(heteroagentoptions, 'CustomModelStats') + error("Universal PType handler for CustomModelStats not yet implemented") + else + for ii=1:PTypeStructure.N_i + iistr=PTypeStructure.iistr{ii}; + if ~isfield(heteroagentoptions, iistr) || ~isfield(heteroagentoptions.(iistr), 'CustomModelStats') + continue + end + if isfinite(PTypeStructure.(iistr).N_j) + CustomStats.(iistr)=heteroagentoptions.(iistr).CustomModelStats(Ptype_cells{ii}{1},Ptype_cells{ii}{2},Ptype_cells{ii}{3},PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).FnsToEvaluate,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).z_gridvals_J,PTypeStructure.(iistr).pi_z_J,heteroagentoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); + else + CustomStats.(iistr)=heteroagentoptions.(iistr).CustomModelStats(Ptype_cells{ii}{1},Ptype_cells{ii}{2},Ptype_cells{ii}{3},PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).FnsToEvaluate,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).z_gridvals,PTypeStructure.(iistr).pi_z,heteroagentoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); + end + % Note: anything else you want, just 'hide' it in heteroagentoptions + customstatnames.(iistr)=fieldnames(CustomStats.(iistr)); + for pp=1:length(customstatnames.(iistr)) + PTypeStructure.(iistr).Parameters.(customstatnames.(iistr){pp})=CustomStats.(iistr).(customstatnames.(iistr){pp}); + end + end + end +end + + %% Evaluate the General Eqm Eqns % use of real() is a hack that could disguise errors, but I couldn't find why matlab was treating output as complex diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m index e763231b..fad0d75d 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m @@ -19,7 +19,7 @@ %% Ptype_cells=cell(1,PTypeStructure.N_i); % Hold results in case needed for CustomStats -AggVars_ConditionalOnPType=zeros(PTypeStructure.numFnsToEvaluate,PTypeStructure.N_i,'gpuArray'); % Create AggVars conditional on ptype. +AggVars_ConditionalOnPType=zeros(PTypeStructure.numFnsToEvaluate,PTypeStructure.N_i); % Create AggVars conditional on ptype. for ii=1:PTypeStructure.N_i @@ -80,17 +80,29 @@ Policy.(iistr)=Policy_ii; StationaryDist.(iistr)=StationaryDist_ii; end + + warning("do we still need to do this for AggVars?") + % Put updated AggVars into subsequent PTypeStructure Parameters, so they can be used for subsequent PType evaluations + FnsToEvaluate_aa=fieldnames(PTypeStructure.(iistr).FnsToEvaluate); + for jj=ii+1:PTypeStructure.N_i + jjstr=PTypeStructure.iistr{jj}; + for aa=1:length(AggVars_ii) + PTypeStructure.(jjstr).Parameters.(FnsToEvaluate_aa{aa})=AggVars_ii(aa); + end + end end -AggVars=gather(sum(AggVars_ConditionalOnPType.*PTypeStructure.ptweights',2)); +AggVars=sum(AggVars_ConditionalOnPType.*PTypeStructure.ptweights',2); % Note: AggVars is a vector -%% Put GE parameters and AggVars in structure, so they can be used for intermediateEqns and GeneralEqmEqns +%% Put GE parameters and AggVars in structure, so they can be used for intermediateEqns and GeneralEqmEqns % already did the basic GE params % for pp=1:nGEprices % Parameters.(GEPriceParamNames{pp})=GEprices(pp); % end + +% We pushed AggVars down into the PTypeStructure parameters; this puts them into the unified Parameter structure for aa=1:length(AggVarNames) Parameters.(AggVarNames{aa})=AggVars(aa); end diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m index 68b99909..48e0d014 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m @@ -33,6 +33,7 @@ %% AggVars=zeros(PTypeStructure.numFnsToEvaluate,1,'gpuArray'); % numFnsToEvaluate is independent of the ptype +Ptype_cells=cell(1,PTypeStructure.N_i); % Hold results in case needed for CustomStats for ii=1:PTypeStructure.N_i @@ -55,11 +56,18 @@ StationaryDist_ii=StationaryDist_InfHorz(Policy_ii,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).simoptions,PTypeStructure.(iistr).Parameters); % PTypeStructure.(iistr).simoptions.outputasstructure=0; % Want AggVars_ii as matrix to make it easier to add them across the PTypes (is set outside this script) AggVars_ii=EvalFnOnAgentDist_AggVars_InfHorz(StationaryDist_ii, Policy_ii, PTypeStructure.(iistr).FnsToEvaluate, PTypeStructure.(iistr).Parameters, PTypeStructure.(iistr).FnsToEvaluateParamNames, PTypeStructure.(iistr).n_d, PTypeStructure.(iistr).n_a, PTypeStructure.(iistr).n_z, PTypeStructure.(iistr).d_grid, PTypeStructure.(iistr).a_grid, PTypeStructure.(iistr).z_grid, PTypeStructure.(iistr).simoptions); - + if heteroagentoptions.useCustomModelStats==1 + Ptype_cells{ii}={V_ii,Policy_ii,StationaryDist_ii}; + end + for kk=1:PTypeStructure.numFnsToEvaluate jj=PTypeStructure.(iistr).WhichFnsForCurrentPType(kk); if jj>0 AggVars(kk)=AggVars(kk)+PTypeStructure.(iistr).PTypeWeight*AggVars_ii(jj); + + % Put updated AggVars into subsequent PTypeStructure Parameters, so they can be used for subsequent PType evaluations + warning("check AggVar parameter insertion here") + PTypeStructure.(iistr).Parameters.(FnsToEvaluate{kk})=AggVars_ii(jj); end end @@ -77,6 +85,8 @@ % for pp=1:nGEprices % Parameters.(GEPriceParamNames{pp})=GEprices(pp); % end + +% We pushed AggVars down into the PTypeStructure parameters; this puts them into the unified Parameter structure for aa=1:length(AggVarNames) Parameters.(AggVarNames{aa})=AggVars(aa); end @@ -110,6 +120,29 @@ end end +%% Custom Model Stats +customstatnames=struct(); +if heteroagentoptions.useCustomModelStats==1 + if isfield(heteroagentoptions, 'CustomModelStats') + error("Universal PType handler for CustomModelStats not yet implemented") + else + for ii=1:PTypeStructure.N_i + iistr=PTypeStructure.iistr{ii}; + if ~isfield(heteroagentoptions, iistr) || ~isfield(heteroagentoptions.(iistr), 'CustomModelStats') + continue + end + CustomStats.(iistr)=heteroagentoptions.(iistr).CustomModelStats(Ptype_cells{ii}{1},Ptype_cells{ii}{2},Ptype_cells{ii}{3},PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).FnsToEvaluate,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).z_gridvals,PTypeStructure.(iistr).pi_z,heteroagentoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); + % Note: anything else you want, just 'hide' it in heteroagentoptions + customstatnames.(iistr)=fieldnames(CustomStats.(iistr)); + for pp=1:length(customstatnames.(iistr)) + PTypeStructure.(iistr).Parameters.(customstatnames.(iistr){pp})=CustomStats.(iistr).(customstatnames.(iistr){pp}); + end + end + end +end + + + %% Evaluate General Eqm Eqns % use of real() is a hack that could disguise errors, but I couldn't find why matlab was treating output as complex GeneralEqmConditionsVec=zeros(1,length(GEeqnNames)); From b75ae647f2b505b0a65f254ff559f4be45ef67b5 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 13 Mar 2026 07:24:27 +1300 Subject: [PATCH 20/90] Improve lowmemory==2 effectiveness Adjust EV calculations to better honor lowmemory==2 promises. --- .../ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m | 6 ++---- ValueFnIter/FHorz/ValueFnIter_FHorz_e_raw.m | 8 ++++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m index 041961dc..9b163359 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m @@ -163,13 +163,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2,n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals,z_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); % (d,aprime,a,z) - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); %Calc the max and its index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -180,7 +178,7 @@ elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); diff --git a/ValueFnIter/FHorz/ValueFnIter_FHorz_e_raw.m b/ValueFnIter/FHorz/ValueFnIter_FHorz_e_raw.m index 0039fc69..a056213c 100644 --- a/ValueFnIter/FHorz/ValueFnIter_FHorz_e_raw.m +++ b/ValueFnIter/FHorz/ValueFnIter_FHorz_e_raw.m @@ -143,12 +143,11 @@ EV(isnan(EV))=0; %multiplications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) EV=sum(EV,2); % sum over z', leaving a singular second dimension - entireEV=repelem(EV,N_d,1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_Disc_Par2e(ReturnFn, n_d, n_a, n_z, n_e, d_grid, a_grid, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec); % (d,aprime,a,z,e) - + + entireEV=repelem(EV,N_d,1,1); entireRHS=ReturnMatrix+DiscountFactorParamsVec*entireEV; %repmat(entireEV,1,N_a,1,N_e); % Calc the max and it's index @@ -164,6 +163,7 @@ ReturnMatrix_e=CreateReturnFnMatrix_Case1_Disc_Par2e(ReturnFn, n_d, n_a, n_z, special_n_e, d_grid, a_grid, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec); % (d,aprime,a,z) + entireEV=repelem(EV,N_d,1,1); entireRHS_e=ReturnMatrix_e+DiscountFactorParamsVec*entireEV; %.*ones(1,N_a,1); % Calc the max and it's index @@ -177,7 +177,7 @@ for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); EV_z=EV(:,:,z_c); - entireEV_z=entireEV(:,:,z_c); + entireEV_z=repelem(EV_z,N_d,1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); From bf9d646ee624f8414447f2c36089e54e7a3fc247 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 13 Mar 2026 14:57:19 +1300 Subject: [PATCH 21/90] Revert making StationaryDist a full matrix for GPU's sake It should not be sparse in the first place. --- .../InfHorz/EvalFnOnAgentDist_AggVars_InfHorz.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_AggVars_InfHorz.m b/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_AggVars_InfHorz.m index 97dadf43..916d7968 100644 --- a/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_AggVars_InfHorz.m +++ b/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_AggVars_InfHorz.m @@ -150,7 +150,7 @@ %% if simoptions.parallel==2 - StationaryDist=gpuArray(full(StationaryDist)); % MATLAB 2025a supports reshaping sparse gpuarray, but not isnan(sparse(gpuarray)) + StationaryDist=gpuArray(StationaryDist); Policy=gpuArray(Policy); n_d=gpuArray(n_d); n_a=gpuArray(n_a); From bd3ff0881365cbec1494c45afaee4aba184dd10b Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sun, 15 Mar 2026 19:41:25 +1300 Subject: [PATCH 22/90] Support larger ExpAsset models The changes expand the range of the VFI Toolkit to handle an experience asset with `length(n_a)==5`, following accepted changes to support experience asset with `length(n_a)==4`. To handle exponentially increasing grid sizes, changes are proposed to `ValueFnIter_FHorz_ExpAsset_raw` (and its `_e` variant) to treat the experience asset as something that can be iterated when `lowmemory==2`, either instead of or along side the iteration of `e` shocks. In the course of testing, a transcription error was disovered in `CreateGridvals` when `l_x`==5. And for my own testing purposes, I needed to manually expand the options accepted by `EvalFnOnAgentDist_Grid_J`, though I'm sure that a better solution is to run the code generator used to generate the guts of this file to support `l_daprime==7` across its many possible instantiations. --- EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m index ab9d2d9e..a4f3f600 100644 --- a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m +++ b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m @@ -661,6 +661,10 @@ elseif l_a==5 && l_z==8 Values=arrayfun(FnToEvaluate, PolicyValuesPermute(:,:,:,1),PolicyValuesPermute(:,:,:,2),PolicyValuesPermute(:,:,:,3),PolicyValuesPermute(:,:,:,4),PolicyValuesPermute(:,:,:,5),PolicyValuesPermute(:,:,:,6),PolicyValuesPermute(:,:,:,7), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals_J(1,:,:,1),z_gridvals_J(1,:,:,2),z_gridvals_J(1,:,:,3),z_gridvals_J(1,:,:,4),z_gridvals_J(1,:,:,5),z_gridvals_J(1,:,:,6),z_gridvals_J(1,:,:,7),z_gridvals_J(1,:,:,8), CellOverAgeOfParamValues{:}); end +elseif l_daprime==7 + if l_a==5 && l_z==1 + Values=arrayfun(FnToEvaluate, PolicyValuesPermute(:,:,:,1),PolicyValuesPermute(:,:,:,2),PolicyValuesPermute(:,:,:,3),PolicyValuesPermute(:,:,:,4),PolicyValuesPermute(:,:,:,5),PolicyValuesPermute(:,:,:,6),PolicyValuesPermute(:,:,:,7), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals_J(1,:,:,1), CellOverAgeOfParamValues{:}); + end end % Note: No need to reshape the output as it is already the appropriate size From 7bae2736f6ab225ba621f46c7117f9da6348d02e Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sun, 15 Mar 2026 20:11:00 +1300 Subject: [PATCH 23/90] Additional support for larger ExpAsset models This just missed the previous commit. --- EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m index f6103336..d49ba1b4 100644 --- a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m +++ b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m @@ -986,6 +986,10 @@ elseif l_a==5 && l_z==12 Values=arrayfun(FnToEvaluate, daprime1vals,daprime2vals,daprime3vals,daprime4vals,daprime5vals,daprime6vals,daprime7vals, a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals(1,:,1),z_gridvals(1,:,2),z_gridvals(1,:,3),z_gridvals(1,:,4),z_gridvals(1,:,5),z_gridvals(1,:,6),z_gridvals(1,:,7),z_gridvals(1,:,8),z_gridvals(1,:,9),z_gridvals(1,:,10),z_gridvals(1,:,11),z_gridvals(1,:,12), FnToEvaluateParamsCell{:}); end +elseif l_daprime==7 + if l_a==5 && l_z==1 + Values=arrayfun(FnToEvaluate, daprime1vals,daprime2vals,daprime3vals,daprime4vals,daprime5vals,daprime6vals,daprime7vals, a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals(1,:,1), FnToEvaluateParamsCell{:}); + end end % Note: No need to reshape the output as it is already the appropriate size From ba3cb3fd504c6a62aa190dcae7e6f3188b2bdde9 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 17 Mar 2026 09:42:08 +1300 Subject: [PATCH 24/90] Knocking out l_daprime=7 in favor of l_daprime=6 If there's going to be only one special case, let it be the one I plan to use. --- EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m | 7 ++----- EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m index d49ba1b4..82639160 100644 --- a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m +++ b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m @@ -26,9 +26,6 @@ daprime5vals=PolicyValuesPermute(:,5); if l_daprime>=6 daprime6vals=PolicyValuesPermute(:,6); - if l_daprime>=7 - daprime7vals=PolicyValuesPermute(:,7); - end end end end @@ -986,9 +983,9 @@ elseif l_a==5 && l_z==12 Values=arrayfun(FnToEvaluate, daprime1vals,daprime2vals,daprime3vals,daprime4vals,daprime5vals,daprime6vals,daprime7vals, a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals(1,:,1),z_gridvals(1,:,2),z_gridvals(1,:,3),z_gridvals(1,:,4),z_gridvals(1,:,5),z_gridvals(1,:,6),z_gridvals(1,:,7),z_gridvals(1,:,8),z_gridvals(1,:,9),z_gridvals(1,:,10),z_gridvals(1,:,11),z_gridvals(1,:,12), FnToEvaluateParamsCell{:}); end -elseif l_daprime==7 +elseif l_daprime==6 if l_a==5 && l_z==1 - Values=arrayfun(FnToEvaluate, daprime1vals,daprime2vals,daprime3vals,daprime4vals,daprime5vals,daprime6vals,daprime7vals, a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals(1,:,1), FnToEvaluateParamsCell{:}); + Values=arrayfun(FnToEvaluate, daprime1vals,daprime2vals,daprime3vals,daprime4vals,daprime5vals,daprime6vals, a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals(1,:,1), FnToEvaluateParamsCell{:}); end end diff --git a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m index a4f3f600..34e49469 100644 --- a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m +++ b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m @@ -661,9 +661,9 @@ elseif l_a==5 && l_z==8 Values=arrayfun(FnToEvaluate, PolicyValuesPermute(:,:,:,1),PolicyValuesPermute(:,:,:,2),PolicyValuesPermute(:,:,:,3),PolicyValuesPermute(:,:,:,4),PolicyValuesPermute(:,:,:,5),PolicyValuesPermute(:,:,:,6),PolicyValuesPermute(:,:,:,7), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals_J(1,:,:,1),z_gridvals_J(1,:,:,2),z_gridvals_J(1,:,:,3),z_gridvals_J(1,:,:,4),z_gridvals_J(1,:,:,5),z_gridvals_J(1,:,:,6),z_gridvals_J(1,:,:,7),z_gridvals_J(1,:,:,8), CellOverAgeOfParamValues{:}); end -elseif l_daprime==7 +elseif l_daprime==6 if l_a==5 && l_z==1 - Values=arrayfun(FnToEvaluate, PolicyValuesPermute(:,:,:,1),PolicyValuesPermute(:,:,:,2),PolicyValuesPermute(:,:,:,3),PolicyValuesPermute(:,:,:,4),PolicyValuesPermute(:,:,:,5),PolicyValuesPermute(:,:,:,6),PolicyValuesPermute(:,:,:,7), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals_J(1,:,:,1), CellOverAgeOfParamValues{:}); + Values=arrayfun(FnToEvaluate, PolicyValuesPermute(:,:,:,1),PolicyValuesPermute(:,:,:,2),PolicyValuesPermute(:,:,:,3),PolicyValuesPermute(:,:,:,4),PolicyValuesPermute(:,:,:,5),PolicyValuesPermute(:,:,:,6), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals_J(1,:,:,1), CellOverAgeOfParamValues{:}); end end From e5db55d1931ee93a6b2076593d0cc2e5c4445021 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 20 Mar 2026 07:17:11 +1300 Subject: [PATCH 25/90] Pre-merge checkpoint Prepare to merge in changes from last week that I didn't see coming. These changes seem to work for models that have z and e, but for the confusion about the joint ze grid in late-stage Transition Path evaluations. Making the changes work without any e ever should be easy, as should be making them work without z (and other necessary shapes). --- ...AgentDist_AggVars_FHorz_ExpAsset_fastOLG.m | 395 ++++++++++++++++++ .../subcodes/EvalFnOnAgentDist_Grid.m | 2 + .../subcodes/EvalFnOnAgentDist_Grid_J.m | 2 + ...KronPolicyIndexes_Case1_FHorz_ExpAsset_e.m | 93 +++++ ...tionPath_FHorz_PType_ExpAsset_singlepath.m | 38 ++ ...th_FHorz_PType_ExpAsset_singlepath_e_raw.m | 220 ++++++++++ ..._PType_ExpAsset_singlepath_fastOLG_e_raw.m | 179 ++++++++ ...Path_FHorz_PType_ExpAsset_singlepath_raw.m | 205 +++++++++ .../PType/TransitionPath_Case1_FHorz_PType.m | 159 +++++-- ...ransitionPath_Case1_FHorz_PType_shooting.m | 2 +- ...ueFnIter_FHorz_TPath_ExpAsset_SingleStep.m | 30 ++ ...FnIter_FHorz_TPath_ExpAsset_SingleStep_e.m | 30 ++ ...er_FHorz_TPath_ExpAsset_SingleStep_e_raw.m | 201 +++++++++ ...Iter_FHorz_TPath_ExpAsset_SingleStep_raw.m | 100 +++++ .../TransitionPath_InfHorz_PType_singlepath.m | 16 +- .../ValueFnIter_InfHorz_TPath_SingleStep.m | 7 +- 16 files changed, 1622 insertions(+), 57 deletions(-) create mode 100644 EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m create mode 100644 SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e.m create mode 100644 TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m create mode 100644 TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m create mode 100644 TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m create mode 100644 TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m create mode 100644 TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m create mode 100644 TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e.m create mode 100644 TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw.m create mode 100644 TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m diff --git a/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m b/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m new file mode 100644 index 00000000..c99a32da --- /dev/null +++ b/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m @@ -0,0 +1,395 @@ +function AggVars=EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG(AgentDist,PolicyValues_d, PolicyValues_aprime, FnsToEvaluate,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_z,N_a,N_z,a_gridvals,z_gridvals_J_fastOLG,outputasstructure) +% fastOLG: so (a,j)-by-z +% Policy can be in fastOLG for or not, use fastOLGpolicy=1 or 0 to indicate this +% No z is treated elsewhere +% If you have e or semiz, just disguise them as z for this command + +% If no d variable, set l_d=0, and then PolicyValues_d=[], d_gridvals=[]. + +% PolicyValues_d is [N_a,N_j,N_z,l_d] +% PolicyValues_aprime is [N_a,N_j,N_z,l_aprime] +% a_gridvals is [N_a,l_a] +% z_gridvals_J_fastOLG is [1,N_j,N_z,l_z] (convert internally to [1,N_j,N_z,l_z]) + +% parameters that depend on age must be [1,N_j] + +% Note: FnsToEvaluate is already cell (converted from struct) + +%% +if outputasstructure==1 + AggVars=struct(); +else % outputasstructure==0 + AggVars=zeros(length(FnsToEvaluate),1,'gpuArray'); +end + +% AgentDist is [N_a*N_j*N_z,1] or [N_a*N_j,N_e] or [N_a*N_j*N_z,N_e] +% PolicyValues is [N_a,N_j,N_ze] + +for ff=1:length(FnsToEvaluate) + Values=zeros(N_a,N_j,N_z,'gpuArray'); + + + if isempty(FnsToEvaluateParamNames(ff).Names) + ParamCell=cell(0,1); + else + % Create a matrix containing all the return function parameters (in order). + % Each column will be a specific parameter with the values at every age. + FnToEvaluateParamsAgeMatrix=CreateAgeMatrixFromParams(Parameters, FnsToEvaluateParamNames(ff).Names,N_j); % this will be a matrix, row indexes ages and column indexes the parameters (parameters which are not dependent on age appear as a constant valued column) + + nFnToEvaluateParams=size(FnToEvaluateParamsAgeMatrix,2); + + ParamCell=cell(nFnToEvaluateParams,1); + for ii=1:nFnToEvaluateParams + ParamCell(ii,1)={shiftdim(FnToEvaluateParamsAgeMatrix(:,ii),-1)}; % (a,j,z,l_d+l_a), so we want j to be after a (which is N_a) + end + end + + if l_z==1 + if l_d==0 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + end + elseif l_d==1 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + end + elseif l_d==2 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + end + elseif l_d==3 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + end + elseif l_d==4 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + end + end + elseif l_z==2 + if l_d==0 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + end + elseif l_d==1 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + end + elseif l_d==2 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + end + elseif l_d==3 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + end + elseif l_d==4 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + end + end + elseif l_z==3 + if l_d==0 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + end + elseif l_d==1 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + end + elseif l_d==2 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + end + elseif l_d==3 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + end + elseif l_d==4 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); + end + end + elseif l_z==4 + if l_d==0 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + end + elseif l_d==1 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + end + elseif l_d==2 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + end + elseif l_d==3 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + end + elseif l_d==4 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); + end + end + elseif l_z==5 + if l_d==0 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + end + elseif l_d==1 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + end + elseif l_d==2 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + end + elseif l_d==3 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + end + elseif l_d==4 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); + end + end + elseif l_z==6 + if l_d==0 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + end + elseif l_d==1 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + end + elseif l_d==2 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + end + elseif l_d==3 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + end + elseif l_d==4 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); + end + end + elseif l_z==7 + if l_d==0 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + end + elseif l_d==1 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + end + elseif l_d==2 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + end + elseif l_d==3 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + end + elseif l_d==4 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); + end + end + elseif l_z==8 + if l_d==0 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + end + elseif l_d==1 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + end + elseif l_d==2 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + end + elseif l_d==3 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + end + elseif l_d==4 + if l_aprime==1 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + elseif l_aprime==2 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + elseif l_aprime==3 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); + end + end + end + + if outputasstructure==1 + AggVars.(AggVarNames{ff}).Mean=sum(Values(:).*AgentDist(:)); + else % outputasstructure==0 + AggVars(ff)=sum(Values(:).*AgentDist(:)); + end +end + + +end diff --git a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m index 82639160..d50fb99d 100644 --- a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m +++ b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m @@ -986,6 +986,8 @@ elseif l_daprime==6 if l_a==5 && l_z==1 Values=arrayfun(FnToEvaluate, daprime1vals,daprime2vals,daprime3vals,daprime4vals,daprime5vals,daprime6vals, a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals(1,:,1), FnToEvaluateParamsCell{:}); + elseif l_a==5 && l_z==2 + Values=arrayfun(FnToEvaluate, daprime1vals,daprime2vals,daprime3vals,daprime4vals,daprime5vals,daprime6vals, a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals(1,:,1),z_gridvals(1,:,2), FnToEvaluateParamsCell{:}); end end diff --git a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m index 34e49469..3f458033 100644 --- a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m +++ b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m @@ -664,6 +664,8 @@ elseif l_daprime==6 if l_a==5 && l_z==1 Values=arrayfun(FnToEvaluate, PolicyValuesPermute(:,:,:,1),PolicyValuesPermute(:,:,:,2),PolicyValuesPermute(:,:,:,3),PolicyValuesPermute(:,:,:,4),PolicyValuesPermute(:,:,:,5),PolicyValuesPermute(:,:,:,6), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals_J(1,:,:,1), CellOverAgeOfParamValues{:}); + elseif l_a==5 && l_z==2 + Values=arrayfun(FnToEvaluate, PolicyValuesPermute(:,:,:,1),PolicyValuesPermute(:,:,:,2),PolicyValuesPermute(:,:,:,3),PolicyValuesPermute(:,:,:,4),PolicyValuesPermute(:,:,:,5),PolicyValuesPermute(:,:,:,6), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals_J(1,:,:,1), z_gridvals_J(1,:,:,2), CellOverAgeOfParamValues{:}); end end diff --git a/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e.m b/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e.m new file mode 100644 index 00000000..b941d55e --- /dev/null +++ b/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e.m @@ -0,0 +1,93 @@ +function Policy=UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e(PolicyKron, n_d1,n_d2,n_a1,n_a2, n_z,n_e,N_j, vfoptions) +% Can use vfoptions OR simoptions +% Input: PolicyKron is (3,N_a,N_z,N_e,N_j); % first dim indexes the optimal choice for (d1,d2,aprime) +% without d1, the first dim is 2 and indexes the optimal choice for (d2,aprime) +% vfoptions.gridinterplayer=1 will mean the first dimension has one extra value (so 4 if d1, 3 without) +% Output: Policy (l_d+l_a,n_a1,n_a2,n_z,n_e,N_j); + +N_d1=prod(n_d1); +% N_d2=prod(n_d2); +N_a1=prod(n_a1); +N_a2=prod(n_a2); +N_a=N_a1*N_a2; +N_z=prod(n_z); +N_e=prod(n_e); + +l_d2=length(n_d2); % if ever there is more than one d2 for ExpAsset +l_aprime=length(n_a1); +n_aprime=n_a1; +extra=(vfoptions.gridinterplayer==1); + +if N_d1==0 + Policy=zeros(l_d2+l_aprime+extra,N_a,N_z,N_e,N_j,'gpuArray'); + + Policy(1,:,:,:,:)=rem(PolicyKron(1,:,:,:,:)-1,n_d2(1))+1; + if l_d2>1 + if l_d2>2 + for ii=1:l_d2-1 + Policy(ii,:,:,:,:)=rem(ceil(PolicyKron(1,:,:,:,:)/prod(n_d2(1:ii-1)))-1,n_d2(ii))+1; + end + end + Policy(l_d2,:,:,:,:)=ceil(PolicyKron(1,:,:,:,:)/prod(n_d2(1:l_d2-1))); + end + + Policy(l_d2+1,:,:,:,:)=rem(PolicyKron(2,:,:,:,:)-1,n_aprime(1))+1; + if l_aprime>1 + if l_aprime>2 + for ii=1:l_aprime-1 + Policy(l_d2+ii,:,:,:,:)=rem(ceil(PolicyKron(2,:,:,:,:)/prod(n_aprime(1:ii-1)))-1,n_aprime(ii))+1; + end + end + Policy(l_d2+l_aprime,:,:,:,:)=ceil(PolicyKron(2,:,:,:,:)/prod(n_aprime(1:l_aprime-1))); + end + + if vfoptions.gridinterplayer==1 + Policy(l_d2+l_aprime+1,:,:,:,:)=PolicyKron(3,:,:,:,:); + end + + Policy=reshape(Policy,[l_d2+l_aprime+extra,n_a,n_z,n_e,N_j]); + +else % N_d1>0 + l_d1=length(n_d1); % Note: this is anyway only used is N_d1~=0 + + Policy=zeros(l_d1+l_d2+l_aprime+extra,N_a,N_z,N_e,N_j,'gpuArray'); + + Policy(1,:,:,:,:)=rem(PolicyKron(1,:,:,:,:)-1,n_d1(1))+1; + if l_d1>1 + if l_d1>2 + for ii=1:l_d1-1 + Policy(ii,:,:,:,:)=rem(ceil(PolicyKron(1,:,:,:,:)/prod(n_d1(1:ii-1)))-1,n_d1(ii))+1; + end + end + Policy(l_d1,:,:,:,:)=ceil(PolicyKron(1,:,:,:,:)/prod(n_d1(1:l_d1-1))); + end + + Policy(l_d1+1,:,:,:,:)=rem(PolicyKron(2,:,:,:,:)-1,n_d2(1))+1; + if l_d2>1 + if l_d2>2 + for ii=1:l_d2-1 + Policy(l_d1+ii,:,:,:,:)=rem(ceil(PolicyKron(2,:,:,:,:)/prod(n_d2(1:ii-1)))-1,n_d2(ii))+1; + end + end + Policy(l_d1+l_d2,:,:,:,:)=ceil(PolicyKron(2,:,:,:,:)/prod(n_d2(1:l_d2-1))); + end + + Policy(l_d1+l_d2+1,:,:,:,:)=rem(PolicyKron(3,:,:,:,:)-1,n_aprime(1))+1; + if l_aprime>1 + if l_aprime>2 + for ii=1:l_aprime-1 + Policy(l_d1+l_d2+ii,:,:,:,:)=rem(ceil(PolicyKron(3,:,:,:,:)/prod(n_aprime(1:ii-1)))-1,n_aprime(ii))+1; + end + end + Policy(l_d1+l_d2+l_aprime,:,:,:,:)=ceil(PolicyKron(3,:,:,:,:)/prod(n_aprime(1:l_aprime-1))); + end + + if vfoptions.gridinterplayer==1 + Policy(l_d1+l_d2+l_aprime+1,:,:,:,:)=PolicyKron(4,:,:,:,:); + end + + Policy=reshape(Policy,[l_d1+l_d2+l_aprime+extra,n_a1,n_a2,n_z,n_e,N_j]); +end + + +end \ No newline at end of file diff --git a/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m b/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m new file mode 100644 index 00000000..5e4ecd3e --- /dev/null +++ b/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m @@ -0,0 +1,38 @@ +function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions) +% When doing shooting alogrithm on TPath FHorz PType, this is for a given ptype, and does the steps of back-iterate to get policy, then forward to get agent dist and agg vars. +% The only output is the agg vars path. + +% AggVarsPath=zeros(length(FnsToEvaluate),T-1,'gpuArray'); +if transpathoptions.fastOLG==0 + if N_z==0 + if N_e==0 + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_noz_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + else + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_noz_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + end + else + if N_e==0 + % Principal test case... + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + else + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + end + end +elseif transpathoptions.fastOLG==1 + if N_z==0 + if N_e==0 + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_noz_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + else + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_noz_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + end + else + if N_e==0 + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + else + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + end + end +end + + +end diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m new file mode 100644 index 00000000..76cce4de --- /dev/null +++ b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m @@ -0,0 +1,220 @@ +function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T, ... + l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG, ... + ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, ... + use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, ... + II1orII, II2, exceptlastj,exceptfirstj,justfirstj, ... + transpathoptions, vfoptions, simoptions) +% When doing shooting alogrithm on TPath FHorz PType, this is for a given ptype, and does the steps of back-iterate to get policy, then forward to get agent dist and agg vars. +% The only output is the agg vars path. + +AggVarsPath=zeros(length(FnsToEvaluateCell),T-1,'gpuArray'); % Note: does not include the final AggVars, might be good to add them later as a way to make if obvious to user it things are incorrect + +N_a1=prod(n_a1); +N_a2=prod(n_a2); +N_a=N_a1*N_a2; +N_ze=N_z*N_e; + +l_aprime=length(n_a1); +l_a=l_aprime+length(n_a2); +l_z=length(n_z); +l_ze=l_z*length(n_e); + +%% +% Shapes: +% V is [N_a,N_z,N_e,N_j] +% AgentDist for basic is [N_a*N_z*N_e,N_j] +% AgentDist for fastOLG is [N_a*N_j*N_z,N_e] + +if vfoptions.gridinterplayer==0 + PolicyIndexesPath=zeros(l_d+l_aprime,N_a,N_z,N_e,N_j,T-1,'gpuArray'); %Periods 1 to T-1 +elseif vfoptions.gridinterplayer==1 + PolicyIndexesPath=zeros(l_d+l_aprime+1,N_a,N_z,N_e,N_j,T-1,'gpuArray'); %Periods 1 to T-1 +end +if simoptions.gridinterplayer==0 + if simoptions.fastOLG==0 + II1=1:1:N_a*N_z*N_e; + II2=ones(N_a*N_z*N_e,1); + elseif simoptions.fastOLG==1 + II1=1:1:N_a*(N_j-1)*N_z*N_e; + II2=ones(N_a*(N_j-1)*N_z*N_e,1); + exceptlastj=repmat((1:1:N_a)',(N_j-1)*N_z*N_e,1)+repmat(repelem(N_a*(0:1:N_j-2)',N_a,1),N_z*N_e,1)+repelem(N_a*N_j*(0:1:N_z*N_e-1)',N_a*(N_j-1),1); + exceptfirstj=repmat((1:1:N_a)',(N_j-1)*N_z*N_e,1)+repmat(repelem(N_a*(1:1:N_j-1)',N_a,1),N_z*N_e,1)+repelem(N_a*N_j*(0:1:N_z*N_e-1)',N_a*(N_j-1),1); + justfirstj=repmat((1:1:N_a)',N_z*N_e,1)+N_a*N_j*repelem((0:1:N_z*N_e-1)',N_a,1); + end +elseif simoptions.gridinterplayer==1 + N_probs=2; + if simoptions.fastOLG==0 + error('Cannot use simoptions.fastOLG=0 with grid interpolation layer') + elseif simoptions.fastOLG==1 + PolicyProbsPath=zeros(N_a*(N_j-1)*N_z*N_e,N_probs,T-1,'gpuArray'); % preallocate + II=repelem((1:1:N_a*(N_j-1)*N_z*N_e)',1,N_probs); + exceptlastj=repmat((1:1:N_a)',(N_j-1)*N_z*N_e,1)+repmat(repelem(N_a*(0:1:N_j-2)',N_a,1),N_z*N_e,1)+repelem(N_a*N_j*(0:1:N_z*N_e-1)',N_a*(N_j-1),1); + exceptfirstj=repmat((1:1:N_a)',(N_j-1)*N_z*N_e,1)+repmat(repelem(N_a*(1:1:N_j-1)',N_a,1),N_z*N_e,1)+repelem(N_a*N_j*(0:1:N_z*N_e-1)',N_a*(N_j-1),1); + justfirstj=repmat((1:1:N_a)',N_z*N_e,1)+N_a*N_j*repelem((0:1:N_z*N_e-1)',N_a,1); + end +end + + +%% First, go from T-1 to 1 calculating the Value function and Optimal policy function at each step. +% Since we won't need to keep the value functions for anything later we just store the current one in V +V=V_final; +for ttr=1:T-1 %so tt=T-ttr + + for kk=1:length(PricePathNames) + Parameters.(PricePathNames{kk})=PricePathOld(T-ttr,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); + end + for kk=1:length(ParamPathNames) + Parameters.(ParamPathNames{kk})=ParamPath(T-ttr,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); + end + + if transpathoptions.zpathtrivial==0 + z_gridvals_J=transpathoptions.z_gridvals_J_T(:,:,:,T-ttr); + pi_z_J=transpathoptions.pi_z_J_T(:,:,:,T-ttr); + end + if transpathoptions.epathtrivial==0 + e_gridvals_J=transpathoptions.e_gridvals_J_T(:,:,:,T-ttr); + pi_e_J=transpathoptions.pi_e_J_T(:,:,T-ttr); + end + + [V, Policy]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e(V,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); + % The V input is next period value fn, the V output is this period. + % Policy is kept in the form where it is just a single-value in (d,a') + + PolicyIndexesPath(:,:,:,:,:,T-ttr)=Policy; +end + +%% Modify PolicyIndexesPath into forms needed for forward iteration +% Create version of PolicyIndexesPath called PolicyaprimePath, which only tracks aprime and has j=1:N_j-1 as we don't use N_j to iterate agent dist (there is no N_j+1) +% For fastOLG we use PolicyaprimejPath, if there is z then PolicyaprimejzPath +% When using grid interpolation layer also PolicyProbsPath +if isscalar(n_a1) + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,1:N_j-1,:),[N_a*N_z*N_e,N_j-1,T-1]); % aprime index +elseif length(n_a1)==2 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,1:N_j-1,:)-1),[N_a*N_z*N_e,N_j-1,T-1]); +elseif length(n_a1)==3 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,1:N_j-1,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,:,1:N_j-1,:)-1),[N_a*N_z*N_e,N_j-1,T-1]); +elseif length(n_a1)==4 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,1:N_j-1,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,:,1:N_j-1,:)-1)+prod(n_a1(1:3))*(PolicyIndexesPath(l_d+4,:,:,:,1:N_j-1,:)-1),[N_a*N_z*N_e,N_j-1,T-1]); +end +if simoptions.fastOLG==0 + PolicyaprimezPath_slowOLG=PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1),N_e,1); +elseif simoptions.fastOLG==1 + PolicyaprimePath=reshape(permute(reshape(PolicyaprimePath,[N_a,N_z*N_e,N_j-1,T-1]),[1,3,2,4]),[N_a*(N_j-1)*N_z*N_e,T-1]); + PolicyaprimejzPath=PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:(N_j-1)*N_z-1)',N_a,1),N_e,1); + if simoptions.gridinterplayer==1 + L2index=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,:,:,1:N_j-1,:),[1,N_a,N_z*N_e,N_j-1,T-1]); % PolicyIndexesPath is of size [l_d+l_aprime+1,N_a,N_z,N_e,N_j,T] + L2index=reshape(permute(L2index,[2,4,3,1,5]),[N_a*(N_j-1)*N_z*N_e,1,T-1]); + PolicyaprimejzPath=reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z*N_e,1,T-1]); % reinterpret this as lower grid index + PolicyaprimejzPath=repelem(PolicyaprimejzPath,1,2,1); % create copy that will be the upper grid index + PolicyaprimejzPath(:,2,:)=PolicyaprimejzPath(:,2,:)+1; % upper grid index + PolicyProbsPath(:,2,:)=L2index; % L2 index + PolicyProbsPath(:,2,:)=(PolicyProbsPath(:,2,:)-1)/(1+simoptions.ngridinterp); % probability of upper grid point + PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point + end +end +% Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats +PolicyValuesPath=PolicyInd2Val_FHorz_TPath(PolicyIndexesPath,[n_d1,n_d2],[n_a1,n_a2],[n_z,n_e],N_j,T-1,d_gridvals,a_gridvals,vfoptions,1,0); +PolicyValuesPath=permute(PolicyValuesPath,[2,4,3,1,5]); %[N_a,N_j,N_z*N_e,l_d+l_aprime,T-1] % fastOLG ordering is needed for AggVars + +%% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm +% Call AgentDist the current periods distn and AgentDistnext the next periods distn which we must calculate +AgentDist=AgentDist_initial; +for tt=1:T-1 + + % Get t-1 PricePath and ParamPath before we update them + if use_tminus1price==1 + for pp=1:length(tminus1priceNames) + if tt>1 + Parameters.([tminus1priceNames{pp},'_tminus1'])=Parameters.(tminus1priceNames{pp}); + else + Parameters.([tminus1priceNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1priceNames{pp}); + end + end + end + if use_tminus1params==1 + for pp=1:length(tminus1paramNames) + if tt>1 + Parameters.([tminus1paramNames{pp},'_tminus1'])=Parameters.(tminus1paramNames{pp}); + else + Parameters.([tminus1paramNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1paramNames{pp}); + end + end + end + % Get t-1 AggVars before we update them + if use_tminus1AggVars==1 + for pp=1:length(tminus1AggVarsNames) + if tt>1 + % The AggVars have not yet been updated, so they still contain previous period values + Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); + else + Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); + end + end + end + + % Update current PricePath and ParamPath + for kk=1:length(PricePathNames) + Parameters.(PricePathNames{kk})=PricePathOld(tt,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); + end + for kk=1:length(ParamPathNames) + Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); + end + + % Get t+1 PricePath + if use_tplus1price==1 + for pp=1:length(tplus1priceNames) + kk=tplus1pricePathkk(pp); + Parameters.([tplus1priceNames{pp},'_tplus1'])=PricePathOld(tt+1,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); % Make is so that the time t+1 variables can be used + end + end + + %% Get the current optimal policy, and iterate the agent dist + if transpathoptions.zepathtrivial==0 + ze_gridvals_J_fastOLG=transpathoptions.ze_gridvals_J_T_fastOLG(:,:,:,:,tt); + end + if transpathoptions.zpathtrivial==0 + z_gridvals_J_fastOLG=transpathoptions.ze_gridvals_J_fastOLG(:,:,:,tt); + if simoptions.fastOLG==0 + pi_z_J=transpathoptions.pi_z_J_T(:,:,:,tt); + else + pi_z_J_sim=transpathoptions.pi_z_J_sim_T(:,:,:,tt); + end + end + if transpathoptions.epathtrivial==0 + if simoptions.fastOLG==0 + pi_e_J=transpathoptions.pi_e_J_T(:,:,tt); + else + pi_e_J_sim=transpathoptions.pi_e_J_sim_T(:,:,tt); + end + end + + AgeWeights=AgeWeights_T(:,:,tt); % by coincidence this is for fastOLG=0,1 + + jequalOneDist=jequalOneDist_T(:,tt+1); % Note: t+1 as we are about to create the next period AgentDist + + % No specialization for ExpAsset needed here! + if simoptions.fastOLG==0 + AgentDistnext=AgentDist_FHorz_TPath_SingleStep_Iteration_e_raw(AgentDist,PolicyaprimezPath_slowOLG(:,:,tt),N_a,N_z,N_e,N_j,pi_z_J,pi_e_J,II1,II2,jequalOneDist); + else % simoptions.fastOLG==1 + if simoptions.gridinterplayer==0 + AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_e_raw(AgentDist,PolicyaprimejzPath(:,tt),N_a,N_z,N_e,N_j,pi_z_J_sim,pi_e_J_sim,II1,II2,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); + elseif simoptions.gridinterplayer==1 + AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_nProbs_e_raw(AgentDist,PolicyaprimejzPath(:,:,tt),PolicyProbsPath(:,:,tt),N_a,N_z,N_e,N_j,pi_z_J_sim,pi_e_J_sim,II,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); + end + end + + %% AggVars + AggVars=EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_ze,N_a,N_ze,a_gridvals,ze_gridvals_J_fastOLG,1); + for ff=1:length(AggVarNames) + Parameters.(AggVarNames{ff})=AggVars.(AggVarNames{ff}).Mean; + end + % Keep AggVars in the AggVarsPath + for ff=1:length(AggVarNames) + AggVarsPath(ff,tt)=AggVars.(AggVarNames{ff}).Mean; + end + + + AgentDist=AgentDistnext; +end + +end \ No newline at end of file diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m new file mode 100644 index 00000000..a31e1713 --- /dev/null +++ b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m @@ -0,0 +1,179 @@ +function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions) +% When doing shooting alogrithm on TPath FHorz PType, this is for a given ptype, and does the steps of back-iterate to get policy, then forward to get agent dist and agg vars. +% The only output is the agg vars path. + +error("these are not yet the droids you are looking for") + +AggVarsPath=zeros(length(FnsToEvaluateCell),T-1,'gpuArray'); % Note: does not include the final AggVars, might be good to add them later as a way to make if obvious to user it things are incorrect + +if N_d==0 + l_d=0; +else + l_d=length(n_d); +end +l_a=length(n_a); +l_aprime=length(n_a); +l_z=length(n_z); +l_e=length(n_e); +l_ze=l_z+l_e; +N_ze=N_z*N_e; + +%% +% fastOLG so everything is (a,j,z,e) +% Shapes: +% V is [N_a,N_j,N_z,N_e] +% AgentDist for fastOLG is [N_a*N_j*N_z,N_e] + +if vfoptions.gridinterplayer==0 + PolicyIndexesPath=zeros(l_d+l_aprime,N_a,N_j,N_z,N_e,T-1,'gpuArray'); %Periods 1 to T-1 +elseif vfoptions.gridinterplayer==1 + N_probs=2; + PolicyIndexesPath=zeros(l_d+l_aprime+1,N_a,N_j,N_z,N_e,T-1,'gpuArray'); %Periods 1 to T-1 + PolicyProbsPath=zeros(N_a*(N_j-1)*N_z*N_e,N_probs,T-1,'gpuArray'); % preallocate +end + +%% First, go from T-1 to 1 calculating the Value function and Optimal policy function at each step. +% Since we won't need to keep the value functions for anything later we just store the current one in V +V=V_final; +for ttr=1:T-1 %so tt=T-ttr + + for kk=1:length(PricePathNames) + Parameters.(PricePathNames{kk})=PricePathOld(T-ttr,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); + end + for kk=1:length(ParamPathNames) + Parameters.(ParamPathNames{kk})=ParamPath(T-ttr,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); + end + + if transpathoptions.zpathtrivial==0 + pi_z_J=transpathoptions.pi_z_J_T(:,:,:,T-ttr); % fastOLG value function uses (j,z',z) + z_gridvals_J=transpathoptions.z_gridvals_J(:,:,T-ttr); + end + if transpathoptions.epathtrivial==0 + pi_e_J=transpathoptions.pi_e_J_T(:,1,:,T-ttr); + e_gridvals_J=transpathoptions.e_gridvals_J_T(:,:,:,:,T-ttr); + end + + [V, Policy]=ValueFnIter_FHorz_TPath_SingleStep_fastOLG_e(V,n_d,n_a,n_z,n_e,N_j,d_grid, a_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + % The V input is next period value fn, the V output is this period. + % Policy in fastOLG is [N_a,N_j,N_z,N_e] and contains the joint-index for (d,aprime) + + PolicyIndexesPath(:,:,:,:,:,T-ttr)=Policy; % fastOLG: so a-by-j-by-z-by-e + +end + +%% Modify PolicyIndexesPath into forms needed for forward iteration +% Create version of PolicyIndexesPath in form we want for the agent distribution iteration +% Creates PolicyaprimejzPath (omits j=N_j), and when using grid interpolation layer also PolicyProbsPath +if isscalar(n_a) + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,1:N_j-1,:,:,:),[N_a*(N_j-1)*N_z*N_e,T-1]); % aprime index +elseif length(n_a)==2 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,1:N_j-1,:,:,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,1:N_j-1,:,:,:)-1),[N_a*(N_j-1)*N_z*N_e,T-1]); +elseif length(n_a)==3 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,1:N_j-1,:,:,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,1:N_j-1,:,:,:)-1)+n_a(1)*n_a(2)*(PolicyIndexesPath(l_d+3,:,1:N_j-1,:,:,:)-1),[N_a*(N_j-1)*N_z*N_e,T-1]); +elseif length(n_a)==4 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,1:N_j-1,:,:,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,1:N_j-1,:,:,:)-1)+n_a(1)*n_a(2)*(PolicyIndexesPath(l_d+3,:,1:N_j-1,:,:,:)-1)+n_a(1)*n_a(2)*n_a(3)*(PolicyIndexesPath(l_d+4,:,1:N_j-1,:,:,:)-1),[N_a*(N_j-1)*N_z*N_e,T-1]); +end +PolicyaprimejzPath=PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:(N_j-1)*N_z-1)',N_a,1),N_e,1); +if simoptions.gridinterplayer==1 + L2index=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,1:N_j-1,:,:,:),[N_a*(N_j-1)*N_z*N_e,1,T-1]); % PolicyIndexesPath is of size [l_d+l_aprime+1,N_a,N_j,N_z,N_e,T-1] + PolicyaprimejzPath=reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z*N_e,1,T-1]); % reinterpret this as lower grid index + PolicyaprimejzPath=repelem(PolicyaprimejzPath,1,2,1); % create copy that will be the upper grid index + PolicyaprimejzPath(:,2,:)=PolicyaprimejzPath(:,2,:)+1; % upper grid index + PolicyProbsPath(:,2,:)=L2index; % L2 index + PolicyProbsPath(:,2,:)=(PolicyProbsPath(:,2,:)-1)/(1+simoptions.ngridinterp); % probability of upper grid point + PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point +end +% Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats +PolicyValuesPath=PolicyInd2Val_FHorz_TPath(PolicyIndexesPath,n_d,n_a,[n_z,n_e],N_j,T-1,d_gridvals,aprime_gridvals,vfoptions,1,1); % [size(PolicyValuesPath,1),N_a,N_j,N_z*N_e,T] +PolicyValuesPath=permute(PolicyValuesPath,[2,3,4,1,5]); %[N_a,N_j,N_z*N_e,l_d+l_aprime,T-1] % fastOLG ordering is needed for AggVars + + +%% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm +% Call AgentDist the current periods distn and AgentDistnext the next periods distn which we must calculate +AgentDist=AgentDist_initial; +for tt=1:T-1 + + % Get t-1 PricePath and ParamPath before we update them + if use_tminus1price==1 + for pp=1:length(tminus1priceNames) + if tt>1 + Parameters.([tminus1priceNames{pp},'_tminus1'])=Parameters.(tminus1priceNames{pp}); + else + Parameters.([tminus1priceNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1priceNames{pp}); + end + end + end + if use_tminus1params==1 + for pp=1:length(tminus1paramNames) + if tt>1 + Parameters.([tminus1paramNames{pp},'_tminus1'])=Parameters.(tminus1paramNames{pp}); + else + Parameters.([tminus1paramNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1paramNames{pp}); + end + end + end + % Get t-1 AggVars before we update them + if use_tminus1AggVars==1 + for pp=1:length(tminus1AggVarsNames) + if tt>1 + % The AggVars have not yet been updated, so they still contain previous period values + Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); + else + Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); + end + end + end + + % Update current PricePath and ParamPath + for kk=1:length(PricePathNames) + Parameters.(PricePathNames{kk})=PricePathOld(tt,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); + end + for kk=1:length(ParamPathNames) + Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); + end + + % Get t+1 PricePath + if use_tplus1price==1 + for pp=1:length(tplus1priceNames) + kk=tplus1pricePathkk(pp); + Parameters.([tplus1priceNames{pp},'_tplus1'])=PricePathOld(tt+1,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); % Make is so that the time t+1 variables can be used + end + end + + %% Get the current optimal policy, and iterate the agent dist + if transpathoptions.zepathtrivial==0 + ze_gridvals_J_fastOLG=transpathoptions.ze_gridvals_J_T_fastOLG(:,:,:,:,tt); + end + if transpathoptions.zpathtrivial==0 + pi_z_J_sim=transpathoptions.pi_z_J_sim_T(:,:,:,tt); + end + if transpathoptions.epathtrivial==0 + pi_e_J_sim=transpathoptions.pi_e_J_sim_T(:,:,tt); % (a,j,z)-by-e + end + + jequalOneDist=jequalOneDist_T(:,tt+1); % Note: t+1 as we are about to create the next period AgentDist + + AgeWeights=AgeWeights_T(:,:,tt); % by coincidence this is for fastOLG=0,1 + + % simoptions.fastOLG=1 is hardcoded + if simoptions.gridinterplayer==0 + AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_e_raw(AgentDist,PolicyaprimejzPath(:,tt),N_a,N_z,N_e,N_j,pi_z_J_sim, pi_e_J_sim,II1orII,II2,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); % II1orII is II1 + elseif simoptions.gridinterplayer==1 + AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_nProbs_e_raw(AgentDist,PolicyaprimejzPath(:,:,tt),PolicyProbsPath(:,:,tt),N_a,N_z,N_e,N_j,pi_z_J_sim,pi_e_J_sim,II1orII,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); % II1orII is II + end + + %% AggVars + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_a,l_a,l_ze,N_a,N_ze,a_gridvals,ze_gridvals_J_fastOLG,1); + for ff=1:length(AggVarNames) + Parameters.(AggVarNames{ff})=AggVars.(AggVarNames{ff}).Mean; + end + % Keep AggVars in the AggVarsPath + for ff=1:length(AggVarNames) + AggVarsPath(ff,tt)=AggVars.(AggVarNames{ff}).Mean; + end + + AgentDist=AgentDistnext; +end + + +end diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m new file mode 100644 index 00000000..527d9c88 --- /dev/null +++ b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m @@ -0,0 +1,205 @@ +function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,N_d,n_d,N_a,n_a,N_z,n_z,N_j,d_grid,a_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,z_gridvals_J_fastOLG,ReturnFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, transpathoptions, vfoptions, simoptions) +% When doing shooting alogrithm on TPath FHorz PType, this is for a given ptype, and does the steps of back-iterate to get policy, then forward to get agent dist and agg vars. +% The only output is the agg vars path. + +error("these are not yet the droids you are looking for") + +AggVarsPath=zeros(length(FnsToEvaluateCell),T-1,'gpuArray'); % Note: does not include the final AggVars, might be good to add them later as a way to make if obvious to user it things are incorrect + +if N_d==0 + l_d=0; +else + l_d=length(n_d); +end +l_a=length(n_a); +l_aprime=length(n_a); +l_z=length(n_z); + +%% +% Shapes: +% V is [N_a,N_z,N_j] +% AgentDist for basic is [N_a*N_z,N_j] +% AgentDist for fastOLG is [N_a*N_j*N_z,1] + +if vfoptions.gridinterplayer==0 + PolicyIndexesPath=zeros(l_d+l_aprime,N_a,N_z,N_j,T-1,'gpuArray'); %Periods 1 to T-1 +elseif vfoptions.gridinterplayer==1 + PolicyIndexesPath=zeros(l_d+l_aprime+1,N_a,N_z,N_j,T-1,'gpuArray'); %Periods 1 to T-1 +end +if simoptions.gridinterplayer==0 + if simoptions.fastOLG==0 + II1=1:1:N_a*N_z; + II2=ones(N_a*N_z,1); + elseif simoptions.fastOLG==1 + II1=1:1:N_a*(N_j-1)*N_z; + II2=ones(N_a*(N_j-1)*N_z,1); + exceptlastj=repmat((1:1:N_a)',(N_j-1)*N_z,1)+repmat(repelem(N_a*(0:1:N_j-2)',N_a,1),N_z,1)+repelem(N_a*N_j*(0:1:N_z-1)',N_a*(N_j-1),1); + exceptfirstj=repmat((1:1:N_a)',(N_j-1)*N_z,1)+repmat(repelem(N_a*(1:1:N_j-1)',N_a,1),N_z,1)+repelem(N_a*N_j*(0:1:N_z-1)',N_a*(N_j-1),1); + justfirstj=repmat((1:1:N_a)',N_z,1)+N_a*N_j*repelem((0:1:N_z-1)',N_a,1); + end +elseif simoptions.gridinterplayer==1 + N_probs=2; + if simoptions.fastOLG==0 + error('Cannot use simoptions.fastOLG=0 with grid interpolation layer') + elseif simoptions.fastOLG==1 + PolicyProbsPath=zeros(N_a*(N_j-1)*N_z,N_probs,T-1,'gpuArray'); % preallocate + II=repelem((1:1:N_a*(N_j-1)*N_z)',1,N_probs); + exceptlastj=repmat((1:1:N_a)',(N_j-1)*N_z,1)+repmat(repelem(N_a*(0:1:N_j-2)',N_a,1),N_z,1)+repelem(N_a*N_j*(0:1:N_z-1)',N_a*(N_j-1),1); + exceptfirstj=repmat((1:1:N_a)',(N_j-1)*N_z,1)+repmat(repelem(N_a*(1:1:N_j-1)',N_a,1),N_z,1)+repelem(N_a*N_j*(0:1:N_z-1)',N_a*(N_j-1),1); + justfirstj=repmat((1:1:N_a)',N_z,1)+N_a*N_j*repelem((0:1:N_z-1)',N_a,1); + end +end + + +%% First, go from T-1 to 1 calculating the Value function and Optimal policy function at each step. +% Since we won't need to keep the value functions for anything later we just store the current one in V +V=V_final; +for ttr=1:T-1 %so tt=T-ttr + + for kk=1:length(PricePathNames) + Parameters.(PricePathNames{kk})=PricePathOld(T-ttr,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); + end + for kk=1:length(ParamPathNames) + Parameters.(ParamPathNames{kk})=ParamPath(T-ttr,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); + end + + if transpathoptions.zpathtrivial==0 + z_gridvals_J=transpathoptions.z_gridvals_J_T(:,:,:,T-ttr); + pi_z_J=transpathoptions.pi_z_J_T(:,:,:,T-ttr); + end + + [V, Policy]=ValueFnIter_FHorz_TPath_SingleStep(V,n_d,n_a,n_z,N_j,d_gridvals, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + % The V input is next period value fn, the V output is this period. + % Policy is kept in the form where it is just a single-value in (d,a') + + PolicyIndexesPath(:,:,:,:,T-ttr)=Policy; +end + +%% Modify PolicyIndexesPath into forms needed for forward iteration +% Create version of PolicyIndexesPath called PolicyaprimePath, which only tracks aprime and has j=1:N_j-1 as we don't use N_j to iterate agent dist (there is no N_j+1) +% For fastOLG we use PolicyaprimejPath, if there is z then PolicyaprimejzPath +% When using grid interpolation layer also PolicyProbsPath +if isscalar(n_a) + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:),[N_a*N_z,N_j-1,T-1]); % aprime index +elseif length(n_a)==2 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); +elseif length(n_a)==3 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1)+n_a(1)*n_a(2)*(PolicyIndexesPath(l_d+3,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); +elseif length(n_a)==4 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1)+n_a(1)*n_a(2)*(PolicyIndexesPath(l_d+3,:,:,1:N_j-1,:)-1)+n_a(1)*n_a(2)*n_a(3)*(PolicyIndexesPath(l_d+4,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); +end +if simoptions.fastOLG==0 + PolicyaprimezPath_slowOLG=PolicyaprimePath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1); +elseif simoptions.fastOLG==1 + PolicyaprimePath=reshape(permute(reshape(PolicyaprimePath,[N_a,N_z,N_j-1,T-1]),[1,3,2,4]),[N_a*(N_j-1)*N_z,T-1]); + PolicyaprimejzPath=PolicyaprimePath+repelem(N_a*gpuArray(0:1:(N_j-1)*N_z-1)',N_a,1); + if simoptions.gridinterplayer==1 + L2index=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,:,1:N_j-1,:),[1,N_a,N_z,N_j-1,T-1]); % PolicyIndexesPath is of size [l_d+l_aprime+1,N_a,N_z,N_j,T] + L2index=reshape(permute(L2index,[2,4,3,1,5]),[N_a*(N_j-1)*N_z,1,T-1]); + PolicyaprimejzPath=reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z,1,T-1]); % reinterpret this as lower grid index + PolicyaprimejzPath=repelem(PolicyaprimejzPath,1,2,1); % create copy that will be the upper grid index + PolicyaprimejzPath(:,2,:)=PolicyaprimejzPath(:,2,:)+1; % upper grid index + PolicyProbsPath(:,2,:)=L2index; % L2 index + PolicyProbsPath(:,2,:)=(PolicyProbsPath(:,2,:)-1)/(1+simoptions.ngridinterp); % probability of upper grid point + PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point + end +end +% Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats +PolicyValuesPath=PolicyInd2Val_FHorz_TPath(PolicyIndexesPath,n_d,n_a,n_z,N_j,T-1,d_grid,a_grid,vfoptions,1,0); +PolicyValuesPath=permute(PolicyValuesPath,[2,4,3,1,5]); %[N_a,N_j,N_z,l_d+l_aprime,T-1] % fastOLG ordering is needed for AggVars + +%% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm +% Call AgentDist the current periods distn and AgentDistnext the next periods distn which we must calculate +AgentDist=AgentDist_initial; +for tt=1:T-1 + + % Get t-1 PricePath and ParamPath before we update them + if use_tminus1price==1 + for pp=1:length(tminus1priceNames) + if tt>1 + Parameters.([tminus1priceNames{pp},'_tminus1'])=Parameters.(tminus1priceNames{pp}); + else + Parameters.([tminus1priceNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1priceNames{pp}); + end + end + end + if use_tminus1params==1 + for pp=1:length(tminus1paramNames) + if tt>1 + Parameters.([tminus1paramNames{pp},'_tminus1'])=Parameters.(tminus1paramNames{pp}); + else + Parameters.([tminus1paramNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1paramNames{pp}); + end + end + end + % Get t-1 AggVars before we update them + if use_tminus1AggVars==1 + for pp=1:length(tminus1AggVarsNames) + if tt>1 + % The AggVars have not yet been updated, so they still contain previous period values + Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); + else + Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); + end + end + end + + % Update current PricePath and ParamPath + for kk=1:length(PricePathNames) + Parameters.(PricePathNames{kk})=PricePathOld(tt,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); + end + for kk=1:length(ParamPathNames) + Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); + end + + % Get t+1 PricePath + if use_tplus1price==1 + for pp=1:length(tplus1priceNames) + kk=tplus1pricePathkk(pp); + Parameters.([tplus1priceNames{pp},'_tplus1'])=PricePathOld(tt+1,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); % Make is so that the time t+1 variables can be used + end + end + + %% Get the current optimal policy, and iterate the agent dist + if transpathoptions.zpathtrivial==0 + z_gridvals_J_fastOLG=transpathoptions.ze_gridvals_J_fastOLG(:,:,:,tt); + if simoptions.fastOLG==0 + pi_z_J=transpathoptions.pi_z_J_T(:,:,:,tt); + else + pi_z_J_sim=transpathoptions.pi_z_J_sim_T(:,:,:,tt); + end + end + + if simoptions.fastOLG==0 + AgeWeights=AgeWeights_T(:,:,tt); + else % simoptions.fastOLG==1 + AgeWeights=AgeWeights_T(:,tt); + end + + jequalOneDist=jequalOneDist_T(:,tt+1); % Note: t+1 as we are about to create the next period AgentDist + + if simoptions.fastOLG==0 + AgentDistnext=AgentDist_FHorz_TPath_SingleStep_Iteration_raw(AgentDist,PolicyaprimezPath_slowOLG(:,:,tt),N_a,N_z,N_j,pi_z_J,II1,II2,jequalOneDist); + else % simoptions.fastOLG==1 + if simoptions.gridinterplayer==0 + AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_raw(AgentDist,PolicyaprimejzPath(:,tt),N_a,N_z,N_j,pi_z_J_sim,II1,II2,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); + elseif simoptions.gridinterplayer==1 + AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_nProbs_raw(AgentDist,PolicyaprimejzPath(:,:,tt),PolicyProbsPath(:,:,tt),N_a,N_z,N_j,pi_z_J_sim,II,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); + end + end + + %% AggVars + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_a,l_a,l_z,N_a,N_z,a_gridvals,z_gridvals_J_fastOLG,1); + for ff=1:length(AggVarNames) + Parameters.(AggVarNames{ff})=AggVars.(AggVarNames{ff}).Mean; + end + % Keep AggVars in the AggVarsPath + for ff=1:length(AggVarNames) + AggVarsPath(ff,tt)=AggVars.(AggVarNames{ff}).Mean; + end + + + AgentDist=AgentDistnext; +end + +end \ No newline at end of file diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m index 16382bad..e294cec9 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m @@ -133,20 +133,35 @@ %% Some internal commands require a few vfoptions and simoptions to be set vfoptions.EVpre=0; % Not actually an option that can be used here -if ~isfield(vfoptions, 'experienceasset') - vfoptions.experienceasset=0; +if ~isfield(vfoptions,'verbose') + vfoptions.verbose=0; end -if ~isfield(vfoptions,'lowmemory') - lowmemory_in_PType=0; +if ~isfield(vfoptions,'experienceasset') + for ii=1:N_i + vfoptions.experienceasset.(Names_i{ii})=0; + end +else + % User created vfoptions.PType.experienceasset; we have Names_i for ii=1:N_i - if isfield(vfoptions.Name_i{ii}, 'lowmemory') - lowmemory_in_PType=1; - elseif lowmemory_in_PType==1 - vfoptions.Name_i{ii}.lowmemory=0; + if isfield(vfoptions.experienceasset, Names_i{ii}) + if ~isfield(vfoptions, 'aprimeFn') || ~isfield(vfoptions.aprimeFn, Names_i{ii}) + error('To use an experience asset you must define vfoptions.aprimeFn') + end + else + vfoptions.experienceasset.(Names_i{ii})=0; end end - if lowmemory_in_PType==0 - vfoptions.lowmemory=0; +end +if ~isfield(vfoptions,'lowmemory') + for ii=1:N_i + vfoptions.lowmemory.(Names_i{ii})=0; + end +else + % User created vfoptions.PType.lowmemory; we have Names_i + for ii=1:N_i + if ~isfield(vfoptions.lowmemory, Names_i{ii}) + vfoptions.lowmemory.(Names_i{ii})=0; + end end end @@ -308,7 +323,7 @@ if N_d==0 PTypeStructure.(iistr).l_d=0; else - PTypeStructure.(iistr).l_d=length(n_d); + PTypeStructure.(iistr).l_d=length(PTypeStructure.(iistr).n_d); end if isa(n_a,'struct') PTypeStructure.(iistr).n_a=n_a.(iistr); @@ -357,31 +372,92 @@ else PTypeStructure.(iistr).z_grid=gpuArray(z_grid); end - % to be able to EvalFnsOnAgentDist using fastOLG we also need - PTypeStructure.(iistr).a_gridvals=gpuArray(CreateGridvals(PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).a_grid,1)); % a_grivdals is [N_a,l_a] - % use fine grid for aprime_gridvals - if PTypeStructure.(iistr).vfoptions.gridinterplayer==0 - PTypeStructure.(iistr).aprime_gridvals=PTypeStructure.(iistr).a_gridvals; - elseif PTypeStructure.(iistr).vfoptions.gridinterplayer==1 + + if PTypeStructure.(iistr).vfoptions.experienceasset + % Borrowed from TransitionPaths/InfHorz/TransitionPath_Case1.m + % Split decision variables into the standard ones and the one relevant to the experience asset + if isscalar(PTypeStructure.(iistr).n_d) + PTypeStructure.(iistr).n_d1=0; + else + PTypeStructure.(iistr).n_d1=PTypeStructure.(iistr).n_d(1:end-1); + end + PTypeStructure.(iistr).n_d2=PTypeStructure.(iistr).n_d(end); % n_d2 is the decision variable that influences next period vale of the experience asset + PTypeStructure.(iistr).d1_grid=PTypeStructure.(iistr).d_grid(1:sum(PTypeStructure.(iistr).n_d1)); + PTypeStructure.(iistr).d2_grid=PTypeStructure.(iistr).d_grid(sum(PTypeStructure.(iistr).n_d1)+1:end); + % Split endogenous assets into the standard ones and the experience asset if isscalar(PTypeStructure.(iistr).n_a) - n_aprime=PTypeStructure.(iistr).n_a+(PTypeStructure.(iistr).n_a-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; - aprime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).N_a)',PTypeStructure.(iistr).a_grid,gpuArray(linspace(1,PTypeStructure.(iistr).N_a,n_aprime))'); - PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); + PTypeStructure.(iistr).n_a1=0; + else + PTypeStructure.(iistr).n_a1=PTypeStructure.(iistr).n_a(1:end-1); + end + PTypeStructure.(iistr).n_a2=PTypeStructure.(iistr).n_a(end); % n_a2 is the experience asset + PTypeStructure.(iistr).a1_grid=PTypeStructure.(iistr).a_grid(1:sum(PTypeStructure.(iistr).n_a1)); + PTypeStructure.(iistr).a2_grid=PTypeStructure.(iistr).a_grid(sum(PTypeStructure.(iistr).n_a1)+1:end); + + % aprimeFnParamNames in same fashion + l_d2=length(PTypeStructure.(iistr).n_d2); + l_a2=length(PTypeStructure.(iistr).n_a2); + temp=getAnonymousFnInputNames(PTypeStructure.(iistr).vfoptions.aprimeFn); + if length(temp)>(l_d2+l_a2) + PTypeStructure.(iistr).aprimeFnParamNames={temp{l_d2+l_a2+1:end}}; % the first inputs will always be (d2,a2) else - a1_grid=PTypeStructure.(iistr).a_grid(1:PTypeStructure.(iistr).n_a(1)); - n_a1prime=PTypeStructure.(iistr).n_a(1)+(PTypeStructure.(iistr).n_a(1)-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; - n_aprime=[n_a1prime,PTypeStructure.(iistr).n_a(2:end)]; - a1prime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).n_a(1))',a1_grid,gpuArray(linspace(1,PTypeStructure.(iistr).n_a(1),n_a1prime))'); - aprime_grid=[a1prime_grid; PTypeStructure.(iistr).a_grid(PTypeStructure.(iistr).n_a(1)+1:end)]; - PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); + PTypeStructure.(iistr).aprimeFnParamNames={}; + end + + PTypeStructure.(iistr).N_a1=prod(PTypeStructure.(iistr).n_a1); + + % to be able to EvalFnsOnAgentDist using fastOLG we also need + PTypeStructure.(iistr).a_gridvals=gpuArray(CreateGridvals(PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).a_grid,1)); % a_grivdals is [N_a,l_a] + % use fine grid for aprime_gridvals + if PTypeStructure.(iistr).vfoptions.gridinterplayer==0 + PTypeStructure.(iistr).aprime_gridvals=PTypeStructure.(iistr).a_gridvals; + elseif PTypeStructure.(iistr).vfoptions.gridinterplayer==1 + if isscalar(PTypeStructure.(iistr).n_a) + n_aprime=PTypeStructure.(iistr).n_a+(PTypeStructure.(iistr).n_a-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; + aprime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).N_a)',PTypeStructure.(iistr).a_grid,gpuArray(linspace(1,PTypeStructure.(iistr).N_a,n_aprime))'); + PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); + else + a1_grid=PTypeStructure.(iistr).a_grid(1:PTypeStructure.(iistr).n_a(1)); + n_a1prime=PTypeStructure.(iistr).n_a(1)+(PTypeStructure.(iistr).n_a(1)-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; + n_aprime=[n_a1prime,PTypeStructure.(iistr).n_a(2:end)]; + a1prime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).n_a(1))',a1_grid,gpuArray(linspace(1,PTypeStructure.(iistr).n_a(1),n_a1prime))'); + aprime_grid=[a1prime_grid; PTypeStructure.(iistr).a_grid(PTypeStructure.(iistr).n_a(1)+1:end)]; + PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); + end end + PTypeStructure.(iistr).d_gridvals=CreateGridvals(PTypeStructure.(iistr).n_d,gpuArray(PTypeStructure.(iistr).d_grid),1); + % if N_d==0 + % PTypeStructure.(iistr).daprime_gridvals=gpuArray(PTypeStructure.(iistr).a_gridvals); + % else + % PTypeStructure.(iistr).daprime_gridvals=gpuArray([kron(ones(N_a,1),CreateGridvals(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).d_grid,1)), kron(PTypeStructure.(iistr).a_gridvals,ones(PTypeStructure.(iistr).N_d,1))]); % daprime_gridvals is [N_d*N_aprime,l_d+l_aprime] + % end + else + % to be able to EvalFnsOnAgentDist using fastOLG we also need + PTypeStructure.(iistr).a_gridvals=gpuArray(CreateGridvals(PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).a_grid,1)); % a_grivdals is [N_a,l_a] + % use fine grid for aprime_gridvals + if PTypeStructure.(iistr).vfoptions.gridinterplayer==0 + PTypeStructure.(iistr).aprime_gridvals=PTypeStructure.(iistr).a_gridvals; + elseif PTypeStructure.(iistr).vfoptions.gridinterplayer==1 + if isscalar(PTypeStructure.(iistr).n_a) + n_aprime=PTypeStructure.(iistr).n_a+(PTypeStructure.(iistr).n_a-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; + aprime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).N_a)',PTypeStructure.(iistr).a_grid,gpuArray(linspace(1,PTypeStructure.(iistr).N_a,n_aprime))'); + PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); + else + a1_grid=PTypeStructure.(iistr).a_grid(1:PTypeStructure.(iistr).n_a(1)); + n_a1prime=PTypeStructure.(iistr).n_a(1)+(PTypeStructure.(iistr).n_a(1)-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; + n_aprime=[n_a1prime,PTypeStructure.(iistr).n_a(2:end)]; + a1prime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).n_a(1))',a1_grid,gpuArray(linspace(1,PTypeStructure.(iistr).n_a(1),n_a1prime))'); + aprime_grid=[a1prime_grid; PTypeStructure.(iistr).a_grid(PTypeStructure.(iistr).n_a(1)+1:end)]; + PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); + end + end + PTypeStructure.(iistr).d_gridvals=CreateGridvals(PTypeStructure.(iistr).n_d,gpuArray(PTypeStructure.(iistr).d_grid),1); + % if N_d==0 + % PTypeStructure.(iistr).daprime_gridvals=gpuArray(PTypeStructure.(iistr).a_gridvals); + % else + % PTypeStructure.(iistr).daprime_gridvals=gpuArray([kron(ones(N_a,1),CreateGridvals(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).d_grid,1)), kron(PTypeStructure.(iistr).a_gridvals,ones(PTypeStructure.(iistr).N_d,1))]); % daprime_gridvals is [N_d*N_aprime,l_d+l_aprime] + % end end - PTypeStructure.(iistr).d_gridvals=CreateGridvals(PTypeStructure.(iistr).n_d,gpuArray(PTypeStructure.(iistr).d_grid),1); - % if N_d==0 - % PTypeStructure.(iistr).daprime_gridvals=gpuArray(PTypeStructure.(iistr).a_gridvals); - % else - % PTypeStructure.(iistr).daprime_gridvals=gpuArray([kron(ones(N_a,1),CreateGridvals(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).d_grid,1)), kron(PTypeStructure.(iistr).a_gridvals,ones(PTypeStructure.(iistr).N_d,1))]); % daprime_gridvals is [N_d*N_aprime,l_d+l_aprime] - % end if isa(pi_z,'struct') PTypeStructure.(iistr).pi_z=pi_z.(iistr); % Different grids by permanent type, but not depending on age. (same as the case just above; this case can occour with or without the existence of vfoptions, as long as there is no vfoptions.agedependentgrids) @@ -437,7 +513,13 @@ else l_d=length(PTypeStructure.(iistr).n_d); end - l_a=length(PTypeStructure.(iistr).n_a); + if PTypeStructure.(iistr).vfoptions.experienceasset + l_aprime=length(PTypeStructure.(iistr).n_a1); + l_a=l_aprime+length(PTypeStructure.(iistr).n_a2); + else + l_aprime=length(PTypeStructure.(iistr).n_a); + l_a=l_aprime; + end l_z=length(PTypeStructure.(iistr).n_z); if PTypeStructure.(iistr).n_z(1)==0 l_z=0; @@ -451,13 +533,8 @@ l_e=length(PTypeStructure.(iistr).vfoptions.n_e); end end - % Figure out ReturnFnParamNames from ReturnFn - temp=getAnonymousFnInputNames(PTypeStructure.(iistr).ReturnFn); - if length(temp)>(l_d+l_a+l_a+l_z+l_e) % This is largely pointless, the ReturnFn is always going to have some parameters - ReturnFnParamNames={temp{l_d+l_a+l_a+l_z+l_e+1:end}}; % the first inputs will always be (d,aprime,a,z) - else - ReturnFnParamNames={}; - end + %% Implement new way of handling ReturnFn inputs + ReturnFnParamNames=ReturnFnParamNamesFn(PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).vfoptions,Parameters); PTypeStructure.(iistr).ReturnFnParamNames=ReturnFnParamNames; @@ -487,7 +564,7 @@ PTypeStructure.(iistr).FnsToEvaluate.(FnNames{kk})=FnsToEvaluate.(FnNames{kk}); % Figure out FnsToEvaluateParamNames temp=getAnonymousFnInputNames(FnsToEvaluate.(FnNames{kk})); - PTypeStructure.(iistr).FnsToEvaluateParamNames(jj).Names={temp{l_d+l_a+l_a+l_z+l_e+1:end}}; % the first inputs will always be (d,aprime,a,z) + PTypeStructure.(iistr).FnsToEvaluateParamNames(jj).Names={temp{l_d+l_aprime+l_a+l_z+l_e+1:end}}; % the first inputs will always be (d,aprime,a,z) PTypeStructure.(iistr).WhichFnsForCurrentPType(kk)=jj; jj=jj+1; PTypeStructure.FnsAndPTypeIndicator(kk,ii)=1; end diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m index f8ba00bc..ef277367 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m @@ -128,7 +128,7 @@ % After that is finished we can put the AggVars together, evaluate GE conditions, and update price path AggVarsFullPath=zeros(PTypeStructure.numFnsToEvaluate,T-1,N_i); % Does not include period T for ii=1:N_i - iistr=PTypeStructure.Names_i{ii}; + iistr=PTypeStructure.Names_i{ii}; % Following few lines I would normally do outside of the while loop, but have to set them for each ptype % AgentDist=AgentDist_initial.(iistr); diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m new file mode 100644 index 00000000..973246b5 --- /dev/null +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m @@ -0,0 +1,30 @@ +function [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep(VKron,n_d,n_a,n_z,N_j,d_gridvals, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions) +% The VKron input is next period value fn, the VKron output is this period. + +error("these are not yet the droids you are looking for") + +N_d=prod(n_d); +N_a=prod(n_a); +N_z=prod(n_z); + +%% If get to here then not using exoticpreferences nor StateDependentVariables_z +% N_z==0 is handled by a different command +if vfoptions.divideandconquer==0 + if vfoptions.gridinterplayer==0 + if N_d==0 + [VKron,PolicyKron]=ValueFnIter_FHorz_TPath_SingleStep_nod_raw(VKron,n_a, n_z, N_j, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + else + [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_SingleStep_raw(VKron,n_d,n_a,n_z, N_j, d_gridvals, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + end + else + error('Have not yet implemented grid interpolation layer for FHorz TPath without fastOLG=1. Ask on forum if you need this.') + end +else + error('Have not yet implemented divide and conquer for FHorz TPath without fastOLG=1. Ask on forum if you need this.') +end + +%% Policy in transition paths +PolicyKron=UnKronPolicyIndexes_Case1_FHorz(PolicyKron,n_d,n_a,N_z,N_j,vfoptions); +PolicyKron=reshape(PolicyKron,[size(PolicyKron,1),N_a,N_z,N_j]); + +end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e.m new file mode 100644 index 00000000..f2fe3ed5 --- /dev/null +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e.m @@ -0,0 +1,30 @@ +function [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e(VKron,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) +% The VKron input is next period value fn, the VKron output is this period. + +N_d1=prod(n_d1); +% N_a1=prod(n_a1); +N_z=prod(n_z); +N_e=prod(n_e); + +%% If get to here then not using exoticpreferences nor StateDependentVariables_z +% N_z==0 is handled by a different command +if vfoptions.divideandconquer==0 + if vfoptions.gridinterplayer==0 + if N_d1==0 + error("not implemented") + [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_nod_e_raw(VKron,n_a, n_z,n_e, N_j, a_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + else + [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw(VKron,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); + end + else + error('Have not yet implemented grid interpolation layer for FHorz TPath without fastOLG=1. Ask on forum if you need this.') + end +else + error('Have not yet implemented divide and conquer for FHorz TPath without fastOLG=1. Ask on forum if you need this.') +end + +%% Policy in transition paths +PolicyKron=UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e(PolicyKron,n_d1,n_d2,n_a1,n_a2,N_z,N_e,N_j,vfoptions); +PolicyKron=reshape(PolicyKron,[size(PolicyKron,1),prod(n_a1)*prod(n_a2),N_z,N_e,N_j]); + +end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw.m new file mode 100644 index 00000000..9b59bd6f --- /dev/null +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw.m @@ -0,0 +1,201 @@ +function [V,Policy3]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) + +N_d1=prod(n_d1); +N_d2=prod(n_d2); +N_d=N_d1*N_d2; +N_a1=prod(n_a1); +N_a2=prod(n_a2); +N_a=N_a1*N_a2; +N_z=prod(n_z); +N_e=prod(n_e); + +V=zeros(N_a,N_z,N_e,N_j,'gpuArray'); +Policy=zeros(N_a,N_z,N_e,N_j,'gpuArray'); %first dim indexes the optimal choice for d and a1prime rest of dimensions a,z + +%% +a2_gridvals=CreateGridvals(n_a2,a2_grid,1); + +if vfoptions.lowmemory==1 + special_n_e=ones(1,length(n_e)); +elseif vfoptions.lowmemory==2 + special_n_e=ones(1,length(n_e)); + special_n_z=ones(1,length(n_z)); +elseif vfoptions.lowmemory==3 + special_n_e=ones(1,length(n_e)); + special_n_z=ones(1,length(n_z)); + special_n_ea=ones(1,length(n_a2)); +end + +%% j=N_j +% Create a vector containing all the return function parameters (in order) +ReturnFnParamsVec=CreateVectorFromParams(Parameters, ReturnFnParamNames,N_j); + +if vfoptions.lowmemory==0 + ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); + %Calc the max and its index + [Vtemp,maxindex]=max(ReturnMatrix,[],1); + V(:,:,:,N_j)=Vtemp; + Policy(:,:,:,N_j)=maxindex; +elseif vfoptions.lowmemory==1 + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_val, ReturnFnParamsVec,0,0); + %Calc the max and its index + [Vtemp,maxindex]=max(ReturnMatrix_e,[],1); + V(:,:,e_c,N_j)=Vtemp; + Policy(:,:,e_c,N_j)=maxindex; + end +elseif vfoptions.lowmemory==2 + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2, special_n_z, special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); + % Calc the max and its index + [Vtemp,maxindex]=max(ReturnMatrix_ze); + V(:,z_c,e_c,N_j)=Vtemp; + Policy(:,z_c,e_c,N_j)=maxindex; + end + end +elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1, special_n_ea, special_n_z, special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + % Calc the max and its index + [Vtemp,maxindex]=max(ReturnMatrix_ze); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=maxindex; + end + end + end +end + +%% Iterate backwards through j. +for reverse_j=1:N_j-1 + jj=N_j-reverse_j; + + if vfoptions.verbose==1 + fprintf('Finite horizon: %i of %i \n',jj, N_j) + end + + % Create a vector containing all the return function parameters (in order) + ReturnFnParamsVec=CreateVectorFromParams(Parameters, ReturnFnParamNames,jj); + DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,jj); + DiscountFactorParamsVec=prod(DiscountFactorParamsVec); + + aprimeFnParamsVec=CreateVectorFromParams(Parameters, aprimeFnParamNames,jj); + [a2primeIndex,a2primeProbs]=CreateExperienceAssetFnMatrix_Case1(aprimeFn, n_d2, n_a2, d2_grid, a2_grid, aprimeFnParamsVec,2); % Note, is actually aprime_grid (but a_grid is anyway same for all ages) + % Note: aprimeIndex is [N_d2,N_a2], whereas aprimeProbs is [N_d2,N_a2] + + aprimeIndex=repelem((1:1:N_a1)',N_d2,N_a2)+N_a1*repmat(a2primeIndex-1,N_a1,1,1); % [N_d2*N_a1,N_a2] + aprimeplus1Index=repelem((1:1:N_a1)',N_d2,N_a2)+N_a1*repmat(a2primeIndex,N_a1,1,1); % [N_d2*N_a1,N_a2] + aprimeProbs=repmat(a2primeProbs,N_a1,1,N_z); % [N_d2*N_a1,N_a2,N_z] + + EVpre=sum(shiftdim(pi_e_J(:,N_j),-2).*V(:,:,:,jj+1),3); % First, switch V_Jplus1 into Kron form + + Vlower=reshape(EVpre(aprimeIndex(:),:),[N_d2*N_a1,N_a2,N_z]); + Vupper=reshape(EVpre(aprimeplus1Index(:),:),[N_d2*N_a1,N_a2,N_z]); + % Skip interpolation when upper and lower are equal (otherwise can cause numerical rounding errors) + skipinterp=(Vlower==Vupper); + aprimeProbs(skipinterp)=0; % effectively skips interpolation + + % Switch EV from being in terps of a2prime to being in terms of d2 and a2 + EV=aprimeProbs.*Vlower+(1-aprimeProbs).*Vupper; % (d2,a1prime,a2,u,zprime) + % Already applied the probabilities from interpolating onto grid + + EV=EV.*shiftdim(pi_z_J(:,:,jj)',-2); + EV(isnan(EV))=0; % remove nan created where value fn is -Inf but probability is zero + EV=squeeze(sum(EV,3)); + % EV is over (d2,a1prime,a2,z) + + if vfoptions.lowmemory==0 + + ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); + + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); % should autofill e dimension + + % Calc the max and its index + [Vtemp,maxindex]=max(entireRHS,[],1); + + V(:,:,:,jj)=shiftdim(Vtemp,1); + Policy(:,:,:,jj)=shiftdim(maxindex,1); + + elseif vfoptions.lowmemory==1 + + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,jj); + ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec,0,0); + + entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); % should autofill e dimension + + % Calc the max and its index + [Vtemp,maxindex]=max(entireRHS,[],1); + + V(:,:,e_c,jj)=shiftdim(Vtemp,1); + Policy(:,:,e_c,jj)=shiftdim(maxindex,1); + end + + elseif vfoptions.lowmemory==2 + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,jj); + + ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); + + entireRHS=ReturnMatrix_ze+DiscountedEV_z; + + %Calc the max and its index + [Vtemp,maxindex]=max(entireRHS,[],1); + + V(:,z_c,e_c,jj)=shiftdim(Vtemp,1); + Policy(:,z_c,e_c,jj)=shiftdim(maxindex,1); + end + end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,jj); + + ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + + entireRHS=ReturnMatrix_ze+DiscountedEV_z; + + %Calc the max and its index + [Vtemp,maxindex]=max(entireRHS,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(maxindex,1); + end + end + end + end +end + +%% Separate d and aprime +if vfoptions.gridinterplayer + error("gridinterp not implemented with ExpAsset yet") +end +if N_d1==0 + Policy3=zeros(2,N_a,N_z,N_e,N_j,'gpuArray'); % first dim indexes the optimal choice for d2 and aprime rest of dimensions a,z + Policy3(1,:,:,:,:)=1; % is meaningless anyway + Policy3(2,:,:,:,:)=shiftdim(rem(Policy-1,N_d2)+1,-1); + Policy3(3,:,:,:,:)=shiftdim(ceil(Policy/N_d2),-1); +else + Policy2=zeros(3,N_a,N_z,N_e,N_j,'gpuArray'); % first dim indexes the optimal choice for (d1,d2,aprime) rest of dimensions a,z + % second dim indexes the optimal choice for (d2,aprime) rest of dimensions a,z + Policy3(1,:,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); % one of these is wrong + Policy3(2,:,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); % one of these is wrong + Policy3(3,:,:,:,:)=shiftdim(ceil(Policy/N_d),-1); % this may be wrong +end + +end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m new file mode 100644 index 00000000..c4fe8abb --- /dev/null +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m @@ -0,0 +1,100 @@ +function [V,Policy2]=ValueFnIter_FHorz_TPath_SingleStep_raw(V,n_d,n_a,n_z,N_j, d_gridvals, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions) + +N_d=prod(n_d); +N_a=prod(n_a); +N_z=prod(n_z); + +Policy=zeros(N_a,N_z,N_j,'gpuArray'); %first dim indexes the optimal choice for d and aprime rest of dimensions a,z + +if vfoptions.lowmemory>0 + special_n_z=ones(1,length(n_z)); +end + + +%% j=N_j + +% Temporarily save the time period of V that is being replaced +Vtemp_j=V(:,:,N_j); + +% Create a vector containing all the return function parameters (in order) +ReturnFnParamsVec=CreateVectorFromParams(Parameters, ReturnFnParamNames,N_j); + +if vfoptions.lowmemory==0 + + %if vfoptions.returnmatrix==2 % GPU + ReturnMatrix=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn, n_d, n_a, n_z, d_gridvals, a_grid, z_gridvals_J(:,:,N_j), ReturnFnParamsVec); + %Calc the max and it's index + [Vtemp,maxindex]=max(ReturnMatrix,[],1); + V(:,:,N_j)=Vtemp; + Policy(:,:,N_j)=maxindex; + +elseif vfoptions.lowmemory==1 + + %if vfoptions.returnmatrix==2 % GPU + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + ReturnMatrix_z=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn, n_d, n_a, special_n_z, d_gridvals, a_grid, z_val, ReturnFnParamsVec); + %Calc the max and it's index + [Vtemp,maxindex]=max(ReturnMatrix_z,[],1); + V(:,z_c,N_j)=Vtemp; + Policy(:,z_c,N_j)=maxindex; + end + +end + +%% Iterate backwards through j. +for reverse_j=1:N_j-1 + jj=N_j-reverse_j; + + % Create a vector containing all the return function parameters (in order) + ReturnFnParamsVec=CreateVectorFromParams(Parameters, ReturnFnParamNames,jj); + DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,jj); + DiscountFactorParamsVec=prod(DiscountFactorParamsVec); + + VKronNext_j=Vtemp_j; % Has been presaved before it was replaced + Vtemp_j=V(:,:,jj); % Grab this before it is replaced/updated + + if vfoptions.lowmemory==0 + + ReturnMatrix=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn, n_d, n_a, n_z, d_gridvals, a_grid, z_gridvals_J(:,:,jj), ReturnFnParamsVec); + + EV=VKronNext_j.*shiftdim(pi_z_J(:,:,jj)',-1); + EV(isnan(EV))=0; %multilications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) + EV=sum(EV,2); % sum over z', leaving a singular second dimension + + entireEV=repelem(EV,N_d,1,1); + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repmat(entireEV,1,N_a,1); + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS,[],1); + + V(:,:,jj)=shiftdim(Vtemp,1); + Policy(:,:,jj)=shiftdim(maxindex,1); + + elseif vfoptions.lowmemory==1 + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + ReturnMatrix_z=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn, n_d, n_a, special_n_z, d_gridvals, a_grid, z_val, ReturnFnParamsVec); + + %Calc the condl expectation term (except beta), which depends on z but not on control variables + EV_z=VKronNext_j.*(ones(N_a,1,'gpuArray')*pi_z_J(z_c,:,jj)); + EV_z(isnan(EV_z))=0; %multilications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) + EV_z=sum(EV_z,2); + + entireEV_z=kron(EV_z,ones(N_d,1)); + entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*entireEV_z*ones(1,N_a,1); + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_z,[],1); + V(:,z_c,jj)=Vtemp; + Policy(:,z_c,jj)=maxindex; + end + end +end + +%% Separate d and aprime +Policy2=zeros(2,N_a,N_z,N_j,'gpuArray'); % first dim indexes the optimal choice for d and aprime rest of dimensions a,z +Policy2(1,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); +Policy2(2,:,:,:)=shiftdim(ceil(Policy/N_d),-1); + +end diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m index d8899f78..647fcac6 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m @@ -11,13 +11,9 @@ % For this agent type, first go back through the value & policy fns. % Then forwards through agent dist and agg vars. -if N_d>0 - l_d=length(n_d); - PolicyIndexesPath=zeros(2,N_a,N_z,T-1,'gpuArray'); %Periods 1 to T-1 -else - l_d=0; - PolicyIndexesPath=zeros(N_a,N_z,T-1,'gpuArray'); %Periods 1 to T-1 -end + +l_a=length(n_a); +PolicyIndexesPath=zeros(l_d+l_a,N_a,N_z,T-1,'gpuArray'); %Periods 1 to T-1 % This and much of the rest of this code borrowed from TransitionPath_InfHorz_shooting.m if simoptions.gridinterplayer==0 @@ -52,11 +48,7 @@ % The VKron input is next period value fn, the VKron output is this period. % Policy is kept in the form where it is just a single-value in (d,a') - if N_d>0 - PolicyIndexesPath(:,:,:,T-ttr)=Policy; - else - PolicyIndexesPath(:,:,T-ttr)=Policy; - end + PolicyIndexesPath(:,:,:,T-ttr)=Policy; Vnext=V; end diff --git a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/ValueFnIter_InfHorz_TPath_SingleStep.m b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/ValueFnIter_InfHorz_TPath_SingleStep.m index 6b584913..d94c5164 100644 --- a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/ValueFnIter_InfHorz_TPath_SingleStep.m +++ b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/ValueFnIter_InfHorz_TPath_SingleStep.m @@ -4,8 +4,8 @@ % vfoptions must be already fully set up (this command is for internal use only so it should be) N_d=prod(n_d); -% N_a=prod(n_a); -% N_z=prod(n_z); +N_a=prod(n_a); +N_z=prod(n_z); %% if strcmp(vfoptions.exoticpreferences,'QuasiHyperbolic') @@ -21,7 +21,8 @@ if vfoptions.gridinterplayer==0 if vfoptions.divideandconquer==0 if N_d==0 - [VKron,PolicyKron]=ValueFnIter_InfHorz_TPath_SingleStep_nod_raw(VKron,n_a, n_z, a_grid, z_gridvals, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + [VKron,Policy]=ValueFnIter_InfHorz_TPath_SingleStep_nod_raw(VKron,n_a, n_z, a_grid, z_gridvals, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + PolicyKron=reshape(Policy,[size(Policy,1),N_a,N_z]); else [VKron, PolicyKron]=ValueFnIter_InfHorz_TPath_SingleStep_raw(VKron,n_d,n_a,n_z, d_gridvals, a_grid, z_gridvals, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); end From 2e9ac40a5ba2ce1352ef7688ec28078fc46562ea Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 20 Mar 2026 07:42:33 +1300 Subject: [PATCH 26/90] Fix missing polindorval in ValueFnIter_Case1 Follow the conventions of ValueFnIter_Case1_FHorz to initialize `vfoptions.polindorval`. --- ValueFnIter/InfHorz/ValueFnIter_Case1.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ValueFnIter/InfHorz/ValueFnIter_Case1.m b/ValueFnIter/InfHorz/ValueFnIter_Case1.m index 5eae604b..6e0db6f2 100644 --- a/ValueFnIter/InfHorz/ValueFnIter_Case1.m +++ b/ValueFnIter/InfHorz/ValueFnIter_Case1.m @@ -2,4 +2,4 @@ error('VFI Toolkit Version 2.5 renamed _Case1 to _InfHorz: vfitoolkit.com/updates-blog/2026/version-2-5-infhorz') -end +end \ No newline at end of file From d5b249a3bcea2803c0357f4e24045aabec074650 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 20 Mar 2026 13:09:07 +1300 Subject: [PATCH 27/90] Fix l_ze calculation, and other needs We are now iteration through the solution space of transition convergence. --- EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m | 2 +- EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m | 6 ------ EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m | 6 ------ .../TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m | 2 +- 4 files changed, 2 insertions(+), 14 deletions(-) diff --git a/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m b/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m index 01ce0348..76ff9dcb 100644 --- a/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m +++ b/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m @@ -467,7 +467,7 @@ for jj=j1:jend % Includes check for cases in which no parameters are actually required FnToEvaluateParamsCell=CreateCellFromParams(Parameters,FnsToEvaluateParamNames(ff).Names,jj); - Values(:,:,jj-j1+1)=EvalFnOnAgentDist_Grid(FnsToEvaluate{ff}, FnToEvaluateParamsCell,PolicyValuesPermuteJ(:,:,:,jj),l_daprime,n_a,n_z,a_gridvals,z_gridvals_J(:,:,jj)); + Values(:,:,jj-j1+1)=EvalFnOnAgentDist_Grid(FnsToEvaluate{ff}, FnToEvaluateParamsCell,PolicyValuesPermuteJ(:,:,:,jj),l_daprime,n_a,n_z,a_gridvals,z_gridvals_J(:,:,jj)); end Values=reshape(Values,[N_a*N_z*(jend-j1+1),1]); diff --git a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m index d50fb99d..48367f86 100644 --- a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m +++ b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m @@ -983,12 +983,6 @@ elseif l_a==5 && l_z==12 Values=arrayfun(FnToEvaluate, daprime1vals,daprime2vals,daprime3vals,daprime4vals,daprime5vals,daprime6vals,daprime7vals, a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals(1,:,1),z_gridvals(1,:,2),z_gridvals(1,:,3),z_gridvals(1,:,4),z_gridvals(1,:,5),z_gridvals(1,:,6),z_gridvals(1,:,7),z_gridvals(1,:,8),z_gridvals(1,:,9),z_gridvals(1,:,10),z_gridvals(1,:,11),z_gridvals(1,:,12), FnToEvaluateParamsCell{:}); end -elseif l_daprime==6 - if l_a==5 && l_z==1 - Values=arrayfun(FnToEvaluate, daprime1vals,daprime2vals,daprime3vals,daprime4vals,daprime5vals,daprime6vals, a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals(1,:,1), FnToEvaluateParamsCell{:}); - elseif l_a==5 && l_z==2 - Values=arrayfun(FnToEvaluate, daprime1vals,daprime2vals,daprime3vals,daprime4vals,daprime5vals,daprime6vals, a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals(1,:,1),z_gridvals(1,:,2), FnToEvaluateParamsCell{:}); - end end % Note: No need to reshape the output as it is already the appropriate size diff --git a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m index 3f458033..ab9d2d9e 100644 --- a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m +++ b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid_J.m @@ -661,12 +661,6 @@ elseif l_a==5 && l_z==8 Values=arrayfun(FnToEvaluate, PolicyValuesPermute(:,:,:,1),PolicyValuesPermute(:,:,:,2),PolicyValuesPermute(:,:,:,3),PolicyValuesPermute(:,:,:,4),PolicyValuesPermute(:,:,:,5),PolicyValuesPermute(:,:,:,6),PolicyValuesPermute(:,:,:,7), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals_J(1,:,:,1),z_gridvals_J(1,:,:,2),z_gridvals_J(1,:,:,3),z_gridvals_J(1,:,:,4),z_gridvals_J(1,:,:,5),z_gridvals_J(1,:,:,6),z_gridvals_J(1,:,:,7),z_gridvals_J(1,:,:,8), CellOverAgeOfParamValues{:}); end -elseif l_daprime==6 - if l_a==5 && l_z==1 - Values=arrayfun(FnToEvaluate, PolicyValuesPermute(:,:,:,1),PolicyValuesPermute(:,:,:,2),PolicyValuesPermute(:,:,:,3),PolicyValuesPermute(:,:,:,4),PolicyValuesPermute(:,:,:,5),PolicyValuesPermute(:,:,:,6), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals_J(1,:,:,1), CellOverAgeOfParamValues{:}); - elseif l_a==5 && l_z==2 - Values=arrayfun(FnToEvaluate, PolicyValuesPermute(:,:,:,1),PolicyValuesPermute(:,:,:,2),PolicyValuesPermute(:,:,:,3),PolicyValuesPermute(:,:,:,4),PolicyValuesPermute(:,:,:,5),PolicyValuesPermute(:,:,:,6), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4),a_gridvals(:,5), z_gridvals_J(1,:,:,1), z_gridvals_J(1,:,:,2), CellOverAgeOfParamValues{:}); - end end % Note: No need to reshape the output as it is already the appropriate size diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m index 76cce4de..527c990a 100644 --- a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m +++ b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m @@ -17,7 +17,7 @@ l_aprime=length(n_a1); l_a=l_aprime+length(n_a2); l_z=length(n_z); -l_ze=l_z*length(n_e); +l_ze=l_z+length(n_e); %% % Shapes: From 7aa32ae101894f5178f7a34af03a6b1b9033ac9b Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 20 Mar 2026 23:09:59 +1300 Subject: [PATCH 28/90] Translate the ExpAsset _e codes to work without `e` Satisfied with the first draft of supporting models with both z and e, now implement changes to support models with z and no e. Additional varieties can be implemented from these two as needed. --- ...UnKronPolicyIndexes_Case1_FHorz_ExpAsset.m | 92 +++++++++++ ...tionPath_FHorz_PType_ExpAsset_singlepath.m | 14 +- ...Path_FHorz_PType_ExpAsset_singlepath_raw.m | 49 +++--- ...ueFnIter_FHorz_TPath_ExpAsset_SingleStep.m | 19 +-- ...Iter_FHorz_TPath_ExpAsset_SingleStep_raw.m | 154 ++++++++++++------ 5 files changed, 236 insertions(+), 92 deletions(-) create mode 100644 SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset.m diff --git a/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset.m b/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset.m new file mode 100644 index 00000000..d753f044 --- /dev/null +++ b/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset.m @@ -0,0 +1,92 @@ +function Policy=UnKronPolicyIndexes_Case1_FHorz_ExpAsset(PolicyKron, n_d1,n_d2,n_a1,n_a2, n_z,N_j, vfoptions) +% Can use vfoptions OR simoptions +% Input: PolicyKron is (3,N_a,N_z,N_j); % first dim indexes the optimal choice for (d1,d2,aprime) +% without d1, the first dim is 2 and indexes the optimal choice for (d2,aprime) +% vfoptions.gridinterplayer=1 will mean the first dimension has one extra value (so 4 if d1, 3 without) +% Output: Policy (l_d+l_a,n_a1,n_a2,n_z,N_j); + +N_d1=prod(n_d1); +% N_d2=prod(n_d2); +N_a1=prod(n_a1); +N_a2=prod(n_a2); +N_a=N_a1*N_a2; +N_z=prod(n_z); + +l_d2=length(n_d2); % if ever there is more than one d2 for ExpAsset +l_aprime=length(n_a1); +n_aprime=n_a1; +extra=(vfoptions.gridinterplayer==1); + +if N_d1==0 + Policy=zeros(l_d2+l_aprime+extra,N_a,N_z,N_j,'gpuArray'); + + Policy(1,:,:,:)=rem(PolicyKron(1,:,:,:)-1,n_d2(1))+1; + if l_d2>1 + if l_d2>2 + for ii=1:l_d2-1 + Policy(ii,:,:,:)=rem(ceil(PolicyKron(1,:,:,:)/prod(n_d2(1:ii-1)))-1,n_d2(ii))+1; + end + end + Policy(l_d2,:,:,:)=ceil(PolicyKron(1,:,:,:)/prod(n_d2(1:l_d2-1))); + end + + Policy(l_d2+1,:,:,:)=rem(PolicyKron(2,:,:,:)-1,n_aprime(1))+1; + if l_aprime>1 + if l_aprime>2 + for ii=1:l_aprime-1 + Policy(l_d2+ii,:,:,:)=rem(ceil(PolicyKron(2,:,:,:)/prod(n_aprime(1:ii-1)))-1,n_aprime(ii))+1; + end + end + Policy(l_d2+l_aprime,:,:,:)=ceil(PolicyKron(2,:,:,:)/prod(n_aprime(1:l_aprime-1))); + end + + if vfoptions.gridinterplayer==1 + Policy(l_d2+l_aprime+1,:,:,:)=PolicyKron(3,:,:,:); + end + + Policy=reshape(Policy,[l_d2+l_aprime+extra,n_a,n_z,N_j]); + +else % N_d1>0 + l_d1=length(n_d1); % Note: this is anyway only used is N_d1~=0 + + Policy=zeros(l_d1+l_d2+l_aprime+extra,N_a,N_z,N_j,'gpuArray'); + + Policy(1,:,:,:)=rem(PolicyKron(1,:,:,:)-1,n_d1(1))+1; + if l_d1>1 + if l_d1>2 + for ii=1:l_d1-1 + Policy(ii,:,:,:)=rem(ceil(PolicyKron(1,:,:,:)/prod(n_d1(1:ii-1)))-1,n_d1(ii))+1; + end + end + Policy(l_d1,:,:,:)=ceil(PolicyKron(1,:,:,:)/prod(n_d1(1:l_d1-1))); + end + + Policy(l_d1+1,:,:,:)=rem(PolicyKron(2,:,:,:)-1,n_d2(1))+1; + if l_d2>1 + if l_d2>2 + for ii=1:l_d2-1 + Policy(l_d1+ii,:,:,:)=rem(ceil(PolicyKron(2,:,:,:)/prod(n_d2(1:ii-1)))-1,n_d2(ii))+1; + end + end + Policy(l_d1+l_d2,:,:,:)=ceil(PolicyKron(2,:,:,:)/prod(n_d2(1:l_d2-1))); + end + + Policy(l_d1+l_d2+1,:,:,:)=rem(PolicyKron(3,:,:,:)-1,n_aprime(1))+1; + if l_aprime>1 + if l_aprime>2 + for ii=1:l_aprime-1 + Policy(l_d1+l_d2+ii,:,:,:)=rem(ceil(PolicyKron(3,:,:,:)/prod(n_aprime(1:ii-1)))-1,n_aprime(ii))+1; + end + end + Policy(l_d1+l_d2+l_aprime,:,:,:)=ceil(PolicyKron(3,:,:,:)/prod(n_aprime(1:l_aprime-1))); + end + + if vfoptions.gridinterplayer==1 + Policy(l_d1+l_d2+l_aprime+1,:,:,:)=PolicyKron(4,:,:,:); + end + + Policy=reshape(Policy,[l_d1+l_d2+l_aprime+extra,n_a1,n_a2,n_z,N_j]); +end + + +end \ No newline at end of file diff --git a/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m b/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m index 5e4ecd3e..68cf70d0 100644 --- a/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m +++ b/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m @@ -6,14 +6,14 @@ if transpathoptions.fastOLG==0 if N_z==0 if N_e==0 - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_noz_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_noz_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); else - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_noz_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_noz_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); end else if N_e==0 % Principal test case... - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); else AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); end @@ -21,15 +21,15 @@ elseif transpathoptions.fastOLG==1 if N_z==0 if N_e==0 - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_noz_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_noz_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); else - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_noz_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_noz_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); end else if N_e==0 - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); else - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); + AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); end end end diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m index 527d9c88..49e40e3f 100644 --- a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m +++ b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m @@ -1,18 +1,20 @@ -function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,N_d,n_d,N_a,n_a,N_z,n_z,N_j,d_grid,a_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,z_gridvals_J_fastOLG,ReturnFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, transpathoptions, vfoptions, simoptions) +function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T, ... + l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,z_gridvals_J_fastOLG, ... + ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, ... + use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, ... + II1orII, II2, exceptlastj,exceptfirstj,justfirstj, ... + transpathoptions, vfoptions, simoptions) % When doing shooting alogrithm on TPath FHorz PType, this is for a given ptype, and does the steps of back-iterate to get policy, then forward to get agent dist and agg vars. % The only output is the agg vars path. -error("these are not yet the droids you are looking for") - AggVarsPath=zeros(length(FnsToEvaluateCell),T-1,'gpuArray'); % Note: does not include the final AggVars, might be good to add them later as a way to make if obvious to user it things are incorrect -if N_d==0 - l_d=0; -else - l_d=length(n_d); -end -l_a=length(n_a); -l_aprime=length(n_a); +N_a1=prod(n_a1); +N_a2=prod(n_a2); +N_a=N_a1*N_a2; + +l_aprime=length(n_a1); +l_a=l_aprime+length(n_a2); l_z=length(n_z); %% @@ -68,7 +70,7 @@ pi_z_J=transpathoptions.pi_z_J_T(:,:,:,T-ttr); end - [V, Policy]=ValueFnIter_FHorz_TPath_SingleStep(V,n_d,n_a,n_z,N_j,d_gridvals, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + [V, Policy]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); % The V input is next period value fn, the V output is this period. % Policy is kept in the form where it is just a single-value in (d,a') @@ -79,14 +81,14 @@ % Create version of PolicyIndexesPath called PolicyaprimePath, which only tracks aprime and has j=1:N_j-1 as we don't use N_j to iterate agent dist (there is no N_j+1) % For fastOLG we use PolicyaprimejPath, if there is z then PolicyaprimejzPath % When using grid interpolation layer also PolicyProbsPath -if isscalar(n_a) +if isscalar(n_a1) PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:),[N_a*N_z,N_j-1,T-1]); % aprime index -elseif length(n_a)==2 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); -elseif length(n_a)==3 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1)+n_a(1)*n_a(2)*(PolicyIndexesPath(l_d+3,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); -elseif length(n_a)==4 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1)+n_a(1)*n_a(2)*(PolicyIndexesPath(l_d+3,:,:,1:N_j-1,:)-1)+n_a(1)*n_a(2)*n_a(3)*(PolicyIndexesPath(l_d+4,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); +elseif length(n_a1)==2 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); +elseif length(n_a1)==3 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); +elseif length(n_a1)==4 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,1:N_j-1,:)-1)+prod(n_a1(1:3))*(PolicyIndexesPath(l_d+4,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); end if simoptions.fastOLG==0 PolicyaprimezPath_slowOLG=PolicyaprimePath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1); @@ -105,7 +107,7 @@ end end % Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats -PolicyValuesPath=PolicyInd2Val_FHorz_TPath(PolicyIndexesPath,n_d,n_a,n_z,N_j,T-1,d_grid,a_grid,vfoptions,1,0); +PolicyValuesPath=PolicyInd2Val_FHorz_TPath(PolicyIndexesPath,[n_d1,n_d2],[n_a1,n_a2],n_z,N_j,T-1,d_gridvals,a_gridvals,vfoptions,1,0); PolicyValuesPath=permute(PolicyValuesPath,[2,4,3,1,5]); %[N_a,N_j,N_z,l_d+l_aprime,T-1] % fastOLG ordering is needed for AggVars %% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm @@ -170,14 +172,11 @@ end end - if simoptions.fastOLG==0 - AgeWeights=AgeWeights_T(:,:,tt); - else % simoptions.fastOLG==1 - AgeWeights=AgeWeights_T(:,tt); - end + AgeWeights=AgeWeights_T(:,:,tt); % by coincidence this is for fastOLG=0,1 jequalOneDist=jequalOneDist_T(:,tt+1); % Note: t+1 as we are about to create the next period AgentDist + % No specialization for ExpAsset needed here! if simoptions.fastOLG==0 AgentDistnext=AgentDist_FHorz_TPath_SingleStep_Iteration_raw(AgentDist,PolicyaprimezPath_slowOLG(:,:,tt),N_a,N_z,N_j,pi_z_J,II1,II2,jequalOneDist); else % simoptions.fastOLG==1 @@ -189,7 +188,7 @@ end %% AggVars - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_a,l_a,l_z,N_a,N_z,a_gridvals,z_gridvals_J_fastOLG,1); + AggVars=EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_z,N_a,N_z,a_gridvals,z_gridvals_J_fastOLG,1); for ff=1:length(AggVarNames) Parameters.(AggVarNames{ff})=AggVars.(AggVarNames{ff}).Mean; end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m index 973246b5..aac711bf 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m @@ -1,20 +1,19 @@ -function [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep(VKron,n_d,n_a,n_z,N_j,d_gridvals, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions) +function [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep(VKron,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) % The VKron input is next period value fn, the VKron output is this period. -error("these are not yet the droids you are looking for") - -N_d=prod(n_d); -N_a=prod(n_a); +N_d1=prod(n_d1); +% N_a1=prod(n_a1); N_z=prod(n_z); %% If get to here then not using exoticpreferences nor StateDependentVariables_z % N_z==0 is handled by a different command if vfoptions.divideandconquer==0 if vfoptions.gridinterplayer==0 - if N_d==0 - [VKron,PolicyKron]=ValueFnIter_FHorz_TPath_SingleStep_nod_raw(VKron,n_a, n_z, N_j, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + if N_d1==0 + error("not implemented") + [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_nod_raw(VKron,n_a, n_z, N_j, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); else - [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_SingleStep_raw(VKron,n_d,n_a,n_z, N_j, d_gridvals, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw(VKron,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); end else error('Have not yet implemented grid interpolation layer for FHorz TPath without fastOLG=1. Ask on forum if you need this.') @@ -24,7 +23,7 @@ end %% Policy in transition paths -PolicyKron=UnKronPolicyIndexes_Case1_FHorz(PolicyKron,n_d,n_a,N_z,N_j,vfoptions); -PolicyKron=reshape(PolicyKron,[size(PolicyKron,1),N_a,N_z,N_j]); +PolicyKron=UnKronPolicyIndexes_Case1_FHorz_ExpAsset(PolicyKron,n_d1,n_d2,n_a1,n_a2,N_z,N_j,vfoptions); +PolicyKron=reshape(PolicyKron,[size(PolicyKron,1),prod(n_a1)*prod(n_a2),N_z,N_j]); end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m index c4fe8abb..615255ad 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m @@ -1,71 +1,102 @@ -function [V,Policy2]=ValueFnIter_FHorz_TPath_SingleStep_raw(V,n_d,n_a,n_z,N_j, d_gridvals, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions) - -N_d=prod(n_d); -N_a=prod(n_a); +function [V,Policy3]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) + +N_d1=prod(n_d1); +N_d2=prod(n_d2); +N_d=N_d1*N_d2; +N_a1=prod(n_a1); +N_a2=prod(n_a2); +N_a=N_a1*N_a2; N_z=prod(n_z); -Policy=zeros(N_a,N_z,N_j,'gpuArray'); %first dim indexes the optimal choice for d and aprime rest of dimensions a,z +V=zeros(N_a,N_z,N_j,'gpuArray'); +Policy=zeros(N_a,N_z,N_j,'gpuArray'); %first dim indexes the optimal choice for d and a1prime rest of dimensions a,z + +%% +a2_gridvals=CreateGridvals(n_a2,a2_grid,1); -if vfoptions.lowmemory>0 +if vfoptions.lowmemory==1 special_n_z=ones(1,length(n_z)); +elseif vfoptions.lowmemory==3 + special_n_z=ones(1,length(n_z)); + special_n_ea=ones(1,length(n_a2)); end - %% j=N_j - -% Temporarily save the time period of V that is being replaced -Vtemp_j=V(:,:,N_j); - % Create a vector containing all the return function parameters (in order) ReturnFnParamsVec=CreateVectorFromParams(Parameters, ReturnFnParamNames,N_j); if vfoptions.lowmemory==0 - - %if vfoptions.returnmatrix==2 % GPU - ReturnMatrix=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn, n_d, n_a, n_z, d_gridvals, a_grid, z_gridvals_J(:,:,N_j), ReturnFnParamsVec); - %Calc the max and it's index + ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); + %Calc the max and its index [Vtemp,maxindex]=max(ReturnMatrix,[],1); V(:,:,N_j)=Vtemp; Policy(:,:,N_j)=maxindex; - elseif vfoptions.lowmemory==1 - - %if vfoptions.returnmatrix==2 % GPU for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - ReturnMatrix_z=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn, n_d, n_a, special_n_z, d_gridvals, a_grid, z_val, ReturnFnParamsVec); - %Calc the max and it's index - [Vtemp,maxindex]=max(ReturnMatrix_z,[],1); + ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); + % Calc the max and its index + [Vtemp,maxindex]=max(ReturnMatrix_z); V(:,z_c,N_j)=Vtemp; Policy(:,z_c,N_j)=maxindex; end - +elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1, special_n_ea, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + % Calc the max and its index + [Vtemp,maxindex]=max(ReturnMatrix_z); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=maxindex; + end + end end %% Iterate backwards through j. for reverse_j=1:N_j-1 jj=N_j-reverse_j; + if vfoptions.verbose==1 + fprintf('Finite horizon: %i of %i \n',jj, N_j) + end + % Create a vector containing all the return function parameters (in order) ReturnFnParamsVec=CreateVectorFromParams(Parameters, ReturnFnParamNames,jj); DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,jj); DiscountFactorParamsVec=prod(DiscountFactorParamsVec); - - VKronNext_j=Vtemp_j; % Has been presaved before it was replaced - Vtemp_j=V(:,:,jj); % Grab this before it is replaced/updated - if vfoptions.lowmemory==0 - - ReturnMatrix=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn, n_d, n_a, n_z, d_gridvals, a_grid, z_gridvals_J(:,:,jj), ReturnFnParamsVec); + aprimeFnParamsVec=CreateVectorFromParams(Parameters, aprimeFnParamNames,jj); + [a2primeIndex,a2primeProbs]=CreateExperienceAssetFnMatrix_Case1(aprimeFn, n_d2, n_a2, d2_grid, a2_grid, aprimeFnParamsVec,2); % Note, is actually aprime_grid (but a_grid is anyway same for all ages) + % Note: aprimeIndex is [N_d2,N_a2], whereas aprimeProbs is [N_d2,N_a2] + + aprimeIndex=repelem((1:1:N_a1)',N_d2,N_a2)+N_a1*repmat(a2primeIndex-1,N_a1,1,1); % [N_d2*N_a1,N_a2] + aprimeplus1Index=repelem((1:1:N_a1)',N_d2,N_a2)+N_a1*repmat(a2primeIndex,N_a1,1,1); % [N_d2*N_a1,N_a2] + aprimeProbs=repmat(a2primeProbs,N_a1,1,N_z); % [N_d2*N_a1,N_a2,N_z] + + Vlower=reshape(V(aprimeIndex(:),:,jj+1),[N_d2*N_a1,N_a2,N_z]); + Vupper=reshape(V(aprimeplus1Index(:),:,jj+1),[N_d2*N_a1,N_a2,N_z]); + % Skip interpolation when upper and lower are equal (otherwise can cause numerical rounding errors) + skipinterp=(Vlower==Vupper); + aprimeProbs(skipinterp)=0; % effectively skips interpolation + + % Switch EV from being in terps of a2prime to being in terms of d2 and a2 + EV=aprimeProbs.*Vlower+(1-aprimeProbs).*Vupper; % (d2,a1prime,a2,u,zprime) + % Already applied the probabilities from interpolating onto grid + + EV=EV.*shiftdim(pi_z_J(:,:,N_j)',-2); + EV(isnan(EV))=0; % remove nan created where value fn is -Inf but probability is zero + EV=squeeze(sum(EV,3)); + % EV is over (d2,a1prime,a2,z) - EV=VKronNext_j.*shiftdim(pi_z_J(:,:,jj)',-1); - EV(isnan(EV))=0; %multilications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) - EV=sum(EV,2); % sum over z', leaving a singular second dimension + if vfoptions.lowmemory==0 + + ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - entireEV=repelem(EV,N_d,1,1); - entireRHS=ReturnMatrix+DiscountFactorParamsVec*repmat(entireEV,1,N_a,1); + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); - %Calc the max and it's index + % Calc the max and its index [Vtemp,maxindex]=max(entireRHS,[],1); V(:,:,jj)=shiftdim(Vtemp,1); @@ -74,27 +105,50 @@ elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); - ReturnMatrix_z=CreateReturnFnMatrix_Case1_Disc_Par2(ReturnFn, n_d, n_a, special_n_z, d_gridvals, a_grid, z_val, ReturnFnParamsVec); - - %Calc the condl expectation term (except beta), which depends on z but not on control variables - EV_z=VKronNext_j.*(ones(N_a,1,'gpuArray')*pi_z_J(z_c,:,jj)); - EV_z(isnan(EV_z))=0; %multilications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) - EV_z=sum(EV_z,2); - - entireEV_z=kron(EV_z,ones(N_d,1)); - entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*entireEV_z*ones(1,N_a,1); - - %Calc the max and it's index + ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); + + entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1); + + %Calc the max and its index [Vtemp,maxindex]=max(entireRHS_z,[],1); - V(:,z_c,jj)=Vtemp; - Policy(:,z_c,jj)=maxindex; + + V(:,z_c,jj)=shiftdim(Vtemp,1); + Policy(:,z_c,jj)=shiftdim(maxindex,1); + end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); + + %Calc the max and its index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=shiftdim(maxindex,1); + end end end end %% Separate d and aprime -Policy2=zeros(2,N_a,N_z,N_j,'gpuArray'); % first dim indexes the optimal choice for d and aprime rest of dimensions a,z -Policy2(1,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); -Policy2(2,:,:,:)=shiftdim(ceil(Policy/N_d),-1); +if vfoptions.gridinterplayer + error("gridinterp not implemented with ExpAsset yet") +end +if N_d1==0 + Policy3=zeros(2,N_a,N_z,N_j,'gpuArray'); % first dim indexes the optimal choice for d2 and aprime rest of dimensions a,z + Policy3(1,:,:,:)=1; % is meaningless anyway + Policy3(2,:,:,:)=shiftdim(rem(Policy-1,N_d2)+1,-1); + Policy3(3,:,:,:)=shiftdim(ceil(Policy/N_d2),-1); +else + Policy2=zeros(3,N_a,N_z,N_j,'gpuArray'); % first dim indexes the optimal choice for (d1,d2,aprime) rest of dimensions a,z + % second dim indexes the optimal choice for (d2,aprime) rest of dimensions a,z + Policy3(1,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); % one of these is wrong + Policy3(2,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); % one of these is wrong + Policy3(3,:,:,:)=shiftdim(ceil(Policy/N_d),-1); % this may be wrong +end end From 014fe33ecf60f3a7f708ac92fb687521b6b4fac2 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 24 Mar 2026 15:30:40 +1300 Subject: [PATCH 29/90] Reharmonize with new CustomModelStats A few fixes need to push upstream... --- .../HeteroAgentStationaryEqm_Case1_FHorz.m | 1 + ...teroAgentStationaryEqm_Case1_FHorz_PType.m | 3 +- ...onaryEqm_Case1_FHorz_PType_GEptype_subfn.m | 25 ------ ...entStationaryEqm_Case1_FHorz_PType_subfn.m | 76 ++++++++++++++++--- 4 files changed, 67 insertions(+), 38 deletions(-) diff --git a/HeterogeneousAgent/FHorz/HeteroAgentStationaryEqm_Case1_FHorz.m b/HeterogeneousAgent/FHorz/HeteroAgentStationaryEqm_Case1_FHorz.m index e80f0c89..cebc35db 100644 --- a/HeterogeneousAgent/FHorz/HeteroAgentStationaryEqm_Case1_FHorz.m +++ b/HeterogeneousAgent/FHorz/HeteroAgentStationaryEqm_Case1_FHorz.m @@ -133,6 +133,7 @@ heteroagentoptions.useCustomModelStats=1; % Stash some of the inputs so they can be passed to CustomModelStats later (only things we otherwise overright). % So that user gets exactly what they input, not any internally reworked things + % In this (non-PType) context, these assignments are arrays or other simple types heteroagentoptions.CustomModelStatsInputs.z_grid=z_grid; heteroagentoptions.CustomModelStatsInputs.pi_z=pi_z; % Need the following two as otherwise they would contain alreadygridvals=1 diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m index f3e18ffa..08d00af8 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m @@ -165,8 +165,9 @@ heteroagentoptions.useCustomModelStats=0; if isfield(heteroagentoptions,'CustomModelStats') heteroagentoptions.useCustomModelStats=1; - % Stash some of the inputs so they can be passed to CustomModelStats later (only things we otherwise overright). + % Stash some of the inputs so they can be passed to CustomModelStats later (only things we otherwise overwrite). % So that user gets exactly what they input, not any internally reworked things + % In this (PType) context, these assignments are typically structs that each have all the PType fields within them heteroagentoptions.CustomModelStatsInputs.FnsToEvaluate=FnsToEvaluate; heteroagentoptions.CustomModelStatsInputs.n_d=n_d; heteroagentoptions.CustomModelStatsInputs.n_a=n_a; diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m index 67d940ab..61fd1c1a 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m @@ -188,31 +188,6 @@ end end -%% Custom Model Stats -customstatnames=struct(); -if heteroagentoptions.useCustomModelStats==1 - if isfield(heteroagentoptions, 'CustomModelStats') - error("Universal PType handler for CustomModelStats not yet implemented") - else - for ii=1:PTypeStructure.N_i - iistr=PTypeStructure.iistr{ii}; - if ~isfield(heteroagentoptions, iistr) || ~isfield(heteroagentoptions.(iistr), 'CustomModelStats') - continue - end - if isfinite(PTypeStructure.(iistr).N_j) - CustomStats.(iistr)=heteroagentoptions.(iistr).CustomModelStats(Ptype_cells{ii}{1},Ptype_cells{ii}{2},Ptype_cells{ii}{3},PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).FnsToEvaluate,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).z_gridvals_J,PTypeStructure.(iistr).pi_z_J,heteroagentoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); - else - CustomStats.(iistr)=heteroagentoptions.(iistr).CustomModelStats(Ptype_cells{ii}{1},Ptype_cells{ii}{2},Ptype_cells{ii}{3},PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).FnsToEvaluate,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).z_gridvals,PTypeStructure.(iistr).pi_z,heteroagentoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); - end - % Note: anything else you want, just 'hide' it in heteroagentoptions - customstatnames.(iistr)=fieldnames(CustomStats.(iistr)); - for pp=1:length(customstatnames.(iistr)) - PTypeStructure.(iistr).Parameters.(customstatnames.(iistr){pp})=CustomStats.(iistr).(customstatnames.(iistr){pp}); - end - end - end -end - %% Evaluate the General Eqm Eqns diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m index fad0d75d..0545cc90 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m @@ -3,11 +3,41 @@ heteroagentparamsvecindex=0:1:length(GEpricesvec); [GEpricesvec,penalty]=ParameterConstraints_TransformParamsToOriginal(GEpricesvec,heteroagentparamsvecindex,GEPriceParamNames,heteroagentoptions); +if heteroagentoptions.verbose>0 + GEpricesvec_tminus1=zeros(nGEprices,1); + AggVars_tminus1=NaN(length(AggVarNames),1); + + for pp=1:nGEprices + GEpricesvec_tminus1(pp)=Parameters.(GEPriceParamNames{pp}); + end + GEpricesvec_delta=GEpricesvec-GEpricesvec_tminus1; + for aa=1:length(AggVarNames) + if isfield(Parameters,AggVarNames{aa}) + AggVars_tminus1(aa)=Parameters.(AggVarNames{aa}); + end + end + if heteroagentoptions.useintermediateEqns==1 + intEqnnames=fieldnames(heteroagentoptions.intermediateEqns); + intermediateEqns_tminus1=zeros(length(intEqnnames),1); + for aa=1:length(intEqnnames) + if isfield(Parameters,intEqnnames{aa}) + intEqns_tminus1(aa)=Parameters.(intEqnnames{aa}); + end + end + end + % We don't do anything special for CustomModelStats, which are not as easily done as others above. +end + if heteroagentoptions.verbose==2 + [~,maxidx]=max(GEpricesvec_delta.^2); fprintf(' \n') fprintf('Current GE prices: \n') for pp=1:nGEprices - fprintf(' %s: %8.4f \n',GEPriceParamNames{pp},GEpricesvec(pp)) + if pp==maxidx + cprintf('err',' %s: %8.4f \n',GEPriceParamNames{pp},GEpricesvec(pp)) + else + fprintf(' %s: %8.4f \n',GEPriceParamNames{pp},GEpricesvec(pp)) + end end end @@ -90,6 +120,13 @@ PTypeStructure.(jjstr).Parameters.(FnsToEvaluate_aa{aa})=AggVars_ii(aa); end end + + warning("do we need CustomModelStats here?") + if heteroagentoptions.useCustomModelStats==1 + V.(iistr)=V_ii; + Policy.(iistr)=Policy_ii; + StationaryDist.(iistr)=StationaryDist_ii; + end end AggVars=sum(AggVars_ConditionalOnPType.*PTypeStructure.ptweights',2); % Note: AggVars is a vector @@ -169,37 +206,52 @@ %% Feedback on progress if heteroagentoptions.verbose==1 % When=2, we report these earlier + [~,maxidx]=max(GEpricesvec_delta.^2); fprintf(' \n') fprintf('Current GE prices: \n') for pp=1:nGEprices - fprintf(heteroagentoptions.verboseaccuracy1,GEPriceParamNames{pp},GEpricesvec(pp)) + if pp==maxidx + cprintf('err',heteroagentoptions.verboseaccuracy1,GEPriceParamNames{pp},GEpricesvec(pp)) + else + fprintf(heteroagentoptions.verboseaccuracy1,GEPriceParamNames{pp},GEpricesvec(pp)) + end end end if heteroagentoptions.verbose>=1 fprintf('Current aggregate variables: \n') for aa=1:length(AggVarNames) - fprintf(heteroagentoptions.verboseaccuracy1,AggVarNames{aa},AggVars(aa)) % Note, this is done differently here because AggVars itself has been set as a matrix + if ~isnan(AggVars_tminus1(aa)) + cprintf('comment',heteroagentoptions.verboseaccuracy1,AggVarNames{aa},AggVars(aa)) % Note, this is done differently here because AggVars itself has been set as a matrix + else + fprintf(heteroagentoptions.verboseaccuracy1,AggVarNames{aa},AggVars(aa)) % Note, this is done differently here because AggVars itself has been set as a matrix + end end if heteroagentoptions.useintermediateEqns==1 + intEqns_delta=intermediateEqnsVec-intermediateEqns_tminus1; + [~,maxidx]=max(intEqns_delta.^2); fprintf('Current intermediateEqn variables: \n') for aa=1:length(intEqnnames) - fprintf(heteroagentoptions.verboseaccuracy1,intEqnnames{aa},intermediateEqnsVec(aa)) % Note, this is done differently here because AggVars itself has been set as a matrix + if aa==maxidx + cprintf('err',heteroagentoptions.verboseaccuracy1,intEqnnames{aa},intermediateEqnsVec(aa)) % Note, this is done differently here because AggVars itself has been set as a matrix + else + fprintf(heteroagentoptions.verboseaccuracy1,intEqnnames{aa},intermediateEqnsVec(aa)) % Note, this is done differently here because AggVars itself has been set as a matrix + end end end if heteroagentoptions.useCustomModelStats==1 fprintf('Current CustomModelStats variables: \n') - for ii=1:PTypeStructure.N_i - iistr=PTypeStructure.iistr{ii}; - if isfield(customstatnames, iistr) - for pp=1:length(customstatnames.(iistr)) - fprintf(heteroagentoptions.verboseaccuracy1,customstatnames.(iistr){pp},CustomStats.(iistr).(customstatnames.(iistr){pp})) - end - end + for aa=1:length(customstatnames) + fprintf(heteroagentoptions.verboseaccuracy1,customstatnames{aa},CustomStats.(customstatnames{aa})) % Note, this is done differently here because AggVars itself has been set as a matrix end end fprintf('Current GeneralEqmEqns: \n') + [~,maxidx]=max(GeneralEqmConditionsVec.^2); for gg=1:length(GEeqnNames) - fprintf(heteroagentoptions.verboseaccuracy2,GEeqnNames{gg},GeneralEqmConditionsVec(gg)) + if gg==maxidx + cprintf('err', heteroagentoptions.verboseaccuracy2,GEeqnNames{gg},GeneralEqmConditionsVec(gg)) + else + fprintf(heteroagentoptions.verboseaccuracy2,GEeqnNames{gg},GeneralEqmConditionsVec(gg)) + end end end From a27e35326f31602e34f2402f1253ed0451984dba Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 24 Mar 2026 16:44:04 +1300 Subject: [PATCH 30/90] Parameters are the one element that is supra-PType With these change, the program has iterated through initial and final GE and is running the transition convergence. --- .../FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m index 08d00af8..c741d077 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m @@ -417,8 +417,8 @@ PTypeStructure.(iistr).a_grid=a_grid.(Names_i{ii}); else PTypeStructure.(iistr).a_grid=a_grid; - end - + end + %% Parameter Structure % Parameters are allowed to be given as structure, or as vector/matrix % (in terms of their dependence on permanent type). So go through each of From 35340076da7dc6c4c40c0630b2ebcceb39ddcbfd Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 24 Mar 2026 17:02:57 +1300 Subject: [PATCH 31/90] Need to initialize PTypeStructure.(iistr).Parameters before use Easily enough done... --- .../FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m index c741d077..9c522120 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m @@ -424,11 +424,10 @@ % (in terms of their dependence on permanent type). So go through each of % these in term. % ie. Parameters.alpha=[0;1]; or Parameters.alpha.ptype1=0; Parameters.alpha.ptype2=1; - PTypeStructure.(iistr).Parameters=Parameters; FullParamNames=fieldnames(Parameters); % all the different parameters nFields=length(FullParamNames); for kField=1:nFields - if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form + if isstruct(Parameters.(FullParamNames{kField})) % Check the current parameter for permanent type in structure form % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); From c0ec397f9e980597f80ee43912406c6f0f03fffe Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 24 Mar 2026 17:36:24 +1300 Subject: [PATCH 32/90] More merge cleanups --- ...onaryEqm_Case1_FHorz_PType_GEptype_subfn.m | 4 ---- ...roAgentStationaryEqm_InfHorz_PType_subfn.m | 23 ------------------- 2 files changed, 27 deletions(-) diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m index 61fd1c1a..7d03dd05 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m @@ -31,7 +31,6 @@ Parameters.(GEPriceParamNames{pp})=GEpricesvec(GEpriceindexes(pp,1):GEpriceindexes(pp,2)); end -Ptype_cells=cell(1,PTypeStructure.N_i); % Hold results in case needed for CustomStats AggVars_ConditionalOnPType=zeros(PTypeStructure.numFnsToEvaluate,PTypeStructure.N_i); % Create AggVars conditional on ptype. for ii=1:PTypeStructure.N_i @@ -77,9 +76,6 @@ AggVars_ii=EvalFnOnAgentDist_AggVars_Case1(StationaryDist_ii, Policy_ii, PTypeStructure.(iistr).FnsToEvaluate, PTypeStructure.(iistr).Parameters, PTypeStructure.(iistr).FnsToEvaluateParamNames, PTypeStructure.(iistr).n_d, PTypeStructure.(iistr).n_a, PTypeStructure.(iistr).n_z, PTypeStructure.(iistr).d_grid, PTypeStructure.(iistr).a_grid, PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).simoptions); end - if heteroagentoptions.useCustomModelStats==1 - Ptype_cells{ii}={V_ii,Policy_ii,StationaryDist_ii}; - end AggVars_ConditionalOnPType(PTypeStructure.(iistr).FnsAndPTypeIndicator_ii,ii)=AggVars_ii; if heteroagentoptions.useCustomModelStats==1 diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m index 48e0d014..68e67b89 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m @@ -79,7 +79,6 @@ end - %% Put GE parameters and AggVars in structure, so they can be used for intermediateEqns and GeneralEqmEqns % already did the basic GE params % for pp=1:nGEprices @@ -120,28 +119,6 @@ end end -%% Custom Model Stats -customstatnames=struct(); -if heteroagentoptions.useCustomModelStats==1 - if isfield(heteroagentoptions, 'CustomModelStats') - error("Universal PType handler for CustomModelStats not yet implemented") - else - for ii=1:PTypeStructure.N_i - iistr=PTypeStructure.iistr{ii}; - if ~isfield(heteroagentoptions, iistr) || ~isfield(heteroagentoptions.(iistr), 'CustomModelStats') - continue - end - CustomStats.(iistr)=heteroagentoptions.(iistr).CustomModelStats(Ptype_cells{ii}{1},Ptype_cells{ii}{2},Ptype_cells{ii}{3},PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).FnsToEvaluate,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).z_gridvals,PTypeStructure.(iistr).pi_z,heteroagentoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); - % Note: anything else you want, just 'hide' it in heteroagentoptions - customstatnames.(iistr)=fieldnames(CustomStats.(iistr)); - for pp=1:length(customstatnames.(iistr)) - PTypeStructure.(iistr).Parameters.(customstatnames.(iistr){pp})=CustomStats.(iistr).(customstatnames.(iistr){pp}); - end - end - end -end - - %% Evaluate General Eqm Eqns % use of real() is a hack that could disguise errors, but I couldn't find why matlab was treating output as complex From 0e367ee8b20e1b5e53e5a783b5bbc62d6074372d Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 24 Mar 2026 17:06:13 +1300 Subject: [PATCH 33/90] Need to initialize PTypeStructure.(iistr).Parameters before use Easily enough done... --- ...teroAgentStationaryEqm_Case1_FHorz_PType.m | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m index 9c522120..755f4e06 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m @@ -470,6 +470,32 @@ end end + %% Parameter Structure + % Parameters are allowed to be given as structure, or as vector/matrix + % (in terms of their dependence on permanent type). So go through each of + % these in term. + % ie. Parameters.alpha=[0;1]; or Parameters.alpha.ptype1=0; Parameters.alpha.ptype2=1; + % Need to establish this in PTypeStructure before we use it below. + PTypeStructure.(iistr).Parameters=Parameters; + FullParamNames=fieldnames(Parameters); % all the different parameters + nFields=length(FullParamNames); + for kField=1:nFields + if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form + % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) + if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); + end + elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. + temp=Parameters.(FullParamNames{kField}); + [~,ptypedim]=max(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i); % Parameters as vector/matrix can be at most two dimensional, figure out which relates to PType, it should be the row dimension, if it is not then give a warning. + if ptypedim==1 + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=temp(ii,:); + elseif ptypedim==2 + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=temp(:,ii); + end + end + end + % Check if using ExogShockFn or EiidShockFn, and if so, do these use a parameter that is being determined in general eqm heteroagentoptions.gridsinGE(ii)=0; From 80e8899bed9a76921edc9b1e1454fbbb9c02f268 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 24 Mar 2026 17:12:55 +1300 Subject: [PATCH 34/90] Fix merge error...and minor Changed iterator for `customstatnames` to match with `intEqnnames` and `AggVarNames`. --- .../PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m | 1 - 1 file changed, 1 deletion(-) diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m index 0545cc90..6c43f88f 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m @@ -173,7 +173,6 @@ end end - %% Evaluate General Eqm Eqns % use of real() is a hack that could disguise errors, but I couldn't find why matlab was treating output as complex GeneralEqmConditionsVec=zeros(1,length(GEeqnNames)); From 032d7a5c2cc4e335ade51a5e843e1c17a9bef18a Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 25 Mar 2026 02:38:22 +1300 Subject: [PATCH 35/90] Revert stylistic change Don't try to be too clever when N_z==1 (treating it the same as when N_z==0). The efficiency is not worth the code divergence. --- aprimeFnMatrix/CreateaprimePolicyExperienceAsset.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aprimeFnMatrix/CreateaprimePolicyExperienceAsset.m b/aprimeFnMatrix/CreateaprimePolicyExperienceAsset.m index 5b779ce1..0c1b2dfa 100644 --- a/aprimeFnMatrix/CreateaprimePolicyExperienceAsset.m +++ b/aprimeFnMatrix/CreateaprimePolicyExperienceAsset.m @@ -114,7 +114,7 @@ %% Calcuate grid indexes and probs from the values -if N_z<2 +if N_z==0 a2primeVals=reshape(a2primeVals,[1,N_a]); else a2primeVals=reshape(a2primeVals,[1,N_a*N_z]); @@ -131,7 +131,7 @@ % Those points which tried to leave the top of the grid have probability 1 of the 'upper' point (0 of lower point) offTopOfGrid=(a2primeVals>=a2_grid(end)); a2primeIndexes(offTopOfGrid)=n_a2-1; % lower grid point is the one before the end point -if N_z<2 +if N_z==0 a2primeIndexes=reshape(a2primeIndexes,[N_a,1]); else a2primeIndexes=reshape(a2primeIndexes,[N_a*N_z,1]); @@ -145,7 +145,7 @@ a2primeProbs(offBottomOfGrid)=1; a2primeProbs(offTopOfGrid)=0; -if N_z<2 +if N_z==0 a2primeIndexes=reshape(a2primeIndexes,[N_a,1]); % Index of lower grid point a2primeProbs=reshape(a2primeProbs,[N_a,1]); % Probability of lower grid point else From 82359668fccc1bea68f3d856a482c4b879453a28 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 25 Mar 2026 06:34:21 +1300 Subject: [PATCH 36/90] Remaining PType and TPath changes (no ExpAsset) These are the non-ExpAsset changes needed thus far to progress a model through all the ptype and tpath things I have encountered. --- .../EvalFnOnTransPath_AggVars_InfHorz.m | 1 - ...lFnOnTransPath_AggVars_Case1_FHorz_PType.m | 2 +- ...teroAgentStationaryEqm_Case1_FHorz_PType.m | 10 +---- ..._PType_ExpAsset_singlepath_fastOLG_e_raw.m | 2 +- .../PType/TransitionPath_Case1_FHorz_PType.m | 42 +++++++++---------- .../ValueFnIter_nod_HowardGreedy_raw.m | 9 ---- 6 files changed, 23 insertions(+), 43 deletions(-) diff --git a/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m b/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m index e620407d..341e6f74 100644 --- a/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m +++ b/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m @@ -12,7 +12,6 @@ transpathoptions.verbose=0; end end - if ~exist('simoptions','var') % If simoptions is not given, just use all the defaults simoptions.experienceasset=0; diff --git a/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m b/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m index a760775b..9f6ab038 100644 --- a/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m +++ b/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m @@ -188,7 +188,7 @@ AggVarsPath_ii=EvalFnOnTransPath_AggVars_Case1_FHorz(FnsToEvaluate_temp, AgentDistPath_temp, PolicyPath_temp, PricePath_temp, ParamPath_temp, Parameters_temp, T, n_d_temp, n_a_temp, n_z_temp, N_j_temp, d_grid_temp, a_grid_temp,z_grid_temp, transpathoptions_temp, simoptions_temp); else AggVarsPath_ii=EvalFnOnTransPath_AggVars_Case1(FnsToEvaluate_temp, AgentDistPath_temp, PolicyPath_temp, PricePath_temp, ParamPath_temp, Parameters_temp, T, n_d_temp, n_a_temp, n_z_temp, d_grid_temp, a_grid_temp,z_grid_temp, transpathoptions_temp, simoptions_temp); - end + end FnNames_temp=fieldnames(FnsToEvaluate_temp); for ff=1:length(FnNames_temp) diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m index 755f4e06..6b0eb13c 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m @@ -596,7 +596,6 @@ PTypeStructure.(iistr).vfoptions.alreadygridvals_semiexo=1; PTypeStructure.(iistr).simoptions.alreadygridvals_semiexo=1; end - %% DiscountFactor and ReturnFn % The parameter names can be made to depend on the permanent-type @@ -615,7 +614,6 @@ %% jequaloneDist and AgeWeightsParamNames if isfinite(PTypeStructure.(iistr).N_j) % FHorz - if isstruct(jequaloneDist) if isfield(jequaloneDist,PTypeStructure.Names_i{ii}) if isa(jequaloneDist, 'function_handle') @@ -624,9 +622,7 @@ PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(PTypeStructure.Names_i{ii}); end else - if isfinite(PTypeStructure.(iistr).N_j) - error(['You must input jequaloneDist for permanent type ', PTypeStructure.Names_i{ii}, ' \n']) - end + error(['You must input jequaloneDist for permanent type ', PTypeStructure.Names_i{ii}, ' \n']) end else PTypeStructure.(iistr).jequaloneDist=jequaloneDist; @@ -637,9 +633,7 @@ if isfield(AgeWeightParamNames,Names_i{ii}) PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(Names_i{ii}); else - if isfinite(PTypeStructure.(iistr).N_j) - error(['You must input AgeWeightParamNames for permanent type ', Names_i{ii}, ' \n']) - end + error(['You must input AgeWeightParamNames for permanent type ', Names_i{ii}, ' \n']) end end end diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m index a31e1713..0940fead 100644 --- a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m +++ b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m @@ -163,7 +163,7 @@ end %% AggVars - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_a,l_a,l_ze,N_a,N_ze,a_gridvals,ze_gridvals_J_fastOLG,1); + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_ze,N_a,N_ze,a_gridvals,ze_gridvals_J_fastOLG,1); for ff=1:length(AggVarNames) Parameters.(AggVarNames{ff})=AggVars.(AggVarNames{ff}).Mean; end diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m index e294cec9..1356ab44 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m @@ -165,7 +165,7 @@ end end -%% Get AgeWeights from Parameters +%% Get AgeWeights from Parameters if isstruct(AgeWeightsParamNames) for ii=1:N_i try @@ -660,33 +660,17 @@ AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); end end - end - % Get AgeWeights and switch into the transpathoptions.ageweightstrivial=0 setup (and this is what subfns hardcode when doing PTypes) - % It is assumed there is only one Age Weight Parameter (name)) - % AgeWeights_T is (a,j,z)-by-T (create as j-by-T to start, then switch) - if isstruct(AgeWeights) - AgeWeights_ii=AgeWeights.(iistr); - if all(size(AgeWeights_ii)==[N_j,1]) + if all(size(AgeWeights_ii)==[N_j_temp,1]) % Does not depend on transition path period PTypeStructure.(iistr).AgeWeights_T=gather(AgeWeights_ii.*ones(1,T)); elseif all(size(AgeWeights)==[1,N_j]) % Does not depend on transition path period PTypeStructure.(iistr).AgeWeights_T=gather(AgeWeights_ii'.*ones(1,T)); else - fprintf('Following error applies to agent permanent type: %s \n',Names_i{ii}) - error('The age weights parameter seems to be the wrong size') - end - else % not a structure, so must apply to all permanent types - if all(size(AgeWeights)==[N_j,1]) - % Does not depend on transition path period - PTypeStructure.(iistr).AgeWeights_T=gather(AgeWeights.*ones(1,T)); - elseif all(size(AgeWeights)==[1,N_j]) - % Does not depend on transition path period - PTypeStructure.(iistr).AgeWeights_T=gather(AgeWeights'.*ones(1,T)); - else + fprintf('Following error applies to agent permanent type: %s \n',iistr) error('The age weights parameter seems to be the wrong size') end - end + % Check ParamPath to see if the AgeWeights vary over the transition % (and overwrite PTypeStructure.(iistr).AgeWeights_T if it does) temp=strcmp(ParamPathNames,AgeWeightsParamNames{1}); @@ -755,6 +739,7 @@ end PTypeStructure.(iistr).jequalOneDist=jequalOneDist_temp; end + AgentDist_initial.(iistr)=AgentDist_init; clear AgentDist_init @@ -870,9 +855,20 @@ use_tminus1price=0; if ~isempty(tminus1priceNames) use_tminus1price=1; - for ii=1:length(tminus1priceNames) - if ~isfield(transpathoptions.initialvalues,tminus1priceNames{ii}) - error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1priceNames{ii}) + if isstruct(tminus1AggVarsNames) + AggVarsPTypes=fieldnames(tminus1AggVarsNames); + for nn=1:length(AggVarsPTypes) + for ii=1:length(tminus1AggVarsNames.(AggVarsPTypes{nn})) + if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames.(AggVarsPTypes{nn}){ii}) + error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames{ii}) + end + end + end + else + for ii=1:length(tminus1AggVarsNames) + if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames{ii}) + error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames{ii}) + end end end end diff --git a/ValueFnIter/InfHorz/ValueFnIter_nod_HowardGreedy_raw.m b/ValueFnIter/InfHorz/ValueFnIter_nod_HowardGreedy_raw.m index 0f9686e9..0e69070d 100644 --- a/ValueFnIter/InfHorz/ValueFnIter_nod_HowardGreedy_raw.m +++ b/ValueFnIter/InfHorz/ValueFnIter_nod_HowardGreedy_raw.m @@ -56,14 +56,5 @@ warning('Value fn iteration has stopped due to reaching the maximum number of iterations (not due to convergence); can be set by vfoptions.maxiter.') end -%% Howards greedy cannot solve models where V contains values of -Inf. Can kind of test for this by looking for -Inf in Ftemp -if any(~isfinite(Ftemp)) - warning('Howards-greedy cannot be used for models where V contains values of -Inf. This model looks like it may be one where V takes a value of -Inf at some points in the state-space. Consider checking solution against that with vfoptions.howardsgreedy=0') -end - -if tempcounter>=maxiter - warning('Value fn iteration has stopped due to reaching the maximum number of iterations (not due to convergence); can be set by vfoptions.maxiter.') -end - end From 12b333dfef8e881507876362344e07f5a2aef746 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Mon, 6 Apr 2026 11:10:50 +1200 Subject: [PATCH 37/90] Re-align with InfHorz ExpAsset changes in main This realignment involves renaming some files to put ExpAsset further down the name path. This might also suggest moving some directories around, but we leave that until later. Also collect other changes needed to run OLG-Electrify model. --- ...AgentDist_AggVars_FHorz_ExpAsset_fastOLG.m | 2 +- PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m | 8 +- PolicyInd2Val/PolicyInd2Val_InfHorz_TPath.m | 3 + .../StationaryDist_InfHorz_ExpAsset.m | 11 +- .../KronPolicyIndexes_Case1_ExpAsset_e.m | 113 ++++++++++++ ...KronPolicyIndexes_FHorz_Case1_ExpAsset_e.m | 50 +++++ ...tionPath_FHorz_PType_ExpAsset_singlepath.m | 6 +- ...th_FHorz_PType_ExpAsset_singlepath_e_raw.m | 2 +- ...Path_FHorz_PType_ExpAsset_singlepath_raw.m | 2 +- ...ueFnIter_FHorz_TPath_ExpAsset_SingleStep.m | 29 --- ...FnIter_FHorz_TPath_ExpAsset_SingleStep_e.m | 30 --- ...ueFnIter_FHorz_TPath_SingleStep_ExpAsset.m | 1 + ...FnIter_FHorz_TPath_SingleStep_ExpAsset_e.m | 1 + ...r_FHorz_TPath_SingleStep_ExpAsset_e_raw.m} | 2 +- ...ter_FHorz_TPath_SingleStep_ExpAsset_raw.m} | 10 +- ...onPath_InfHorz_PType_ExpAsset_singlepath.m | 172 ++++++++++++++++++ .../ValueFnIter_FHorz_ExpAsset_raw.m | 2 +- 17 files changed, 369 insertions(+), 75 deletions(-) create mode 100644 SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_Case1_ExpAsset_e.m create mode 100644 SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_FHorz_Case1_ExpAsset_e.m delete mode 100644 TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m delete mode 100644 TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e.m rename TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/{ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw.m => ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m} (99%) rename TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/{ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m => ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m} (94%) create mode 100644 TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m diff --git a/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m b/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m index c99a32da..ef0f15df 100644 --- a/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m +++ b/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m @@ -67,7 +67,7 @@ elseif l_aprime==2 Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==4 Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); end diff --git a/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m b/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m index 5cabcb95..4adf09f6 100644 --- a/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m +++ b/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m @@ -28,6 +28,9 @@ l_aprime=length(n_a); if vfoptions.experienceasset==1 l_aprime=l_aprime-1; + if n_d(1)==0 + n_d=n_d(2:end); + end end PolicyPath=reshape(PolicyPath,[size(PolicyPath,1),N_a,N_z,N_j,T]); @@ -89,7 +92,10 @@ % If using vfoptions.gridinterplater=1, it should be aprime_gridvals for the fine grid, not the rough grid l_aprime=size(a_grid,2);% it is aprime_gridvals, not actually a_grid - + if vfoptions.experienceasset==1 && n_d(1)==0 + n_d=n_d(2:end); + end + if n_d(1)==0 PolicyValuesPath=zeros(l_aprime,N_a*N_j*N_z*T,'gpuArray'); % N_a*N_j*N_z*T is because of how indexing gridvals will work if vfoptions.gridinterplayer==1 diff --git a/PolicyInd2Val/PolicyInd2Val_InfHorz_TPath.m b/PolicyInd2Val/PolicyInd2Val_InfHorz_TPath.m index 96df97fd..b0a12253 100644 --- a/PolicyInd2Val/PolicyInd2Val_InfHorz_TPath.m +++ b/PolicyInd2Val/PolicyInd2Val_InfHorz_TPath.m @@ -15,6 +15,9 @@ l_aprime=length(n_a); if vfoptions.experienceasset==1 l_aprime=l_aprime-1; + if n_d(1)==0 + n_d=n_d(2:end); + end end PolicyPath=reshape(PolicyPath,[size(PolicyPath,1),N_a,N_z,T]); diff --git a/StationaryDist/InfHorz/ExpAsset/StationaryDist_InfHorz_ExpAsset.m b/StationaryDist/InfHorz/ExpAsset/StationaryDist_InfHorz_ExpAsset.m index ec881b99..211c2cf9 100644 --- a/StationaryDist/InfHorz/ExpAsset/StationaryDist_InfHorz_ExpAsset.m +++ b/StationaryDist/InfHorz/ExpAsset/StationaryDist_InfHorz_ExpAsset.m @@ -50,11 +50,14 @@ N_a=prod(n_a); N_z=prod(n_z); - - -%% if isfield(simoptions,'n_e') N_e=prod(simoptions.n_e); +else + N_e=0; +end + +%% +if N_e>0 StationaryDistKron=reshape(StationaryDistKron,[N_a*N_z*N_e,1]); Policy=reshape(Policy,[size(Policy,1),N_a,N_z*N_e]); n_ze=[n_z,simoptions.n_e]; @@ -99,7 +102,7 @@ %% % Note: N_z=0 is a different code -if isfield(simoptions,'n_e') +if N_e>0 StationaryDist=StationaryDist_InfHorz_Iteration_nProbs_e_raw(StationaryDistKron,Policy_aprime,PolicyProbs,2,N_a,N_z,N_e,pi_z,pi_e,simoptions); % zero is n_d, because we already converted Policy to only contain aprime else % no e StationaryDist=StationaryDist_InfHorz_Iteration_nProbs_raw(StationaryDistKron,Policy_aprime,PolicyProbs,2,N_a,N_z,pi_z,simoptions); % zero is n_d, because we already converted Policy to only contain aprime diff --git a/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_Case1_ExpAsset_e.m b/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_Case1_ExpAsset_e.m new file mode 100644 index 00000000..e586404c --- /dev/null +++ b/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_Case1_ExpAsset_e.m @@ -0,0 +1,113 @@ +function PolicyKron=KronPolicyIndexes_Case1_ExpAsset_e(Policy, n_d, n_a1, n_a2, n_z, n_e, simoptions) + +% Input: Policy (l_d+l_a,n_a,n_z,n_e); +% +% Output: PolicyKron=zeros(2,N_a,N_z,N_e); %first dim indexes the optimal choice for d and aprime rest of dimensions a,z +% (N_a,N_z,N_e) if there is no d +% Note: simoptions.gridinterplayer=1 means there will be an additional index for the second layer in both the input and output versions + +N_a1=prod(n_a1); +N_a2=prod(n_a2); +N_a=N_a1*N_a2; +N_z=prod(n_z); +N_e=prod(n_e); + +l_aprime=length(n_a1); + +if simoptions.gridinterplayer==0 + % Reshape Policy + % if n_d(1)==0 && l_a==1 + % Policy=reshape(Policy,[N_a,N_z,N_e]); + % else + Policy=reshape(Policy,[size(Policy,1),N_a,N_z,N_e]); + % end + + if n_d(1)==0 + if l_aprime==1 + PolicyKron=reshape(Policy,[N_a,N_z,N_e]); + else %l_a>1 + temp=ones(l_aprime,1,'gpuArray')-eye(l_aprime,1,'gpuArray'); + temp2=gpuArray(cumprod(n_a1')); % column vector + PolicyTemp=(reshape(Policy,[l_aprime,N_a*N_z*N_e])-temp*ones(1,N_a*N_z*N_e,'gpuArray')).*([1;temp2(1:end-1)]*ones(1,N_a*N_z*N_e,'gpuArray')); + PolicyKron=reshape(sum(PolicyTemp,1),[N_a,N_z,N_e]); + end + else + l_d=length(n_d); + + % Should test whether code runs faster with this predeclaration of PolicyKron commented or uncommented + PolicyKron=zeros(2,N_a,N_z,N_e,'gpuArray'); + + if l_d==1 + PolicyKron(1,:,:,:)=Policy(1,:,:,:); + else + temp=ones(l_d,1,'gpuArray')-eye(l_d,1,'gpuArray'); + temp2=gpuArray(cumprod(n_d')); % column vector + PolicyTemp=(reshape(Policy(1:l_d,:,:,:),[l_d,N_a*N_z*N_e])-temp*ones(1,N_a*N_z*N_e,'gpuArray')).*([1;temp2(1:end-1)]*ones(1,N_a*N_z*N_e,'gpuArray')); + PolicyKron(1,:,:,:)=reshape(sum(PolicyTemp,1),[1,N_a,N_z,N_e]); + end + % Then, a + if l_aprime==1 + PolicyKron(2,:,:,:)=Policy(l_d+1,:,:,:); + else + temp=ones(l_aprime,1,'gpuArray')-eye(l_aprime,1,'gpuArray'); + temp2=gpuArray(cumprod(n_a1')); % column vector + PolicyTemp=(reshape(Policy(l_d+1:l_d+l_aprime,:,:,:),[l_aprime,N_a*N_z*N_e])-temp*ones(1,N_a*N_z*N_e,'gpuArray')).*([1;temp2(1:end-1)]*ones(1,N_a*N_z*N_e,'gpuArray')); + PolicyKron(2,:,:,:)=reshape(sum(PolicyTemp,1),[1,N_a,N_z,N_e]); + end + end + +elseif simoptions.gridinterplayer==1 + % Reshape Policy + Policy=reshape(Policy,[size(Policy,1),N_a,N_z,N_e]); + + if n_d(1)==0 + if l_aprime==1 || l_aprime==2 + PolicyKron=Policy; % a1, possibly a2, L2 + else %l_a>2 + PolicyKron=zeros(3,N_a,N_z,N_e,'gpuArray'); + PolicyKron(1,:,:,:)=Policy(1,:,:); % a1 + temp=[0; ones(l_aprime-2,1,'gpuArray')]; + temp2=gpuArray(cumprod(n_a1(2:end)')); % column vector + PolicyTemp=(reshape(Policy(2:l_aprime,:,:,:),[l_aprime-1,N_a*N_z*N_e])-temp).*[1;temp2(1:end-1)]; + PolicyKron(2,:,:,:)=reshape(sum(PolicyTemp,1),[1,N_a,N_z,N_e]); + PolicyKron(3,:,:,:)=Policy(l_aprime+1,:,:,:); % L2 index + end + else + l_d=length(n_d); + if l_aprime==1 + PolicyKron=zeros(3,N_a,N_z,N_e,'gpuArray'); + else + PolicyKron=zeros(4,N_a,N_z,N_e,'gpuArray'); + end + + if l_d==1 + PolicyKron(1,:,:,:)=Policy(1,:,:,:); + else + temp=[0; ones(l_d-1,1,'gpuArray')]; + temp2=gpuArray(cumprod(n_d')); % column vector + PolicyTemp=(reshape(Policy(1:l_d,:,:,:),[l_d,N_a*N_z*N_e])-temp).*[1;temp2(1:end-1)]; + PolicyKron(1,:,:,:)=reshape(sum(PolicyTemp,1),[1,N_a,N_z,N_e]); + end + % Then, a + if l_aprime==1 + PolicyKron(2,:,:,:)=Policy(l_d+1,:,:,:); + PolicyKron(3,:,:,:)=Policy(l_aprime+l_d+1,:,:,:); % L2 index + elseif l_aprime==2 + PolicyKron(2,:,:,:)=Policy(l_d+1,:,:,:); + PolicyKron(3,:,:,:)=Policy(l_d+2,:,:,:); + PolicyKron(4,:,:,:)=Policy(l_aprime+l_d+1,:,:,:); % L2 index + else + PolicyKron(2,:,:,:)=Policy(l_d+1,:,:,:); + temp=[0; ones(l_aprime-2,1,'gpuArray')]; + temp2=gpuArray(cumprod(n_a1(2:end)')); % column vector + PolicyTemp=(reshape(Policy(l_d+2:l_d+l_aprime,:,:,:),[l_aprime-1,N_a*N_z*N_e])-temp).*[1;temp2(1:end-1)]; + PolicyKron(3,:,:,:)=reshape(sum(PolicyTemp,1),[1,N_a,N_z,N_e]); + PolicyKron(4,:,:,:)=Policy(l_aprime+l_d+1,:,:,:); % L2 index + end + + end + +end + + +end \ No newline at end of file diff --git a/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_FHorz_Case1_ExpAsset_e.m b/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_FHorz_Case1_ExpAsset_e.m new file mode 100644 index 00000000..96f3c3c0 --- /dev/null +++ b/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_FHorz_Case1_ExpAsset_e.m @@ -0,0 +1,50 @@ +function PolicyKron=KronPolicyIndexes_FHorz_Case1_ExpAsset_e(Policy, n_d, n_a1, n_a2, n_z, n_e, N_j, simoptions) +% Input: Policy (l_d+l_aprime,n_a,n_z,n_e,N_j); +% +% Output: Policy=zeros(2,N_a,N_z,N_e,N_j); %first dim indexes the optimal choice for d and aprime rest of dimensions a,z +% (N_a,N_z,N_e,N_j) if there is no d + +N_a1=prod(n_a1); +N_a2=prod(n_a2); +N_a=N_a1*N_a2; +N_z=prod(n_z); + +% When using n_e, is instead: +% Input: Policy (l_d+l_aprime,n_a,n_z,n_e,N_j); +% +% Output: Policy=zeros(2,N_a,N_z,N_e,N_j); %first dim indexes the optimal choice for d and aprime rest of dimensions a,z +% (N_a,N_z,N_e,N_j) if there is no d + + +%% +N_e=prod(n_e); +Policy=reshape(Policy,[size(Policy,1),N_a,N_z,N_e,N_j]); + +% gpu only +if simoptions.gridinterplayer==0 + if n_d(1)==0 + PolicyKron=zeros(N_a,N_z,N_e,N_j,'gpuArray'); + for jj=1:N_j + PolicyKron(:,:,:,jj)=KronPolicyIndexes_Case1_ExpAsset_e(Policy(:,:,:,:,jj), n_d, n_a1, n_a2, n_z, n_e, simoptions); + end + else + PolicyKron=zeros(2,N_a,N_z,N_e,N_j,'gpuArray'); + for jj=1:N_j + PolicyKron(:,:,:,:,jj)=KronPolicyIndexes_Case1_ExpAsset_e(Policy(:,:,:,:,jj), n_d, n_a1, n_a2, n_z, n_e, simoptions); + end + end +elseif simoptions.gridinterplayer==1 + if n_d(1)==0 + PolicyKron=zeros(2,N_a,N_z,N_e,N_j,'gpuArray'); + for jj=1:N_j + PolicyKron(:,:,:,:,jj)=KronPolicyIndexes_Case1_ExpAsset_e(Policy(:,:,:,:,jj), n_d, n_a1, n_a2, n_z, n_e, simoptions); + end + else + PolicyKron=zeros(3,N_a,N_z,N_e,N_j,'gpuArray'); + for jj=1:N_j + PolicyKron(:,:,:,:,jj)=KronPolicyIndexes_Case1_ExpAsset_e(Policy(:,:,:,:,jj), n_d, n_a1, n_a2, n_z, n_e, simoptions); + end + end +end + +end \ No newline at end of file diff --git a/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m b/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m index 68cf70d0..347dd190 100644 --- a/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m +++ b/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m @@ -1,4 +1,8 @@ -function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions) +function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T, ... + l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG, ... + ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, ... + PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, ... + transpathoptions, vfoptions, simoptions) % When doing shooting alogrithm on TPath FHorz PType, this is for a given ptype, and does the steps of back-iterate to get policy, then forward to get agent dist and agg vars. % The only output is the agg vars path. diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m index 527c990a..5f9b43d4 100644 --- a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m +++ b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m @@ -76,7 +76,7 @@ pi_e_J=transpathoptions.pi_e_J_T(:,:,T-ttr); end - [V, Policy]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e(V,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); + [V, Policy]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e(V,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); % The V input is next period value fn, the V output is this period. % Policy is kept in the form where it is just a single-value in (d,a') diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m index 49e40e3f..7b7a6db3 100644 --- a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m +++ b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m @@ -70,7 +70,7 @@ pi_z_J=transpathoptions.pi_z_J_T(:,:,:,T-ttr); end - [V, Policy]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); + [V, Policy]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); % The V input is next period value fn, the V output is this period. % Policy is kept in the form where it is just a single-value in (d,a') diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m deleted file mode 100644 index aac711bf..00000000 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep.m +++ /dev/null @@ -1,29 +0,0 @@ -function [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep(VKron,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) -% The VKron input is next period value fn, the VKron output is this period. - -N_d1=prod(n_d1); -% N_a1=prod(n_a1); -N_z=prod(n_z); - -%% If get to here then not using exoticpreferences nor StateDependentVariables_z -% N_z==0 is handled by a different command -if vfoptions.divideandconquer==0 - if vfoptions.gridinterplayer==0 - if N_d1==0 - error("not implemented") - [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_nod_raw(VKron,n_a, n_z, N_j, a_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); - else - [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw(VKron,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); - end - else - error('Have not yet implemented grid interpolation layer for FHorz TPath without fastOLG=1. Ask on forum if you need this.') - end -else - error('Have not yet implemented divide and conquer for FHorz TPath without fastOLG=1. Ask on forum if you need this.') -end - -%% Policy in transition paths -PolicyKron=UnKronPolicyIndexes_Case1_FHorz_ExpAsset(PolicyKron,n_d1,n_d2,n_a1,n_a2,N_z,N_j,vfoptions); -PolicyKron=reshape(PolicyKron,[size(PolicyKron,1),prod(n_a1)*prod(n_a2),N_z,N_j]); - -end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e.m deleted file mode 100644 index f2fe3ed5..00000000 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e.m +++ /dev/null @@ -1,30 +0,0 @@ -function [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e(VKron,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) -% The VKron input is next period value fn, the VKron output is this period. - -N_d1=prod(n_d1); -% N_a1=prod(n_a1); -N_z=prod(n_z); -N_e=prod(n_e); - -%% If get to here then not using exoticpreferences nor StateDependentVariables_z -% N_z==0 is handled by a different command -if vfoptions.divideandconquer==0 - if vfoptions.gridinterplayer==0 - if N_d1==0 - error("not implemented") - [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_nod_e_raw(VKron,n_a, n_z,n_e, N_j, a_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); - else - [VKron, PolicyKron]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw(VKron,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); - end - else - error('Have not yet implemented grid interpolation layer for FHorz TPath without fastOLG=1. Ask on forum if you need this.') - end -else - error('Have not yet implemented divide and conquer for FHorz TPath without fastOLG=1. Ask on forum if you need this.') -end - -%% Policy in transition paths -PolicyKron=UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e(PolicyKron,n_d1,n_d2,n_a1,n_a2,N_z,N_e,N_j,vfoptions); -PolicyKron=reshape(PolicyKron,[size(PolicyKron,1),prod(n_a1)*prod(n_a2),N_z,N_e,N_j]); - -end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset.m index 3d4cc731..7d98f0a5 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset.m @@ -28,4 +28,5 @@ end % PolicyKron=reshape(PolicyKron,[size(PolicyKron,1),N_a,N_z,N_j]); + end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e.m index 7de76894..0c4afb13 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e.m @@ -29,4 +29,5 @@ end % PolicyKron=reshape(PolicyKron,[size(PolicyKron,1),N_a,N_z,N_e,N_j]); + end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m similarity index 99% rename from TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw.m rename to TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m index 9b59bd6f..e135c9c2 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m @@ -1,4 +1,4 @@ -function [V,Policy3]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_e_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) +function [V,Policy3]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) N_d1=prod(n_d1); N_d2=prod(n_d2); diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m similarity index 94% rename from TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m rename to TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m index 615255ad..7982d7b4 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m @@ -1,4 +1,4 @@ -function [V,Policy3]=ValueFnIter_FHorz_TPath_ExpAsset_SingleStep_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) +function [V,Policy3]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) N_d1=prod(n_d1); N_d2=prod(n_d2); @@ -144,11 +144,11 @@ Policy3(2,:,:,:)=shiftdim(rem(Policy-1,N_d2)+1,-1); Policy3(3,:,:,:)=shiftdim(ceil(Policy/N_d2),-1); else - Policy2=zeros(3,N_a,N_z,N_j,'gpuArray'); % first dim indexes the optimal choice for (d1,d2,aprime) rest of dimensions a,z + Policy3=zeros(3,N_a,N_z,N_j,'gpuArray'); % first dim indexes the optimal choice for (d1,d2,aprime) rest of dimensions a,z % second dim indexes the optimal choice for (d2,aprime) rest of dimensions a,z - Policy3(1,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); % one of these is wrong - Policy3(2,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); % one of these is wrong - Policy3(3,:,:,:)=shiftdim(ceil(Policy/N_d),-1); % this may be wrong + Policy3(1,:,:,:)=shiftdim(rem(Policy-1,N_d1)+1,-1); % one of these is wrong + Policy3(2,:,:,:)=shiftdim(ceil(Policy/N_d1),-1); % one of these is wrong + Policy3(3,:,:,:)=shiftdim(ceil(Policy/N_d),-1); % this may be wrong end end diff --git a/TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m b/TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m new file mode 100644 index 00000000..0158e916 --- /dev/null +++ b/TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m @@ -0,0 +1,172 @@ +function AggVarsPath=TransitionPath_InfHorz_PType_ExpAsset_singlepath(PricePathOld, ParamPath, PricePathNames, ParamPathNames, T, V_final, AgentDist_initial, ... + l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_grid,pi_z, ... + ReturnFn, aprimeFn, FnsToEvaluate, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, ... + PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, ... + transpathoptions, vfoptions, simoptions) +% PricePathOld is matrix of size T-by-'number of prices' +% ParamPath is matrix of size T-by-'number of parameters that change over path' + +% Remark to self: No real need for T as input, as this is anyway the length of PricePathOld + +% For this agent type, first go back through the value & policy fns. +% Then forwards through agent dist and agg vars. + +N_a1=prod(n_a1); +N_a2=prod(n_a2); +N_a=N_a1*N_a2; + +l_aprime=length(n_a1); +l_a=l_aprime+length(n_a2); + +PolicyIndexesPath=zeros(l_d+l_aprime,N_a,N_z,T-1,'gpuArray'); %Periods 1 to T-1 + +% This and much of the rest of this code borrowed from TransitionPath_InfHorz_shooting.m +if simoptions.gridinterplayer==0 + II1=(1:1:N_a*N_z); % Index for this period (a,z) + IIones=ones(N_a*N_z,1); % Next period 'probabilities' +elseif simoptions.gridinterplayer==1 + PolicyProbsPath=zeros(N_a*N_z,2,T-1,'gpuArray'); % preallocate + II2=([1:1:N_a*N_z; 1:1:N_a*N_z]'); % Index for this period (a,z), note the 2 copies +end + +if size(V_final)~=[N_a,N_z] + error("V_final wrong shape") + V_final=reshape(V_final,[N_a,N_z]); +end + +%First, go from T-1 to 1 calculating the Value function and Optimal +%policy function at each step. Since we won't need to keep the value +%functions for anything later we just store the next period one in +%Vnext, and the current period one to be calculated in V +Vnext=V_final; +for ttr=1:T-1 %so t=T-i + % The following digs deeper into PricePathOld and ParamPath in + % FHorz case--check it + for kk=1:length(PricePathNames) + Parameters.(PricePathNames{kk})=PricePathOld(T-ttr,kk); + end + for kk=1:length(ParamPathNames) + Parameters.(ParamPathNames{kk})=ParamPath(T-ttr,kk); + end + [V, Policy]=ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset(Vnext,n_d1,n_d2,n_a1,n_a2,n_z,d_gridvals, d2_grid, a1_gridvals, a2_grid, z_grid, pi_z, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); + % The VKron input is next period value fn, the VKron output is this period. + % Policy is kept in the form where it is just a single-value in (d,a') + + PolicyIndexesPath(:,:,:,T-ttr)=Policy; + Vnext=V; + +end +% Free up space on GPU by deleting things no longer needed +clear V Vnext + +%% Modify PolicyIndexesPath into forms needed for forward iteration +% Create version of PolicyIndexesPath in form we want for the agent distribution iteration +% Creates PolicyaprimezPath, and when using grid interpolation layer also PolicyProbsPath +if isscalar(n_a1) + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:),[N_a*N_z,T-1]); % aprime index +elseif length(n_a1)==2 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1),[N_a*N_z,T-1]); +elseif length(n_a1)==3 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,:)-1),[N_a*N_z,T-1]); +elseif length(n_a1)==41 + PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,:)-1)+prod(n_a1(1:3))*(PolicyIndexesPath(l_d+4,:,:,:)-1),[N_a*N_z,T-1]); +end +PolicyaprimezPath=PolicyaprimePath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1); +if simoptions.gridinterplayer==1 + PolicyaprimezPath=reshape(PolicyaprimezPath,[N_a*N_z,1,T-1]); % reinterpret this as lower grid index + PolicyaprimezPath=repelem(PolicyaprimezPath,1,2,1); % create copy that will be the upper grid index + PolicyaprimezPath(:,2,:)=PolicyaprimezPath(:,2,:)+1; % upper grid index + PolicyProbsPath(:,2,:)=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,:),[N_a*N_z,1,T-1]); % L2 index + PolicyProbsPath(:,2,:)=(PolicyProbsPath(:,2,:)-1)/(1+simoptions.ngridinterp); % probability of upper grid point + PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point +end +% Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats +PolicyValuesPath=PolicyInd2Val_InfHorz_TPath(PolicyIndexesPath,[n_d1,n_d2],[n_a1,n_a2],n_z,T-1,d_gridvals,a_gridvals,vfoptions,1); +PolicyValuesPath=permute(reshape(PolicyValuesPath,[size(PolicyValuesPath,1),N_a,N_z,T-1]),[2,3,1,4]); %[N_a,N_z,l_d+l_a,T-1] + +%Now we have the full PolicyIndexesPath, we go forward in time from 1 +%to T using the policies to update the agents distribution generating a +%new price path +%Call AgentDist the current periods distn + +AgentDist=sparse(gather(reshape(AgentDist_initial,[N_a*N_z,1]))); +AggVarsPath=zeros(length(FnsToEvaluate),T-1); +pi_z_sparse=sparse(gather(pi_z)); % Need full pi_z for value fn, and sparse for agent dist + +for tt=1:T-1 + %% Setup the Parameters for period tt + + % Get t-1 PricePath and ParamPath before we update them + if use_tminus1price==1 + for pp=1:length(tminus1priceNames) + if tt>1 + Parameters.([tminus1priceNames{pp},'_tminus1'])=Parameters.(tminus1priceNames{pp}); + else + Parameters.([tminus1priceNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1priceNames{pp}); + end + end + end + if use_tminus1params==1 + for pp=1:length(tminus1paramNames) + if tt>1 + Parameters.([tminus1paramNames{pp},'_tminus1'])=Parameters.(tminus1paramNames{pp}); + else + Parameters.([tminus1paramNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1paramNames{pp}); + end + end + end + % Get t-1 AggVars before we update them + if use_tminus1AggVars==1 + for pp=1:length(tminus1AggVarsNames) + if tt>1 + % The AggVars have not yet been updated, so they still contain previous period values + Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); + else + Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); + end + end + end + + % Update current PricePath and ParamPath + for kk=1:length(PricePathNames) + Parameters.(PricePathNames{kk})=PricePathOld(tt,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); + end + for kk=1:length(ParamPathNames) + Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); + end + + % Get t+1 PricePath + if use_tplus1price==1 + for pp=1:length(tplus1priceNames) + kk=tplus1pricePathkk(pp); + Parameters.([tplus1priceNames{pp},'_tplus1'])=PricePathOld(tt+1,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); % Make is so that the time t+1 variables can be used + end + end + + %% Get the current optimal policy, and iterate the agent dist + if simoptions.gridinterplayer==0 + AgentDistnext=AgentDist_InfHorz_TPath_SingleStep(AgentDist,PolicyaprimezPath(:,tt),II1,IIones,N_a,N_z,pi_z_sparse); + elseif simoptions.gridinterplayer==1 + AgentDistnext=AgentDist_InfHorz_TPath_SingleStep_nProbs(AgentDist,PolicyaprimezPath(:,:,tt),II2,PolicyProbsPath(:,:,tt),N_a,N_z,pi_z_sparse); + end + + %% AggVars + AggVars_Means=EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars(gpuArray(full(AgentDist)), PolicyValuesPath(:,:,:,tt), FnsToEvaluate, Parameters, FnsToEvaluateParamNames, AggVarNames, [n_a1,n_a2], n_z, a_gridvals, z_grid,1); + AggVars=zeros(length(AggVars_Means),1); + if length(fieldnames(AggVars_Means))~=length(AggVarNames) + error(["AggVar length disparity:";"---------";AggVarNames;"--- vs ---";fieldnames(AggVars_Means)]); + end + for ii=1:length(AggVarNames) + AggVars(ii)=AggVars_Means.(AggVarNames{ii}).Mean; + Parameters.(AggVarNames{ii})=AggVars(ii); + end + + % Do nothing with IntermediateEqns and GeneralEqmEqns as they are outside PType scope (they are where all the PTypes meet). + + AgentDist=AgentDistnext; + + AggVarsPath(:,tt)=AggVars; +end + + +end diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m index 9b163359..6fa5bb58 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m @@ -153,7 +153,7 @@ skipinterp=(Vlower==Vupper); aprimeProbs(skipinterp)=0; % effectively skips interpolation - % Switch EV from being in terps of a2prime to being in terms of d2 and a2 + % Switch EV from being in terms of a2prime to being in terms of d2 and a2 EV=aprimeProbs.*Vlower+(1-aprimeProbs).*Vupper; % (d2,a1prime,a2,zprime) % Already applied the probabilities from interpolating onto grid From 703e9caa99dea3fed0af464c0d3817c37ed47234 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Thu, 9 Apr 2026 11:49:19 +1200 Subject: [PATCH 38/90] Fix half of EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m Manually fix first several cases of what is hopefully automatically generated code. Setting Values to zeros up front papers over what happens when Values is not assigned by the conditional logic. I don't see a reason to assign it ahead of time, only to be overwritten. --- ...AgentDist_AggVars_FHorz_ExpAsset_fastOLG.m | 77 ++++++++++++------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m b/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m index ef0f15df..c80f947a 100644 --- a/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m +++ b/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m @@ -47,25 +47,29 @@ if l_z==1 if l_d==0 if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); end elseif l_d==1 if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); end elseif l_d==2 if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==3 Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==4 @@ -73,63 +77,78 @@ end elseif l_d==3 if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); end elseif l_d==4 if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); + elseif l_aprime==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); end end elseif l_z==2 if l_d==0 if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); end elseif l_d==1 if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); end elseif l_d==2 if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); end elseif l_d==3 if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); end elseif l_d==4 if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_aprime==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); end end + %% Fix these with code generator... elseif l_z==3 if l_d==0 if l_aprime==1 From a70096ac0a24ecf4cd490aeb876fccf25e87b4f6 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 10 Apr 2026 07:18:06 +1200 Subject: [PATCH 39/90] minor Just fix a few comments and get ready to fix more things. --- .../TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m | 1 + .../ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m index 5f9b43d4..d3afc43f 100644 --- a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m +++ b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m @@ -204,6 +204,7 @@ end %% AggVars + % PolicyValuesPath is [N_a,N_j,N_z*N_e,l_d+l_aprime,T-1] % fastOLG ordering is needed for AggVars AggVars=EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_ze,N_a,N_ze,a_gridvals,ze_gridvals_J_fastOLG,1); for ff=1:length(AggVarNames) Parameters.(AggVarNames{ff})=AggVars.(AggVarNames{ff}).Mean; diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m index e135c9c2..8d731417 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m @@ -186,12 +186,12 @@ error("gridinterp not implemented with ExpAsset yet") end if N_d1==0 - Policy3=zeros(2,N_a,N_z,N_e,N_j,'gpuArray'); % first dim indexes the optimal choice for d2 and aprime rest of dimensions a,z + Policy3=zeros(3,N_a,N_z,N_e,N_j,'gpuArray'); % first dim indexes the optimal choice for d2 and aprime rest of dimensions a,z Policy3(1,:,:,:,:)=1; % is meaningless anyway Policy3(2,:,:,:,:)=shiftdim(rem(Policy-1,N_d2)+1,-1); Policy3(3,:,:,:,:)=shiftdim(ceil(Policy/N_d2),-1); else - Policy2=zeros(3,N_a,N_z,N_e,N_j,'gpuArray'); % first dim indexes the optimal choice for (d1,d2,aprime) rest of dimensions a,z + Policy3=zeros(3,N_a,N_z,N_e,N_j,'gpuArray'); % first dim indexes the optimal choice for (d1,d2,aprime) rest of dimensions a,z % second dim indexes the optimal choice for (d2,aprime) rest of dimensions a,z Policy3(1,:,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); % one of these is wrong Policy3(2,:,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); % one of these is wrong From a8c45073ebc6297161bf234a14af903ccc6481c7 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Mon, 13 Apr 2026 11:11:48 +1200 Subject: [PATCH 40/90] minor revert Revert unnecessary code in the case where n_d(1)==0. --- PolicyInd2Val/PolicyInd2Val_InfHorz_TPath.m | 3 --- 1 file changed, 3 deletions(-) diff --git a/PolicyInd2Val/PolicyInd2Val_InfHorz_TPath.m b/PolicyInd2Val/PolicyInd2Val_InfHorz_TPath.m index b0a12253..96df97fd 100644 --- a/PolicyInd2Val/PolicyInd2Val_InfHorz_TPath.m +++ b/PolicyInd2Val/PolicyInd2Val_InfHorz_TPath.m @@ -15,9 +15,6 @@ l_aprime=length(n_a); if vfoptions.experienceasset==1 l_aprime=l_aprime-1; - if n_d(1)==0 - n_d=n_d(2:end); - end end PolicyPath=reshape(PolicyPath,[size(PolicyPath,1),N_a,N_z,T]); From 4d2ffd184501df693e617a72a45a2e5d017c27e4 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 14 Apr 2026 12:21:29 +1200 Subject: [PATCH 41/90] Revert deletion of extra space at EOL Help simplify other merges in process. --- EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m b/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m index 76ff9dcb..01ce0348 100644 --- a/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m +++ b/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m @@ -467,7 +467,7 @@ for jj=j1:jend % Includes check for cases in which no parameters are actually required FnToEvaluateParamsCell=CreateCellFromParams(Parameters,FnsToEvaluateParamNames(ff).Names,jj); - Values(:,:,jj-j1+1)=EvalFnOnAgentDist_Grid(FnsToEvaluate{ff}, FnToEvaluateParamsCell,PolicyValuesPermuteJ(:,:,:,jj),l_daprime,n_a,n_z,a_gridvals,z_gridvals_J(:,:,jj)); + Values(:,:,jj-j1+1)=EvalFnOnAgentDist_Grid(FnsToEvaluate{ff}, FnToEvaluateParamsCell,PolicyValuesPermuteJ(:,:,:,jj),l_daprime,n_a,n_z,a_gridvals,z_gridvals_J(:,:,jj)); end Values=reshape(Values,[N_a*N_z*(jend-j1+1),1]); From de88803bc479759a0116edbd842142e261265deb Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 14 Apr 2026 15:31:16 +1200 Subject: [PATCH 42/90] Fix calls to PolicyInd2Val_InfHorz_TPath when n_d1 is zero Change callers so we don't have to special case logic inside `PolicyInd2Val_InfHorz_TPath`. --- .../TransitionPath_InfHorz_PType_ExpAsset_singlepath.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m b/TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m index 0158e916..bf94c1f9 100644 --- a/TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m +++ b/TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m @@ -11,6 +11,7 @@ % For this agent type, first go back through the value & policy fns. % Then forwards through agent dist and agg vars. +N_d1=prod(n_d1); N_a1=prod(n_a1); N_a2=prod(n_a2); N_a=N_a1*N_a2; @@ -81,7 +82,11 @@ PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point end % Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats -PolicyValuesPath=PolicyInd2Val_InfHorz_TPath(PolicyIndexesPath,[n_d1,n_d2],[n_a1,n_a2],n_z,T-1,d_gridvals,a_gridvals,vfoptions,1); +if N_d1==0 + PolicyValuesPath=PolicyInd2Val_InfHorz_TPath(PolicyIndexesPath,n_d2,[n_a1,n_a2],n_z,T-1,d_gridvals,a_gridvals,vfoptions,1); +else + PolicyValuesPath=PolicyInd2Val_InfHorz_TPath(PolicyIndexesPath,[n_d1,n_d2],[n_a1,n_a2],n_z,T-1,d_gridvals,a_gridvals,vfoptions,1); +end PolicyValuesPath=permute(reshape(PolicyValuesPath,[size(PolicyValuesPath,1),N_a,N_z,T-1]),[2,3,1,4]); %[N_a,N_z,l_d+l_a,T-1] %Now we have the full PolicyIndexesPath, we go forward in time from 1 From c56b2ccca5464a5c89708a78b3d2b63ac71f9263 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 14 Apr 2026 20:32:06 +1200 Subject: [PATCH 43/90] Final reharmonization with latest VFI Toolkit merges Fix a few errant parameter transpositions. Honor l_aprime for experience assets. Ensure PolicyProbs is 2D when calling `AgentDist_InfHorz_TPath_SingleStep_nProbs` Keying off some missing special_n_z initializations, minimally support lowmemory==3 by accepting lowmemory>=1 and lowmemory=>2 where appropriate in a few of the ExpAsset cases. Let's talk about how thoroughly I should plant the lowmemory==3 easter eggs. Take advantage of `Policy` being built the way we need it in `ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw` Don't use `d_gridvals` when N_d1==0. Use `d2_grid` instead. --- .../TransPath/AgentDistOnTransPath_InfHorz.m | 4 ++-- .../AgentDistOnTransPath_Case1_FHorz_PType.m | 2 +- ...ter_FHorz_TPath_SingleStep_ExpAsset_nod1_raw.m | 2 +- ...ueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m | 7 ++++--- ...ueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m | 15 ++------------- ...r_InfHorz_TPath_SingleStep_ExpAsset_nod1_raw.m | 2 +- ...FnIter_InfHorz_TPath_SingleStep_ExpAsset_raw.m | 2 +- .../ValueFnIter_FHorz_ExpAsset_nod1_raw.m | 6 +++--- .../ValueFnOnTransPath_Case1_FHorz_PType.m | 2 +- 9 files changed, 16 insertions(+), 26 deletions(-) diff --git a/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m b/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m index a5d72752..1c34d035 100644 --- a/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m +++ b/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m @@ -173,8 +173,8 @@ Policy_aprime(:,:,2)=reshape(Policy(ndvars+1,:,:),[N_a,N_z,1])+n_a1*Policy_a2prime(:,:,1); % Note: upper grid point minus 1 is anyway just lower grid point end PolicyaprimezPath=reshape(Policy_aprime+N_a*(0:1:N_z-1),[N_a*N_z,2]); - - AgentDist=AgentDist_InfHorz_TPath_SingleStep_nProbs_raw(AgentDist,PolicyaprimezPath,II2,PolicyProbs,N_a,N_z,pi_z_sparse); + % PolicyProbs_2D=reshape(PolicyProbs,[N_a*N_z,2]); % sparse matricies are limited to 2D; match PolicyaprimezPath shape + AgentDist=AgentDist_InfHorz_TPath_SingleStep_nProbs(AgentDist,PolicyaprimezPath,II2,PolicyProbs,N_a,N_z,pi_z_sparse); AgentDistPath(:,tt+1)=gpuArray(full(AgentDist)); end diff --git a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m index 37708531..7b55dd7f 100644 --- a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m +++ b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m @@ -182,7 +182,7 @@ if isfinite(N_j_temp) AgentDistPath_ii=AgentDistOnTransPath_Case1_FHorz(AgentDist_initial_temp, jequalOneDist_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, AgeWeightsParamNames_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,pi_z_temp, T,Parameters_temp, transpathoptions_temp, simoptions_temp); else - AgentDistPath_ii=AgentDistOnTransPath_Case1(AgentDist_initial_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, n_d_temp,n_a_temp,n_z_temp,pi_z_temp,T,Parameters_temp,simoptions_temp); + AgentDistPath_ii=AgentDistOnTransPath_Case1(AgentDist_initial_temp, PolicyPath_temp, n_d_temp,n_a_temp,n_z_temp,pi_z_temp,T,simoptions_temp, Parameters_temp, PricePath_temp, ParamPath_temp); end % Note: T cannot depend on ptype, nor can PricePath depend on ptype diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_raw.m index c17175a5..5bd8a84c 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_raw.m @@ -31,7 +31,7 @@ [Vtemp,maxindex]=max(ReturnMatrix,[],1); V(:,:,N_j)=Vtemp; Policy(:,:,N_j)=maxindex; -elseif vfoptions.lowmemory==1 +elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m index 2a0ee2ed..c4700ea7 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m @@ -12,7 +12,7 @@ %% a2_gridvals=CreateGridvals(n_a2,a2_grid,1); -if vfoptions.lowmemory==1 +if vfoptions.lowmemory>=1 special_n_z=ones(1,length(n_z)); end @@ -30,7 +30,7 @@ [Vtemp,maxindex]=max(ReturnMatrix,[],1); V(:,:,N_j)=Vtemp; Policy(:,:,N_j)=maxindex; -elseif vfoptions.lowmemory==1 +elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1, n_d2, n_a1, n_a1,n_a2, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); @@ -96,7 +96,8 @@ V(:,:,jj)=shiftdim(Vtemp,1); Policy(:,:,jj)=shiftdim(maxindex,1); - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 + special_n_z=ones(1,length(n_z),'gpuArray'); for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); DiscountedEV_z=DiscountedEV(:,:,z_c); diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m index 7982d7b4..020ad262 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m @@ -1,4 +1,4 @@ -function [V,Policy3]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) +function [V,Policy]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) N_d1=prod(n_d1); N_d2=prod(n_d2); @@ -138,17 +138,6 @@ if vfoptions.gridinterplayer error("gridinterp not implemented with ExpAsset yet") end -if N_d1==0 - Policy3=zeros(2,N_a,N_z,N_j,'gpuArray'); % first dim indexes the optimal choice for d2 and aprime rest of dimensions a,z - Policy3(1,:,:,:)=1; % is meaningless anyway - Policy3(2,:,:,:)=shiftdim(rem(Policy-1,N_d2)+1,-1); - Policy3(3,:,:,:)=shiftdim(ceil(Policy/N_d2),-1); -else - Policy3=zeros(3,N_a,N_z,N_j,'gpuArray'); % first dim indexes the optimal choice for (d1,d2,aprime) rest of dimensions a,z - % second dim indexes the optimal choice for (d2,aprime) rest of dimensions a,z - Policy3(1,:,:,:)=shiftdim(rem(Policy-1,N_d1)+1,-1); % one of these is wrong - Policy3(2,:,:,:)=shiftdim(ceil(Policy/N_d1),-1); % one of these is wrong - Policy3(3,:,:,:)=shiftdim(ceil(Policy/N_d),-1); % this may be wrong -end + end diff --git a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_nod1_raw.m b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_nod1_raw.m index 1f6e1c9c..3fdec186 100644 --- a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_nod1_raw.m +++ b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_nod1_raw.m @@ -58,7 +58,7 @@ Policy(1,:,:)=rem(maxindex-1,N_d2)+1; Policy(2,:,:)=ceil(maxindex/N_d2); -elseif vfoptions.lowmemory==1 +elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals(z_c,:); diff --git a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_raw.m b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_raw.m index 27651f3d..10c51ce4 100644 --- a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_raw.m +++ b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_raw.m @@ -61,7 +61,7 @@ V=shiftdim(Vtemp,1); PolicyTemp=maxindex; -elseif vfoptions.lowmemory==1 +elseif vfoptions.lowmemory>=1 for z_c=1:N_z ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals, ReturnFnParamsVec,0,0); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m index 9fcb0625..af3c4597 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m @@ -30,7 +30,7 @@ [Vtemp,maxindex]=max(ReturnMatrix,[],1); V(:,:,N_j)=Vtemp; Policy(:,:,N_j)=maxindex; - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); @@ -79,7 +79,7 @@ V(:,:,N_j)=shiftdim(Vtemp,1); Policy(:,:,N_j)=shiftdim(maxindex,1); - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -147,7 +147,7 @@ V(:,:,jj)=shiftdim(Vtemp,1); Policy(:,:,jj)=shiftdim(maxindex,1); - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); diff --git a/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz_PType.m b/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz_PType.m index 4c02800b..48605741 100644 --- a/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz_PType.m +++ b/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz_PType.m @@ -193,7 +193,7 @@ if isfinite(N_j_temp) [VPath_ii,PolicyPath_ii]=ValueFnOnTransPath_Case1_FHorz(PricePath_temp, ParamPath_temp, T, V_final_temp, Policy_final_temp, Parameters_temp, n_d_temp, n_a_temp, n_z_temp, N_j_temp, d_grid_temp, a_grid_temp,z_grid_temp,pi_z_temp, DiscountFactorParamNames_temp, ReturnFn_temp, transpathoptions_temp, vfoptions_temp); else - [VPath_ii,PolicyPath_ii]=ValueFnOnTransPath_Case1(PricePath_temp, ParamPath_temp, T, V_final_temp, Policy_final_temp, Parameters_temp, n_d_temp, n_a_temp, n_z_temp, d_grid_temp, a_grid_temp,z_grid_temp, pi_z_temp, DiscountFactorParamNames_temp, ReturnFn_temp, transpathoptions_temp, vfoptions_temp); + [VPath_ii,PolicyPath_ii]=ValueFnOnTransPath_Case1(PricePath_temp, ParamPath_temp, T, V_final_temp, Policy_final_temp, Parameters_temp, n_d_temp, n_a_temp, n_z_temp, pi_z_temp, d_grid_temp, a_grid_temp,z_grid_temp, DiscountFactorParamNames_temp, ReturnFn_temp, transpathoptions_temp, vfoptions_temp); end % Note: T cannot depend on ptype, nor can PricePath depend on ptype From 32682a184333efb3eb315b10799202c10622d670 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 14 Apr 2026 20:38:11 +1200 Subject: [PATCH 44/90] Remove unnecessary ExpAsset files in KronPolicyIndexes Done! --- .../KronPolicyIndexes_Case1_ExpAsset_e.m | 113 ------------------ ...KronPolicyIndexes_FHorz_Case1_ExpAsset_e.m | 50 -------- 2 files changed, 163 deletions(-) delete mode 100644 SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_Case1_ExpAsset_e.m delete mode 100644 SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_FHorz_Case1_ExpAsset_e.m diff --git a/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_Case1_ExpAsset_e.m b/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_Case1_ExpAsset_e.m deleted file mode 100644 index e586404c..00000000 --- a/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_Case1_ExpAsset_e.m +++ /dev/null @@ -1,113 +0,0 @@ -function PolicyKron=KronPolicyIndexes_Case1_ExpAsset_e(Policy, n_d, n_a1, n_a2, n_z, n_e, simoptions) - -% Input: Policy (l_d+l_a,n_a,n_z,n_e); -% -% Output: PolicyKron=zeros(2,N_a,N_z,N_e); %first dim indexes the optimal choice for d and aprime rest of dimensions a,z -% (N_a,N_z,N_e) if there is no d -% Note: simoptions.gridinterplayer=1 means there will be an additional index for the second layer in both the input and output versions - -N_a1=prod(n_a1); -N_a2=prod(n_a2); -N_a=N_a1*N_a2; -N_z=prod(n_z); -N_e=prod(n_e); - -l_aprime=length(n_a1); - -if simoptions.gridinterplayer==0 - % Reshape Policy - % if n_d(1)==0 && l_a==1 - % Policy=reshape(Policy,[N_a,N_z,N_e]); - % else - Policy=reshape(Policy,[size(Policy,1),N_a,N_z,N_e]); - % end - - if n_d(1)==0 - if l_aprime==1 - PolicyKron=reshape(Policy,[N_a,N_z,N_e]); - else %l_a>1 - temp=ones(l_aprime,1,'gpuArray')-eye(l_aprime,1,'gpuArray'); - temp2=gpuArray(cumprod(n_a1')); % column vector - PolicyTemp=(reshape(Policy,[l_aprime,N_a*N_z*N_e])-temp*ones(1,N_a*N_z*N_e,'gpuArray')).*([1;temp2(1:end-1)]*ones(1,N_a*N_z*N_e,'gpuArray')); - PolicyKron=reshape(sum(PolicyTemp,1),[N_a,N_z,N_e]); - end - else - l_d=length(n_d); - - % Should test whether code runs faster with this predeclaration of PolicyKron commented or uncommented - PolicyKron=zeros(2,N_a,N_z,N_e,'gpuArray'); - - if l_d==1 - PolicyKron(1,:,:,:)=Policy(1,:,:,:); - else - temp=ones(l_d,1,'gpuArray')-eye(l_d,1,'gpuArray'); - temp2=gpuArray(cumprod(n_d')); % column vector - PolicyTemp=(reshape(Policy(1:l_d,:,:,:),[l_d,N_a*N_z*N_e])-temp*ones(1,N_a*N_z*N_e,'gpuArray')).*([1;temp2(1:end-1)]*ones(1,N_a*N_z*N_e,'gpuArray')); - PolicyKron(1,:,:,:)=reshape(sum(PolicyTemp,1),[1,N_a,N_z,N_e]); - end - % Then, a - if l_aprime==1 - PolicyKron(2,:,:,:)=Policy(l_d+1,:,:,:); - else - temp=ones(l_aprime,1,'gpuArray')-eye(l_aprime,1,'gpuArray'); - temp2=gpuArray(cumprod(n_a1')); % column vector - PolicyTemp=(reshape(Policy(l_d+1:l_d+l_aprime,:,:,:),[l_aprime,N_a*N_z*N_e])-temp*ones(1,N_a*N_z*N_e,'gpuArray')).*([1;temp2(1:end-1)]*ones(1,N_a*N_z*N_e,'gpuArray')); - PolicyKron(2,:,:,:)=reshape(sum(PolicyTemp,1),[1,N_a,N_z,N_e]); - end - end - -elseif simoptions.gridinterplayer==1 - % Reshape Policy - Policy=reshape(Policy,[size(Policy,1),N_a,N_z,N_e]); - - if n_d(1)==0 - if l_aprime==1 || l_aprime==2 - PolicyKron=Policy; % a1, possibly a2, L2 - else %l_a>2 - PolicyKron=zeros(3,N_a,N_z,N_e,'gpuArray'); - PolicyKron(1,:,:,:)=Policy(1,:,:); % a1 - temp=[0; ones(l_aprime-2,1,'gpuArray')]; - temp2=gpuArray(cumprod(n_a1(2:end)')); % column vector - PolicyTemp=(reshape(Policy(2:l_aprime,:,:,:),[l_aprime-1,N_a*N_z*N_e])-temp).*[1;temp2(1:end-1)]; - PolicyKron(2,:,:,:)=reshape(sum(PolicyTemp,1),[1,N_a,N_z,N_e]); - PolicyKron(3,:,:,:)=Policy(l_aprime+1,:,:,:); % L2 index - end - else - l_d=length(n_d); - if l_aprime==1 - PolicyKron=zeros(3,N_a,N_z,N_e,'gpuArray'); - else - PolicyKron=zeros(4,N_a,N_z,N_e,'gpuArray'); - end - - if l_d==1 - PolicyKron(1,:,:,:)=Policy(1,:,:,:); - else - temp=[0; ones(l_d-1,1,'gpuArray')]; - temp2=gpuArray(cumprod(n_d')); % column vector - PolicyTemp=(reshape(Policy(1:l_d,:,:,:),[l_d,N_a*N_z*N_e])-temp).*[1;temp2(1:end-1)]; - PolicyKron(1,:,:,:)=reshape(sum(PolicyTemp,1),[1,N_a,N_z,N_e]); - end - % Then, a - if l_aprime==1 - PolicyKron(2,:,:,:)=Policy(l_d+1,:,:,:); - PolicyKron(3,:,:,:)=Policy(l_aprime+l_d+1,:,:,:); % L2 index - elseif l_aprime==2 - PolicyKron(2,:,:,:)=Policy(l_d+1,:,:,:); - PolicyKron(3,:,:,:)=Policy(l_d+2,:,:,:); - PolicyKron(4,:,:,:)=Policy(l_aprime+l_d+1,:,:,:); % L2 index - else - PolicyKron(2,:,:,:)=Policy(l_d+1,:,:,:); - temp=[0; ones(l_aprime-2,1,'gpuArray')]; - temp2=gpuArray(cumprod(n_a1(2:end)')); % column vector - PolicyTemp=(reshape(Policy(l_d+2:l_d+l_aprime,:,:,:),[l_aprime-1,N_a*N_z*N_e])-temp).*[1;temp2(1:end-1)]; - PolicyKron(3,:,:,:)=reshape(sum(PolicyTemp,1),[1,N_a,N_z,N_e]); - PolicyKron(4,:,:,:)=Policy(l_aprime+l_d+1,:,:,:); % L2 index - end - - end - -end - - -end \ No newline at end of file diff --git a/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_FHorz_Case1_ExpAsset_e.m b/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_FHorz_Case1_ExpAsset_e.m deleted file mode 100644 index 96f3c3c0..00000000 --- a/SubCodes/KronPolicyIndexes/ExpAsset/KronPolicyIndexes_FHorz_Case1_ExpAsset_e.m +++ /dev/null @@ -1,50 +0,0 @@ -function PolicyKron=KronPolicyIndexes_FHorz_Case1_ExpAsset_e(Policy, n_d, n_a1, n_a2, n_z, n_e, N_j, simoptions) -% Input: Policy (l_d+l_aprime,n_a,n_z,n_e,N_j); -% -% Output: Policy=zeros(2,N_a,N_z,N_e,N_j); %first dim indexes the optimal choice for d and aprime rest of dimensions a,z -% (N_a,N_z,N_e,N_j) if there is no d - -N_a1=prod(n_a1); -N_a2=prod(n_a2); -N_a=N_a1*N_a2; -N_z=prod(n_z); - -% When using n_e, is instead: -% Input: Policy (l_d+l_aprime,n_a,n_z,n_e,N_j); -% -% Output: Policy=zeros(2,N_a,N_z,N_e,N_j); %first dim indexes the optimal choice for d and aprime rest of dimensions a,z -% (N_a,N_z,N_e,N_j) if there is no d - - -%% -N_e=prod(n_e); -Policy=reshape(Policy,[size(Policy,1),N_a,N_z,N_e,N_j]); - -% gpu only -if simoptions.gridinterplayer==0 - if n_d(1)==0 - PolicyKron=zeros(N_a,N_z,N_e,N_j,'gpuArray'); - for jj=1:N_j - PolicyKron(:,:,:,jj)=KronPolicyIndexes_Case1_ExpAsset_e(Policy(:,:,:,:,jj), n_d, n_a1, n_a2, n_z, n_e, simoptions); - end - else - PolicyKron=zeros(2,N_a,N_z,N_e,N_j,'gpuArray'); - for jj=1:N_j - PolicyKron(:,:,:,:,jj)=KronPolicyIndexes_Case1_ExpAsset_e(Policy(:,:,:,:,jj), n_d, n_a1, n_a2, n_z, n_e, simoptions); - end - end -elseif simoptions.gridinterplayer==1 - if n_d(1)==0 - PolicyKron=zeros(2,N_a,N_z,N_e,N_j,'gpuArray'); - for jj=1:N_j - PolicyKron(:,:,:,:,jj)=KronPolicyIndexes_Case1_ExpAsset_e(Policy(:,:,:,:,jj), n_d, n_a1, n_a2, n_z, n_e, simoptions); - end - else - PolicyKron=zeros(3,N_a,N_z,N_e,N_j,'gpuArray'); - for jj=1:N_j - PolicyKron(:,:,:,:,jj)=KronPolicyIndexes_Case1_ExpAsset_e(Policy(:,:,:,:,jj), n_d, n_a1, n_a2, n_z, n_e, simoptions); - end - end -end - -end \ No newline at end of file From 686ff5dbe5f424b5a42ce7d2ec190deae21748d2 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 15 Apr 2026 15:29:22 +1200 Subject: [PATCH 45/90] Tidy and regularize ExpAsset raw handlers Handle vfoptions.lowmemory=3 consistantly. Push down calculation of DiscountedEV in most places so that we don't accidentally construct too-large EV before sliding down to size with e, z, and ea loops. --- ...er_FHorz_TPath_SingleStep_ExpAsset_e_raw.m | 58 ++++- ...orz_TPath_SingleStep_ExpAsset_nod1_e_raw.m | 55 ++++- ...FHorz_TPath_SingleStep_ExpAsset_nod1_raw.m | 48 ++++- ...Iter_FHorz_TPath_SingleStep_ExpAsset_raw.m | 51 ++++- ...rz_TPath_SingleStep_fastOLG_DC1_GI_e_raw.m | 38 ++-- ...er_FHorz_TPath_SingleStep_ExpAsset_e_raw.m | 201 ------------------ ...Iter_FHorz_TPath_SingleStep_ExpAsset_raw.m | 143 ------------- ...ValueFnIter_FHorz_TPath_SingleStep_e_raw.m | 4 +- ...z_TPath_SingleStep_ExpAsset_DC1_nod1_raw.m | 6 +- ...nfHorz_TPath_SingleStep_ExpAsset_DC1_raw.m | 8 +- ...fHorz_TPath_SingleStep_ExpAsset_nod1_raw.m | 32 ++- ...er_InfHorz_TPath_SingleStep_ExpAsset_raw.m | 38 +++- .../ValueFnIter_FHorz_ExpAsset_e_raw.m | 41 ++-- .../ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m | 16 +- .../ValueFnIter_FHorz_ExpAsset_noa1_raw.m | 6 +- .../ValueFnIter_FHorz_ExpAsset_nod1_e_raw.m | 79 ++++++- ...lueFnIter_FHorz_ExpAsset_nod1_noa1_e_raw.m | 18 +- ...ValueFnIter_FHorz_ExpAsset_nod1_noa1_raw.m | 6 +- .../ValueFnIter_FHorz_ExpAsset_nod1_raw.m | 69 +++++- .../ValueFnIter_FHorz_ExpAsset_raw.m | 17 +- .../ValueFnIter_FHorz_ExpAssetu_e_raw.m | 87 ++++++-- .../ValueFnIter_FHorz_ExpAssetu_noa1_e_raw.m | 20 +- .../ValueFnIter_FHorz_ExpAssetu_noa1_raw.m | 24 +-- .../ValueFnIter_FHorz_ExpAssetu_nod1_e_raw.m | 86 +++++++- ...ueFnIter_FHorz_ExpAssetu_nod1_noa1_e_raw.m | 12 +- ...alueFnIter_FHorz_ExpAssetu_nod1_noa1_raw.m | 6 +- .../ValueFnIter_FHorz_ExpAssetu_nod1_raw.m | 64 +++++- .../ValueFnIter_FHorz_ExpAssetu_raw.m | 66 +++++- 28 files changed, 760 insertions(+), 539 deletions(-) delete mode 100644 TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m delete mode 100644 TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m index 2b928daa..9103c8c0 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m @@ -13,11 +13,14 @@ %% a2_gridvals=CreateGridvals(n_a2,a2_grid,1); -if vfoptions.lowmemory==1 - special_n_e=ones(1,length(n_e)); -elseif vfoptions.lowmemory==2 - special_n_e=ones(1,length(n_e)); - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory>=1 + special_n_e=ones(1,length(n_e),'gpuArray'); +end +if vfoptions.lowmemory>=2 + special_n_z=ones(1,length(n_z),'gpuArray'); +end +if vfoptions.lowmemory>=3 + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -55,6 +58,21 @@ Policy(:,z_c,e_c,N_j)=maxindex; end end +elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1, special_n_ea, special_n_z, special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + % Calc the max and its index + [Vtemp,maxindex]=max(ReturnMatrix_ea_ze); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=maxindex; + end + end + end end @@ -99,13 +117,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); % should autofill e dimension % Calc the max and its index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -115,6 +131,7 @@ elseif vfoptions.lowmemory==1 + DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec,0,0); @@ -136,15 +153,36 @@ ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix_ze+DiscountedEV_z; + entireRHS_ze=ReturnMatrix_ze+DiscountedEV_z; %Calc the max and its index - [Vtemp,maxindex]=max(entireRHS,[],1); + [Vtemp,maxindex]=max(entireRHS_ze,[],1); V(:,z_c,e_c,jj)=shiftdim(Vtemp,1); Policy(:,z_c,e_c,jj)=shiftdim(maxindex,1); end end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,jj); + + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; + + %Calc the max and its index + [Vtemp,maxindex]=max(entireRHS_ea_ze,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(maxindex,1); + end + end + end end end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_e_raw.m index b6d70c58..f2d068ac 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_e_raw.m @@ -13,10 +13,13 @@ a2_gridvals=CreateGridvals(n_a2,a2_grid,1); if vfoptions.lowmemory>=1 - special_n_e=ones(1,length(n_e)); + special_n_e=ones(1,length(n_e),'gpuArray'); end -if vfoptions.lowmemory==2 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory>=2 + special_n_z=ones(1,length(n_z),'gpuArray'); +end +if vfoptions.lowmemory==3 + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -60,6 +63,23 @@ end end + +elseif vfoptions.lowmemory==3 + + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0, n_d2,n_a1,n_a1, special_n_ea, special_n_z, special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + % Calc the max and its index + [Vtemp,maxindex]=max(ReturnMatrix_ea_ze); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=maxindex; + end + end + end end %% Iterate backwards through j. @@ -103,12 +123,10 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); % should autofill e dimension %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -117,7 +135,7 @@ Policy(:,:,:,jj)=shiftdim(maxindex,1); elseif vfoptions.lowmemory==1 - + DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec,0,0); @@ -133,7 +151,7 @@ elseif vfoptions.lowmemory==2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); @@ -147,6 +165,27 @@ Policy(:,z_c,e_c,jj)=shiftdim(maxindex,1); end end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,jj); + + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; + + %Calc the max and its index + [Vtemp,maxindex]=max(entireRHS_ea_ze,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(maxindex,1); + end + end + end end end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_raw.m index 5bd8a84c..1b82ef29 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_nod1_raw.m @@ -13,8 +13,13 @@ % a1prime_gridvals=a1_gridvals; a2_gridvals=CreateGridvals(n_a2,a2_grid,1); -if vfoptions.lowmemory>0 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory==1 + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + special_n_z=ones(1,length(n_z),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -31,7 +36,7 @@ [Vtemp,maxindex]=max(ReturnMatrix,[],1); V(:,:,N_j)=Vtemp; Policy(:,:,N_j)=maxindex; -elseif vfoptions.lowmemory>=1 +elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); @@ -40,6 +45,18 @@ V(:,z_c,N_j)=Vtemp; Policy(:,z_c,N_j)=maxindex; end +elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,special_n_ea, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + %Calc the max and it's index + [Vtemp,maxindex]=max(ReturnMatrix_ea_z,[],1); + V(:,z_c,N_j)=Vtemp; + Policy(:,z_c,N_j)=maxindex; + end + end end %% Iterate backwards through j. @@ -80,12 +97,10 @@ EV(isnan(EV))=0; %multiplications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) EV=squeeze(sum(EV,3)); % sum over z', leaving a singular third dimension - DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -97,7 +112,7 @@ for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),1,N_a1,1); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); @@ -108,6 +123,25 @@ V(:,z_c,jj)=Vtemp; Policy(:,z_c,jj)=maxindex; end + + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); + + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0,n_d2,n_a1,n_a1,special_n_ea,special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountedEV_ea_z; + + % Calc the max and its index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=maxindex; + end + end end end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m index c4700ea7..ff6252f7 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m @@ -12,8 +12,13 @@ %% a2_gridvals=CreateGridvals(n_a2,a2_grid,1); -if vfoptions.lowmemory>=1 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory==1 + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + special_n_z=ones(1,length(n_z),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -30,7 +35,7 @@ [Vtemp,maxindex]=max(ReturnMatrix,[],1); V(:,:,N_j)=Vtemp; Policy(:,:,N_j)=maxindex; -elseif vfoptions.lowmemory>=1 +elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1, n_d2, n_a1, n_a1,n_a2, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); @@ -39,6 +44,18 @@ V(:,z_c,N_j)=Vtemp; Policy(:,z_c,N_j)=maxindex; end +elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + % Calc the max and its index + [Vtemp,maxindex]=max(ReturnMatrix_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=shiftdim(maxindex,1); + end + end end @@ -82,9 +99,8 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); - if vfoptions.lowmemory==0 + DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2,n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals,z_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); % (d,aprime,a,z) @@ -96,12 +112,10 @@ V(:,:,jj)=shiftdim(Vtemp,1); Policy(:,:,jj)=shiftdim(maxindex,1); - elseif vfoptions.lowmemory>=1 - special_n_z=ones(1,length(n_z),'gpuArray'); + elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); - DiscountedEV_z=DiscountedEV(:,:,z_c); - + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); entireRHS_z=ReturnMatrix_z+DiscountedEV_z; @@ -111,6 +125,25 @@ V(:,z_c,jj)=Vtemp; Policy(:,z_c,jj)=maxindex; end + + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); + + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountedEV_ea_z; + + % Calc the max and its index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=shiftdim(maxindex,1); + end + end end end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/fastOLG/DivideConquer/GridInterpLayer/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_DC1_GI_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/fastOLG/DivideConquer/GridInterpLayer/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_DC1_GI_e_raw.m index ca32a88c..a5587322 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/fastOLG/DivideConquer/GridInterpLayer/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_DC1_GI_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/fastOLG/DivideConquer/GridInterpLayer/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_DC1_GI_e_raw.m @@ -21,7 +21,8 @@ midpoints_jj=zeros(N_d,1,N_a,N_j,N_z,N_e,'gpuArray'); elseif vfoptions.lowmemory==1 midpoints_jj=zeros(N_d,1,N_a,N_j,N_z,'gpuArray'); -elseif vfoptions.lowmemory==2 +elseif vfoptions.lowmemory>=2 + % We don't special-case ExpAsset looping in the fastOLG/DC1 case midpoints_jj=zeros(N_d,1,N_a,N_j,'gpuArray'); end @@ -69,19 +70,13 @@ EV=reshape(sum(EV,4),[N_a,1,N_j,N_z]); % (aprime,1,j,z), 2nd dim will be autofilled with a end -% Interpolate EV over aprime_grid -EVinterp=interp1(a_grid,EV,aprime_grid); - -DiscountedEV=DiscountFactorParamsVec.*EV; -DiscountedEV=repelem(shiftdim(DiscountedEV,-1),N_d,1,1,1); % [d,aprime,1,j,z] - -DiscountedEVinterp=DiscountFactorParamsVec.*EVinterp; % [n2aprime fine,1,j,z] - if vfoptions.lowmemory==0 % n-Monotonicity ReturnMatrix_ii=CreateReturnFnMatrix_Case1_Disc_fastOLG_DC1_Par2e(ReturnFn, n_d, n_z,n_e,N_j, d_gridvals, a_grid, a_grid(level1ii), z_gridvals_J,e_gridvals_J, ReturnFnParamsAgeMatrix,1); + DiscountedEV=DiscountFactorParamsVec.*EV; + DiscountedEV=repelem(shiftdim(DiscountedEV,-1),N_d,1,1,1); % [d,aprime,1,j,z] entireRHS_ii=ReturnMatrix_ii+DiscountedEV; % (d,aprime,a,j,z,e), autofills a for expectation term % First, we want aprime conditional on (d,1,a,j,e) @@ -110,6 +105,10 @@ end end + % Interpolate EV over aprime_grid + EVinterp=interp1(a_grid,EV,aprime_grid); + DiscountedEVinterp=DiscountFactorParamsVec.*EVinterp; % [n2aprime fine,1,j,z] + % Turn this into the 'midpoint' midpoints_jj=max(min(midpoints_jj,n_a-1),2); % avoid the top end (inner), and avoid the bottom end (outer) % midpoint is n_d-by-1-by-n_a-by-N_j-by-n_z-by-n_e @@ -128,9 +127,16 @@ elseif vfoptions.lowmemory==1 - special_n_e=ones(1,length(n_e)); + special_n_e=ones(1,length(n_e),'gpuArray'); V=zeros(N_a*N_j,N_z,N_e,'gpuArray'); % V is over (a,j,z,e) + DiscountedEV=DiscountFactorParamsVec.*EV; + DiscountedEV=repelem(shiftdim(DiscountedEV,-1),N_d,1,1,1); % [d,aprime,1,j,z] + + % Interpolate EV over aprime_grid + EVinterp=interp1(a_grid,EV,aprime_grid); + DiscountedEVinterp=DiscountFactorParamsVec.*EVinterp; % [n2aprime fine,1,j,z] + for e_c=1:N_e e_vals=e_gridvals_J(1,1,1,:,1,e_c,:); % e_gridvals_J has shape (1,1,1,N_j,1,N_z,l_z) for fastOLG with d @@ -184,14 +190,18 @@ elseif vfoptions.lowmemory==2 - special_n_e=ones(1,length(n_e)); - special_n_z=ones(1,length(n_z)); + special_n_e=ones(1,length(n_e),'gpuArray'); + special_n_z=ones(1,length(n_z),'gpuArray'); V=zeros(N_a*N_j,N_z,N_e,'gpuArray'); % V is over (a,j,z,e) for z_c=1:N_z z_vals=z_gridvals_J(1,1,1,:,z_c,:); % z_gridvals_J has shape (1,1,1,N_j,N_z,l_z) for fastOLG - DiscountedEV_z=DiscountedEV(:,:,:,:,z_c); - DiscountedEVinterp_z=DiscountedEVinterp(:,:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec.*EV(:,:,:,:,z_c); + DiscountedEV_z=repelem(shiftdim(DiscountedEV_z,-1),N_d,1,1,1); % [d,aprime,1,j,z] + + % Interpolate EV over aprime_grid + EVinterp_z=interp1(a_grid,EV(:,:,:,:,z_c),aprime_grid); + DiscountedEVinterp_z=DiscountFactorParamsVec.*EVinterp_z; % [n2aprime fine,1,j,z] for e_c=1:N_e e_vals=e_gridvals_J(1,1,1,:,1,e_c,:); % e_gridvals_J has shape (1,1,1,N_j,1,N_z,l_z) for fastOLG with d diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m deleted file mode 100644 index 8d731417..00000000 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m +++ /dev/null @@ -1,201 +0,0 @@ -function [V,Policy3]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) - -N_d1=prod(n_d1); -N_d2=prod(n_d2); -N_d=N_d1*N_d2; -N_a1=prod(n_a1); -N_a2=prod(n_a2); -N_a=N_a1*N_a2; -N_z=prod(n_z); -N_e=prod(n_e); - -V=zeros(N_a,N_z,N_e,N_j,'gpuArray'); -Policy=zeros(N_a,N_z,N_e,N_j,'gpuArray'); %first dim indexes the optimal choice for d and a1prime rest of dimensions a,z - -%% -a2_gridvals=CreateGridvals(n_a2,a2_grid,1); - -if vfoptions.lowmemory==1 - special_n_e=ones(1,length(n_e)); -elseif vfoptions.lowmemory==2 - special_n_e=ones(1,length(n_e)); - special_n_z=ones(1,length(n_z)); -elseif vfoptions.lowmemory==3 - special_n_e=ones(1,length(n_e)); - special_n_z=ones(1,length(n_z)); - special_n_ea=ones(1,length(n_a2)); -end - -%% j=N_j -% Create a vector containing all the return function parameters (in order) -ReturnFnParamsVec=CreateVectorFromParams(Parameters, ReturnFnParamNames,N_j); - -if vfoptions.lowmemory==0 - ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); - %Calc the max and its index - [Vtemp,maxindex]=max(ReturnMatrix,[],1); - V(:,:,:,N_j)=Vtemp; - Policy(:,:,:,N_j)=maxindex; -elseif vfoptions.lowmemory==1 - for e_c=1:N_e - e_val=e_gridvals_J(e_c,:,N_j); - ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_val, ReturnFnParamsVec,0,0); - %Calc the max and its index - [Vtemp,maxindex]=max(ReturnMatrix_e,[],1); - V(:,:,e_c,N_j)=Vtemp; - Policy(:,:,e_c,N_j)=maxindex; - end -elseif vfoptions.lowmemory==2 - for z_c=1:N_z - z_val=z_gridvals_J(z_c,:,N_j); - for e_c=1:N_e - e_val=e_gridvals_J(e_c,:,N_j); - ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2, special_n_z, special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); - % Calc the max and its index - [Vtemp,maxindex]=max(ReturnMatrix_ze); - V(:,z_c,e_c,N_j)=Vtemp; - Policy(:,z_c,e_c,N_j)=maxindex; - end - end -elseif vfoptions.lowmemory==3 - for ea_c=1:N_a2 - ea_val=a2_gridvals(ea_c); - for z_c=1:N_z - z_val=z_gridvals_J(z_c,:,N_j); - for e_c=1:N_e - e_val=e_gridvals_J(e_c,:,N_j); - ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1, special_n_ea, special_n_z, special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); - % Calc the max and its index - [Vtemp,maxindex]=max(ReturnMatrix_ze); - V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=Vtemp; - Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=maxindex; - end - end - end -end - -%% Iterate backwards through j. -for reverse_j=1:N_j-1 - jj=N_j-reverse_j; - - if vfoptions.verbose==1 - fprintf('Finite horizon: %i of %i \n',jj, N_j) - end - - % Create a vector containing all the return function parameters (in order) - ReturnFnParamsVec=CreateVectorFromParams(Parameters, ReturnFnParamNames,jj); - DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,jj); - DiscountFactorParamsVec=prod(DiscountFactorParamsVec); - - aprimeFnParamsVec=CreateVectorFromParams(Parameters, aprimeFnParamNames,jj); - [a2primeIndex,a2primeProbs]=CreateExperienceAssetFnMatrix_Case1(aprimeFn, n_d2, n_a2, d2_grid, a2_grid, aprimeFnParamsVec,2); % Note, is actually aprime_grid (but a_grid is anyway same for all ages) - % Note: aprimeIndex is [N_d2,N_a2], whereas aprimeProbs is [N_d2,N_a2] - - aprimeIndex=repelem((1:1:N_a1)',N_d2,N_a2)+N_a1*repmat(a2primeIndex-1,N_a1,1,1); % [N_d2*N_a1,N_a2] - aprimeplus1Index=repelem((1:1:N_a1)',N_d2,N_a2)+N_a1*repmat(a2primeIndex,N_a1,1,1); % [N_d2*N_a1,N_a2] - aprimeProbs=repmat(a2primeProbs,N_a1,1,N_z); % [N_d2*N_a1,N_a2,N_z] - - EVpre=sum(shiftdim(pi_e_J(:,N_j),-2).*V(:,:,:,jj+1),3); % First, switch V_Jplus1 into Kron form - - Vlower=reshape(EVpre(aprimeIndex(:),:),[N_d2*N_a1,N_a2,N_z]); - Vupper=reshape(EVpre(aprimeplus1Index(:),:),[N_d2*N_a1,N_a2,N_z]); - % Skip interpolation when upper and lower are equal (otherwise can cause numerical rounding errors) - skipinterp=(Vlower==Vupper); - aprimeProbs(skipinterp)=0; % effectively skips interpolation - - % Switch EV from being in terps of a2prime to being in terms of d2 and a2 - EV=aprimeProbs.*Vlower+(1-aprimeProbs).*Vupper; % (d2,a1prime,a2,u,zprime) - % Already applied the probabilities from interpolating onto grid - - EV=EV.*shiftdim(pi_z_J(:,:,jj)',-2); - EV(isnan(EV))=0; % remove nan created where value fn is -Inf but probability is zero - EV=squeeze(sum(EV,3)); - % EV is over (d2,a1prime,a2,z) - - if vfoptions.lowmemory==0 - - ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - - entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); % should autofill e dimension - - % Calc the max and its index - [Vtemp,maxindex]=max(entireRHS,[],1); - - V(:,:,:,jj)=shiftdim(Vtemp,1); - Policy(:,:,:,jj)=shiftdim(maxindex,1); - - elseif vfoptions.lowmemory==1 - - for e_c=1:N_e - e_val=e_gridvals_J(e_c,:,jj); - ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec,0,0); - - entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); % should autofill e dimension - - % Calc the max and its index - [Vtemp,maxindex]=max(entireRHS,[],1); - - V(:,:,e_c,jj)=shiftdim(Vtemp,1); - Policy(:,:,e_c,jj)=shiftdim(maxindex,1); - end - - elseif vfoptions.lowmemory==2 - for z_c=1:N_z - z_val=z_gridvals_J(z_c,:,jj); - DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1); - for e_c=1:N_e - e_val=e_gridvals_J(e_c,:,jj); - - ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); - - entireRHS=ReturnMatrix_ze+DiscountedEV_z; - - %Calc the max and its index - [Vtemp,maxindex]=max(entireRHS,[],1); - - V(:,z_c,e_c,jj)=shiftdim(Vtemp,1); - Policy(:,z_c,e_c,jj)=shiftdim(maxindex,1); - end - end - elseif vfoptions.lowmemory==3 - for ea_c=1:N_a2 - ea_val=a2_gridvals(ea_c); - for z_c=1:N_z - z_val=z_gridvals_J(z_c,:,jj); - DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); - for e_c=1:N_e - e_val=e_gridvals_J(e_c,:,jj); - - ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); - - entireRHS=ReturnMatrix_ze+DiscountedEV_z; - - %Calc the max and its index - [Vtemp,maxindex]=max(entireRHS,[],1); - - V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(Vtemp,1); - Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(maxindex,1); - end - end - end - end -end - -%% Separate d and aprime -if vfoptions.gridinterplayer - error("gridinterp not implemented with ExpAsset yet") -end -if N_d1==0 - Policy3=zeros(3,N_a,N_z,N_e,N_j,'gpuArray'); % first dim indexes the optimal choice for d2 and aprime rest of dimensions a,z - Policy3(1,:,:,:,:)=1; % is meaningless anyway - Policy3(2,:,:,:,:)=shiftdim(rem(Policy-1,N_d2)+1,-1); - Policy3(3,:,:,:,:)=shiftdim(ceil(Policy/N_d2),-1); -else - Policy3=zeros(3,N_a,N_z,N_e,N_j,'gpuArray'); % first dim indexes the optimal choice for (d1,d2,aprime) rest of dimensions a,z - % second dim indexes the optimal choice for (d2,aprime) rest of dimensions a,z - Policy3(1,:,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); % one of these is wrong - Policy3(2,:,:,:,:)=shiftdim(rem(Policy-1,N_d)+1,-1); % one of these is wrong - Policy3(3,:,:,:,:)=shiftdim(ceil(Policy/N_d),-1); % this may be wrong -end - -end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m deleted file mode 100644 index 020ad262..00000000 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m +++ /dev/null @@ -1,143 +0,0 @@ -function [V,Policy]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) - -N_d1=prod(n_d1); -N_d2=prod(n_d2); -N_d=N_d1*N_d2; -N_a1=prod(n_a1); -N_a2=prod(n_a2); -N_a=N_a1*N_a2; -N_z=prod(n_z); - -V=zeros(N_a,N_z,N_j,'gpuArray'); -Policy=zeros(N_a,N_z,N_j,'gpuArray'); %first dim indexes the optimal choice for d and a1prime rest of dimensions a,z - -%% -a2_gridvals=CreateGridvals(n_a2,a2_grid,1); - -if vfoptions.lowmemory==1 - special_n_z=ones(1,length(n_z)); -elseif vfoptions.lowmemory==3 - special_n_z=ones(1,length(n_z)); - special_n_ea=ones(1,length(n_a2)); -end - -%% j=N_j -% Create a vector containing all the return function parameters (in order) -ReturnFnParamsVec=CreateVectorFromParams(Parameters, ReturnFnParamNames,N_j); - -if vfoptions.lowmemory==0 - ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); - %Calc the max and its index - [Vtemp,maxindex]=max(ReturnMatrix,[],1); - V(:,:,N_j)=Vtemp; - Policy(:,:,N_j)=maxindex; -elseif vfoptions.lowmemory==1 - for z_c=1:N_z - z_val=z_gridvals_J(z_c,:,N_j); - ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); - % Calc the max and its index - [Vtemp,maxindex]=max(ReturnMatrix_z); - V(:,z_c,N_j)=Vtemp; - Policy(:,z_c,N_j)=maxindex; - end -elseif vfoptions.lowmemory==3 - for ea_c=1:N_a2 - ea_val=a2_gridvals(ea_c); - for z_c=1:N_z - z_val=z_gridvals_J(z_c,:,N_j); - ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1, special_n_ea, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); - % Calc the max and its index - [Vtemp,maxindex]=max(ReturnMatrix_z); - V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=Vtemp; - Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=maxindex; - end - end -end - -%% Iterate backwards through j. -for reverse_j=1:N_j-1 - jj=N_j-reverse_j; - - if vfoptions.verbose==1 - fprintf('Finite horizon: %i of %i \n',jj, N_j) - end - - % Create a vector containing all the return function parameters (in order) - ReturnFnParamsVec=CreateVectorFromParams(Parameters, ReturnFnParamNames,jj); - DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,jj); - DiscountFactorParamsVec=prod(DiscountFactorParamsVec); - - aprimeFnParamsVec=CreateVectorFromParams(Parameters, aprimeFnParamNames,jj); - [a2primeIndex,a2primeProbs]=CreateExperienceAssetFnMatrix_Case1(aprimeFn, n_d2, n_a2, d2_grid, a2_grid, aprimeFnParamsVec,2); % Note, is actually aprime_grid (but a_grid is anyway same for all ages) - % Note: aprimeIndex is [N_d2,N_a2], whereas aprimeProbs is [N_d2,N_a2] - - aprimeIndex=repelem((1:1:N_a1)',N_d2,N_a2)+N_a1*repmat(a2primeIndex-1,N_a1,1,1); % [N_d2*N_a1,N_a2] - aprimeplus1Index=repelem((1:1:N_a1)',N_d2,N_a2)+N_a1*repmat(a2primeIndex,N_a1,1,1); % [N_d2*N_a1,N_a2] - aprimeProbs=repmat(a2primeProbs,N_a1,1,N_z); % [N_d2*N_a1,N_a2,N_z] - - Vlower=reshape(V(aprimeIndex(:),:,jj+1),[N_d2*N_a1,N_a2,N_z]); - Vupper=reshape(V(aprimeplus1Index(:),:,jj+1),[N_d2*N_a1,N_a2,N_z]); - % Skip interpolation when upper and lower are equal (otherwise can cause numerical rounding errors) - skipinterp=(Vlower==Vupper); - aprimeProbs(skipinterp)=0; % effectively skips interpolation - - % Switch EV from being in terps of a2prime to being in terms of d2 and a2 - EV=aprimeProbs.*Vlower+(1-aprimeProbs).*Vupper; % (d2,a1prime,a2,u,zprime) - % Already applied the probabilities from interpolating onto grid - - EV=EV.*shiftdim(pi_z_J(:,:,N_j)',-2); - EV(isnan(EV))=0; % remove nan created where value fn is -Inf but probability is zero - EV=squeeze(sum(EV,3)); - % EV is over (d2,a1prime,a2,z) - - if vfoptions.lowmemory==0 - - ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - - entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); - - % Calc the max and its index - [Vtemp,maxindex]=max(entireRHS,[],1); - - V(:,:,jj)=shiftdim(Vtemp,1); - Policy(:,:,jj)=shiftdim(maxindex,1); - - elseif vfoptions.lowmemory==1 - for z_c=1:N_z - z_val=z_gridvals_J(z_c,:,jj); - ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); - - entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1); - - %Calc the max and its index - [Vtemp,maxindex]=max(entireRHS_z,[],1); - - V(:,z_c,jj)=shiftdim(Vtemp,1); - Policy(:,z_c,jj)=shiftdim(maxindex,1); - end - elseif vfoptions.lowmemory==3 - for ea_c=1:N_a2 - ea_val=a2_gridvals(ea_c); - for z_c=1:N_z - z_val=z_gridvals_J(z_c,:,jj); - ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); - - entireRHS_ea_z=ReturnMatrix_ea_z+DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); - - %Calc the max and its index - [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); - - V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=shiftdim(Vtemp,1); - Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=shiftdim(maxindex,1); - end - end - end -end - -%% Separate d and aprime -if vfoptions.gridinterplayer - error("gridinterp not implemented with ExpAsset yet") -end - - -end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_e_raw.m index e928ed08..e39cf7e4 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_e_raw.m @@ -11,10 +11,10 @@ %% if vfoptions.lowmemory>0 - special_n_e=ones(1,length(n_e)); + special_n_e=ones(1,length(n_e),'gpuArray'); end if vfoptions.lowmemory>1 - special_n_z=ones(1,length(n_z)); + special_n_z=ones(1,length(n_z),'gpuArray'); end %% j=N_j diff --git a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/DivideAndConquer/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_DC1_nod1_raw.m b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/DivideAndConquer/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_DC1_nod1_raw.m index 22a316d3..a2f3836b 100644 --- a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/DivideAndConquer/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_DC1_nod1_raw.m +++ b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/DivideAndConquer/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_DC1_nod1_raw.m @@ -16,9 +16,9 @@ % a1prime_gridvals=a1_gridvals; if vfoptions.lowmemory>0 - special_n_z=ones(1,length(n_z)); + special_n_z=ones(1,length(n_z),'gpuArray'); else - % precompute + % precompute; we don't special-case ExpAsset looping in DC1 case zind=shiftdim((0:1:N_z-1),-1); % already includes -1 end @@ -119,7 +119,7 @@ end end -elseif vfoptions.lowmemory==1 +elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals(z_c,:); diff --git a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/DivideAndConquer/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_DC1_raw.m b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/DivideAndConquer/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_DC1_raw.m index 7a2b3ed6..3f8469dc 100644 --- a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/DivideAndConquer/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_DC1_raw.m +++ b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/DivideAndConquer/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_DC1_raw.m @@ -15,10 +15,10 @@ a2_gridvals=CreateGridvals(n_a2,a2_grid,1); n_a1prime=n_a1; -if vfoptions.lowmemory==1 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory>=1 + special_n_z=ones(1,length(n_z),'gpuArray'); else - % precompute + % precompute; we don't special-case ExpAsset looping in DC1 case zind=shiftdim((0:1:N_z-1),-1); % already includes -1 end @@ -114,7 +114,7 @@ end end -elseif vfoptions.lowmemory==1 +elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals(z_c,:); diff --git a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_nod1_raw.m b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_nod1_raw.m index 3fdec186..2bd6f307 100644 --- a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_nod1_raw.m +++ b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_nod1_raw.m @@ -14,8 +14,13 @@ % n_a1prime=n_a1; % a1prime_gridvals=a1_gridvals; -if vfoptions.lowmemory>0 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory==1 + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + special_n_z=ones(1,length(n_z),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end % Create a vector containing all the return function parameters (in order) @@ -58,7 +63,7 @@ Policy(1,:,:)=rem(maxindex-1,N_d2)+1; Policy(2,:,:)=ceil(maxindex/N_d2); -elseif vfoptions.lowmemory>=1 +elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals(z_c,:); @@ -78,6 +83,27 @@ Policy(2,:,z_c)=ceil(maxindex/N_d2); end + +elseif vfoptions.lowmemory==3 + + for z_c=1:N_z + z_val=z_gridvals(z_c,:); + % Calc the condl expectation term (except beta), which depends on z but not on control variables + EV_z=EV.*shiftdim(pi_z(z_c,:)',-2); + EV_z(isnan(EV_z))=0; %multilications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) + EV_z=sum(EV_z,3); + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,special_n_ea, special_n_z, d2_grid, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountFactorParamsVec*repelem(EV_z,1,N_a1,1); + % Calc the max and its index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c)=Vtemp; + Policy(1,1+(ea_c-1)*N_a1:ea_c*N_a1,z_c)=rem(maxindex-1,N_d2)+1; + Policy(2,1+(ea_c-1)*N_a1:ea_c*N_a1,z_c)=ceil(maxindex/N_d2); + end + end end diff --git a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_raw.m b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_raw.m index 10c51ce4..6c038077 100644 --- a/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_raw.m +++ b/TransitionPaths/InfHorz/subcodes/ValueFnSingleStep/experienceasset/ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset_raw.m @@ -16,8 +16,13 @@ % n_a1prime=n_a1; % a1prime_gridvals=a1_gridvals; -if vfoptions.lowmemory>0 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory==1 + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + special_n_z=ones(1,length(n_z),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end % Create a vector containing all the return function parameters (in order) @@ -61,23 +66,44 @@ V=shiftdim(Vtemp,1); PolicyTemp=maxindex; -elseif vfoptions.lowmemory>=1 +elseif vfoptions.lowmemory==1 for z_c=1:N_z - ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals, ReturnFnParamsVec,0,0); - + z_val=z_gridvals(z_c,:); % Calc the condl expectation term (except beta), which depends on z but not on control variables EV_z=EV.*shiftdim(pi_z(z_c,:)',-2); EV_z(isnan(EV_z))=0; %multiplications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) EV_z=sum(EV_z,3); - entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*repelem(EV_z,N_d1,N_a1,1); + ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); + + entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*repelem(EV_z,N_d1,N_a1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS_z,[],1); V(:,z_c)=Vtemp; PolicyTemp(1,:,z_c)=maxindex; end + +elseif vfoptions.lowmemory==3 + + for z_c=1:N_z + z_val=z_gridvals(z_c,:); + % Calc the condl expectation term (except beta), which depends on z but not on control variables + EV_z=EV.*shiftdim(pi_z(z_c,:)',-2); + EV_z(isnan(EV_z))=0; %multilications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) + EV_z=sum(EV_z,3); + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1, n_d2, n_a1, n_a1,special_n_ea, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountFactorParamsVec*repelem(EV_z,1,N_a1); + % Calc the max and its index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c)=Vtemp; + PolicyTemp(1,1+(ea_c-1)*N_a1:ea_c*N_a1,z_c)=maxindex; + end + end end dind=rem(PolicyTemp-1,N_d1*N_d2)+1; diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m index fc4c1330..ec05566b 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m @@ -14,15 +14,14 @@ %% a2_gridvals=CreateGridvals(n_a2,a2_grid,1); -if vfoptions.lowmemory==1 - special_n_e=ones(1,length(n_e)); -elseif vfoptions.lowmemory==2 - special_n_e=ones(1,length(n_e)); - special_n_z=ones(1,length(n_z)); -elseif vfoptions.lowmemory==3 - special_n_e=ones(1,length(n_e)); - special_n_z=ones(1,length(n_z)); - special_n_ea=ones(1,length(n_a2)); +if vfoptions.lowmemory>=1 + special_n_e=ones(1,length(n_e),'gpuArray'); +end +if vfoptions.lowmemory>=2 + special_n_z=ones(1,length(n_z),'gpuArray'); +end +if vfoptions.lowmemory==3 + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -65,9 +64,9 @@ z_val=z_gridvals_J(z_c,:,N_j); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); - ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1, special_n_ea, special_n_z, special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1, special_n_ea, special_n_z, special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); % Calc the max and its index - [Vtemp,maxindex]=max(ReturnMatrix_ze); + [Vtemp,maxindex]=max(ReturnMatrix_ea_ze); V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=Vtemp; Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=maxindex; end @@ -137,10 +136,10 @@ e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix_ze+DiscountedEV_z; + entireRHS_ze=ReturnMatrix_ze+DiscountedEV_z; % Calc the max and its index - [Vtemp,maxindex]=max(entireRHS,[],1); + [Vtemp,maxindex]=max(entireRHS_ze,[],1); V(:,z_c,e_c,N_j)=shiftdim(Vtemp,1); Policy(:,z_c,e_c,N_j)=shiftdim(maxindex,1); @@ -151,15 +150,15 @@ ea_val=a2_gridvals(ea_c); for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); - ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix_ze+DiscountedEV_z; + entireRHS_ea_z=ReturnMatrix_ea_ze+DiscountedEV_ea_z; % Calc the max and its index - [Vtemp,maxindex]=max(entireRHS,[],1); + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=shiftdim(Vtemp,1); Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=shiftdim(maxindex,1); @@ -257,16 +256,16 @@ ea_val=a2_gridvals(ea_c); for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); - DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); - ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix_ze+DiscountedEV_z; + entireRHS_ea_z=ReturnMatrix_ea_ze+DiscountedEV_ea_z; %Calc the max and its index - [Vtemp,maxindex]=max(entireRHS,[],1); + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(Vtemp,1); Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(maxindex,1); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m index dec6b4e6..6145cd41 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m @@ -14,11 +14,13 @@ d2_gridvals=gpuArray(d2_gridvals); a2_grid=gpuArray(a2_grid); -if vfoptions.lowmemory>1 - special_n_e=ones(1,length(n_e)); -end -if vfoptions.lowmemory==2 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory==1 + special_n_e=ones(1,length(n_e),'gpuArray'); +elseif vfoptions.lowmemory==2 + special_n_e=ones(1,length(n_e),'gpuArray'); + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==3 + error("There is no a1 to iterate, so cannot set vfoptions.lowmemory=3") end %% j=N_j @@ -174,12 +176,12 @@ V(:,:,:,jj)=shiftdim(Vtemp,1); Policy(:,:,:,jj)=shiftdim(maxindex,1); elseif vfoptions.lowmemory==1 - + DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); ReturnMatrix_e=CreateReturnFnMatrix_Case2_Disc_Par2e(ReturnFn,[n_d1,n_d2], n_a2, n_z, special_n_e, d_gridvals, a2_grid, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*repelem(EV,N_d1,1); + entireRHS=ReturnMatrix_e+DiscountedEV; %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_raw.m index 8b72125e..b1f3ad29 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_raw.m @@ -14,7 +14,11 @@ a2_grid=gpuArray(a2_grid); if vfoptions.lowmemory==1 - special_n_z=ones(1,length(n_z)); + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + error("There is no a1 to iterate, so cannot set vfoptions.lowmemory=3") end %% j=N_j diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_e_raw.m index 74a85d34..116244f3 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_e_raw.m @@ -14,10 +14,13 @@ a2_gridvals=CreateGridvals(n_a2,a2_grid,1); if vfoptions.lowmemory>=1 - special_n_e=ones(1,length(n_e)); + special_n_e=ones(1,length(n_e),'gpuArray'); end -if vfoptions.lowmemory==2 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory>=2 + special_n_z=ones(1,length(n_z),'gpuArray'); +end +if vfoptions.lowmemory==3 + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -60,6 +63,22 @@ end end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0, n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + % Calc the max and it's index + [Vtemp,maxindex]=max(ReturnMatrix_ea_ze); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=maxindex; + + end + end + end end else DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,N_j); @@ -90,13 +109,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); % should autofill e dimension %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -106,6 +123,7 @@ elseif vfoptions.lowmemory==1 + DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_val, ReturnFnParamsVec,0,0); @@ -121,7 +139,7 @@ elseif vfoptions.lowmemory==2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); @@ -135,6 +153,26 @@ Policy(:,z_c,e_c,N_j)=shiftdim(maxindex,1); end end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),1,N_a1,1); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_ze,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=shiftdim(maxindex,1); + end + end + end end end @@ -176,12 +214,10 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); % should autofill e dimension %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -191,6 +227,7 @@ elseif vfoptions.lowmemory==1 + DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec,0,0); @@ -206,7 +243,7 @@ elseif vfoptions.lowmemory==2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); @@ -220,6 +257,26 @@ Policy(:,z_c,e_c,jj)=shiftdim(maxindex,1); end end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),1,N_a1,1); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_ze,[],1); + + V(:,z_c,e_c,jj)=shiftdim(Vtemp,1); + Policy(:,z_c,e_c,jj)=shiftdim(maxindex,1); + end + end + end end end diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_e_raw.m index 43d2d6b3..0132ab82 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_e_raw.m @@ -14,11 +14,13 @@ a2_grid=gpuArray(a2_grid); -if vfoptions.lowmemory>1 - special_n_e=ones(1,length(n_e)); -end -if vfoptions.lowmemory==2 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory==1 + special_n_e=ones(1,length(n_e),'gpuArray'); +elseif vfoptions.lowmemory==2 + special_n_e=ones(1,length(n_e),'gpuArray'); + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==3 + error("There is no a1 to iterate, so cannot set vfoptions.lowmemory=3") end @@ -174,12 +176,14 @@ Policy(:,:,:,jj)=shiftdim(maxindex,1); elseif vfoptions.lowmemory==1 + + DiscountedEV=DiscountFactorParamsVec*EV; for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); ReturnMatrix_e=CreateReturnFnMatrix_Case2_Disc_Par2e(ReturnFn,n_d2, n_a2, n_z, special_n_e, d2_gridvals, a2_grid, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*EV; + entireRHS=ReturnMatrix_e+DiscountedEV; %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -187,7 +191,7 @@ V(:,:,e_c,jj)=shiftdim(Vtemp,1); Policy(:,:,e_c,jj)=shiftdim(maxindex,1); end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); EV_z=EV(:,:,z_c); diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_raw.m index 85578670..1c665ae1 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_raw.m @@ -13,7 +13,11 @@ a2_grid=gpuArray(a2_grid); if vfoptions.lowmemory==1 - special_n_z=ones(1,length(n_z)); + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + error("There is no a1 to iterate, so cannot set vfoptions.lowmemory=3") end %% j=N_j diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m index af3c4597..400a4ff4 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_raw.m @@ -14,8 +14,13 @@ % n_a1prime=n_a1; % a1prime_gridvals=a1_gridvals; -if vfoptions.lowmemory>0 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory==1 + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + special_n_z=ones(1,length(n_z),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -30,7 +35,7 @@ [Vtemp,maxindex]=max(ReturnMatrix,[],1); V(:,:,N_j)=Vtemp; Policy(:,:,N_j)=maxindex; - elseif vfoptions.lowmemory>=1 + elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); @@ -39,6 +44,18 @@ V(:,z_c,N_j)=Vtemp; Policy(:,z_c,N_j)=maxindex; end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,special_n_ea, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + %Calc the max and it's index + [Vtemp,maxindex]=max(ReturnMatrix_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=maxindex; + end + end end else DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,N_j); @@ -79,7 +96,7 @@ V(:,:,N_j)=shiftdim(Vtemp,1); Policy(:,:,N_j)=shiftdim(maxindex,1); - elseif vfoptions.lowmemory>=1 + elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -94,6 +111,24 @@ V(:,z_c,N_j)=Vtemp; Policy(:,z_c,N_j)=maxindex; end + + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),1,N_a1,1); + + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,special_n_ea, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=maxindex; + end + end end end @@ -134,12 +169,10 @@ EV(isnan(EV))=0; %multiplications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites) EV=squeeze(sum(EV,3)); % sum over z', leaving a singular third dimension - DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -147,11 +180,11 @@ V(:,:,jj)=shiftdim(Vtemp,1); Policy(:,:,jj)=shiftdim(maxindex,1); - elseif vfoptions.lowmemory>=1 + elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),1,N_a1,1); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); @@ -162,6 +195,24 @@ V(:,z_c,jj)=Vtemp; Policy(:,z_c,jj)=maxindex; end + + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),1,N_a1,1); + + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,special_n_ea, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=maxindex; + end + end end end diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m index 6fa5bb58..e6cf794a 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m @@ -14,12 +14,12 @@ a2_gridvals=CreateGridvals(n_a2,a2_grid,1); if vfoptions.lowmemory==1 - special_n_z=ones(1,length(n_z)); + special_n_z=ones(1,length(n_z),'gpuArray'); elseif vfoptions.lowmemory==2 error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") elseif vfoptions.lowmemory==3 - special_n_z=ones(1,length(n_z)); - special_n_ea=ones(1,length(n_a2)); + special_n_z=ones(1,length(n_z),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -116,10 +116,11 @@ for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); % Calc the max and its index - [Vtemp,maxindex]=max(ReturnMatrix_ea_z,[],1); - V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=shiftdim(Vtemp,1); - Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=shiftdim(maxindex,1); + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=maxindex; end end end @@ -203,8 +204,8 @@ % Calc the max and its index [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); - V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=shiftdim(Vtemp,1); - Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=shiftdim(maxindex,1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=maxindex; end end end diff --git a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_e_raw.m index afbc939b..617d9957 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_e_raw.m @@ -17,12 +17,15 @@ pi_u=shiftdim(pi_u,-2); % put it into third dimension -if vfoptions.lowmemory==1 - special_n_e=ones(1,length(n_e),'gpuArray'); -elseif vfoptions.lowmemory==2 +if vfoptions.lowmemory>=1 special_n_e=ones(1,length(n_e),'gpuArray'); +end +if vfoptions.lowmemory>=2 special_n_z=ones(1,length(n_z),'gpuArray'); end +if vfoptions.lowmemory>=3 + special_n_ea=ones(1,length(n_a2),'gpuArray'); +end %% j=N_j @@ -57,6 +60,21 @@ Policy(:,z_c,e_c,N_j)=maxindex; end end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea, special_n_z, special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + % Calc the max and it's index + [Vtemp,maxindex]=max(ReturnMatrix_ea_ze); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=maxindex; + end + end + end end else DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,N_j); @@ -88,13 +106,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); % should autofill e dimension %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -103,7 +119,7 @@ Policy(:,:,:,N_j)=shiftdim(maxindex,1); elseif vfoptions.lowmemory==1 - + DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_val, ReturnFnParamsVec,0,0); @@ -119,7 +135,7 @@ elseif vfoptions.lowmemory==2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); @@ -133,6 +149,26 @@ Policy(:,z_c,e_c,N_j)=shiftdim(maxindex,1); end end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1,1); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; + + % Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_ze,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=shiftdim(maxindex,1); + end + end + end end end @@ -176,13 +212,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); % should autofill e dimension % Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -196,10 +230,10 @@ e_val=e_gridvals_J(e_c,:,jj); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix_e+DiscountedEV; + entireRHS_e=ReturnMatrix_e+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1,1); % should autofill e dimension % Calc the max and it's index - [Vtemp,maxindex]=max(entireRHS,[],1); + [Vtemp,maxindex]=max(entireRHS_e,[],1); V(:,:,e_c,jj)=shiftdim(Vtemp,1); Policy(:,:,e_c,jj)=shiftdim(maxindex,1); @@ -207,21 +241,42 @@ elseif vfoptions.lowmemory==2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix_ze+DiscountedEV_z; + entireRHS_ze=ReturnMatrix_ze+DiscountedEV_z; %Calc the max and it's index - [Vtemp,maxindex]=max(entireRHS,[],1); + [Vtemp,maxindex]=max(entireRHS_ze,[],1); V(:,z_c,e_c,jj)=shiftdim(Vtemp,1); Policy(:,z_c,e_c,jj)=shiftdim(maxindex,1); end end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1,1); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,jj); + + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_ze,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(maxindex,1); + end + end + end end end diff --git a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_noa1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_noa1_e_raw.m index baab2ea8..51824286 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_noa1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_noa1_e_raw.m @@ -17,11 +17,13 @@ pi_u=shiftdim(pi_u,-2); % put it into third dimension -if vfoptions.lowmemory>1 - special_n_e=ones(1,length(n_e)); -end -if vfoptions.lowmemory==2 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory==1 + special_n_e=ones(1,length(n_e),'gpuArray'); +elseif vfoptions.lowmemory==2 + special_n_e=ones(1,length(n_e),'gpuArray'); + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==3 + error("There is no a1 to iterate, so cannot set vfoptions.lowmemory=3") end %% j=N_j @@ -96,12 +98,12 @@ V(:,:,:,N_j)=shiftdim(Vtemp,1); Policy(:,:,:,N_j)=shiftdim(maxindex,1); elseif vfoptions.lowmemory==1 - + DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,1); % should autofill e dimension for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case2_Disc_Par2e(ReturnFn,[n_d1,n_d2], n_a2, n_z,special_n_e, d_gridvals, a2_grid, z_gridvals_J(:,:,N_j), e_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*repelem(EV,N_d1,1); % should autofill e dimension + entireRHS=ReturnMatrix_e+DiscountedEV; %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -176,12 +178,12 @@ V(:,:,:,jj)=shiftdim(Vtemp,1); Policy(:,:,:,jj)=shiftdim(maxindex,1); elseif vfoptions.lowmemory==1 - + DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); ReturnMatrix_e=CreateReturnFnMatrix_Case2_Disc_Par2e(ReturnFn,[n_d1,n_d2], n_a2, n_z,special_n_e, d_gridvals, a2_grid, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix_e+DiscountFactorParamsVec*repelem(EV,N_d1,1); + entireRHS=ReturnMatrix_e+DiscountedEV; %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_noa1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_noa1_raw.m index ef8e7935..ca84ba6c 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_noa1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_noa1_raw.m @@ -17,7 +17,11 @@ pi_u=shiftdim(pi_u,-2); % put it into third dimension if vfoptions.lowmemory==1 - special_n_z=ones(1,length(n_z)); + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + error("There is no a1 to iterate, so cannot set vfoptions.lowmemory=3") end %% j=N_j @@ -32,7 +36,7 @@ [Vtemp,maxindex]=max(ReturnMatrix,[],1); V(:,:,N_j)=Vtemp; Policy(:,:,N_j)=maxindex; - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); ReturnMatrix=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,[n_d1,n_d2], n_a2, n_z, d_gridvals, a2_grid, z_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command @@ -69,23 +73,21 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,[n_d1,n_d2], n_a2, n_z, d_gridvals, a2_grid, z_gridvals_J(:,:,N_j), ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); V(:,:,N_j)=shiftdim(Vtemp,1); Policy(:,:,N_j)=shiftdim(maxindex,1); - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,1); ReturnMatrix_z=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,[n_d1,n_d2], n_a2, special_n_z, d_gridvals, a2_grid, z_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command @@ -134,23 +136,21 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,[n_d1,n_d2], n_a2, n_z, d_gridvals, a2_grid, z_gridvals_J(:,:,jj), ReturnFnParamsVec); % with only the experience asset, can just use Case2 command - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); V(:,:,jj)=shiftdim(Vtemp,1); Policy(:,:,jj)=shiftdim(maxindex,1); - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,1); ReturnMatrix_z=CreateReturnFnMatrix_Case2_Disc_Par2(ReturnFn,[n_d1,n_d2], n_a2, special_n_z, d_gridvals, a2_grid, z_val, ReturnFnParamsVec); % with only the experience asset, can just use Case2 command diff --git a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_e_raw.m index 99f85794..653c0957 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_e_raw.m @@ -17,10 +17,13 @@ pi_u=shiftdim(pi_u,-2); % put it into third dimension if vfoptions.lowmemory>=1 - special_n_e=ones(1,length(n_e)); + special_n_e=ones(1,length(n_e),'gpuArray'); end -if vfoptions.lowmemory==2 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory>=2 + special_n_z=ones(1,length(n_z),'gpuArray'); +end +if vfoptions.lowmemory>=3 + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -63,6 +66,23 @@ end end + elseif vfoptions.lowmemory==3 + + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0, n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + % Calc the max and it's index + [Vtemp,maxindex]=max(ReturnMatrix_ea_ze); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=maxindex; + + end + end + end end else DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,N_j); @@ -94,13 +114,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); % should autofill e dimension %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -110,6 +128,7 @@ elseif vfoptions.lowmemory==1 + DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), e_val, ReturnFnParamsVec,0,0); @@ -122,7 +141,9 @@ V(:,:,e_c,N_j)=shiftdim(Vtemp,1); Policy(:,:,e_c,N_j)=shiftdim(maxindex,1); end + elseif vfoptions.lowmemory==2 + for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); DiscountedEV_z=DiscountedEV(:,:,z_c); @@ -139,6 +160,28 @@ Policy(:,z_c,e_c,N_j)=shiftdim(maxindex,1); end end + + elseif vfoptions.lowmemory==3 + + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),1,N_a1,1); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_ze,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=shiftdim(maxindex,1); + end + end + end end end @@ -181,12 +224,10 @@ EV=squeeze(sum(EV,3)); % EV is over (d2,a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,n_z,n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; % should autofill e dimension + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); % should autofill e dimension %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -196,6 +237,7 @@ elseif vfoptions.lowmemory==1 + DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,n_z,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec,0,0); @@ -208,10 +250,12 @@ V(:,:,e_c,jj)=shiftdim(Vtemp,1); Policy(:,:,e_c,jj)=shiftdim(maxindex,1); end + elseif vfoptions.lowmemory==2 + for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),1,N_a1,1); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,n_a2,special_n_z,special_n_e, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, e_val, ReturnFnParamsVec,0,0); @@ -225,6 +269,28 @@ Policy(:,z_c,e_c,jj)=shiftdim(maxindex,1); end end + + elseif vfoptions.lowmemory==3 + + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),1,N_a1,1); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, 0,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_ze,[],1); + + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,jj)=shiftdim(maxindex,1); + end + end + end end end diff --git a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_noa1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_noa1_e_raw.m index 530c12fc..77de0cc6 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_noa1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_noa1_e_raw.m @@ -16,11 +16,13 @@ pi_u=shiftdim(pi_u,-2); % put it into third dimension -if vfoptions.lowmemory>1 - special_n_e=ones(1,length(n_e)); -end -if vfoptions.lowmemory==2 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory==1 + special_n_e=ones(1,length(n_e),'gpuArray'); +elseif vfoptions.lowmemory==2 + special_n_e=ones(1,length(n_e),'gpuArray'); + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==3 + error("There is no a1 to iterate, so cannot set vfoptions.lowmemory=3") end diff --git a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_noa1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_noa1_raw.m index 5a4581ea..d4351baf 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_noa1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_noa1_raw.m @@ -16,7 +16,11 @@ pi_u=shiftdim(pi_u,-2); % put it into third dimension if vfoptions.lowmemory==1 - special_n_z=ones(1,length(n_z)); + special_n_z=ones(1,length(n_z),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + error("There is no a1 to iterate, so cannot set vfoptions.lowmemory=3") end %% j=N_j diff --git a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_raw.m index 9b63d230..7a472bfe 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_nod1_raw.m @@ -17,8 +17,13 @@ pi_u=shiftdim(pi_u,-2); % put it into third dimension -if vfoptions.lowmemory>0 - special_n_z=ones(1,length(n_z)); +if vfoptions.lowmemory==1 + special_n_z=ones(1,length(n_e),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + special_n_z=ones(1,length(n_e),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -42,6 +47,19 @@ V(:,z_c,N_j)=Vtemp; Policy(:,z_c,N_j)=maxindex; end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2,n_a1,n_a1,special_n_ea,special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + % Calc the max and it's index + [Vtemp,maxindex]=max(ReturnMatrix_ea_z); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=maxindex; + + end + end end else DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,N_j); @@ -78,7 +96,7 @@ if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_jj), ReturnFnParamsVec,0,0); - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,1,N_a1,1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -90,7 +108,7 @@ for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),1,N_a1,1); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); @@ -101,6 +119,25 @@ V(:,z_c,N_j)=Vtemp; Policy(:,z_c,N_j)=maxindex; end + + elseif vfoptions.lowmemory==3 + + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),1,N_a1,1); + + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,special_n_ea, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_z+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=maxindex; + end + end end end @@ -172,6 +209,25 @@ V(:,z_c,jj)=Vtemp; Policy(:,z_c,jj)=maxindex; end + + elseif vfoptions.lowmemory==3 + + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + DiscountedEV_ea_z=DiscountedEV(:,ea_c,z_c); + + ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,special_n_ea, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_z+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + V(:,z_c,jj)=Vtemp; + Policy(:,z_c,jj)=maxindex; + end + end end end diff --git a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_raw.m index 30bfec7f..27da9025 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_raw.m @@ -17,7 +17,12 @@ pi_u=shiftdim(pi_u,-2); % put it into third dimension if vfoptions.lowmemory==1 - special_n_z=ones(1,length(n_z)); + special_n_z=ones(1,length(n_e),'gpuArray'); +elseif vfoptions.lowmemory==2 + error("There is no e to iterate, so cannot set vfoptions.lowmemory=2") +elseif vfoptions.lowmemory==3 + special_n_z=ones(1,length(n_e),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -41,6 +46,19 @@ V(:,z_c,N_j)=Vtemp; Policy(:,z_c,N_j)=maxindex; end + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2,n_a1,n_a1,special_n_ea,special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + % Calc the max and it's index + [Vtemp,maxindex]=max(ReturnMatrix_ea_z); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=maxindex; + + end + end end else DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,N_j); @@ -78,7 +96,7 @@ ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2,n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_gridvals_J(:,:,N_j), ReturnFnParamsVec,0,0); % (d,a1prime,a) - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -89,7 +107,7 @@ elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); @@ -100,6 +118,24 @@ V(:,z_c,N_j)=Vtemp; Policy(:,z_c,N_j)=maxindex; end + + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); + + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,special_n_ea, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=maxindex; + end + end end end @@ -143,13 +179,11 @@ EV=squeeze(sum(EV,3)); % EV is over (d2-a1prime,a2,z) - DiscountedEV=DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2,n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals,z_gridvals_J(:,:,jj), ReturnFnParamsVec,0,0); % (d,aprime,a,z) - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec*repelem(EV,N_d1,N_a1); %Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -160,7 +194,7 @@ elseif vfoptions.lowmemory==1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); - DiscountedEV_z=DiscountedEV(:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec*repelem(EV(:,:,z_c),N_d1,N_a1); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,n_a2, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,0,0); @@ -171,6 +205,24 @@ V(:,z_c,jj)=Vtemp; Policy(:,z_c,jj)=maxindex; end + + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,jj); + DiscountedEV_ea_z=DiscountFactorParamsVec*repelem(EV(:,ea_c,z_c),N_d1,N_a1); + + ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2, n_a1, n_a1,special_n_ea, special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountedEV_ea_z; + + %Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,jj)=maxindex; + end + end end end From efcb3c7319a58db73c6b1761e386fb302099762b Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 15 Apr 2026 15:39:04 +1200 Subject: [PATCH 46/90] Add missing comma to ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw declaration A typo slipped back in? --- .../slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m index 9103c8c0..ac74a6db 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m @@ -1,4 +1,4 @@ -function [V,Policy]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals,d2_gridvals,a1_gridvals,a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames vfoptions) +function [V,Policy]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals,d2_gridvals,a1_gridvals,a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) N_d1=prod(n_d1); N_d2=prod(n_d2); From 571aa32cfaf53db75a54d8080a431630bdb481b6 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Thu, 16 Apr 2026 13:53:30 +1200 Subject: [PATCH 47/90] Remove unnecessary ExpAsset files RIP. --- ...AgentDist_AggVars_FHorz_ExpAsset_fastOLG.m | 414 ------------------ ...UnKronPolicyIndexes_Case1_FHorz_ExpAsset.m | 92 ---- ...KronPolicyIndexes_Case1_FHorz_ExpAsset_e.m | 93 ---- 3 files changed, 599 deletions(-) delete mode 100644 EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m delete mode 100644 SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset.m delete mode 100644 SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e.m diff --git a/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m b/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m deleted file mode 100644 index c80f947a..00000000 --- a/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/ExpAsset/EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG.m +++ /dev/null @@ -1,414 +0,0 @@ -function AggVars=EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG(AgentDist,PolicyValues_d, PolicyValues_aprime, FnsToEvaluate,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_z,N_a,N_z,a_gridvals,z_gridvals_J_fastOLG,outputasstructure) -% fastOLG: so (a,j)-by-z -% Policy can be in fastOLG for or not, use fastOLGpolicy=1 or 0 to indicate this -% No z is treated elsewhere -% If you have e or semiz, just disguise them as z for this command - -% If no d variable, set l_d=0, and then PolicyValues_d=[], d_gridvals=[]. - -% PolicyValues_d is [N_a,N_j,N_z,l_d] -% PolicyValues_aprime is [N_a,N_j,N_z,l_aprime] -% a_gridvals is [N_a,l_a] -% z_gridvals_J_fastOLG is [1,N_j,N_z,l_z] (convert internally to [1,N_j,N_z,l_z]) - -% parameters that depend on age must be [1,N_j] - -% Note: FnsToEvaluate is already cell (converted from struct) - -%% -if outputasstructure==1 - AggVars=struct(); -else % outputasstructure==0 - AggVars=zeros(length(FnsToEvaluate),1,'gpuArray'); -end - -% AgentDist is [N_a*N_j*N_z,1] or [N_a*N_j,N_e] or [N_a*N_j*N_z,N_e] -% PolicyValues is [N_a,N_j,N_ze] - -for ff=1:length(FnsToEvaluate) - Values=zeros(N_a,N_j,N_z,'gpuArray'); - - - if isempty(FnsToEvaluateParamNames(ff).Names) - ParamCell=cell(0,1); - else - % Create a matrix containing all the return function parameters (in order). - % Each column will be a specific parameter with the values at every age. - FnToEvaluateParamsAgeMatrix=CreateAgeMatrixFromParams(Parameters, FnsToEvaluateParamNames(ff).Names,N_j); % this will be a matrix, row indexes ages and column indexes the parameters (parameters which are not dependent on age appear as a constant valued column) - - nFnToEvaluateParams=size(FnToEvaluateParamsAgeMatrix,2); - - ParamCell=cell(nFnToEvaluateParams,1); - for ii=1:nFnToEvaluateParams - ParamCell(ii,1)={shiftdim(FnToEvaluateParamsAgeMatrix(:,ii),-1)}; % (a,j,z,l_d+l_a), so we want j to be after a (which is N_a) - end - end - - if l_z==1 - if l_d==0 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==4 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - end - elseif l_d==1 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==4 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - end - elseif l_d==2 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==4 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - end - elseif l_d==3 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==4 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - end - elseif l_d==4 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - elseif l_aprime==4 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), ParamCell{:}); - end - end - elseif l_z==2 - if l_d==0 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==4 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - end - elseif l_d==1 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==4 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - end - elseif l_d==2 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==4 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - end - elseif l_d==3 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==4 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - end - elseif l_d==4 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - elseif l_aprime==4 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), PolicyValues_aprime(:,:,:,4), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), a_gridvals(:,4), a_gridvals(:,5), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); - end - end - %% Fix these with code generator... - elseif l_z==3 - if l_d==0 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - end - elseif l_d==1 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - end - elseif l_d==2 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - end - elseif l_d==3 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - end - elseif l_d==4 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), ParamCell{:}); - end - end - elseif l_z==4 - if l_d==0 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - end - elseif l_d==1 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - end - elseif l_d==2 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - end - elseif l_d==3 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - end - elseif l_d==4 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), ParamCell{:}); - end - end - elseif l_z==5 - if l_d==0 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - end - elseif l_d==1 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - end - elseif l_d==2 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - end - elseif l_d==3 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - end - elseif l_d==4 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), ParamCell{:}); - end - end - elseif l_z==6 - if l_d==0 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - end - elseif l_d==1 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - end - elseif l_d==2 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - end - elseif l_d==3 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - end - elseif l_d==4 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), ParamCell{:}); - end - end - elseif l_z==7 - if l_d==0 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - end - elseif l_d==1 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - end - elseif l_d==2 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - end - elseif l_d==3 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - end - elseif l_d==4 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), ParamCell{:}); - end - end - elseif l_z==8 - if l_d==0 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - end - elseif l_d==1 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - end - elseif l_d==2 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - end - elseif l_d==3 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - end - elseif l_d==4 - if l_aprime==1 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), a_gridvals, z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - elseif l_aprime==2 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), a_gridvals(:,1), a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - elseif l_aprime==3 - Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1), PolicyValues_d(:,:,:,2), PolicyValues_d(:,:,:,3), PolicyValues_d(:,:,:,4), PolicyValues_aprime(:,:,:,1), PolicyValues_aprime(:,:,:,2), PolicyValues_aprime(:,:,:,3), a_gridvals(:,1), a_gridvals(:,2), a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1), z_gridvals_J_fastOLG(1,:,:,2), z_gridvals_J_fastOLG(1,:,:,3), z_gridvals_J_fastOLG(1,:,:,4), z_gridvals_J_fastOLG(1,:,:,5), z_gridvals_J_fastOLG(1,:,:,6), z_gridvals_J_fastOLG(1,:,:,7), z_gridvals_J_fastOLG(1,:,:,8), ParamCell{:}); - end - end - end - - if outputasstructure==1 - AggVars.(AggVarNames{ff}).Mean=sum(Values(:).*AgentDist(:)); - else % outputasstructure==0 - AggVars(ff)=sum(Values(:).*AgentDist(:)); - end -end - - -end diff --git a/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset.m b/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset.m deleted file mode 100644 index d753f044..00000000 --- a/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset.m +++ /dev/null @@ -1,92 +0,0 @@ -function Policy=UnKronPolicyIndexes_Case1_FHorz_ExpAsset(PolicyKron, n_d1,n_d2,n_a1,n_a2, n_z,N_j, vfoptions) -% Can use vfoptions OR simoptions -% Input: PolicyKron is (3,N_a,N_z,N_j); % first dim indexes the optimal choice for (d1,d2,aprime) -% without d1, the first dim is 2 and indexes the optimal choice for (d2,aprime) -% vfoptions.gridinterplayer=1 will mean the first dimension has one extra value (so 4 if d1, 3 without) -% Output: Policy (l_d+l_a,n_a1,n_a2,n_z,N_j); - -N_d1=prod(n_d1); -% N_d2=prod(n_d2); -N_a1=prod(n_a1); -N_a2=prod(n_a2); -N_a=N_a1*N_a2; -N_z=prod(n_z); - -l_d2=length(n_d2); % if ever there is more than one d2 for ExpAsset -l_aprime=length(n_a1); -n_aprime=n_a1; -extra=(vfoptions.gridinterplayer==1); - -if N_d1==0 - Policy=zeros(l_d2+l_aprime+extra,N_a,N_z,N_j,'gpuArray'); - - Policy(1,:,:,:)=rem(PolicyKron(1,:,:,:)-1,n_d2(1))+1; - if l_d2>1 - if l_d2>2 - for ii=1:l_d2-1 - Policy(ii,:,:,:)=rem(ceil(PolicyKron(1,:,:,:)/prod(n_d2(1:ii-1)))-1,n_d2(ii))+1; - end - end - Policy(l_d2,:,:,:)=ceil(PolicyKron(1,:,:,:)/prod(n_d2(1:l_d2-1))); - end - - Policy(l_d2+1,:,:,:)=rem(PolicyKron(2,:,:,:)-1,n_aprime(1))+1; - if l_aprime>1 - if l_aprime>2 - for ii=1:l_aprime-1 - Policy(l_d2+ii,:,:,:)=rem(ceil(PolicyKron(2,:,:,:)/prod(n_aprime(1:ii-1)))-1,n_aprime(ii))+1; - end - end - Policy(l_d2+l_aprime,:,:,:)=ceil(PolicyKron(2,:,:,:)/prod(n_aprime(1:l_aprime-1))); - end - - if vfoptions.gridinterplayer==1 - Policy(l_d2+l_aprime+1,:,:,:)=PolicyKron(3,:,:,:); - end - - Policy=reshape(Policy,[l_d2+l_aprime+extra,n_a,n_z,N_j]); - -else % N_d1>0 - l_d1=length(n_d1); % Note: this is anyway only used is N_d1~=0 - - Policy=zeros(l_d1+l_d2+l_aprime+extra,N_a,N_z,N_j,'gpuArray'); - - Policy(1,:,:,:)=rem(PolicyKron(1,:,:,:)-1,n_d1(1))+1; - if l_d1>1 - if l_d1>2 - for ii=1:l_d1-1 - Policy(ii,:,:,:)=rem(ceil(PolicyKron(1,:,:,:)/prod(n_d1(1:ii-1)))-1,n_d1(ii))+1; - end - end - Policy(l_d1,:,:,:)=ceil(PolicyKron(1,:,:,:)/prod(n_d1(1:l_d1-1))); - end - - Policy(l_d1+1,:,:,:)=rem(PolicyKron(2,:,:,:)-1,n_d2(1))+1; - if l_d2>1 - if l_d2>2 - for ii=1:l_d2-1 - Policy(l_d1+ii,:,:,:)=rem(ceil(PolicyKron(2,:,:,:)/prod(n_d2(1:ii-1)))-1,n_d2(ii))+1; - end - end - Policy(l_d1+l_d2,:,:,:)=ceil(PolicyKron(2,:,:,:)/prod(n_d2(1:l_d2-1))); - end - - Policy(l_d1+l_d2+1,:,:,:)=rem(PolicyKron(3,:,:,:)-1,n_aprime(1))+1; - if l_aprime>1 - if l_aprime>2 - for ii=1:l_aprime-1 - Policy(l_d1+l_d2+ii,:,:,:)=rem(ceil(PolicyKron(3,:,:,:)/prod(n_aprime(1:ii-1)))-1,n_aprime(ii))+1; - end - end - Policy(l_d1+l_d2+l_aprime,:,:,:)=ceil(PolicyKron(3,:,:,:)/prod(n_aprime(1:l_aprime-1))); - end - - if vfoptions.gridinterplayer==1 - Policy(l_d1+l_d2+l_aprime+1,:,:,:)=PolicyKron(4,:,:,:); - end - - Policy=reshape(Policy,[l_d1+l_d2+l_aprime+extra,n_a1,n_a2,n_z,N_j]); -end - - -end \ No newline at end of file diff --git a/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e.m b/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e.m deleted file mode 100644 index b941d55e..00000000 --- a/SubCodes/UnKronPolicyIndexes/UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e.m +++ /dev/null @@ -1,93 +0,0 @@ -function Policy=UnKronPolicyIndexes_Case1_FHorz_ExpAsset_e(PolicyKron, n_d1,n_d2,n_a1,n_a2, n_z,n_e,N_j, vfoptions) -% Can use vfoptions OR simoptions -% Input: PolicyKron is (3,N_a,N_z,N_e,N_j); % first dim indexes the optimal choice for (d1,d2,aprime) -% without d1, the first dim is 2 and indexes the optimal choice for (d2,aprime) -% vfoptions.gridinterplayer=1 will mean the first dimension has one extra value (so 4 if d1, 3 without) -% Output: Policy (l_d+l_a,n_a1,n_a2,n_z,n_e,N_j); - -N_d1=prod(n_d1); -% N_d2=prod(n_d2); -N_a1=prod(n_a1); -N_a2=prod(n_a2); -N_a=N_a1*N_a2; -N_z=prod(n_z); -N_e=prod(n_e); - -l_d2=length(n_d2); % if ever there is more than one d2 for ExpAsset -l_aprime=length(n_a1); -n_aprime=n_a1; -extra=(vfoptions.gridinterplayer==1); - -if N_d1==0 - Policy=zeros(l_d2+l_aprime+extra,N_a,N_z,N_e,N_j,'gpuArray'); - - Policy(1,:,:,:,:)=rem(PolicyKron(1,:,:,:,:)-1,n_d2(1))+1; - if l_d2>1 - if l_d2>2 - for ii=1:l_d2-1 - Policy(ii,:,:,:,:)=rem(ceil(PolicyKron(1,:,:,:,:)/prod(n_d2(1:ii-1)))-1,n_d2(ii))+1; - end - end - Policy(l_d2,:,:,:,:)=ceil(PolicyKron(1,:,:,:,:)/prod(n_d2(1:l_d2-1))); - end - - Policy(l_d2+1,:,:,:,:)=rem(PolicyKron(2,:,:,:,:)-1,n_aprime(1))+1; - if l_aprime>1 - if l_aprime>2 - for ii=1:l_aprime-1 - Policy(l_d2+ii,:,:,:,:)=rem(ceil(PolicyKron(2,:,:,:,:)/prod(n_aprime(1:ii-1)))-1,n_aprime(ii))+1; - end - end - Policy(l_d2+l_aprime,:,:,:,:)=ceil(PolicyKron(2,:,:,:,:)/prod(n_aprime(1:l_aprime-1))); - end - - if vfoptions.gridinterplayer==1 - Policy(l_d2+l_aprime+1,:,:,:,:)=PolicyKron(3,:,:,:,:); - end - - Policy=reshape(Policy,[l_d2+l_aprime+extra,n_a,n_z,n_e,N_j]); - -else % N_d1>0 - l_d1=length(n_d1); % Note: this is anyway only used is N_d1~=0 - - Policy=zeros(l_d1+l_d2+l_aprime+extra,N_a,N_z,N_e,N_j,'gpuArray'); - - Policy(1,:,:,:,:)=rem(PolicyKron(1,:,:,:,:)-1,n_d1(1))+1; - if l_d1>1 - if l_d1>2 - for ii=1:l_d1-1 - Policy(ii,:,:,:,:)=rem(ceil(PolicyKron(1,:,:,:,:)/prod(n_d1(1:ii-1)))-1,n_d1(ii))+1; - end - end - Policy(l_d1,:,:,:,:)=ceil(PolicyKron(1,:,:,:,:)/prod(n_d1(1:l_d1-1))); - end - - Policy(l_d1+1,:,:,:,:)=rem(PolicyKron(2,:,:,:,:)-1,n_d2(1))+1; - if l_d2>1 - if l_d2>2 - for ii=1:l_d2-1 - Policy(l_d1+ii,:,:,:,:)=rem(ceil(PolicyKron(2,:,:,:,:)/prod(n_d2(1:ii-1)))-1,n_d2(ii))+1; - end - end - Policy(l_d1+l_d2,:,:,:,:)=ceil(PolicyKron(2,:,:,:,:)/prod(n_d2(1:l_d2-1))); - end - - Policy(l_d1+l_d2+1,:,:,:,:)=rem(PolicyKron(3,:,:,:,:)-1,n_aprime(1))+1; - if l_aprime>1 - if l_aprime>2 - for ii=1:l_aprime-1 - Policy(l_d1+l_d2+ii,:,:,:,:)=rem(ceil(PolicyKron(3,:,:,:,:)/prod(n_aprime(1:ii-1)))-1,n_aprime(ii))+1; - end - end - Policy(l_d1+l_d2+l_aprime,:,:,:,:)=ceil(PolicyKron(3,:,:,:,:)/prod(n_aprime(1:l_aprime-1))); - end - - if vfoptions.gridinterplayer==1 - Policy(l_d1+l_d2+l_aprime+1,:,:,:,:)=PolicyKron(4,:,:,:,:); - end - - Policy=reshape(Policy,[l_d1+l_d2+l_aprime+extra,n_a1,n_a2,n_z,n_e,N_j]); -end - - -end \ No newline at end of file From 903e53be8cfba35e3c49fcb615ad718176991c01 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sat, 25 Apr 2026 14:57:55 +1200 Subject: [PATCH 48/90] Integration with new 5-step refactorization This is a quick-and-dirty reharmonization with the 5-step refactorization. IT NEEDS A REVIEW. But it does run through the somewhat complex mixed-horizon model with experience assets both with and without `e` shocks (separate option runs). Based on other changes now upstream, testing could be expanded to divide-and-conquer and other grid interpolation optimizations. I have not done this yet. --- .../ExoShocks/ExogShockSetup_TPath_InfHorz.m | 3 +- .../PType/TransitionPath_Case1_FHorz_PType.m | 58 ++++++++++++------- ...ransitionPath_Case1_FHorz_PType_shooting.m | 16 +++-- ...onPath_FHorz_substeps_Step2_AdjustPolicy.m | 22 +++++-- ...itionPath_FHorz_substeps_Step4tt_AggVars.m | 37 +++++++++--- ...Iter_FHorz_TPath_SingleStep_ExpAsset_raw.m | 2 +- ...nsitionPath_InfHorz_substeps_Step0_setup.m | 2 +- ...nPath_InfHorz_substeps_Step1_ValueFnIter.m | 1 + ...Path_InfHorz_substeps_Step2_AdjustPolicy.m | 3 +- ...ionPath_InfHorz_substeps_Step4tt_AggVars.m | 1 - .../Subcodes/createTPathFeedbackPlots.m | 6 +- .../ValueFnIter_InfHorz_ExpAsset_Refine_raw.m | 2 +- .../CreateaprimePolicyExperienceAsset_J.m | 8 +-- 13 files changed, 105 insertions(+), 56 deletions(-) diff --git a/SubCodes/ExoShocks/ExogShockSetup_TPath_InfHorz.m b/SubCodes/ExoShocks/ExogShockSetup_TPath_InfHorz.m index e90ba0b0..7aa60007 100644 --- a/SubCodes/ExoShocks/ExogShockSetup_TPath_InfHorz.m +++ b/SubCodes/ExoShocks/ExogShockSetup_TPath_InfHorz.m @@ -289,4 +289,5 @@ end -end \ No newline at end of file +end + diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m index 1356ab44..30059002 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m @@ -274,6 +274,9 @@ if ~isfield(PTypeStructure.(iistr).vfoptions,'gridinterplayer') PTypeStructure.(iistr).vfoptions.gridinterplayer=0; %default end + if ~isfield(PTypeStructure.(iistr).simoptions,'gridinterplayer') + PTypeStructure.(iistr).simoptions.gridinterplayer=0; %default + end % Model setup if ~isfield(PTypeStructure.(iistr).vfoptions,'exoticpreferences') PTypeStructure.(iistr).vfoptions.exoticpreferences='None'; % not yet implemented, so hardcodes None @@ -354,7 +357,7 @@ if N_e==0 PTypeStructure.(iistr).l_e=0; else - PTypeStructure.(iistr).l_e=length(n_e); + PTypeStructure.(iistr).l_e=length(PTypeStructure.(iistr).n_e); end if isa(d_grid,'struct') @@ -587,9 +590,16 @@ %% Set up exogenous shock processes - [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).pi_z_J_sim, PTypeStructure.(iistr).e_gridvals_J, PTypeStructure.(iistr).pi_e_J, PTypeStructure.(iistr).pi_e_J_sim, PTypeStructure.(iistr).ze_gridvals_J_fastOLG, transpathoptions, simoptions]=ExogShockSetup_TPath_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); - % Convert z and e to age-dependent joint-grids and transtion matrix - % output: z_gridvals_J, pi_z_J, e_gridvals_J, pi_e_J, transpathoptions,vfoptions,simoptions + if isfinite(PTypeStructure.(iistr).N_j) + [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).pi_z_J_sim, PTypeStructure.(iistr).e_gridvals_J, PTypeStructure.(iistr).pi_e_J, PTypeStructure.(iistr).pi_e_J_sim, PTypeStructure.(iistr).ze_gridvals_J_fastOLG, transpathoptions, PTypeStructure.(iistr).simoptions]=ExogShockSetup_TPath_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); + % Convert z and e to age-dependent joint-grids and transtion matrix + % output: z_gridvals_J, pi_z_J, e_gridvals_J, pi_e_J, transpathoptions,vfoptions,simoptions + else + [PTypeStructure.(iistr).z_gridvals, PTypeStructure.(iistr).pi_z, transpathoptions, PTypeStructure.(iistr).simoptions]=ExogShockSetup_TPath_InfHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); + end + + %% If using any non-standard endogenous states, setup for those (both FHorz and InfHorz btw) + [PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions]=SetupNonStandardEndoStates_FHorz_TPath(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); %% Organise V_final, AgeWeights and AgentDist_init % Reshape V_final @@ -652,6 +662,7 @@ AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_z,N_j]),[1,3,2]),[N_a*N_j*N_z,1]); AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); end + PTypeStructure.(iistr).jequalOneDist_T=jequalOneDist_temp; else AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_z*N_e,N_j]); % if simoptions.fastOLG==0 AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j] @@ -659,6 +670,7 @@ AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_z,N_e,N_j]),[1,4,2,3]),[N_a*N_j*N_z,N_e]); AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); end + PTypeStructure.(iistr).jequalOneDist=jequalOneDist_temp; end if all(size(AgeWeights_ii)==[N_j_temp,1]) % Does not depend on transition path period @@ -671,22 +683,24 @@ error('The age weights parameter seems to be the wrong size') end - % Check ParamPath to see if the AgeWeights vary over the transition - % (and overwrite PTypeStructure.(iistr).AgeWeights_T if it does) - temp=strcmp(ParamPathNames,AgeWeightsParamNames{1}); - if any(temp) - transpathoptions.ageweightstrivial=0; % AgeWeights vary over the transition - [~,kk]=max(temp); % Get index for the AgeWeightsParamNames{1} in ParamPathNames - % Create AgeWeights_T - PTypeStructure.(iistr).AgeWeights_T=ParamPath(:,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk))'; % This will always be N_j-by-T (as transpose) - % Note: still leave it in ParamPath just in case it is used in AggVars or somesuch - end - % Because ptypes hardcodes transpathoptions.ageweightstrivial=0, we need - if PTypeStructure.(iistr).simoptions.fastOLG==1 - if N_z==0 - PTypeStructure.(iistr).AgeWeights_T=repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1); % simoptions.fastOLG=1 so this is (a,j)-by-1 - else - PTypeStructure.(iistr).AgeWeights_T=repmat(repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1),N_z,1); % simoptions.fastOLG=1 so this is (a,j,z)-by-1 + % Check ParamPath to see if the AgeWeights vary over the transition + % (and overwrite PTypeStructure.(iistr).AgeWeights_T if it does) + temp=strcmp(ParamPathNames,AgeWeightsParamNames.(iistr){1}); + if any(temp) + transpathoptions.ageweightstrivial=0; % AgeWeights vary over the transition + [~,kk]=max(temp); % Get index for the AgeWeightsParamNames{1} in ParamPathNames + % Create AgeWeights_T + PTypeStructure.(iistr).AgeWeights_T=ParamPath(:,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk))'; % This will always be N_j-by-T (as transpose) + % Note: still leave it in ParamPath just in case it is used in AggVars or somesuch + end + + % Because ptypes hardcodes transpathoptions.ageweightstrivial=0, we need + if PTypeStructure.(iistr).simoptions.fastOLG==1 + if N_z==0 + PTypeStructure.(iistr).AgeWeights_T=repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1); % simoptions.fastOLG=1 so this is (a,j)-by-1 + else + PTypeStructure.(iistr).AgeWeights_T=repmat(repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1),N_z,1); % simoptions.fastOLG=1 so this is (a,j,z)-by-1 + end end end @@ -860,7 +874,7 @@ for nn=1:length(AggVarsPTypes) for ii=1:length(tminus1AggVarsNames.(AggVarsPTypes{nn})) if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames.(AggVarsPTypes{nn}){ii}) - error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames{ii}) + error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames.(AggVarsPTypes{nn}){ii}) end end end @@ -889,7 +903,7 @@ for nn=1:length(AggVarsPTypes) for ii=1:length(tminus1AggVarsNames.(AggVarsPTypes{nn})) if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames.(AggVarsPTypes{nn}){ii}) - error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames{ii}) + error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames.(AggVarsPTypes{nn}){ii}) end end end diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m index ef277367..91fbbb03 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m @@ -110,7 +110,11 @@ %% for ii=1:N_i iistr=PTypeStructure.Names_i{ii}; - [PTypeStructure.(iistr).PolicyIndexesPath,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,PTypeStructure.(iistr).exceptlastj,PTypeStructure.(iistr).exceptfirstj,PTypeStructure.(iistr).justfirstj]=TransitionPath_FHorz_substeps_Step0_setup(PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_j,T,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); + if isfinite(PTypeStructure.(iistr).N_j) + [PTypeStructure.(iistr).PolicyIndexesPath,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,PTypeStructure.(iistr).exceptlastj,PTypeStructure.(iistr).exceptfirstj,PTypeStructure.(iistr).justfirstj]=TransitionPath_FHorz_substeps_Step0_setup(PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_j,T,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); + else + [PTypeStructure.(iistr).PolicyIndexesPath,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2]=TransitionPath_InfHorz_substeps_Step0_setup(PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,T,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); + end end %% @@ -181,12 +185,12 @@ end % Get t-1 AggVars before we update them if use_tminus1AggVars==1 - for pp=1:length(tminus1AggVarsNames) + for pp=1:length(tminus1AggVarsNames.(iistr)) if tt>1 % The AggVars have not yet been updated, so they still contain previous period values - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); + Parameters.([tminus1AggVarsNames.(iistr){pp},'_tminus1'])=Parameters.(tminus1AggVarsNames.(iistr){pp}); else - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); + Parameters.([tminus1AggVarsNames.(iistr){pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames.(iistr){pp}); end end end @@ -241,7 +245,7 @@ end - AggVarsFullPath(PTypeStructure.(iistr).WhichFnsForCurrentPType,:,ii)=AggVarsPath_ii; + AggVarsFullPath(logical(PTypeStructure.(iistr).WhichFnsForCurrentPType),:,ii)=AggVarsPath_ii; end % done loop over ii @@ -476,7 +480,7 @@ end end - % Update current AggVars [we have to add this when doing ptype as GE conditions are in a seperate tt loop to the AggVars] + % Update current AggVars [we have to add this when doing ptype as GE conditions are in a separate tt loop to the AggVars] for ff=1:length(FullAggVarNames) Parameters.(FullAggVarNames{ff})=AggVarsPooledPath(ff,tt); % Keep the AggVars conditional on ptype for all the AggVars; overkill but that is fine diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m index 9fbbf5b6..1aac0b18 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m @@ -33,7 +33,12 @@ aprimeFnParamsVec=CreateAgeMatrixFromParams(Parameters,simoptions.setup_experienceasset.aprimeFnParamNames,N_j); % [N_j,number of params] - [a2primeIndexes, a2primeProbs]=CreateaprimePolicyExperienceAsset_J(PolicyIndexesPath(:,:,:,:,tt),simoptions.setup_experienceasset.aprimeFn, whichisdforexpasset, n_d, simoptions.setup_experienceasset.n_a1,simoptions.setup_experienceasset.n_a2, N_ze, N_j, simoptions.setup_experienceasset.d_grid, simoptions.setup_experienceasset.a2_grid, aprimeFnParamsVec,transpathoptions.fastOLG); + if N_z>0 && N_e>0 + PIP_tt=PolicyIndexesPath(:,:,:,:,:,tt); + else + PIP_tt=PolicyIndexesPath(:,:,:,:,tt); + end + [a2primeIndexes, a2primeProbs]=CreateaprimePolicyExperienceAsset_J(PIP_tt,simoptions.setup_experienceasset.aprimeFn, whichisdforexpasset, n_d, simoptions.setup_experienceasset.n_a1,simoptions.setup_experienceasset.n_a2, N_ze, N_j, simoptions.setup_experienceasset.d_grid, simoptions.setup_experienceasset.a2_grid, aprimeFnParamsVec,transpathoptions.fastOLG); % Note: a2primeIndexes and a2primeProbs are both [N_a,N_z,N_j] for fastOLG=0 % Note: a2primeIndexes is always the 'lower' point (the upper points are just aprimeIndexes+1), and the a2primeProbs are the probability of this lower point (prob of upper point is just 1 minus this). a2primeIndexesPath(:,:,:,tt)=a2primeIndexes(:,:,1:end-1); @@ -46,7 +51,12 @@ aprimeFnParamsVec=CreateAgeMatrixFromParams(Parameters,simoptions.setup_experienceasset.aprimeFnParamNames,N_j); % [N_j,number of params] - [a2primeIndexes, a2primeProbs]=CreateaprimePolicyExperienceAsset_J(PolicyIndexesPath(:,:,:,:,tt),simoptions.setup_experienceasset.aprimeFn, whichisdforexpasset, n_d, simoptions.setup_experienceasset.n_a1,simoptions.setup_experienceasset.n_a2, N_ze, N_j, simoptions.setup_experienceasset.d_grid, simoptions.setup_experienceasset.a2_grid, aprimeFnParamsVec,transpathoptions.fastOLG); + if N_z>0 && N_e>0 + PIP_tt=PolicyIndexesPath(:,:,:,:,:,tt); + else + PIP_tt=PolicyIndexesPath(:,:,:,:,tt); + end + [a2primeIndexes, a2primeProbs]=CreateaprimePolicyExperienceAsset_J(PIP_tt,simoptions.setup_experienceasset.aprimeFn, whichisdforexpasset, n_d, simoptions.setup_experienceasset.n_a1,simoptions.setup_experienceasset.n_a2, N_ze, N_j, simoptions.setup_experienceasset.d_grid, simoptions.setup_experienceasset.a2_grid, aprimeFnParamsVec,transpathoptions.fastOLG); % Note: a2primeIndexes and a2primeProbs are both [N_a,N_j,N_z] for fastOLG=1 % Note: a2primeIndexes is always the 'lower' point (the upper points are just aprimeIndexes+1), and the a2primeProbs are the probability of this lower point (prob of upper point is just 1 minus this). a2primeIndexesPath(:,:,:,tt)=a2primeIndexes(:,1:end-1,:); @@ -205,9 +215,9 @@ if simoptions.experienceasset==1 if simoptions.fastOLG==0 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z,(N_j-1),1,T])+a2primeIndexesPath; + PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z,(N_j-1),1,T-1])+a2primeIndexesPath; else - PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z,(N_j-1),1,T])+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z,(N_j-1),1,T-1])+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 @@ -313,9 +323,9 @@ if simoptions.experienceasset==1 if simoptions.fastOLG==0 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z*N_e,(N_j-1),1,T])+a2primeIndexesPath; + PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z*N_e,(N_j-1),1,T-1])+a2primeIndexesPath; else - PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z*N_e,(N_j-1),1,T])+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z*N_e,(N_j-1),1,T-1])+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step4tt_AggVars.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step4tt_AggVars.m index 0eb689e8..63703134 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step4tt_AggVars.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step4tt_AggVars.m @@ -9,29 +9,50 @@ ze_gridvals_J_fastOLG=transpathoptions.e_gridvals_J_fastOLG(:,:,:,tt); end +if transpathoptions.fastOLG==0 + % What's the right thing here? + WeightedAgentDist=AgentDist.*AgeWeights'; + if N_e==0 + if N_z==0 + WeightedAgentDist=reshape(WeightedAgentDist,[N_a*N_j,1]); + else + WeightedAgentDist=reshape(WeightedAgentDist,[N_a*N_j*N_z,1]); + end + else + if N_z==0 + WeightedAgentDist=reshape(WeightedAgentDist,[N_a*N_j,N_e]); + else + WeightedAgentDist=reshape(WeightedAgentDist,[N_a*N_j*N_z,N_e]); + end + end +else + WeightedAgentDist=AgentDist.*AgeWeights; +end + if N_z==0 && N_e==0 if N_d==0 - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG_noz(AgentDist.*AgeWeights, [], PolicyValuesPath_tt, FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,0,l_aprime,l_a,N_a,a_gridvals,1); + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG_noz(WeightedAgentDist, [], PolicyValuesPath_tt, FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,0,l_aprime,l_a,N_a,a_gridvals,1); else - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG_noz(AgentDist.*AgeWeights,PolicyValuesPath_tt(:,:,1:l_d), PolicyValuesPath_tt(:,:,l_d+1:end), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,N_a,a_gridvals,1); + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG_noz(WeightedAgentDist,PolicyValuesPath_tt(:,:,1:l_d), PolicyValuesPath_tt(:,:,l_d+1:end), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,N_a,a_gridvals,1); end elseif N_z>0 && N_e==0 if N_d==0 - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, [], PolicyValuesPath_tt, FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,0,l_aprime,l_a,l_z,N_a,N_z,a_gridvals,ze_gridvals_J_fastOLG,1); + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(WeightedAgentDist, [], PolicyValuesPath_tt, FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,0,l_aprime,l_a,l_z,N_a,N_z,a_gridvals,ze_gridvals_J_fastOLG,1); else - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath_tt(:,:,:,1:l_d), PolicyValuesPath_tt(:,:,:,l_d+1:end), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_z,N_a,N_z,a_gridvals,ze_gridvals_J_fastOLG,1); + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(WeightedAgentDist, PolicyValuesPath_tt(:,:,:,1:l_d), PolicyValuesPath_tt(:,:,:,l_d+1:end), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_z,N_a,N_z,a_gridvals,ze_gridvals_J_fastOLG,1); end elseif N_z==0 && N_e>0 if N_d==0 - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights,[], PolicyValuesPath_tt, FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,0,l_aprime,l_a,l_e,N_a,N_e,a_gridvals,ze_gridvals_J_fastOLG,1); + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(WeightedAgentDist,[], PolicyValuesPath_tt, FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,0,l_aprime,l_a,l_e,N_a,N_e,a_gridvals,ze_gridvals_J_fastOLG,1); else - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath_tt(:,:,:,1:l_d), PolicyValuesPath_tt(:,:,:,l_d+1:end), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_e,N_a,N_e,a_gridvals,ze_gridvals_J_fastOLG,1); + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(WeightedAgentDist, PolicyValuesPath_tt(:,:,:,1:l_d), PolicyValuesPath_tt(:,:,:,l_d+1:end), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_e,N_a,N_e,a_gridvals,ze_gridvals_J_fastOLG,1); end elseif N_z>0 && N_e>0 if N_d==0 - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, [], PolicyValuesPath_tt, FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,0,l_aprime,l_a,l_z+l_e,N_a,N_z*N_e,a_gridvals,ze_gridvals_J_fastOLG,1); + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(WeightedAgentDist, [], PolicyValuesPath_tt, FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,0,l_aprime,l_a,l_z+l_e,N_a,N_z*N_e,a_gridvals,ze_gridvals_J_fastOLG,1); else - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath_tt(:,:,:,1:l_d), PolicyValuesPath_tt(:,:,:,l_d+1:end), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_z+l_e,N_a,N_ze,a_gridvals,ze_gridvals_J_fastOLG,1); + N_ze=N_z*N_e; + AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(WeightedAgentDist, PolicyValuesPath_tt(:,:,:,1:l_d), PolicyValuesPath_tt(:,:,:,l_d+1:end), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_z+l_e,N_a,N_ze,a_gridvals,ze_gridvals_J_fastOLG,1); end end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m index ff6252f7..961851cc 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m @@ -1,4 +1,4 @@ -function [V,Policy2]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals,d2_gridvals,a1_gridvals,a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) +function [V,Policy]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals,d2_gridvals,a1_gridvals,a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions) N_d1=prod(n_d1); N_d2=prod(n_d2); diff --git a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step0_setup.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step0_setup.m index ea8a0343..b14a04d5 100644 --- a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step0_setup.m +++ b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step0_setup.m @@ -89,4 +89,4 @@ -end \ No newline at end of file +end diff --git a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m index 56b462b1..7e322866 100644 --- a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m +++ b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m @@ -108,3 +108,4 @@ end +end diff --git a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m index 4a947dcf..8af203b4 100644 --- a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m @@ -256,6 +256,7 @@ PolicyPath_ForAgentDistIter=PolicyaprimezPath; end + if N_probs==1 % =1 means not being used PolicyProbsPath=[]; end @@ -268,6 +269,4 @@ - - end diff --git a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step4tt_AggVars.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step4tt_AggVars.m index f745d2e2..3581dbdd 100644 --- a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step4tt_AggVars.m +++ b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step4tt_AggVars.m @@ -28,5 +28,4 @@ - end diff --git a/TransitionPaths/Subcodes/createTPathFeedbackPlots.m b/TransitionPaths/Subcodes/createTPathFeedbackPlots.m index 2a5fa4a4..4fbd898a 100644 --- a/TransitionPaths/Subcodes/createTPathFeedbackPlots.m +++ b/TransitionPaths/Subcodes/createTPathFeedbackPlots.m @@ -37,9 +37,9 @@ function createTPathFeedbackPlots(PricePathNames,AggVarNames,GEeqnNames,PricePat title(AggVarNames{pp}) end else - for pp=1:length(AggVarNames) - subplot(nrows,ncolumns,pp); plot(AggVarsPath(:,pp)) - title(AggVarNames{pp}) + for ff=1:length(AggVarNames) + subplot(nrows,ncolumns,ff); plot(AggVarsPath(ff,:)) + title(AggVarNames{ff}) end end sgtitle('Current Agg Vars Path'); diff --git a/ValueFnIter/InfHorz/ExpAsset/ValueFnIter_InfHorz_ExpAsset_Refine_raw.m b/ValueFnIter/InfHorz/ExpAsset/ValueFnIter_InfHorz_ExpAsset_Refine_raw.m index 40b9099a..2c984af2 100644 --- a/ValueFnIter/InfHorz/ExpAsset/ValueFnIter_InfHorz_ExpAsset_Refine_raw.m +++ b/ValueFnIter/InfHorz/ExpAsset/ValueFnIter_InfHorz_ExpAsset_Refine_raw.m @@ -24,7 +24,7 @@ % For refinement, now we solve for d*(aprime,a,z) that maximizes the ReturnFn [ReturnMatrix,dstar]=max(ReturnMatrix,[],1); ReturnMatrix=shiftdim(ReturnMatrix,1); -elseif vfoptions.lowmemory==1 % loop over z +elseif vfoptions.lowmemory>=1 % loop over z %% Refinement: calculate ReturnMatrix and 'remove' the d dimension ReturnMatrix=zeros(N_d2*N_a1,N_a,N_z,'gpuArray'); % 'refined' return matrix dstar=zeros(N_d2*N_a1,N_a,N_z,'gpuArray'); diff --git a/aprimeFnMatrix/CreateaprimePolicyExperienceAsset_J.m b/aprimeFnMatrix/CreateaprimePolicyExperienceAsset_J.m index e95b46a1..1e69afa4 100644 --- a/aprimeFnMatrix/CreateaprimePolicyExperienceAsset_J.m +++ b/aprimeFnMatrix/CreateaprimePolicyExperienceAsset_J.m @@ -67,16 +67,16 @@ else d1grid=d_grid(sum(n_d(1:whichisdforexpasset(1)-1))+1:sum(n_d(1:whichisdforexpasset(1)))); end - d1vals=reshape(d1grid(Policy(whichisdforexpasset(1),:,:)),[N_a*N_z,1]); + d1vals=reshape(d1grid(Policy(whichisdforexpasset(1),:,:)),[N_a*N_z,N_j]); if l_dexp>=2 d2grid=d_grid(sum(n_d(1:whichisdforexpasset(2)-1))+1:sum(n_d(1:whichisdforexpasset(2)))); - d2vals=reshape(d2grid(Policy(whichisdforexpasset(2),:,:)),[N_a*N_z,1]); + d2vals=reshape(d2grid(Policy(whichisdforexpasset(2),:,:)),[N_a*N_z,N_j]); if l_dexp>=3 d3grid=d_grid(sum(n_d(1:whichisdforexpasset(3)-1))+1:sum(n_d(1:whichisdforexpasset(3)))); - d3vals=reshape(d3grid(Policy(whichisdforexpasset(3),:,:)),[N_a*N_z,1]); + d3vals=reshape(d3grid(Policy(whichisdforexpasset(3),:,:)),[N_a*N_z,N_j]); if l_dexp>=4 d4grid=d_grid(sum(n_d(1:whichisdforexpasset(4)-1))+1:sum(n_d(1:whichisdforexpasset(4)))); - d4vals=reshape(d4grid(Policy(whichisdforexpasset(4),:,:)),[N_a*N_z,1]); + d4vals=reshape(d4grid(Policy(whichisdforexpasset(4),:,:)),[N_a*N_z,N_j]); end end end From 3a95d325cb75b6ce1889c445bd6a05efadf17e67 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sat, 25 Apr 2026 15:21:22 +1200 Subject: [PATCH 49/90] Remove unneeded files. Remove files no longer needed in the new 5-steps regime. --- ...tionPath_FHorz_PType_ExpAsset_singlepath.m | 42 ---- ...th_FHorz_PType_ExpAsset_singlepath_e_raw.m | 221 ------------------ ..._PType_ExpAsset_singlepath_fastOLG_e_raw.m | 179 -------------- ...Path_FHorz_PType_ExpAsset_singlepath_raw.m | 204 ---------------- 4 files changed, 646 deletions(-) delete mode 100644 TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m delete mode 100644 TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m delete mode 100644 TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m delete mode 100644 TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m diff --git a/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m b/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m deleted file mode 100644 index 347dd190..00000000 --- a/TransitionPaths/FHorz/PType/ExpAsset/TransitionPath_FHorz_PType_ExpAsset_singlepath.m +++ /dev/null @@ -1,42 +0,0 @@ -function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T, ... - l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG, ... - ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, ... - PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, ... - transpathoptions, vfoptions, simoptions) -% When doing shooting alogrithm on TPath FHorz PType, this is for a given ptype, and does the steps of back-iterate to get policy, then forward to get agent dist and agg vars. -% The only output is the agg vars path. - -% AggVarsPath=zeros(length(FnsToEvaluate),T-1,'gpuArray'); -if transpathoptions.fastOLG==0 - if N_z==0 - if N_e==0 - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_noz_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); - else - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_noz_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); - end - else - if N_e==0 - % Principal test case... - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); - else - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); - end - end -elseif transpathoptions.fastOLG==1 - if N_z==0 - if N_e==0 - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_noz_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); - else - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_noz_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); - end - else - if N_e==0 - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); - else - AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions); - end - end -end - - -end diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m deleted file mode 100644 index d3afc43f..00000000 --- a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw.m +++ /dev/null @@ -1,221 +0,0 @@ -function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T, ... - l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG, ... - ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, ... - use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, ... - II1orII, II2, exceptlastj,exceptfirstj,justfirstj, ... - transpathoptions, vfoptions, simoptions) -% When doing shooting alogrithm on TPath FHorz PType, this is for a given ptype, and does the steps of back-iterate to get policy, then forward to get agent dist and agg vars. -% The only output is the agg vars path. - -AggVarsPath=zeros(length(FnsToEvaluateCell),T-1,'gpuArray'); % Note: does not include the final AggVars, might be good to add them later as a way to make if obvious to user it things are incorrect - -N_a1=prod(n_a1); -N_a2=prod(n_a2); -N_a=N_a1*N_a2; -N_ze=N_z*N_e; - -l_aprime=length(n_a1); -l_a=l_aprime+length(n_a2); -l_z=length(n_z); -l_ze=l_z+length(n_e); - -%% -% Shapes: -% V is [N_a,N_z,N_e,N_j] -% AgentDist for basic is [N_a*N_z*N_e,N_j] -% AgentDist for fastOLG is [N_a*N_j*N_z,N_e] - -if vfoptions.gridinterplayer==0 - PolicyIndexesPath=zeros(l_d+l_aprime,N_a,N_z,N_e,N_j,T-1,'gpuArray'); %Periods 1 to T-1 -elseif vfoptions.gridinterplayer==1 - PolicyIndexesPath=zeros(l_d+l_aprime+1,N_a,N_z,N_e,N_j,T-1,'gpuArray'); %Periods 1 to T-1 -end -if simoptions.gridinterplayer==0 - if simoptions.fastOLG==0 - II1=1:1:N_a*N_z*N_e; - II2=ones(N_a*N_z*N_e,1); - elseif simoptions.fastOLG==1 - II1=1:1:N_a*(N_j-1)*N_z*N_e; - II2=ones(N_a*(N_j-1)*N_z*N_e,1); - exceptlastj=repmat((1:1:N_a)',(N_j-1)*N_z*N_e,1)+repmat(repelem(N_a*(0:1:N_j-2)',N_a,1),N_z*N_e,1)+repelem(N_a*N_j*(0:1:N_z*N_e-1)',N_a*(N_j-1),1); - exceptfirstj=repmat((1:1:N_a)',(N_j-1)*N_z*N_e,1)+repmat(repelem(N_a*(1:1:N_j-1)',N_a,1),N_z*N_e,1)+repelem(N_a*N_j*(0:1:N_z*N_e-1)',N_a*(N_j-1),1); - justfirstj=repmat((1:1:N_a)',N_z*N_e,1)+N_a*N_j*repelem((0:1:N_z*N_e-1)',N_a,1); - end -elseif simoptions.gridinterplayer==1 - N_probs=2; - if simoptions.fastOLG==0 - error('Cannot use simoptions.fastOLG=0 with grid interpolation layer') - elseif simoptions.fastOLG==1 - PolicyProbsPath=zeros(N_a*(N_j-1)*N_z*N_e,N_probs,T-1,'gpuArray'); % preallocate - II=repelem((1:1:N_a*(N_j-1)*N_z*N_e)',1,N_probs); - exceptlastj=repmat((1:1:N_a)',(N_j-1)*N_z*N_e,1)+repmat(repelem(N_a*(0:1:N_j-2)',N_a,1),N_z*N_e,1)+repelem(N_a*N_j*(0:1:N_z*N_e-1)',N_a*(N_j-1),1); - exceptfirstj=repmat((1:1:N_a)',(N_j-1)*N_z*N_e,1)+repmat(repelem(N_a*(1:1:N_j-1)',N_a,1),N_z*N_e,1)+repelem(N_a*N_j*(0:1:N_z*N_e-1)',N_a*(N_j-1),1); - justfirstj=repmat((1:1:N_a)',N_z*N_e,1)+N_a*N_j*repelem((0:1:N_z*N_e-1)',N_a,1); - end -end - - -%% First, go from T-1 to 1 calculating the Value function and Optimal policy function at each step. -% Since we won't need to keep the value functions for anything later we just store the current one in V -V=V_final; -for ttr=1:T-1 %so tt=T-ttr - - for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(T-ttr,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); - end - for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(T-ttr,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); - end - - if transpathoptions.zpathtrivial==0 - z_gridvals_J=transpathoptions.z_gridvals_J_T(:,:,:,T-ttr); - pi_z_J=transpathoptions.pi_z_J_T(:,:,:,T-ttr); - end - if transpathoptions.epathtrivial==0 - e_gridvals_J=transpathoptions.e_gridvals_J_T(:,:,:,T-ttr); - pi_e_J=transpathoptions.pi_e_J_T(:,:,T-ttr); - end - - [V, Policy]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e(V,n_d1,n_d2,n_a1,n_a2,n_z,n_e,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); - % The V input is next period value fn, the V output is this period. - % Policy is kept in the form where it is just a single-value in (d,a') - - PolicyIndexesPath(:,:,:,:,:,T-ttr)=Policy; -end - -%% Modify PolicyIndexesPath into forms needed for forward iteration -% Create version of PolicyIndexesPath called PolicyaprimePath, which only tracks aprime and has j=1:N_j-1 as we don't use N_j to iterate agent dist (there is no N_j+1) -% For fastOLG we use PolicyaprimejPath, if there is z then PolicyaprimejzPath -% When using grid interpolation layer also PolicyProbsPath -if isscalar(n_a1) - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,1:N_j-1,:),[N_a*N_z*N_e,N_j-1,T-1]); % aprime index -elseif length(n_a1)==2 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,1:N_j-1,:)-1),[N_a*N_z*N_e,N_j-1,T-1]); -elseif length(n_a1)==3 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,1:N_j-1,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,:,1:N_j-1,:)-1),[N_a*N_z*N_e,N_j-1,T-1]); -elseif length(n_a1)==4 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,1:N_j-1,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,:,1:N_j-1,:)-1)+prod(n_a1(1:3))*(PolicyIndexesPath(l_d+4,:,:,:,1:N_j-1,:)-1),[N_a*N_z*N_e,N_j-1,T-1]); -end -if simoptions.fastOLG==0 - PolicyaprimezPath_slowOLG=PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1),N_e,1); -elseif simoptions.fastOLG==1 - PolicyaprimePath=reshape(permute(reshape(PolicyaprimePath,[N_a,N_z*N_e,N_j-1,T-1]),[1,3,2,4]),[N_a*(N_j-1)*N_z*N_e,T-1]); - PolicyaprimejzPath=PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:(N_j-1)*N_z-1)',N_a,1),N_e,1); - if simoptions.gridinterplayer==1 - L2index=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,:,:,1:N_j-1,:),[1,N_a,N_z*N_e,N_j-1,T-1]); % PolicyIndexesPath is of size [l_d+l_aprime+1,N_a,N_z,N_e,N_j,T] - L2index=reshape(permute(L2index,[2,4,3,1,5]),[N_a*(N_j-1)*N_z*N_e,1,T-1]); - PolicyaprimejzPath=reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z*N_e,1,T-1]); % reinterpret this as lower grid index - PolicyaprimejzPath=repelem(PolicyaprimejzPath,1,2,1); % create copy that will be the upper grid index - PolicyaprimejzPath(:,2,:)=PolicyaprimejzPath(:,2,:)+1; % upper grid index - PolicyProbsPath(:,2,:)=L2index; % L2 index - PolicyProbsPath(:,2,:)=(PolicyProbsPath(:,2,:)-1)/(1+simoptions.ngridinterp); % probability of upper grid point - PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point - end -end -% Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats -PolicyValuesPath=PolicyInd2Val_FHorz_TPath(PolicyIndexesPath,[n_d1,n_d2],[n_a1,n_a2],[n_z,n_e],N_j,T-1,d_gridvals,a_gridvals,vfoptions,1,0); -PolicyValuesPath=permute(PolicyValuesPath,[2,4,3,1,5]); %[N_a,N_j,N_z*N_e,l_d+l_aprime,T-1] % fastOLG ordering is needed for AggVars - -%% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm -% Call AgentDist the current periods distn and AgentDistnext the next periods distn which we must calculate -AgentDist=AgentDist_initial; -for tt=1:T-1 - - % Get t-1 PricePath and ParamPath before we update them - if use_tminus1price==1 - for pp=1:length(tminus1priceNames) - if tt>1 - Parameters.([tminus1priceNames{pp},'_tminus1'])=Parameters.(tminus1priceNames{pp}); - else - Parameters.([tminus1priceNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1priceNames{pp}); - end - end - end - if use_tminus1params==1 - for pp=1:length(tminus1paramNames) - if tt>1 - Parameters.([tminus1paramNames{pp},'_tminus1'])=Parameters.(tminus1paramNames{pp}); - else - Parameters.([tminus1paramNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1paramNames{pp}); - end - end - end - % Get t-1 AggVars before we update them - if use_tminus1AggVars==1 - for pp=1:length(tminus1AggVarsNames) - if tt>1 - % The AggVars have not yet been updated, so they still contain previous period values - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); - else - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); - end - end - end - - % Update current PricePath and ParamPath - for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(tt,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); - end - for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); - end - - % Get t+1 PricePath - if use_tplus1price==1 - for pp=1:length(tplus1priceNames) - kk=tplus1pricePathkk(pp); - Parameters.([tplus1priceNames{pp},'_tplus1'])=PricePathOld(tt+1,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); % Make is so that the time t+1 variables can be used - end - end - - %% Get the current optimal policy, and iterate the agent dist - if transpathoptions.zepathtrivial==0 - ze_gridvals_J_fastOLG=transpathoptions.ze_gridvals_J_T_fastOLG(:,:,:,:,tt); - end - if transpathoptions.zpathtrivial==0 - z_gridvals_J_fastOLG=transpathoptions.ze_gridvals_J_fastOLG(:,:,:,tt); - if simoptions.fastOLG==0 - pi_z_J=transpathoptions.pi_z_J_T(:,:,:,tt); - else - pi_z_J_sim=transpathoptions.pi_z_J_sim_T(:,:,:,tt); - end - end - if transpathoptions.epathtrivial==0 - if simoptions.fastOLG==0 - pi_e_J=transpathoptions.pi_e_J_T(:,:,tt); - else - pi_e_J_sim=transpathoptions.pi_e_J_sim_T(:,:,tt); - end - end - - AgeWeights=AgeWeights_T(:,:,tt); % by coincidence this is for fastOLG=0,1 - - jequalOneDist=jequalOneDist_T(:,tt+1); % Note: t+1 as we are about to create the next period AgentDist - - % No specialization for ExpAsset needed here! - if simoptions.fastOLG==0 - AgentDistnext=AgentDist_FHorz_TPath_SingleStep_Iteration_e_raw(AgentDist,PolicyaprimezPath_slowOLG(:,:,tt),N_a,N_z,N_e,N_j,pi_z_J,pi_e_J,II1,II2,jequalOneDist); - else % simoptions.fastOLG==1 - if simoptions.gridinterplayer==0 - AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_e_raw(AgentDist,PolicyaprimejzPath(:,tt),N_a,N_z,N_e,N_j,pi_z_J_sim,pi_e_J_sim,II1,II2,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); - elseif simoptions.gridinterplayer==1 - AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_nProbs_e_raw(AgentDist,PolicyaprimejzPath(:,:,tt),PolicyProbsPath(:,:,tt),N_a,N_z,N_e,N_j,pi_z_J_sim,pi_e_J_sim,II,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); - end - end - - %% AggVars - % PolicyValuesPath is [N_a,N_j,N_z*N_e,l_d+l_aprime,T-1] % fastOLG ordering is needed for AggVars - AggVars=EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_ze,N_a,N_ze,a_gridvals,ze_gridvals_J_fastOLG,1); - for ff=1:length(AggVarNames) - Parameters.(AggVarNames{ff})=AggVars.(AggVarNames{ff}).Mean; - end - % Keep AggVars in the AggVarsPath - for ff=1:length(AggVarNames) - AggVarsPath(ff,tt)=AggVars.(AggVarNames{ff}).Mean; - end - - - AgentDist=AgentDistnext; -end - -end \ No newline at end of file diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m deleted file mode 100644 index 0940fead..00000000 --- a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw.m +++ /dev/null @@ -1,179 +0,0 @@ -function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_fastOLG_e_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T,l_d1,l_d2,N_d1,N_d2,n_d1,n_d2,N_a1,N_a2,n_a1,n_a2,N_z,n_z,N_e,n_e,N_j,d1_grid,d2_grid,a1_grid,a2_grid,d_gridvals,aprime_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,e_gridvals_J,pi_e_J,pi_e_J_sim,ze_gridvals_J_fastOLG,ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, II1orII, II2, exceptlastj,exceptfirstj,justfirstj, transpathoptions, vfoptions, simoptions) -% When doing shooting alogrithm on TPath FHorz PType, this is for a given ptype, and does the steps of back-iterate to get policy, then forward to get agent dist and agg vars. -% The only output is the agg vars path. - -error("these are not yet the droids you are looking for") - -AggVarsPath=zeros(length(FnsToEvaluateCell),T-1,'gpuArray'); % Note: does not include the final AggVars, might be good to add them later as a way to make if obvious to user it things are incorrect - -if N_d==0 - l_d=0; -else - l_d=length(n_d); -end -l_a=length(n_a); -l_aprime=length(n_a); -l_z=length(n_z); -l_e=length(n_e); -l_ze=l_z+l_e; -N_ze=N_z*N_e; - -%% -% fastOLG so everything is (a,j,z,e) -% Shapes: -% V is [N_a,N_j,N_z,N_e] -% AgentDist for fastOLG is [N_a*N_j*N_z,N_e] - -if vfoptions.gridinterplayer==0 - PolicyIndexesPath=zeros(l_d+l_aprime,N_a,N_j,N_z,N_e,T-1,'gpuArray'); %Periods 1 to T-1 -elseif vfoptions.gridinterplayer==1 - N_probs=2; - PolicyIndexesPath=zeros(l_d+l_aprime+1,N_a,N_j,N_z,N_e,T-1,'gpuArray'); %Periods 1 to T-1 - PolicyProbsPath=zeros(N_a*(N_j-1)*N_z*N_e,N_probs,T-1,'gpuArray'); % preallocate -end - -%% First, go from T-1 to 1 calculating the Value function and Optimal policy function at each step. -% Since we won't need to keep the value functions for anything later we just store the current one in V -V=V_final; -for ttr=1:T-1 %so tt=T-ttr - - for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(T-ttr,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); - end - for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(T-ttr,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); - end - - if transpathoptions.zpathtrivial==0 - pi_z_J=transpathoptions.pi_z_J_T(:,:,:,T-ttr); % fastOLG value function uses (j,z',z) - z_gridvals_J=transpathoptions.z_gridvals_J(:,:,T-ttr); - end - if transpathoptions.epathtrivial==0 - pi_e_J=transpathoptions.pi_e_J_T(:,1,:,T-ttr); - e_gridvals_J=transpathoptions.e_gridvals_J_T(:,:,:,:,T-ttr); - end - - [V, Policy]=ValueFnIter_FHorz_TPath_SingleStep_fastOLG_e(V,n_d,n_a,n_z,n_e,N_j,d_grid, a_grid, z_gridvals_J, e_gridvals_J, pi_z_J, pi_e_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); - % The V input is next period value fn, the V output is this period. - % Policy in fastOLG is [N_a,N_j,N_z,N_e] and contains the joint-index for (d,aprime) - - PolicyIndexesPath(:,:,:,:,:,T-ttr)=Policy; % fastOLG: so a-by-j-by-z-by-e - -end - -%% Modify PolicyIndexesPath into forms needed for forward iteration -% Create version of PolicyIndexesPath in form we want for the agent distribution iteration -% Creates PolicyaprimejzPath (omits j=N_j), and when using grid interpolation layer also PolicyProbsPath -if isscalar(n_a) - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,1:N_j-1,:,:,:),[N_a*(N_j-1)*N_z*N_e,T-1]); % aprime index -elseif length(n_a)==2 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,1:N_j-1,:,:,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,1:N_j-1,:,:,:)-1),[N_a*(N_j-1)*N_z*N_e,T-1]); -elseif length(n_a)==3 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,1:N_j-1,:,:,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,1:N_j-1,:,:,:)-1)+n_a(1)*n_a(2)*(PolicyIndexesPath(l_d+3,:,1:N_j-1,:,:,:)-1),[N_a*(N_j-1)*N_z*N_e,T-1]); -elseif length(n_a)==4 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,1:N_j-1,:,:,:)+n_a(1)*(PolicyIndexesPath(l_d+2,:,1:N_j-1,:,:,:)-1)+n_a(1)*n_a(2)*(PolicyIndexesPath(l_d+3,:,1:N_j-1,:,:,:)-1)+n_a(1)*n_a(2)*n_a(3)*(PolicyIndexesPath(l_d+4,:,1:N_j-1,:,:,:)-1),[N_a*(N_j-1)*N_z*N_e,T-1]); -end -PolicyaprimejzPath=PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:(N_j-1)*N_z-1)',N_a,1),N_e,1); -if simoptions.gridinterplayer==1 - L2index=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,1:N_j-1,:,:,:),[N_a*(N_j-1)*N_z*N_e,1,T-1]); % PolicyIndexesPath is of size [l_d+l_aprime+1,N_a,N_j,N_z,N_e,T-1] - PolicyaprimejzPath=reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z*N_e,1,T-1]); % reinterpret this as lower grid index - PolicyaprimejzPath=repelem(PolicyaprimejzPath,1,2,1); % create copy that will be the upper grid index - PolicyaprimejzPath(:,2,:)=PolicyaprimejzPath(:,2,:)+1; % upper grid index - PolicyProbsPath(:,2,:)=L2index; % L2 index - PolicyProbsPath(:,2,:)=(PolicyProbsPath(:,2,:)-1)/(1+simoptions.ngridinterp); % probability of upper grid point - PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point -end -% Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats -PolicyValuesPath=PolicyInd2Val_FHorz_TPath(PolicyIndexesPath,n_d,n_a,[n_z,n_e],N_j,T-1,d_gridvals,aprime_gridvals,vfoptions,1,1); % [size(PolicyValuesPath,1),N_a,N_j,N_z*N_e,T] -PolicyValuesPath=permute(PolicyValuesPath,[2,3,4,1,5]); %[N_a,N_j,N_z*N_e,l_d+l_aprime,T-1] % fastOLG ordering is needed for AggVars - - -%% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm -% Call AgentDist the current periods distn and AgentDistnext the next periods distn which we must calculate -AgentDist=AgentDist_initial; -for tt=1:T-1 - - % Get t-1 PricePath and ParamPath before we update them - if use_tminus1price==1 - for pp=1:length(tminus1priceNames) - if tt>1 - Parameters.([tminus1priceNames{pp},'_tminus1'])=Parameters.(tminus1priceNames{pp}); - else - Parameters.([tminus1priceNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1priceNames{pp}); - end - end - end - if use_tminus1params==1 - for pp=1:length(tminus1paramNames) - if tt>1 - Parameters.([tminus1paramNames{pp},'_tminus1'])=Parameters.(tminus1paramNames{pp}); - else - Parameters.([tminus1paramNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1paramNames{pp}); - end - end - end - % Get t-1 AggVars before we update them - if use_tminus1AggVars==1 - for pp=1:length(tminus1AggVarsNames) - if tt>1 - % The AggVars have not yet been updated, so they still contain previous period values - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); - else - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); - end - end - end - - % Update current PricePath and ParamPath - for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(tt,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); - end - for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); - end - - % Get t+1 PricePath - if use_tplus1price==1 - for pp=1:length(tplus1priceNames) - kk=tplus1pricePathkk(pp); - Parameters.([tplus1priceNames{pp},'_tplus1'])=PricePathOld(tt+1,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); % Make is so that the time t+1 variables can be used - end - end - - %% Get the current optimal policy, and iterate the agent dist - if transpathoptions.zepathtrivial==0 - ze_gridvals_J_fastOLG=transpathoptions.ze_gridvals_J_T_fastOLG(:,:,:,:,tt); - end - if transpathoptions.zpathtrivial==0 - pi_z_J_sim=transpathoptions.pi_z_J_sim_T(:,:,:,tt); - end - if transpathoptions.epathtrivial==0 - pi_e_J_sim=transpathoptions.pi_e_J_sim_T(:,:,tt); % (a,j,z)-by-e - end - - jequalOneDist=jequalOneDist_T(:,tt+1); % Note: t+1 as we are about to create the next period AgentDist - - AgeWeights=AgeWeights_T(:,:,tt); % by coincidence this is for fastOLG=0,1 - - % simoptions.fastOLG=1 is hardcoded - if simoptions.gridinterplayer==0 - AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_e_raw(AgentDist,PolicyaprimejzPath(:,tt),N_a,N_z,N_e,N_j,pi_z_J_sim, pi_e_J_sim,II1orII,II2,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); % II1orII is II1 - elseif simoptions.gridinterplayer==1 - AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_nProbs_e_raw(AgentDist,PolicyaprimejzPath(:,:,tt),PolicyProbsPath(:,:,tt),N_a,N_z,N_e,N_j,pi_z_J_sim,pi_e_J_sim,II1orII,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); % II1orII is II - end - - %% AggVars - AggVars=EvalFnOnAgentDist_AggVars_FHorz_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_ze,N_a,N_ze,a_gridvals,ze_gridvals_J_fastOLG,1); - for ff=1:length(AggVarNames) - Parameters.(AggVarNames{ff})=AggVars.(AggVarNames{ff}).Mean; - end - % Keep AggVars in the AggVarsPath - for ff=1:length(AggVarNames) - AggVarsPath(ff,tt)=AggVars.(AggVarNames{ff}).Mean; - end - - AgentDist=AgentDistnext; -end - - -end diff --git a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m b/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m deleted file mode 100644 index 7b7a6db3..00000000 --- a/TransitionPaths/FHorz/PType/ExpAsset/singlepath/TransitionPath_FHorz_PType_ExpAsset_singlepath_raw.m +++ /dev/null @@ -1,204 +0,0 @@ -function AggVarsPath=TransitionPath_FHorz_PType_ExpAsset_singlepath_raw(PricePathOld, ParamPath, PricePathNames,ParamPathNames,T,V_final,AgentDist_initial,jequalOneDist_T,AgeWeights_T, ... - l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,N_j,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_gridvals_J, pi_z_J,pi_z_J_sim,z_gridvals_J_fastOLG, ... - ReturnFn, aprimeFn, FnsToEvaluateCell, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, PricePathSizeVec, ParamPathSizeVec, ... - use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, ... - II1orII, II2, exceptlastj,exceptfirstj,justfirstj, ... - transpathoptions, vfoptions, simoptions) -% When doing shooting alogrithm on TPath FHorz PType, this is for a given ptype, and does the steps of back-iterate to get policy, then forward to get agent dist and agg vars. -% The only output is the agg vars path. - -AggVarsPath=zeros(length(FnsToEvaluateCell),T-1,'gpuArray'); % Note: does not include the final AggVars, might be good to add them later as a way to make if obvious to user it things are incorrect - -N_a1=prod(n_a1); -N_a2=prod(n_a2); -N_a=N_a1*N_a2; - -l_aprime=length(n_a1); -l_a=l_aprime+length(n_a2); -l_z=length(n_z); - -%% -% Shapes: -% V is [N_a,N_z,N_j] -% AgentDist for basic is [N_a*N_z,N_j] -% AgentDist for fastOLG is [N_a*N_j*N_z,1] - -if vfoptions.gridinterplayer==0 - PolicyIndexesPath=zeros(l_d+l_aprime,N_a,N_z,N_j,T-1,'gpuArray'); %Periods 1 to T-1 -elseif vfoptions.gridinterplayer==1 - PolicyIndexesPath=zeros(l_d+l_aprime+1,N_a,N_z,N_j,T-1,'gpuArray'); %Periods 1 to T-1 -end -if simoptions.gridinterplayer==0 - if simoptions.fastOLG==0 - II1=1:1:N_a*N_z; - II2=ones(N_a*N_z,1); - elseif simoptions.fastOLG==1 - II1=1:1:N_a*(N_j-1)*N_z; - II2=ones(N_a*(N_j-1)*N_z,1); - exceptlastj=repmat((1:1:N_a)',(N_j-1)*N_z,1)+repmat(repelem(N_a*(0:1:N_j-2)',N_a,1),N_z,1)+repelem(N_a*N_j*(0:1:N_z-1)',N_a*(N_j-1),1); - exceptfirstj=repmat((1:1:N_a)',(N_j-1)*N_z,1)+repmat(repelem(N_a*(1:1:N_j-1)',N_a,1),N_z,1)+repelem(N_a*N_j*(0:1:N_z-1)',N_a*(N_j-1),1); - justfirstj=repmat((1:1:N_a)',N_z,1)+N_a*N_j*repelem((0:1:N_z-1)',N_a,1); - end -elseif simoptions.gridinterplayer==1 - N_probs=2; - if simoptions.fastOLG==0 - error('Cannot use simoptions.fastOLG=0 with grid interpolation layer') - elseif simoptions.fastOLG==1 - PolicyProbsPath=zeros(N_a*(N_j-1)*N_z,N_probs,T-1,'gpuArray'); % preallocate - II=repelem((1:1:N_a*(N_j-1)*N_z)',1,N_probs); - exceptlastj=repmat((1:1:N_a)',(N_j-1)*N_z,1)+repmat(repelem(N_a*(0:1:N_j-2)',N_a,1),N_z,1)+repelem(N_a*N_j*(0:1:N_z-1)',N_a*(N_j-1),1); - exceptfirstj=repmat((1:1:N_a)',(N_j-1)*N_z,1)+repmat(repelem(N_a*(1:1:N_j-1)',N_a,1),N_z,1)+repelem(N_a*N_j*(0:1:N_z-1)',N_a*(N_j-1),1); - justfirstj=repmat((1:1:N_a)',N_z,1)+N_a*N_j*repelem((0:1:N_z-1)',N_a,1); - end -end - - -%% First, go from T-1 to 1 calculating the Value function and Optimal policy function at each step. -% Since we won't need to keep the value functions for anything later we just store the current one in V -V=V_final; -for ttr=1:T-1 %so tt=T-ttr - - for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(T-ttr,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); - end - for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(T-ttr,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); - end - - if transpathoptions.zpathtrivial==0 - z_gridvals_J=transpathoptions.z_gridvals_J_T(:,:,:,T-ttr); - pi_z_J=transpathoptions.pi_z_J_T(:,:,:,T-ttr); - end - - [V, Policy]=ValueFnIter_FHorz_TPath_SingleStep_ExpAsset(V,n_d1,n_d2,n_a1,n_a2,n_z,N_j, d_gridvals, d2_grid, a1_gridvals, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); - % The V input is next period value fn, the V output is this period. - % Policy is kept in the form where it is just a single-value in (d,a') - - PolicyIndexesPath(:,:,:,:,T-ttr)=Policy; -end - -%% Modify PolicyIndexesPath into forms needed for forward iteration -% Create version of PolicyIndexesPath called PolicyaprimePath, which only tracks aprime and has j=1:N_j-1 as we don't use N_j to iterate agent dist (there is no N_j+1) -% For fastOLG we use PolicyaprimejPath, if there is z then PolicyaprimejzPath -% When using grid interpolation layer also PolicyProbsPath -if isscalar(n_a1) - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:),[N_a*N_z,N_j-1,T-1]); % aprime index -elseif length(n_a1)==2 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); -elseif length(n_a1)==3 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); -elseif length(n_a1)==4 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,1:N_j-1,:)-1)+prod(n_a1(1:3))*(PolicyIndexesPath(l_d+4,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); -end -if simoptions.fastOLG==0 - PolicyaprimezPath_slowOLG=PolicyaprimePath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1); -elseif simoptions.fastOLG==1 - PolicyaprimePath=reshape(permute(reshape(PolicyaprimePath,[N_a,N_z,N_j-1,T-1]),[1,3,2,4]),[N_a*(N_j-1)*N_z,T-1]); - PolicyaprimejzPath=PolicyaprimePath+repelem(N_a*gpuArray(0:1:(N_j-1)*N_z-1)',N_a,1); - if simoptions.gridinterplayer==1 - L2index=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,:,1:N_j-1,:),[1,N_a,N_z,N_j-1,T-1]); % PolicyIndexesPath is of size [l_d+l_aprime+1,N_a,N_z,N_j,T] - L2index=reshape(permute(L2index,[2,4,3,1,5]),[N_a*(N_j-1)*N_z,1,T-1]); - PolicyaprimejzPath=reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z,1,T-1]); % reinterpret this as lower grid index - PolicyaprimejzPath=repelem(PolicyaprimejzPath,1,2,1); % create copy that will be the upper grid index - PolicyaprimejzPath(:,2,:)=PolicyaprimejzPath(:,2,:)+1; % upper grid index - PolicyProbsPath(:,2,:)=L2index; % L2 index - PolicyProbsPath(:,2,:)=(PolicyProbsPath(:,2,:)-1)/(1+simoptions.ngridinterp); % probability of upper grid point - PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point - end -end -% Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats -PolicyValuesPath=PolicyInd2Val_FHorz_TPath(PolicyIndexesPath,[n_d1,n_d2],[n_a1,n_a2],n_z,N_j,T-1,d_gridvals,a_gridvals,vfoptions,1,0); -PolicyValuesPath=permute(PolicyValuesPath,[2,4,3,1,5]); %[N_a,N_j,N_z,l_d+l_aprime,T-1] % fastOLG ordering is needed for AggVars - -%% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm -% Call AgentDist the current periods distn and AgentDistnext the next periods distn which we must calculate -AgentDist=AgentDist_initial; -for tt=1:T-1 - - % Get t-1 PricePath and ParamPath before we update them - if use_tminus1price==1 - for pp=1:length(tminus1priceNames) - if tt>1 - Parameters.([tminus1priceNames{pp},'_tminus1'])=Parameters.(tminus1priceNames{pp}); - else - Parameters.([tminus1priceNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1priceNames{pp}); - end - end - end - if use_tminus1params==1 - for pp=1:length(tminus1paramNames) - if tt>1 - Parameters.([tminus1paramNames{pp},'_tminus1'])=Parameters.(tminus1paramNames{pp}); - else - Parameters.([tminus1paramNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1paramNames{pp}); - end - end - end - % Get t-1 AggVars before we update them - if use_tminus1AggVars==1 - for pp=1:length(tminus1AggVarsNames) - if tt>1 - % The AggVars have not yet been updated, so they still contain previous period values - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); - else - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); - end - end - end - - % Update current PricePath and ParamPath - for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(tt,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); - end - for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); - end - - % Get t+1 PricePath - if use_tplus1price==1 - for pp=1:length(tplus1priceNames) - kk=tplus1pricePathkk(pp); - Parameters.([tplus1priceNames{pp},'_tplus1'])=PricePathOld(tt+1,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); % Make is so that the time t+1 variables can be used - end - end - - %% Get the current optimal policy, and iterate the agent dist - if transpathoptions.zpathtrivial==0 - z_gridvals_J_fastOLG=transpathoptions.ze_gridvals_J_fastOLG(:,:,:,tt); - if simoptions.fastOLG==0 - pi_z_J=transpathoptions.pi_z_J_T(:,:,:,tt); - else - pi_z_J_sim=transpathoptions.pi_z_J_sim_T(:,:,:,tt); - end - end - - AgeWeights=AgeWeights_T(:,:,tt); % by coincidence this is for fastOLG=0,1 - - jequalOneDist=jequalOneDist_T(:,tt+1); % Note: t+1 as we are about to create the next period AgentDist - - % No specialization for ExpAsset needed here! - if simoptions.fastOLG==0 - AgentDistnext=AgentDist_FHorz_TPath_SingleStep_Iteration_raw(AgentDist,PolicyaprimezPath_slowOLG(:,:,tt),N_a,N_z,N_j,pi_z_J,II1,II2,jequalOneDist); - else % simoptions.fastOLG==1 - if simoptions.gridinterplayer==0 - AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_raw(AgentDist,PolicyaprimejzPath(:,tt),N_a,N_z,N_j,pi_z_J_sim,II1,II2,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); - elseif simoptions.gridinterplayer==1 - AgentDistnext=AgentDist_FHorz_TPath_SingleStep_IterFast_nProbs_raw(AgentDist,PolicyaprimejzPath(:,:,tt),PolicyProbsPath(:,:,tt),N_a,N_z,N_j,pi_z_J_sim,II,exceptlastj,exceptfirstj,justfirstj,jequalOneDist); - end - end - - %% AggVars - AggVars=EvalFnOnAgentDist_AggVars_FHorz_ExpAsset_fastOLG(AgentDist.*AgeWeights, PolicyValuesPath(:,:,:,1:l_d,tt), PolicyValuesPath(:,:,:,l_d+1:end,tt), FnsToEvaluateCell,FnsToEvaluateParamNames,AggVarNames,Parameters,N_j,l_d,l_aprime,l_a,l_z,N_a,N_z,a_gridvals,z_gridvals_J_fastOLG,1); - for ff=1:length(AggVarNames) - Parameters.(AggVarNames{ff})=AggVars.(AggVarNames{ff}).Mean; - end - % Keep AggVars in the AggVarsPath - for ff=1:length(AggVarNames) - AggVarsPath(ff,tt)=AggVars.(AggVarNames{ff}).Mean; - end - - - AgentDist=AgentDistnext; -end - -end \ No newline at end of file From 8c0bc8884ce4ccfd2fd1540755eababd2ab51e35 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sun, 3 May 2026 17:18:54 +1200 Subject: [PATCH 50/90] Fixes and cleanups Fix problems with tminus1, exp asset being silently ignored in agg var evaluation, and clean up some typos. --- .../SubCodes/EvalFnOnAgentDist_AggVars_FHorz_fastOLG.m | 2 ++ SubCodes/ParameterStructure/CreateVectorFromParams.m | 4 ++-- .../FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m | 2 +- ValueFnIter/FHorz/ValueFnIter_Case1_FHorz.m | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/EvalFnOnAgentDist_AggVars_FHorz_fastOLG.m b/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/EvalFnOnAgentDist_AggVars_FHorz_fastOLG.m index 17d64629..5b37bd94 100644 --- a/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/EvalFnOnAgentDist_AggVars_FHorz_fastOLG.m +++ b/EvaluateFnOnAgentDist/TransPathFHorz/SubCodes/EvalFnOnAgentDist_AggVars_FHorz_fastOLG.m @@ -253,6 +253,8 @@ Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1),PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1),PolicyValues_aprime(:,:,:,2),PolicyValues_aprime(:,:,:,3), a_gridvals(:,1),a_gridvals(:,2), z_gridvals_J_fastOLG(1,:,:,1),z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); elseif l_a==3 Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1),PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1),PolicyValues_aprime(:,:,:,2),PolicyValues_aprime(:,:,:,3), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3), z_gridvals_J_fastOLG(1,:,:,1),z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); + elseif l_a==4 + Values=arrayfun(FnsToEvaluate{ff}, PolicyValues_d(:,:,:,1),PolicyValues_d(:,:,:,2), PolicyValues_aprime(:,:,:,1),PolicyValues_aprime(:,:,:,2),PolicyValues_aprime(:,:,:,3), a_gridvals(:,1),a_gridvals(:,2),a_gridvals(:,3),a_gridvals(:,4), z_gridvals_J_fastOLG(1,:,:,1),z_gridvals_J_fastOLG(1,:,:,2), ParamCell{:}); end end elseif l_d==3 diff --git a/SubCodes/ParameterStructure/CreateVectorFromParams.m b/SubCodes/ParameterStructure/CreateVectorFromParams.m index ca52b279..964911d5 100644 --- a/SubCodes/ParameterStructure/CreateVectorFromParams.m +++ b/SubCodes/ParameterStructure/CreateVectorFromParams.m @@ -47,7 +47,7 @@ end end if found==0 % Have added this check so that user can see if they are missing a parameter - fprintf(['FAILED TO FIND PARAMETER ',ParamNames{iCalibParam}]) + warning(['FAILED TO FIND PARAMETER ',ParamNames{iCalibParam}]) end end elseif nargin==4 @@ -69,7 +69,7 @@ end end if found==0 % Have added this check so that user can see if they are missing a parameter - fprintf(['FAILED TO FIND PARAMETER ',ParamNames{iCalibParam}]) + warning(['FAILED TO FIND PARAMETER ',ParamNames{iCalibParam}]) end end end diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m index 91fbbb03..a582d0b7 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m @@ -320,7 +320,7 @@ Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); end - % Update current AggVars [we have to add this as GE conditions are in a seperate tt loop to the AggVars] + % Update current AggVars [we have to add this as GE conditions are in a separate tt loop to the AggVars] for ff=1:length(FullAggVarNames) Parameters.(FullAggVarNames{ff})=AggVarsPooledPath(ff,tt); end diff --git a/ValueFnIter/FHorz/ValueFnIter_Case1_FHorz.m b/ValueFnIter/FHorz/ValueFnIter_Case1_FHorz.m index 9845769d..6ec1c3e3 100644 --- a/ValueFnIter/FHorz/ValueFnIter_Case1_FHorz.m +++ b/ValueFnIter/FHorz/ValueFnIter_Case1_FHorz.m @@ -405,7 +405,7 @@ if isfield(vfoptions,'n_semiz') [V,Policy]=ValueFnIter_FHorz_ExpAssetSemiExo(n_d1,n_d2,n_d3,n_a1,n_a2,n_z,vfoptions.n_semiz, N_j, d1_grid , d2_grid, d3_grid, a1_grid, a2_grid, z_gridvals_J, vfoptions.semiz_gridvals_J, pi_z_J, vfoptions.pi_semiz_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); else - [V,Policy]=ValueFnIter_FHorz_ExpAsset(n_d1,n_d2,n_a1,n_a2,n_z, N_j, d1_grid , d2_grid, a1_grid, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + [V,Policy]=ValueFnIter_FHorz_ExpAsset(n_d1,n_d2,n_a1,n_a2,n_z, N_j, d1_grid, d2_grid, a1_grid, a2_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); end elseif vfoptions.experienceassetu==1 if isfield(vfoptions,'n_semiz') From ccc246f602f79ac21769da2732512fb79d6a0bbd Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Thu, 7 May 2026 16:36:03 +1200 Subject: [PATCH 51/90] Establish MixHorz changes Start by adding MixHorz changes. Then we will merge and harmonize from there. --- .../StationaryDist_Case1_MixHorz_PType.m | 253 ++++++++++++++++++ ...onPath_FHorz_substeps_Step2_AdjustPolicy.m | 2 +- ...ath_FHorz_substeps_Step3tt_IterAgentDist.m | 4 + ...nPath_InfHorz_substeps_Step1_ValueFnIter.m | 1 - ...Path_InfHorz_substeps_Step2_AdjustPolicy.m | 2 +- ...entDist_InfHorz_TPath_SingleStep_AggVars.m | 3 + .../TransitionPath_Case1_MixHorz_PType.m | 1 - 7 files changed, 262 insertions(+), 4 deletions(-) create mode 100644 StationaryDist/MixHorz/StationaryDist_Case1_MixHorz_PType.m diff --git a/StationaryDist/MixHorz/StationaryDist_Case1_MixHorz_PType.m b/StationaryDist/MixHorz/StationaryDist_Case1_MixHorz_PType.m new file mode 100644 index 00000000..8fe5bd0f --- /dev/null +++ b/StationaryDist/MixHorz/StationaryDist_Case1_MixHorz_PType.m @@ -0,0 +1,253 @@ +function StationaryDist=StationaryDist_Case1_MixHorz_PType(jequaloneDist,AgeWeightsParamNames,PTypeDistParamNames,Policy,n_d,n_a,n_z,N_j,Names_i,pi_z,Parameters,simoptions) +% Allows for different permanent (fixed) types of agent. +% See ValueFnIter_Case1_FHorz_PType for general idea. +% +% simoptions.verbose=1 will give feedback +% simoptions.verboseparams=1 will give further feedback on the param values of each permanent type +% +% jequaloneDist can either be same for all permanent types, or must be passed as a structure. +% AgeWeightParamNames is either same for all permanent types, or must be passed as a structure. +% +% +% How exactly to handle these differences between permanent (fixed) types +% is to some extent left to the user. You can, for example, input +% parameters that differ by permanent type as a vector with different rows f +% for each type, or as a structure with different fields for each type. +% +% Any input that does not depend on the permanent type is just passed in +% exactly the same form as normal. + +% Names_i can either be a cell containing the 'names' of the different +% permanent types, or if there are no structures used (just parameters that +% depend on permanent type and inputted as vectors or matrices as appropriate) +% then Names_i can just be the number of permanent types (but does not have to be, can still be names). +if iscell(Names_i) + N_i=length(Names_i); +else + N_i=Names_i; % It is the number of PTypes (which have not been given names) + Names_i={'ptype001'}; + for ii=2:N_i + if ii<10 + Names_i{ii}=['ptype00',num2str(ii)]; + elseif ii<100 + Names_i{ii}=['ptype0',num2str(ii)]; + elseif ii<1000 + Names_i{ii}=['ptype',num2str(ii)]; + end + end +end + +%% Setup simoptions a little (needs to be done here so for-loop can be parallelized) +if ~exist('simoptions','var') + simoptions=struct(); % defaults are filled in later +end +if ~isfield(simoptions,'verbose') + simoptions.verbose=0; +end +if ~isfield(simoptions,'verboseparams') + simoptions.verboseparams=0; +end +if ~isfield(simoptions,'ptypestorecpu') + simoptions.ptypestorecpu=0; % GPU memory is limited, so switch solutions to the cpu. Off by default. +end + +%% Check inputs +if abs(sum(Parameters.(PTypeDistParamNames{1}))-1)>10^(-15) + warning('The permanent type mass weights must sum to one (PTypeDistParamNames points to weights that do not sum to one)') +end + +%% Deal with jequaloneDist +[jequaloneDist,idiminj1dist,Parameters]=jequaloneDist_PType(jequaloneDist,Parameters,simoptions,n_a,n_z,N_i,Names_i,PTypeDistParamNames,0); + + +%% +for ii=1:N_i + % First set up simoptions + simoptions_temp=PType_Options(simoptions,Names_i,ii); + + if simoptions_temp.verbose==1 + fprintf('Permanent type: %i of %i \n',ii, N_i) + end + + + Policy_temp=Policy.(Names_i{ii}); + + % Go through everything which might be dependent on permanent type (PType) + % Notice that the way this is coded the grids (etc.) could be either + % fixed, or a function (that depends on age, and possibly on permanent + % type), or they could be a structure. Only in the case where they are + % a structure is there a need to take just a specific part and send + % only that to the 'non-PType' version of the command. + if isa(n_d,'struct') + n_d_temp=n_d.(Names_i{ii}); + else + n_d_temp=n_d; + end + if isa(n_a,'struct') + n_a_temp=n_a.(Names_i{ii}); + else + n_a_temp=n_a; + end + if isa(n_z,'struct') + n_z_temp=n_z.(Names_i{ii}); + else + n_z_temp=n_z; + end + if isa(N_j,'struct') + N_j_temp=N_j.(Names_i{ii}); + else + N_j_temp=N_j; + end + + %% Exogenous shocks + if isstruct(pi_z) + pi_z_temp=pi_z.(Names_i{ii}); + else + nn=size(pi_z,ndims(pi_z)); + if nn==N_i + otherdims = repmat({':'},1,ndims(pi_z)-1); + pi_z_temp=pi_z(otherdims{:},ii); + else + pi_z_temp=pi_z; + end + end + + % e + if isfield(simoptions_temp,'n_e') + % If vfoptions_temp.pi_semiz is a structure that was already dealt with by PType_Options() command + if ~isstruct(simoptions.pi_e) + % So just need to check if last dimension is of length N_i + nn=size(simoptions_temp.pi_e,ndims(simoptions_temp.pi_e)); + if nn==N_i + otherdims = repmat({':'},1,ndims(simoptions_temp.pi_e)-1); + simoptions_temp.pi_e=simoptions_temp.pi_e(otherdims{:},ii); + end + end + end + + % semiz + if isfield(simoptions_temp,'n_semiz') + % Might use SemiExoShockFn or pi_semiz, if the later we need to deal with it + if isfield(simoptions_temp,'pi_semiz') + % If simoptions_temp.pi_semiz is a structure that was already dealt with by PType_Options() command + if ~isstruct(simoptions.pi_semiz) + % So just need to check if last dimension is of length N_i + nn=size(simoptions_temp.pi_semiz,ndims(simoptions_temp.pi_semiz)); + if nn==N_i + otherdims = repmat({':'},1,ndims(simoptions_temp.pi_semiz)-1); + simoptions_temp.pi_semiz=simoptions_temp.pi_semiz(otherdims{:},ii); + end + end + else + % To evaluate SemiExoShockFn, we still require semiz_grid + % If vfoptions_temp.semiz_grid is a structure that was already dealt with by PType_Options() command + if ~isstruct(simoptions.semiz_grid) + % So just need to check if last dimension is of length N_i + nn=size(simoptions_temp.semiz_grid,ndims(simoptions_temp.semiz_grid)); + if nn==N_i + otherdims = repmat({':'},1,ndims(simoptions_temp.semiz_grid)-1); + simoptions_temp.semiz_grid=simoptions_temp.semiz_grid(otherdims{:},ii); + end + end + end + end + + %% Parameters + % Parameters are allowed to be given as structure, or as vector/matrix + % (in terms of their dependence on fixed type). So go through each of + % these in term. + Parameters_temp=Parameters; + FullParamNames=fieldnames(Parameters); + nFields=length(FullParamNames); + for kField=1:nFields + if isa(Parameters.(FullParamNames{kField}), 'struct') % Check for permanent type in structure form + names=fieldnames(Parameters.(FullParamNames{kField})); + for jj=1:length(names) + if strcmp(names{jj},Names_i{ii}) + Parameters_temp.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(names{jj}); + end + end + elseif any(size(Parameters.(FullParamNames{kField}))==N_i) % Check for permanent type in vector/matrix form. + temp=Parameters.(FullParamNames{kField}); + [~,ptypedim]=max(size(Parameters.(FullParamNames{kField}))==N_i); % Parameters as vector/matrix can be at most two dimensional, figure out which relates to PType. + if ptypedim==1 + Parameters_temp.(FullParamNames{kField})=temp(ii,:); + elseif ptypedim==2 + Parameters_temp.(FullParamNames{kField})=temp(:,ii); + end + end + end + + if simoptions_temp.verboseparams==1 + sprintf('Parameter values for the current permanent type') + Parameters_temp + end + + %% jequaloneDist + if isa(jequaloneDist,'struct') + if isfield(jequaloneDist,Names_i{ii}) + jequaloneDist_temp=jequaloneDist.(Names_i{ii}); + % jequaloneDist_temp must be of mass one for the codes to work. + if abs(sum(jequaloneDist_temp(:))-1)>10^(-15) % jequaloneDist_temp(:))~=1, but allowing for small numerical errors + fprintf('Info for following error: sum(jequaloneDist_temp(:))-1=%8.16f (should be zero) \n', sum(jequaloneDist_temp(:))-1) + error(['The jequaloneDist must be of mass one for each type i (it is not for type ',Names_i{ii}]) + end + else + if isfinite(N_j_temp) + error(['You must input a jequaloneDist for permanent type ', Names_i{ii}, ' \n']) + end + end + else + % Note: when jequaloneDist is not a structure all ptypes must have the same grids + if idiminj1dist==0 % ptype is not a dimension + jequaloneDist_temp=jequaloneDist; + else % idminj1dist==1 + % ptype is a dimension, so need to get the jequaloneDist for ii and also normalize mass conditional on ptype to be one + if ndims(jequaloneDist)==5 % has all three of semiz,z,e [other two are a and i] + jequaloneDist_temp=jequaloneDist(:,:,:,:,ii)/sum(sum(jequaloneDist(:,:,:,:,ii))); % includes renormalizing so mass of one conditional on ptype + elseif ndims(jequaloneDist)==4 % has two of semiz,z,e + jequaloneDist_temp=jequaloneDist(:,:,:,ii)/sum(sum(jequaloneDist(:,:,:,ii))); % includes renormalizing so mass of one conditional on ptype + elseif ndims(jequaloneDist)==3 % has one of semiz,z,e + jequaloneDist_temp=jequaloneDist(:,:,ii)/sum(sum(jequaloneDist(:,:,ii))); % includes renormalizing so mass of one conditional on ptype + elseif ndims(jequaloneDist)==2 % has none of semiz,z,e + jequaloneDist_temp=jequaloneDist(:,ii)/sum(jequaloneDist(:,ii)); % includes renormalizing so mass of one conditional on ptype + end + end + if abs(sum(jequaloneDist_temp(:))-1)>10^(-12) + error(['The jequaloneDist must be of mass one for each type i (it is not for type ',Names_i{ii}, ' \n']) + end + end + + %% + AgeWeightParamNames_temp=AgeWeightsParamNames; + if isa(AgeWeightsParamNames,'struct') + if isfield(AgeWeightsParamNames,Names_i{ii}) + AgeWeightParamNames_temp=AgeWeightsParamNames.(Names_i{ii}); + else + if isfinite(N_j_temp) + error(['You must input AgeWeightParamNames for permanent type ', Names_i{ii}, ' \n']) + end + end + end + + if isfinite(N_j_temp) + 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); + else % PType actually allows for infinite horizon as well + StationaryDist_ii=StationaryDist_Case1(Policy_temp,n_d_temp,n_a_temp,n_z_temp,pi_z_temp,simoptions_temp,Parameters_temp); % EntryExitParams not yet supported (is on my to-do list) + end + + if simoptions_temp.ptypestorecpu==1 + StationaryDist.(Names_i{ii})=gather(StationaryDist_ii); + else + StationaryDist.(Names_i{ii})=StationaryDist_ii; + end + +end + +if length(Parameters.(PTypeDistParamNames{:}))==N_i + StationaryDist.ptweights=reshape(Parameters.(PTypeDistParamNames{:}),[],1); % reshape is to make sure this is a column vector +else + error('Parameter for PTypeDistParamNames does not have the same number of permanent types as N_i/Names_i \n') +end + +end diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m index 1aac0b18..93ef9057 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m @@ -519,7 +519,7 @@ end end -if N_probs==1 % =1 means not being used +if isempty(N_probs) || N_probs==1 % =1 means not being used PolicyProbsPath=[]; end diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step3tt_IterAgentDist.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step3tt_IterAgentDist.m index d99319d4..be387211 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step3tt_IterAgentDist.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step3tt_IterAgentDist.m @@ -15,6 +15,10 @@ end end +if isempty(N_probs) + N_probs=0; +end + if N_z==0 && N_e==0 if simoptions.fastOLG==0 % PolicyaprimePath_slowOLG diff --git a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m index 7e322866..1f1484d1 100644 --- a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m +++ b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m @@ -45,7 +45,6 @@ end [V, Policy]=ValueFnIter_InfHorz_TPath_SingleStep(V,n_d,n_a,n_z,d_gridvals, a_grid, z_gridvals, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); - % The V input is next period value fn, the V output is this period. % Policy is kept in the form where it is just a single-value in (d,a') diff --git a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m index 8af203b4..34985346 100644 --- a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m @@ -257,7 +257,7 @@ end -if N_probs==1 % =1 means not being used +if isempty(N_probs) || N_probs==1 % =1 means not being used PolicyProbsPath=[]; end diff --git a/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m b/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m index 8ed6f9ac..d856ced2 100644 --- a/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m +++ b/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m @@ -20,6 +20,9 @@ Values=reshape(Values,[N_a*N_z,1]); % When evaluating value function (which may sometimes give -Inf values) on StationaryDistVec (which at those points will be 0) we get 'NaN'. Use temp as intermediate variable just eliminate those. temp=Values.*AgentDist; + if issparse(temp) + temp=gather(temp); + end val=sum(temp(~isnan(temp))); if outputastruct==0 diff --git a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m index 6909baa6..dede3528 100644 --- a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m @@ -988,7 +988,6 @@ GEcondnPath.(GEeqnNames{gg})=GEcondnPathmatrix(:,gg)'; end - if nargout==1 varargout={PricePathStruct}; elseif nargout==2 From d8823b832321f66d04b440c7229cde78ed8a5292 Mon Sep 17 00:00:00 2001 From: robertdkirkby Date: Mon, 4 May 2026 12:07:14 +1200 Subject: [PATCH 52/90] Version 2.5: switch to _InfHorz --- .../EvalFnOnAgentDist_ProbDensityFn_InfHorz.m | 169 ------------------ .../ValueFnIter_InfHorz_EpsteinZin.m | 2 + ValueFnIter/InfHorz/ValueFnIter_Case1.m | 2 +- .../TransPath/ValueFnOnTransPath_InfHorz.m | 4 + 4 files changed, 7 insertions(+), 170 deletions(-) delete mode 100644 EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_ProbDensityFn_InfHorz.m diff --git a/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_ProbDensityFn_InfHorz.m b/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_ProbDensityFn_InfHorz.m deleted file mode 100644 index 7d6f7e5e..00000000 --- a/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_ProbDensityFn_InfHorz.m +++ /dev/null @@ -1,169 +0,0 @@ -function ProbDensityFns=EvalFnOnAgentDist_ProbDensityFn_InfHorz(StationaryDist, Policy, FnsToEvaluate, Parameters, FnsToEvaluateParamNames, n_d, n_a, n_z, d_grid, a_grid, z_grid,simoptions,EntryExitParamNames) -% Evaluates the aggregate value (weighted sum/integral) for each element of FnsToEvaluate -% -% Parallel, simoptions and EntryExitParamNames are optional inputs, only needed when using endogenous entry - -%% -Parallel=1+(gpuDeviceCount>0); -if ~isfield(simoptions,'gridinterplayer') - simoptions.gridinterplayer=0; -end - -if n_d(1)==0 - l_d=0; -else - l_d=length(n_d); -end -l_a=length(n_a); -l_z=length(n_z); - -N_a=prod(n_a); -N_z=prod(n_z); - -l_daprime=size(Policy,1); -if simoptions.gridinterplayer==1 - l_daprime=l_daprime-1; -end - -%% -if isstruct(StationaryDist) - % Even though Mass is unimportant, still need to deal with 'exit' in PolicyIndexes. - ProbDensityFns=EvalFnOnAgentDist_ProbDensityFn_InfHorz_Mass(StationaryDist.pdf,StationaryDist.mass, Policy, FnsToEvaluate, Parameters, FnsToEvaluateParamNames,EntryExitParamNames, n_d, n_a, n_z, d_grid, a_grid, z_grid, Parallel,simoptions); - return -end - -%% Implement new way of handling FnsToEvaluate -if isstruct(FnsToEvaluate) - FnsToEvaluateStruct=1; - clear FnsToEvaluateParamNames - AggVarNames=fieldnames(FnsToEvaluate); - for ff=1:length(AggVarNames) - temp=getAnonymousFnInputNames(FnsToEvaluate.(AggVarNames{ff})); - if length(temp)>(l_daprime+l_a+l_z) - FnsToEvaluateParamNames(ff).Names={temp{l_daprime+l_a+l_z+1:end}}; % the first inputs will always be (d,aprime,a,z) - else - FnsToEvaluateParamNames(ff).Names={}; - end - FnsToEvaluate2{ff}=FnsToEvaluate.(AggVarNames{ff}); - end - FnsToEvaluate=FnsToEvaluate2; -else - FnsToEvaluateStruct=0; -end - -%% -if Parallel==2 - StationaryDist=gpuArray(StationaryDist); - Policy=gpuArray(Policy); - n_d=gpuArray(n_d); - n_a=gpuArray(n_a); - n_z=gpuArray(n_z); - d_grid=gpuArray(d_grid); - a_gridvals=CreateGridvals(n_z,gpuArray(a_grid),1); - z_gridvals=CreateGridvals(n_z,gpuArray(z_grid),1); - - StationaryDistVec=reshape(StationaryDist,[N_a*N_z,1]); - - ProbDensityFns=zeros(N_a*N_z,length(FnsToEvaluate),'gpuArray'); - - l_daprime=size(Policy,1); - PolicyValues=PolicyInd2Val_InfHorz(Policy,n_d,n_a,n_z,d_grid,a_grid); - permuteindexes=[1+(1:1:(l_a+l_z)),1]; - PolicyValuesPermute=permute(PolicyValues,permuteindexes); %[n_a,n_s,l_d+l_a] - - for ff=1:length(FnsToEvaluate) - FnToEvaluateParamsCell=CreateCellFromParams(Parameters,FnsToEvaluateParamNames(ff).Names); - Values=EvalFnOnAgentDist_Grid(FnsToEvaluate{ff}, FnToEvaluateParamsCell,PolicyValuesPermute,l_daprime,n_a,n_z,a_gridvals,z_gridvals); - Values=reshape(Values,[N_a*N_z,1]); - ProbDensityFns(:,ff)=Values.*StationaryDistVec; - end - -else - [d_gridvals, aprime_gridvals]=CreateGridvals_Policy(Policy,n_d,n_a,n_a,n_z,d_grid,a_grid,1, 2); - a_gridvals=CreateGridvals(n_a,a_grid,2); - z_gridvals=CreateGridvals(n_z,z_grid,2); - - StationaryDistVec=reshape(StationaryDist,[N_a*N_z,1]); - - StationaryDistVec=gather(StationaryDistVec); - - ProbDensityFns=zeros(N_a*N_z,length(FnsToEvaluate)); - - if l_d>0 - for ff=1:length(FnsToEvaluate) - % Includes check for cases in which no parameters are actually required - if isempty(FnsToEvaluateParamNames(ff).Names) % check for 'SSvalueParamNames={}' - Values=zeros(N_a*N_z,1); - for ii=1:N_a*N_z - j1=rem(ii-1,N_a)+1; - j2=ceil(ii/N_a); - Values(ii)=FnsToEvaluate{ff}(d_gridvals{j1+(j2-1)*N_a,:},aprime_gridvals{j1+(j2-1)*N_a,:},a_gridvals{j1,:},z_gridvals{j2,:}); - end - ProbDensityFns(:,ff)=Values.*StationaryDistVec; - else - FnToEvaluateParamsCell=num2cell(CreateVectorFromParams(Parameters,FnsToEvaluateParamNames(ff).Names)); - Values=zeros(N_a*N_z,1); - for ii=1:N_a*N_z - j1=rem(ii-1,N_a)+1; - j2=ceil(ii/N_a); - Values(ii)=FnsToEvaluate{ff}(d_gridvals{j1+(j2-1)*N_a,:},aprime_gridvals{j1+(j2-1)*N_a,:},a_gridvals{j1,:},z_gridvals{j2,:},FnToEvaluateParamsCell{:}); - end - ProbDensityFns(:,ff)=Values.*StationaryDistVec; - end - end - - else %l_d=0 - - for ff=1:length(FnsToEvaluate) - % Includes check for cases in which no parameters are actually required - if isempty(FnsToEvaluateParamNames(ff).Names) % check for 'SSvalueParamNames={}' - Values=zeros(N_a*N_z,1); - for ii=1:N_a*N_z - j1=rem(ii-1,N_a)+1; - j2=ceil(ii/N_a); - Values(ii)=FnsToEvaluate{ff}(aprime_gridvals{j1+(j2-1)*N_a,:},a_gridvals{j1,:},z_gridvals{j2,:}); - end - % When evaluating value function (which may sometimes give -Inf values) on StationaryDistVec (which at those points will be - % 0) we get 'NaN'. Use temp as intermediate variable just eliminate those. - ProbDensityFns(:,ff)=Values.*StationaryDistVec; - else - FnToEvaluateParamsCell=num2cell(CreateVectorFromParams(Parameters,FnsToEvaluateParamNames(ff).Names)); - Values=zeros(N_a*N_z,1); - for ii=1:N_a*N_z - j1=rem(ii-1,N_a)+1; - j2=ceil(ii/N_a); - Values(ii)=FnsToEvaluate{ff}(aprime_gridvals{j1+(j2-1)*N_a,:},a_gridvals{j1,:},z_gridvals{j2,:},FnToEvaluateParamsCell{:}); - end - ProbDensityFns(:,ff)=Values.*StationaryDistVec; - end - end - end -end - -% Normalize to 1 (to make it a pdf) -for ff=1:length(FnsToEvaluate) - ProbDensityFns(:,ff)=ProbDensityFns(:,ff)/sum(ProbDensityFns(:,ff)); -end - -% When evaluating value function (which may sometimes give -Inf values) on StationaryDistVec (which at those points will be -% 0) we get 'NaN'. Just eliminate those. -ProbDensityFns(isnan(ProbDensityFns))=0; - -%% -if FnsToEvaluateStruct==1 - % Change the output into a structure - ProbDensityFns2=ProbDensityFns'; % Note the transpose - clear ProbDensityFns - ProbDensityFns=struct(); -% AggVarNames=fieldnames(FnsToEvaluate); - for ff=1:length(AggVarNames) - ProbDensityFns.(AggVarNames{ff})=reshape(ProbDensityFns2(ff,:),[n_a,n_z]); - end -else - % Change the ordering and size so that ProbDensityFns has same kind of - % shape as StationaryDist, except first dimension indexes the 'FnsToEvaluate'. - ProbDensityFns=ProbDensityFns'; - ProbDensityFns=reshape(ProbDensityFns,[length(FnsToEvaluate),n_a,n_z]); -end - -end diff --git a/ValueFnIter/InfHorz/ExoticPrefs/ValueFnIter_InfHorz_EpsteinZin.m b/ValueFnIter/InfHorz/ExoticPrefs/ValueFnIter_InfHorz_EpsteinZin.m index 1958ecce..2ee46c0d 100644 --- a/ValueFnIter/InfHorz/ExoticPrefs/ValueFnIter_InfHorz_EpsteinZin.m +++ b/ValueFnIter/InfHorz/ExoticPrefs/ValueFnIter_InfHorz_EpsteinZin.m @@ -124,4 +124,6 @@ V=reshape(VKron,[n_a,n_z]); Policy=UnKronPolicyIndexes_Case1(Policy, n_d, n_a, n_z,vfoptions); + end + diff --git a/ValueFnIter/InfHorz/ValueFnIter_Case1.m b/ValueFnIter/InfHorz/ValueFnIter_Case1.m index 6e0db6f2..5eae604b 100644 --- a/ValueFnIter/InfHorz/ValueFnIter_Case1.m +++ b/ValueFnIter/InfHorz/ValueFnIter_Case1.m @@ -2,4 +2,4 @@ error('VFI Toolkit Version 2.5 renamed _Case1 to _InfHorz: vfitoolkit.com/updates-blog/2026/version-2-5-infhorz') -end \ No newline at end of file +end diff --git a/ValueFnIter/TransPath/ValueFnOnTransPath_InfHorz.m b/ValueFnIter/TransPath/ValueFnOnTransPath_InfHorz.m index c3262d94..d2e929ac 100644 --- a/ValueFnIter/TransPath/ValueFnOnTransPath_InfHorz.m +++ b/ValueFnIter/TransPath/ValueFnOnTransPath_InfHorz.m @@ -1,4 +1,8 @@ +<<<<<<< HEAD function [VPath,PolicyPath]=ValueFnOnTransPath_InfHorz(PricePath, ParamPath, T, V_final, Policy_final, Parameters, n_d,n_a,n_z, d_grid,a_grid,z_grid, pi_z, DiscountFactorParamNames, ReturnFn, transpathoptions, vfoptions) +======= +function [VPath,PolicyPath]=ValueFnOnTransPath_InfHorz(PricePath, ParamPath, T, V_final, Policy_final, Parameters, n_d, n_a, n_z, pi_z, d_grid, a_grid,z_grid, DiscountFactorParamNames, ReturnFn, transpathoptions, vfoptions) +>>>>>>> 8f64e227 (Version 2.5: switch to _InfHorz) % transpathoptions, vfoptions and simoptions are optional inputs if prod(n_z)>1 && all(size(d_grid)==[prod(n_z),prod(n_z)]) From d1bf81a0526758f046f089a6605a4e7f9817b07b Mon Sep 17 00:00:00 2001 From: robertdkirkby Date: Wed, 6 May 2026 09:19:33 +1200 Subject: [PATCH 53/90] d_gridvals for InfHorz, including TPath --- TransitionPaths/InfHorz/TransitionPath_InfHorz.m | 2 +- TransitionPaths/InfHorz/TransitionPath_InfHorz_shooting.m | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/TransitionPaths/InfHorz/TransitionPath_InfHorz.m b/TransitionPaths/InfHorz/TransitionPath_InfHorz.m index 61c366f1..3824054d 100644 --- a/TransitionPaths/InfHorz/TransitionPath_InfHorz.m +++ b/TransitionPaths/InfHorz/TransitionPath_InfHorz.m @@ -504,7 +504,7 @@ %% if transpathoptions.GEnewprice~=2 [PricePath,GEcondnPathmatrix]=TransitionPath_InfHorz_shooting(PricePath0, PricePathNames, PricePathSizeVec, ParamPath, ParamPathNames, ParamPathSizeVec, T, V_final, AgentDist_initial, n_d,n_a,n_z,n_e, N_d,N_a,N_z,N_e, l_d,l_aprime,l_a,l_z,l_e, d_gridvals,aprime_gridvals,a_gridvals,a_grid,z_gridvals,e_gridvals,ze_gridvals,pi_z,pi_z_sparse,pi_e, ReturnFn, FnsToEvaluateCell, AggVarNames, FnsToEvaluateParamNames, GEeqnNames, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, Parameters, DiscountFactorParamNames, ReturnFnParamNames, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, vfoptions, simoptions,transpathoptions); - + % Switch to structure for output for pp=1:length(PricePathNames) PricePathStruct.(PricePathNames{pp})=PricePath(:,pp)'; diff --git a/TransitionPaths/InfHorz/TransitionPath_InfHorz_shooting.m b/TransitionPaths/InfHorz/TransitionPath_InfHorz_shooting.m index 133f11a8..a626cfc0 100644 --- a/TransitionPaths/InfHorz/TransitionPath_InfHorz_shooting.m +++ b/TransitionPaths/InfHorz/TransitionPath_InfHorz_shooting.m @@ -17,7 +17,7 @@ AggVarsPath=zeros(T-1,length(AggVarNames),'gpuArray'); % Note: does not include the final AggVars, might be good to add them later as a way to make if obvious to user it things are incorrect GEcondnPath=zeros(T-1,length(GEeqnNames),'gpuArray'); -% Setup, the shapes of verious of these objects vary depending on the setting +% Setup, the shapes of various of these objects vary depending on the setting [PolicyIndexesPath,N_probs,II1,II2]=TransitionPath_InfHorz_substeps_Step0_setup(l_d,l_aprime,N_a,N_z,N_e,T,transpathoptions,vfoptions,simoptions); %% @@ -31,7 +31,7 @@ %% Modify PolicyIndexesPath into forms needed for forward iteration [PolicyPath_ForAgentDistIter,PolicyProbsPath,PolicyValuesPath]=TransitionPath_InfHorz_substeps_Step2_AdjustPolicy(PolicyIndexesPath,T,Parameters,n_d,n_a,n_z,n_e,l_d,l_aprime,N_a,N_z,N_e,N_probs,d_gridvals,aprime_gridvals,transpathoptions,vfoptions,simoptions); - %% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm + %% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm % Call AgentDist the current periods distn and AgentDistnext the next periods distn which we must calculate AgentDist=AgentDist_initial; for tt=1:T-1 From 5c52f955911637f10f915125619bca0a8ede7e5e Mon Sep 17 00:00:00 2001 From: robertdkirkby Date: Thu, 7 May 2026 08:44:57 +1200 Subject: [PATCH 54/90] relocate pi_z to appropriate input location --- TransitionPaths/InfHorz/TransitionPath_InfHorz.m | 2 +- ValueFnIter/TransPath/ValueFnOnTransPath_InfHorz.m | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/TransitionPaths/InfHorz/TransitionPath_InfHorz.m b/TransitionPaths/InfHorz/TransitionPath_InfHorz.m index 3824054d..a5ddd7e5 100644 --- a/TransitionPaths/InfHorz/TransitionPath_InfHorz.m +++ b/TransitionPaths/InfHorz/TransitionPath_InfHorz.m @@ -11,7 +11,7 @@ % Remark to self: No real need for T as input, as this is anyway the length of PricePathOld if all(size(d_grid)==[prod(n_z),prod(n_z)]) - error('Check input order: pi_z comes after z_grid') % Keep this error message until end of 2007, can remove after that + error('Check input order: pi_z comes after z_grid') % Keep this error message until end of 2027, can remove after that end %% Check which transpathoptions have been used, set all others to defaults diff --git a/ValueFnIter/TransPath/ValueFnOnTransPath_InfHorz.m b/ValueFnIter/TransPath/ValueFnOnTransPath_InfHorz.m index d2e929ac..c3262d94 100644 --- a/ValueFnIter/TransPath/ValueFnOnTransPath_InfHorz.m +++ b/ValueFnIter/TransPath/ValueFnOnTransPath_InfHorz.m @@ -1,8 +1,4 @@ -<<<<<<< HEAD function [VPath,PolicyPath]=ValueFnOnTransPath_InfHorz(PricePath, ParamPath, T, V_final, Policy_final, Parameters, n_d,n_a,n_z, d_grid,a_grid,z_grid, pi_z, DiscountFactorParamNames, ReturnFn, transpathoptions, vfoptions) -======= -function [VPath,PolicyPath]=ValueFnOnTransPath_InfHorz(PricePath, ParamPath, T, V_final, Policy_final, Parameters, n_d, n_a, n_z, pi_z, d_grid, a_grid,z_grid, DiscountFactorParamNames, ReturnFn, transpathoptions, vfoptions) ->>>>>>> 8f64e227 (Version 2.5: switch to _InfHorz) % transpathoptions, vfoptions and simoptions are optional inputs if prod(n_z)>1 && all(size(d_grid)==[prod(n_z),prod(n_z)]) From 913c9154c38dceac26edd9b6e32a1ecef4e40496 Mon Sep 17 00:00:00 2001 From: robertdkirkby Date: Thu, 7 May 2026 15:25:14 +1200 Subject: [PATCH 55/90] refactor InfHorz TPath to use five steps approach --- StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m | 2 +- .../TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m | 1 + .../TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m | 6 +++--- TransitionPaths/InfHorz/TransitionPath_InfHorz_shooting.m | 6 +++++- .../AgentDist_InfHorz_TPath_SingleStep_raw.m | 2 +- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m b/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m index 1c34d035..6d774cc5 100644 --- a/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m +++ b/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m @@ -173,7 +173,7 @@ Policy_aprime(:,:,2)=reshape(Policy(ndvars+1,:,:),[N_a,N_z,1])+n_a1*Policy_a2prime(:,:,1); % Note: upper grid point minus 1 is anyway just lower grid point end PolicyaprimezPath=reshape(Policy_aprime+N_a*(0:1:N_z-1),[N_a*N_z,2]); - % PolicyProbs_2D=reshape(PolicyProbs,[N_a*N_z,2]); % sparse matricies are limited to 2D; match PolicyaprimezPath shape + % PolicyProbs_2D=reshape(PolicyProbs,[N_a*N_z,2]); % sparse matrices are limited to 2D; match PolicyaprimezPath shape AgentDist=AgentDist_InfHorz_TPath_SingleStep_nProbs(AgentDist,PolicyaprimezPath,II2,PolicyProbs,N_a,N_z,pi_z_sparse); AgentDistPath(:,tt+1)=gpuArray(full(AgentDist)); end diff --git a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m index 1f1484d1..7e322866 100644 --- a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m +++ b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m @@ -45,6 +45,7 @@ end [V, Policy]=ValueFnIter_InfHorz_TPath_SingleStep(V,n_d,n_a,n_z,d_gridvals, a_grid, z_gridvals, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions); + % The V input is next period value fn, the V output is this period. % Policy is kept in the form where it is just a single-value in (d,a') diff --git a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m index 34985346..bf39e207 100644 --- a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m @@ -39,7 +39,7 @@ a2primeProbsPath(:,:,tt)=a2primeProbs; end end - + if N_e==0 && N_z==0 a2primeIndexesPath=reshape(a2primeIndexesPath,[N_a,1,T-1]); a2primeIndexesPath=repmat(a2primeIndexesPath,1,2,1); @@ -129,6 +129,7 @@ PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,:)-1)+n_a1(1)*n_a1(2)*n_a1(3)*(PolicyIndexesPath(l_d+4,:,:,:)-1),[N_a*N_z,T-1]); end PolicyaprimezPath=gather(PolicyaprimePath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1)); + if simoptions.gridinterplayer==1 L2index=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,:,:),[N_a*N_z,1,T-1]); % PolicyIndexesPath is of size [l_d+l_aprime+1,N_a,N_z,N_j,T] PolicyaprimezPath=reshape(PolicyaprimezPath,[N_a*N_z,1,T-1]); % reinterpret this as lower grid index @@ -256,8 +257,7 @@ PolicyPath_ForAgentDistIter=PolicyaprimezPath; end - -if isempty(N_probs) || N_probs==1 % =1 means not being used +if N_probs==1 % =1 means not being used PolicyProbsPath=[]; end diff --git a/TransitionPaths/InfHorz/TransitionPath_InfHorz_shooting.m b/TransitionPaths/InfHorz/TransitionPath_InfHorz_shooting.m index a626cfc0..ce80a706 100644 --- a/TransitionPaths/InfHorz/TransitionPath_InfHorz_shooting.m +++ b/TransitionPaths/InfHorz/TransitionPath_InfHorz_shooting.m @@ -17,7 +17,11 @@ AggVarsPath=zeros(T-1,length(AggVarNames),'gpuArray'); % Note: does not include the final AggVars, might be good to add them later as a way to make if obvious to user it things are incorrect GEcondnPath=zeros(T-1,length(GEeqnNames),'gpuArray'); +<<<<<<< HEAD % Setup, the shapes of various of these objects vary depending on the setting +======= +% Setup, the shapes of verious of these objects vary depending on the setting +>>>>>>> 64e39950 (refactor InfHorz TPath to use five steps approach) [PolicyIndexesPath,N_probs,II1,II2]=TransitionPath_InfHorz_substeps_Step0_setup(l_d,l_aprime,N_a,N_z,N_e,T,transpathoptions,vfoptions,simoptions); %% @@ -31,7 +35,7 @@ %% Modify PolicyIndexesPath into forms needed for forward iteration [PolicyPath_ForAgentDistIter,PolicyProbsPath,PolicyValuesPath]=TransitionPath_InfHorz_substeps_Step2_AdjustPolicy(PolicyIndexesPath,T,Parameters,n_d,n_a,n_z,n_e,l_d,l_aprime,N_a,N_z,N_e,N_probs,d_gridvals,aprime_gridvals,transpathoptions,vfoptions,simoptions); - %% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm + %% Iterate forward over t: iterate agent dist, calculate aggvars, evaluate general eqm % Call AgentDist the current periods distn and AgentDistnext the next periods distn which we must calculate AgentDist=AgentDist_initial; for tt=1:T-1 diff --git a/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m b/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m index 66a1c395..9cee56c8 100644 --- a/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m +++ b/TransitionPaths/InfHorz/subcodes/AgentDistSingleStep/AgentDist_InfHorz_TPath_SingleStep_raw.m @@ -8,7 +8,7 @@ % AgentDist is already sparse and on cpu -Gammatranspose=sparse(gather(Policy_aprimez),II1,IIones,N_a*N_z,N_a*N_z); +Gammatranspose=sparse(gather(Policy_aprimez),II1,II2,N_a*N_z,N_a*N_z); % Two steps of the Tan improvement AgentDist=reshape(Gammatranspose*AgentDist,[N_a,N_z]); From ff817a5773b3f70d57617655060c0531e84455e5 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Thu, 7 May 2026 20:41:47 +1200 Subject: [PATCH 56/90] Completed run through Electrify code Caveat: there are known problems with TPath codes related to e_gridvals and pi_e matrix. --- .../PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m | 2 +- .../MixHorz/StationaryDist_Case1_MixHorz_PType.m | 2 +- .../PType/TransitionPath_Case1_FHorz_PType_shooting.m | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m b/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m index e776ae7a..0efc23c6 100644 --- a/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m +++ b/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m @@ -357,7 +357,7 @@ end StationaryDist_ii=reshape(StationaryDist.(Names_i{ii}),[N_a_temp*N_z_temp*N_j_temp,1]); % Note: does not impose *StationaryDist.ptweights(ii) else - PolicyValues_temp=PolicyInd2Val_Case1(PolicyIndexes_temp,n_d_temp,n_a_temp,n_z_temp,d_grid_temp,a_grid_temp,simoptions_temp,1); + PolicyValues_temp=PolicyInd2Val_InfHorz(PolicyIndexes_temp,n_d_temp,n_a_temp,n_z_temp,d_grid_temp,a_grid_temp,simoptions_temp,1); if l_z_temp==0 PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,1]); % (N_a,l_daprime) else diff --git a/StationaryDist/MixHorz/StationaryDist_Case1_MixHorz_PType.m b/StationaryDist/MixHorz/StationaryDist_Case1_MixHorz_PType.m index 8fe5bd0f..11a0921a 100644 --- a/StationaryDist/MixHorz/StationaryDist_Case1_MixHorz_PType.m +++ b/StationaryDist/MixHorz/StationaryDist_Case1_MixHorz_PType.m @@ -233,7 +233,7 @@ if isfinite(N_j_temp) 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); else % PType actually allows for infinite horizon as well - StationaryDist_ii=StationaryDist_Case1(Policy_temp,n_d_temp,n_a_temp,n_z_temp,pi_z_temp,simoptions_temp,Parameters_temp); % EntryExitParams not yet supported (is on my to-do list) + StationaryDist_ii=StationaryDist_InfHorz(Policy_temp,n_d_temp,n_a_temp,n_z_temp,pi_z_temp,simoptions_temp,Parameters_temp); % EntryExitParams not yet supported (is on my to-do list) end if simoptions_temp.ptypestorecpu==1 diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m index a582d0b7..4076351f 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m @@ -113,7 +113,7 @@ if isfinite(PTypeStructure.(iistr).N_j) [PTypeStructure.(iistr).PolicyIndexesPath,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,PTypeStructure.(iistr).exceptlastj,PTypeStructure.(iistr).exceptfirstj,PTypeStructure.(iistr).justfirstj]=TransitionPath_FHorz_substeps_Step0_setup(PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_j,T,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); else - [PTypeStructure.(iistr).PolicyIndexesPath,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2]=TransitionPath_InfHorz_substeps_Step0_setup(PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,T,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); + [PTypeStructure.(iistr).PolicyIndexesPath,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2]=TransitionPath_InfHorz_substeps_Step0_setup(PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,T,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); end end @@ -225,10 +225,12 @@ %% AggVars if PTypeStructure.(iistr).N_z==0 && PTypeStructure.(iistr).N_e==0 - AggVars_ii=TransitionPath_FHorz_substeps_Step4tt_AggVars(AgentDist_ii,AgeWeights_ii,PolicyValuesPath_ii(:,:,:,tt),tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,Parameters,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).l_a,PTypeStructure.(iistr).l_z,PTypeStructure.(iistr).l_e,PTypeStructure.(iistr).N_d,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).ze_gridvals_J_fastOLG,transpathoptions); + PVP_ii_t=PolicyValuesPath_ii(:,:,tt); else - AggVars_ii=TransitionPath_FHorz_substeps_Step4tt_AggVars(AgentDist_ii,AgeWeights_ii,PolicyValuesPath_ii(:,:,:,:,tt),tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).l_a,PTypeStructure.(iistr).l_z,PTypeStructure.(iistr).l_e,PTypeStructure.(iistr).N_d,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).ze_gridvals_J_fastOLG,transpathoptions); + PVP_ii_t=PolicyValuesPath_ii(:,:,:,tt); end + AggVars_ii=TransitionPath_FHorz_substeps_Step4tt_AggVars(AgentDist_ii,AgeWeights_ii,PVP_ii_t,tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,Parameters,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).l_a,PTypeStructure.(iistr).l_z,PTypeStructure.(iistr).l_e,PTypeStructure.(iistr).N_d,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).ze_gridvals_J_fastOLG,transpathoptions); + % Uncommenting this allows you to do _tminus1 for AggVars, but only conditional on ptype [not yet possible without conditioning on ptype] % for ff=1:length(AggVarNames) % Parameters.(AggVarNames{ff})=AggVars_ii.(AggVarNames{ff}).Mean; From e8369a6598d830f7cd65cef66488ee0ce05bb4eb Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 8 May 2026 05:50:28 +1200 Subject: [PATCH 57/90] Revert FHorz_PType changes now part of MixHorz changes These changes restore the FHorz_PType codes to what should be on the main branch. MixHorz now deals with all Mixed Horizon PType codes. --- ...FnOnAgentDist_AllStats_FHorz_Case1_PType.m | 76 ++------- ...lFnOnTransPath_AggVars_Case1_FHorz_PType.m | 7 +- .../AgentDistOnTransPath_Case1_FHorz_PType.m | 26 ++- .../PType/TransitionPath_Case1_FHorz_PType.m | 154 ++++++++++-------- ...ransitionPath_Case1_FHorz_PType_shooting.m | 20 ++- .../PricePathParamPath_FHorz_StructToMatrix.m | 8 +- .../ValueFnOnTransPath_Case1_FHorz_PType.m | 6 +- 7 files changed, 137 insertions(+), 160 deletions(-) diff --git a/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m b/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m index 0efc23c6..517b042a 100644 --- a/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m +++ b/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m @@ -335,35 +335,18 @@ N_a_temp=prod(n_a_temp); a_gridvals_temp=CreateGridvals(n_a_temp,a_grid_temp,1); - if isfinite(N_j_temp) - % Turn (semiz,z,e) into z_gridvals_J_temp as FnsToEvalute do not distinguish them - [n_z_temp,z_gridvals_J_temp,N_z_temp,l_z_temp,simoptions_temp]=CreateGridvals_FnsToEvaluate_FHorz(n_z_temp,z_grid_temp,N_j_temp,simoptions_temp,Parameters_temp); - else - l_z_temp=length(n_z_temp); - N_z_temp=prod(n_z_temp); - z_gridvals_temp=CreateGridvals(n_z_temp,z_grid_temp,1); - end + % Turn (semiz,z,e) into z_gridvals_J_temp as FnsToEvalute do not distinguish them + [n_z_temp,z_gridvals_J_temp,N_z_temp,l_z_temp,simoptions_temp]=CreateGridvals_FnsToEvaluate_FHorz(n_z_temp,z_grid_temp,N_j_temp,simoptions_temp,Parameters_temp); if N_z_temp==0 N_z_temp=1; % Just makes things easier below end % Switch to PolicyVals - if isfinite(N_j_temp) - PolicyValues_temp=PolicyInd2Val_FHorz(PolicyIndexes_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,d_grid_temp,a_grid_temp,simoptions_temp,1); - if l_z_temp==0 - PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,3,1]); % (N_a,N_j,l_daprime) - else - PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,3,4,1]); % (N_a,N_z,N_j,l_daprime) - end - StationaryDist_ii=reshape(StationaryDist.(Names_i{ii}),[N_a_temp*N_z_temp*N_j_temp,1]); % Note: does not impose *StationaryDist.ptweights(ii) + PolicyValues_temp=PolicyInd2Val_FHorz(PolicyIndexes_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,d_grid_temp,a_grid_temp,simoptions_temp,1); + if l_z_temp==0 + PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,3,1]); % (N_a,N_j,l_daprime) else - PolicyValues_temp=PolicyInd2Val_InfHorz(PolicyIndexes_temp,n_d_temp,n_a_temp,n_z_temp,d_grid_temp,a_grid_temp,simoptions_temp,1); - if l_z_temp==0 - PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,1]); % (N_a,l_daprime) - else - PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,3,1]); % (N_a,N_z,l_daprime) - end - StationaryDist_ii=reshape(StationaryDist.(Names_i{ii}),[N_a_temp*N_z_temp,1]); % Note: does not impose *StationaryDist.ptweights(ii) + PolicyValuesPermute_temp=permute(PolicyValues_temp,[2,3,4,1]); % (N_a,N_z,N_j,l_daprime) end l_daprime_temp=size(PolicyValues_temp,1); @@ -371,6 +354,7 @@ FnsAndPTypeIndicator(:,ii)=FnsAndPTypeIndicator_ii; %% Some things that don't need to go in the loop over FnsToEvalaute + StationaryDist_ii=reshape(StationaryDist.(Names_i{ii}),[N_a_temp*N_z_temp*N_j_temp,1]); % Note: does not impose *StationaryDist.ptweights(ii) % Eliminate all the zero-weighted points (this doesn't really save runtime for the exact calculation and often can increase it, but % for the createDigest it slashes the runtime. So since we want it then we may as well do it now.) temp=logical(StationaryDist_ii~=0); @@ -392,21 +376,14 @@ CondlRestnFnParamNames={}; end - if isfinite(N_j_temp) - if l_z_temp==0 - CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,CondlRestnFnParamNames,N_j_temp,2); % j in 2nd dimension: (a,j,l_d+l_a), so we want j to be after N_a - RestrictionValues=logical(EvalFnOnAgentDist_Grid_J(CondlRestnFn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,0,a_gridvals_temp,[])); - else - CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,CondlRestnFnParamNames,N_j_temp,3); % j in 3rd dimension: (a,z,j,l_d+l_a), so we want j to be after N_a and N_z - RestrictionValues=logical(EvalFnOnAgentDist_Grid_J(CondlRestnFn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_J_temp)); - end - RestrictionValues=reshape(RestrictionValues,[N_a_temp*N_z_temp*N_j_temp,1]); + if l_z_temp==0 + CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,CondlRestnFnParamNames,N_j_temp,2); % j in 2nd dimension: (a,j,l_d+l_a), so we want j to be after N_a + RestrictionValues=logical(EvalFnOnAgentDist_Grid_J(CondlRestnFn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,0,a_gridvals_temp,[])); else - CondlRestnFnParamsCell=CreateCellFromParams(Parameters_temp,CondlRestnFnParamNames); - - RestrictionValues=logical(EvalFnOnAgentDist_Grid(CondlRestnFn, CondlRestnFnParamsCell,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_temp)); - RestrictionValues=reshape(RestrictionValues,[N_a_temp*N_z_temp,1]); + CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,CondlRestnFnParamNames,N_j_temp,3); % j in 3rd dimension: (a,z,j,l_d+l_a), so we want j to be after N_a and N_z + RestrictionValues=logical(EvalFnOnAgentDist_Grid_J(CondlRestnFn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_J_temp)); end + RestrictionValues=reshape(RestrictionValues,[N_a_temp*N_z_temp*N_j_temp,1]); RestrictedStationaryDistVec=StationaryDist_ii; RestrictedStationaryDistVec(~RestrictionValues)=0; % zero mass on all points that do not meet the restriction @@ -434,35 +411,16 @@ if FnsAndPTypeIndicator_ii(ff)==1 % If this function is relevant to this ptype % Get parameter names for current FnsToEvaluate functions - % Get parameter names for current FnsToEvaluate functions - if isstruct(FnsToEvaluate.(FnsToEvalNames{ff})) - tempfn=FnsToEvaluate.(FnsToEvalNames{ff}).(Names_i{ii}); - else - tempfn=FnsToEvaluate.(FnsToEvalNames{ff}); - end - tempnames=getAnonymousFnInputNames(tempfn); + tempnames=getAnonymousFnInputNames(FnsToEvaluate.(FnsToEvalNames{ff})); if length(tempnames)>(l_daprime_temp+l_a_temp+l_z_temp) FnsToEvaluateParamNames={tempnames{l_daprime_temp+l_a_temp+l_z_temp+1:end}}; % the first inputs will always be (d,aprime,a,z) else FnsToEvaluateParamNames={}; end - if isfinite(N_j_temp) - if l_z_temp==0 - CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,FnsToEvaluateParamNames,N_j_temp,2); - else - CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,FnsToEvaluateParamNames,N_j_temp,3); - end - - %% We have set up the current PType, now do some calculations for it. - simoptions_temp.keepoutputasmatrix=1; - ValuesOnGrid_ii=EvalFnOnAgentDist_Grid_J(tempfn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_J_temp); - ValuesOnGrid_ii=reshape(ValuesOnGrid_ii,[N_a_temp*N_z_temp*N_j_temp,1]); - - % StationaryDist_ii=reshape(StationaryDist.(Names_i{ii}),[N_a_temp*N_z_temp*N_j_temp,1]); % Note: does not impose *StationaryDist.ptweights(ii) + if l_z_temp==0 + CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,FnsToEvaluateParamNames,N_j_temp,2); else - FnToEvaluateParamsCell=CreateCellFromParams(Parameters_temp,FnsToEvaluateParamNames); - ValuesOnGrid_ii=EvalFnOnAgentDist_Grid(tempfn, FnToEvaluateParamsCell,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_temp); - ValuesOnGrid_ii=reshape(ValuesOnGrid_ii,[N_a_temp*N_z_temp,1]); + CellOverAgeOfParamValues=CreateCellOverAgeFromParams(Parameters_temp,FnsToEvaluateParamNames,N_j_temp,3); end %% We have set up the current PType, now do some calculations for it. diff --git a/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m b/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m index 9f6ab038..73201f17 100644 --- a/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m +++ b/EvaluateFnOnAgentDist/TransPathFHorz/PType/EvalFnOnTransPath_AggVars_Case1_FHorz_PType.m @@ -184,11 +184,8 @@ [FnsToEvaluate_temp,~, ~,~]=PType_FnsToEvaluate(FnsToEvaluate,Names_i,ii,l_d_temp,l_a_temp,l_z_temp,0); - if isfinite(N_j_temp) - AggVarsPath_ii=EvalFnOnTransPath_AggVars_Case1_FHorz(FnsToEvaluate_temp, AgentDistPath_temp, PolicyPath_temp, PricePath_temp, ParamPath_temp, Parameters_temp, T, n_d_temp, n_a_temp, n_z_temp, N_j_temp, d_grid_temp, a_grid_temp,z_grid_temp, transpathoptions_temp, simoptions_temp); - else - AggVarsPath_ii=EvalFnOnTransPath_AggVars_Case1(FnsToEvaluate_temp, AgentDistPath_temp, PolicyPath_temp, PricePath_temp, ParamPath_temp, Parameters_temp, T, n_d_temp, n_a_temp, n_z_temp, d_grid_temp, a_grid_temp,z_grid_temp, transpathoptions_temp, simoptions_temp); - end + AggVarsPath_ii=EvalFnOnTransPath_AggVars_Case1_FHorz(FnsToEvaluate_temp, AgentDistPath_temp, PolicyPath_temp, PricePath_temp, ParamPath_temp, Parameters_temp, T, n_d_temp, n_a_temp, n_z_temp, N_j_temp, d_grid_temp, a_grid_temp,z_grid_temp, transpathoptions_temp, simoptions_temp); + FnNames_temp=fieldnames(FnsToEvaluate_temp); for ff=1:length(FnNames_temp) diff --git a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m index 7b55dd7f..d1851e16 100644 --- a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m +++ b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m @@ -129,17 +129,15 @@ else AgentDist_initial_temp=AgentDist_initial; % NEED TO DEAL WITH THIS PROPERLY end - if isfinite(N_j_temp) - if isstruct(AgeWeightsParamNames) - AgeWeightsParamNames_temp=AgeWeightsParamNames.(Names_i{ii}); - else - AgeWeightsParamNames_temp=AgeWeightsParamNames; - end - if isstruct(jequalOneDist) - jequalOneDist_temp=jequalOneDist.(Names_i{ii}); - else - jequalOneDist_temp=jequalOneDist; - end + if isstruct(AgeWeightsParamNames) + AgeWeightsParamNames_temp=AgeWeightsParamNames.(Names_i{ii}); + else + AgeWeightsParamNames_temp=AgeWeightsParamNames; + end + if isstruct(jequalOneDist) + jequalOneDist_temp=jequalOneDist.(iistr); + else + jequalOneDist_temp=jequalOneDist; end @@ -179,11 +177,7 @@ % Compute the agent distribution path for permanent type ii - if isfinite(N_j_temp) - AgentDistPath_ii=AgentDistOnTransPath_Case1_FHorz(AgentDist_initial_temp, jequalOneDist_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, AgeWeightsParamNames_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,pi_z_temp, T,Parameters_temp, transpathoptions_temp, simoptions_temp); - else - AgentDistPath_ii=AgentDistOnTransPath_Case1(AgentDist_initial_temp, PolicyPath_temp, n_d_temp,n_a_temp,n_z_temp,pi_z_temp,T,simoptions_temp, Parameters_temp, PricePath_temp, ParamPath_temp); - end + AgentDistPath_ii=AgentDistOnTransPath_Case1_FHorz(AgentDist_initial_temp, jequalOneDist_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, AgeWeightsParamNames_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,pi_z_temp, T,Parameters_temp, transpathoptions_temp, simoptions_temp); % Note: T cannot depend on ptype, nor can PricePath depend on ptype AgentDistPath.(Names_i{ii})=AgentDistPath_ii; diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m index 30059002..e0ccbd3f 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m @@ -590,23 +590,16 @@ %% Set up exogenous shock processes - if isfinite(PTypeStructure.(iistr).N_j) - [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).pi_z_J_sim, PTypeStructure.(iistr).e_gridvals_J, PTypeStructure.(iistr).pi_e_J, PTypeStructure.(iistr).pi_e_J_sim, PTypeStructure.(iistr).ze_gridvals_J_fastOLG, transpathoptions, PTypeStructure.(iistr).simoptions]=ExogShockSetup_TPath_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); - % Convert z and e to age-dependent joint-grids and transtion matrix - % output: z_gridvals_J, pi_z_J, e_gridvals_J, pi_e_J, transpathoptions,vfoptions,simoptions - else - [PTypeStructure.(iistr).z_gridvals, PTypeStructure.(iistr).pi_z, transpathoptions, PTypeStructure.(iistr).simoptions]=ExogShockSetup_TPath_InfHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); - end + [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).pi_z_J_sim, PTypeStructure.(iistr).e_gridvals_J, PTypeStructure.(iistr).pi_e_J, PTypeStructure.(iistr).pi_e_J_sim, PTypeStructure.(iistr).ze_gridvals_J_fastOLG, transpathoptions, PTypeStructure.(iistr).simoptions]=ExogShockSetup_TPath_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); + % Convert z and e to age-dependent joint-grids and transtion matrix + % output: z_gridvals_J, pi_z_J, e_gridvals_J, pi_e_J, transpathoptions,vfoptions,simoptions %% If using any non-standard endogenous states, setup for those (both FHorz and InfHorz btw) [PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions]=SetupNonStandardEndoStates_FHorz_TPath(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); %% Organise V_final, AgeWeights and AgentDist_init % Reshape V_final - if ~isfinite(PTypeStructure.(iistr).N_j) - % If no z, then N_z=1 here - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z]); - elseif transpathoptions.fastOLG==0 + if transpathoptions.fastOLG==0 N_j_temp=PTypeStructure.(iistr).N_j; if N_z==0 if N_e==0 @@ -637,74 +630,73 @@ end end end - % Reshape AgentDist_initial + + % Reshape AgentDist_initial and turn AgeWeights_T into appropriate size so that we can always just do AgentDist.*AgeWeights + % Note when simoptions.fastOLG==1 we have shapes of [N_a*N_j_temp*whatever,1-or-N_e] instead of [N_a,whatever-and-maybe-N_e,N_j_temp] + AgentDist_init=AgentDist_initial.(iistr); + N_j_temp=PTypeStructure.(iistr).N_j; if N_z==0 if N_e==0 - AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a,N_j]); % if simoptions.fastOLG==0 - AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j] - if PTypeStructure.(iistr).simoptions.fastOLG==1 - AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_j,1]); - AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); + AgentDist_init=reshape(AgentDist_init,[N_a,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] + if PTypeStructure.(iistr).simoptions.fastOLG + AgentDist_init=reshape(AgentDist_init,[N_a*N_j_temp,1]); + AgeWeights_init=repelem(AgeWeights_init',N_a,1); end else - AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_e,N_j]); % if simoptions.fastOLG==0 - AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j] - if PTypeStructure.(iistr).simoptions.fastOLG==1 % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 - AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_e,N_j]),[1,3,2]),[N_a*N_j*N_e,1]); - AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); + AgentDist_init=reshape(AgentDist_init,[N_a*N_e,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] + if PTypeStructure.(iistr).simoptions.fastOLG % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 + AgentDist_init=reshape(permute(reshape(AgentDist_init,[N_a,N_e,N_j_temp]),[1,3,2]),[N_a*N_j_temp,N_e]); + AgeWeights_init=repelem(AgeWeights_init',N_a,1); end end else if N_e==0 - AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_z,N_j]); % if simoptions.fastOLG==0 - AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j] - if PTypeStructure.(iistr).simoptions.fastOLG==1 % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 - AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_z,N_j]),[1,3,2]),[N_a*N_j*N_z,1]); - AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); + AgentDist_init=reshape(AgentDist_init,[N_a*N_z,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] + if PTypeStructure.(iistr).simoptions.fastOLG % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 + AgentDist_init=reshape(permute(reshape(AgentDist_init,[N_a,N_z,N_j_temp]),[1,3,2]),[N_a*N_j_temp*N_z,1]); + AgeWeights_init=repelem(AgeWeights_init',N_a,1); end - PTypeStructure.(iistr).jequalOneDist_T=jequalOneDist_temp; else - AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_z*N_e,N_j]); % if simoptions.fastOLG==0 - AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j] - if PTypeStructure.(iistr).simoptions.fastOLG==1 % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 - AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_z,N_e,N_j]),[1,4,2,3]),[N_a*N_j*N_z,N_e]); - AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); + AgentDist_init=reshape(AgentDist_init,[N_a*N_z*N_e,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] + if PTypeStructure.(iistr).simoptions.fastOLG % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 + AgentDist_init=reshape(permute(reshape(AgentDist_init,[N_a,N_z,N_e,N_j_temp]),[1,4,2,3]),[N_a*N_j_temp*N_z,N_e]); + AgeWeights_init=repelem(AgeWeights_init',N_a,1); end - PTypeStructure.(iistr).jequalOneDist=jequalOneDist_temp; end + end + + % Get AgeWeights and switch into the transpathoptions.ageweightstrivial=0 setup (and this is what subfns hardcode when doing PTypes) + % It is assumed there is only one Age Weight Parameter (name)) + % AgeWeights_T is (a,j,z)-by-T (create as N_j-by-T to start, then switch) + if isstruct(AgeWeights) + AgeWeights_ii=AgeWeights.(iistr); if all(size(AgeWeights_ii)==[N_j_temp,1]) % Does not depend on transition path period PTypeStructure.(iistr).AgeWeights_T=gather(AgeWeights_ii.*ones(1,T)); - elseif all(size(AgeWeights)==[1,N_j]) + elseif all(size(AgeWeights)==[1,N_j_temp]) % Does not depend on transition path period PTypeStructure.(iistr).AgeWeights_T=gather(AgeWeights_ii'.*ones(1,T)); else fprintf('Following error applies to agent permanent type: %s \n',iistr) error('The age weights parameter seems to be the wrong size') end - - % Check ParamPath to see if the AgeWeights vary over the transition - % (and overwrite PTypeStructure.(iistr).AgeWeights_T if it does) - temp=strcmp(ParamPathNames,AgeWeightsParamNames.(iistr){1}); - if any(temp) - transpathoptions.ageweightstrivial=0; % AgeWeights vary over the transition - [~,kk]=max(temp); % Get index for the AgeWeightsParamNames{1} in ParamPathNames - % Create AgeWeights_T - PTypeStructure.(iistr).AgeWeights_T=ParamPath(:,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk))'; % This will always be N_j-by-T (as transpose) - % Note: still leave it in ParamPath just in case it is used in AggVars or somesuch - end - - % Because ptypes hardcodes transpathoptions.ageweightstrivial=0, we need - if PTypeStructure.(iistr).simoptions.fastOLG==1 - if N_z==0 - PTypeStructure.(iistr).AgeWeights_T=repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1); % simoptions.fastOLG=1 so this is (a,j)-by-1 - else - PTypeStructure.(iistr).AgeWeights_T=repmat(repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1),N_z,1); % simoptions.fastOLG=1 so this is (a,j,z)-by-1 - end + else % not a structure, so must apply to all permanent types + if all(size(AgeWeights)==[N_j_temp,1]) + % Does not depend on transition path period + PTypeStructure.(iistr).AgeWeights_T=gather(AgeWeights.*ones(1,T)); + elseif all(size(AgeWeights)==[1,N_j_temp]) + % Does not depend on transition path period + PTypeStructure.(iistr).AgeWeights_T=gather(AgeWeights'.*ones(1,T)); + else + error('The age weights parameter seems to be the wrong size') end end - %% Set up jequalOneDist_T.(iistr) [hardcodes transpathoptions.trivialjequalonedist=0 and simoptions.fastOLG=1] + %% Set up jequalOneDist_T.(iistr) [hardcodes transpathoptions.trivialjequalonedist=0] if ~isstruct(jequalOneDist) jequalOneDist_temp=gpuArray(jequalOneDist); else % jequalOneDist is a structure @@ -718,20 +710,52 @@ if N_e==0 jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a,T]); else - fprintf('Following error applies to agent permanent type: %s \n',iistr) - error('The age weights parameter seems to be the wrong size') + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_e,T]); % simoptions.fastOLG==1 + end + else + if N_e==0 + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z,T]); % simoptions.fastOLG==1 + else + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z*N_e,T]); % simoptions.fastOLG==1 end - else % not a structure, so must apply to all permanent types - if all(size(AgeWeights)==[N_j_temp,1]) - % Does not depend on transition path period - AgeWeights_T.(iistr)=gather(AgeWeights.*ones(1,T)); - elseif all(size(AgeWeights)==[1,N_j_temp]) - % Does not depend on transition path period - AgeWeights_T.(iistr)=gather(AgeWeights'.*ones(1,T)); + end + PTypeStructure.(iistr).jequalOneDist_T=jequalOneDist_temp; + else + transpathoptions.(iistr).trivialjequalonedist=1; + if N_z==0 + if N_e==0 + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a,1]); + else + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_e,1]); % simoptions.fastOLG==1 + end + else + if N_e==0 + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z,1]); % simoptions.fastOLG==1 else - error('The age weights parameter seems to be the wrong size') + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z*N_e,1]); % simoptions.fastOLG==1 (how different than simoptions.fastOLG==0?) end end + PTypeStructure.(iistr).jequalOneDist=jequalOneDist_temp; + end + + % Check ParamPath to see if the AgeWeights vary over the transition + % (and overwrite PTypeStructure.(iistr).AgeWeights_T if it does) + temp=strcmp(ParamPathNames,AgeWeightsParamNames.(iistr){1}); + if any(temp) + transpathoptions.ageweightstrivial=0; % AgeWeights vary over the transition + [~,kk]=max(temp); % Get index for the AgeWeightsParamNames{1} in ParamPathNames + % Create AgeWeights_T + PTypeStructure.(iistr).AgeWeights_T=ParamPath(:,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk))'; % This will always be N_j-by-T (as transpose) + % Note: still leave it in ParamPath just in case it is used in AggVars or somesuch + end + + % Because ptypes hardcodes transpathoptions.ageweightstrivial=0, we need + if PTypeStructure.(iistr).simoptions.fastOLG==1 + if N_z==0 + PTypeStructure.(iistr).AgeWeights_T=repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1); % simoptions.fastOLG=1 so this is (a,j)-by-1 + else + PTypeStructure.(iistr).AgeWeights_T=repmat(repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1),N_z,1); % simoptions.fastOLG=1 so this is (a,j,z)-by-1 + end PTypeStructure.(iistr).jequalOneDist_T=jequalOneDist_temp; else transpathoptions.(iistr).trivialjequalonedist=1; diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m index 4076351f..7c8e3724 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m @@ -110,11 +110,7 @@ %% for ii=1:N_i iistr=PTypeStructure.Names_i{ii}; - if isfinite(PTypeStructure.(iistr).N_j) - [PTypeStructure.(iistr).PolicyIndexesPath,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,PTypeStructure.(iistr).exceptlastj,PTypeStructure.(iistr).exceptfirstj,PTypeStructure.(iistr).justfirstj]=TransitionPath_FHorz_substeps_Step0_setup(PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_j,T,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); - else - [PTypeStructure.(iistr).PolicyIndexesPath,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2]=TransitionPath_InfHorz_substeps_Step0_setup(PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,T,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); - end + [PTypeStructure.(iistr).PolicyIndexesPath,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,PTypeStructure.(iistr).exceptlastj,PTypeStructure.(iistr).exceptfirstj,PTypeStructure.(iistr).justfirstj]=TransitionPath_FHorz_substeps_Step0_setup(PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_j,T,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); end %% @@ -215,7 +211,9 @@ if transpathoptions.(iistr).trivialjequalonedist==0 PTypeStructure.(iistr).jequalOneDist=PTypeStructure.(iistr).jequalOneDist_T(:,tt+1); % Note: t+1 as we are about to create the next period AgentDist end - if PTypeStructure.(iistr).simoptions.fastOLG==0 || PTypeStructure.(iistr).N_e>0 + if ndims(PTypeStructure.(iistr).AgeWeights_T)==2 + AgeWeights_ii=PTypeStructure.(iistr).AgeWeights_T(:,tt); + elseif PTypeStructure.(iistr).simoptions.fastOLG==0 || PTypeStructure.(iistr).N_e>0 AgeWeights_ii=PTypeStructure.(iistr).AgeWeights_T(:,:,tt); else % simoptions.fastOLG==1 AgeWeights_ii=PTypeStructure.(iistr).AgeWeights_T(:,tt); @@ -223,6 +221,16 @@ AgentDistnext_ii=TransitionPath_FHorz_substeps_Step3tt_IterAgentDist(AgentDist_ii,PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,tt,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).pi_z_J,PTypeStructure.(iistr).pi_z_J_sim,PTypeStructure.(iistr).pi_e_J,PTypeStructure.(iistr).pi_e_J_sim,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,PTypeStructure.(iistr).exceptlastj,PTypeStructure.(iistr).exceptfirstj,PTypeStructure.(iistr).justfirstj,PTypeStructure.(iistr).jequalOneDist,transpathoptions,PTypeStructure.(iistr).simoptions); + %% AggVars + if PTypeStructure.(iistr).N_z==0 && PTypeStructure.(iistr).N_e==0 + PVP_ii_t=PolicyValuesPath_ii(:,:,:,tt); + else + PVP_ii_t=PolicyValuesPath_ii(:,:,:,:,tt); + end + AggVars_ii=TransitionPath_FHorz_substeps_Step4tt_AggVars(AgentDist_ii,AgeWeights_ii,PVP_ii_t,tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).l_a,PTypeStructure.(iistr).l_z,PTypeStructure.(iistr).l_e,PTypeStructure.(iistr).N_d,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).ze_gridvals_J_fastOLG,transpathoptions); + + AgentDistnext_ii=TransitionPath_FHorz_substeps_Step3tt_IterAgentDist(AgentDist_ii,PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,tt,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).pi_z_J,PTypeStructure.(iistr).pi_z_J_sim,PTypeStructure.(iistr).pi_e_J,PTypeStructure.(iistr).pi_e_J_sim,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,PTypeStructure.(iistr).exceptlastj,PTypeStructure.(iistr).exceptfirstj,PTypeStructure.(iistr).justfirstj,PTypeStructure.(iistr).jequalOneDist,transpathoptions,PTypeStructure.(iistr).simoptions); + %% AggVars if PTypeStructure.(iistr).N_z==0 && PTypeStructure.(iistr).N_e==0 PVP_ii_t=PolicyValuesPath_ii(:,:,tt); diff --git a/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m b/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m index ca780736..4d0706ea 100644 --- a/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m +++ b/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m @@ -82,7 +82,7 @@ PricePathSizeVec(pp)=length(tempptypenames)*tempsize(tempsize~=T); % Get the dimension which is not T for ii=1:N_i N_j_temp=N_j.(Names_i{ii}); - if isfinite(N_j_temp) && ~any(PricePathSizeVec(pp)==[1,N_i,N_j_temp,N_i*N_j_temp]) + if ~any(PricePathSizeVec(pp)==[1,N_i,N_j_temp,N_i*N_j_temp]) error(['PricePath for ', PricePathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) end end @@ -92,7 +92,7 @@ PricePathSizeVec(pp)=tempsize(tempsize~=T); % Get the dimension which is not T for ii=1:N_i N_j_temp=N_j.(Names_i{ii}); - if isfinite(N_j_temp) && ~any(PricePathSizeVec(pp)==[1,N_i,N_j_temp]) + if ~any(PricePathSizeVec(pp)==[1,N_i,N_j_temp]) error(['PricePath for ', PricePathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) end end @@ -146,7 +146,7 @@ ParamPathSizeVec(pp)=length(tempptypenames)*tempsize(tempsize~=T); % Get the dimension which is not T for ii=1:N_i N_j_temp=N_j.(Names_i{ii}); - if isfinite(N_j_temp) && ~any(ParamPathSizeVec(pp)==[1,N_i,N_j_temp,N_i*N_j_temp]) + if ~any(ParamPathSizeVec(pp)==[1,N_i,N_j_temp,N_i*N_j_temp]) error(['ParamPath for ', ParamPathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) end end @@ -156,7 +156,7 @@ ParamPathSizeVec(pp)=tempsize(tempsize~=T); % Get the dimension which is not T for ii=1:N_i N_j_temp=N_j.(Names_i{ii}); - if isfinite(N_j_temp) && ~any(ParamPathSizeVec(pp)==[1,N_i,N_j_temp]) + if ~any(ParamPathSizeVec(pp)==[1,N_i,N_j_temp]) error(['ParamPath for ', ParamPathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) end end diff --git a/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz_PType.m b/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz_PType.m index 48605741..f0ae812a 100644 --- a/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz_PType.m +++ b/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz_PType.m @@ -190,11 +190,7 @@ end end - if isfinite(N_j_temp) - [VPath_ii,PolicyPath_ii]=ValueFnOnTransPath_Case1_FHorz(PricePath_temp, ParamPath_temp, T, V_final_temp, Policy_final_temp, Parameters_temp, n_d_temp, n_a_temp, n_z_temp, N_j_temp, d_grid_temp, a_grid_temp,z_grid_temp,pi_z_temp, DiscountFactorParamNames_temp, ReturnFn_temp, transpathoptions_temp, vfoptions_temp); - else - [VPath_ii,PolicyPath_ii]=ValueFnOnTransPath_Case1(PricePath_temp, ParamPath_temp, T, V_final_temp, Policy_final_temp, Parameters_temp, n_d_temp, n_a_temp, n_z_temp, pi_z_temp, d_grid_temp, a_grid_temp,z_grid_temp, DiscountFactorParamNames_temp, ReturnFn_temp, transpathoptions_temp, vfoptions_temp); - end + [VPath_ii,PolicyPath_ii]=ValueFnOnTransPath_Case1_FHorz(PricePath_temp, ParamPath_temp, T, V_final_temp, Policy_final_temp, Parameters_temp, n_d_temp, n_a_temp, n_z_temp, N_j_temp, d_grid_temp, a_grid_temp,z_grid_temp,pi_z_temp, DiscountFactorParamNames_temp, ReturnFn_temp, transpathoptions_temp, vfoptions_temp); % Note: T cannot depend on ptype, nor can PricePath depend on ptype if vfoptions_temp.ptypestorecpu==1 From 0436f41f833efeacb73eba2234c38144080cc74d Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sun, 10 May 2026 11:36:42 +1200 Subject: [PATCH 58/90] Fix redundancies and resync The MixHorz code was based on a wrong version of the FHorz code. In the course of fixing that, found some redundancies in the FHorz code that could be eliminated. Also fixed some typos in comments in the InfHorz case. --- .../PType/TransitionPath_Case1_FHorz_PType.m | 120 +--- .../PType/TransitionPath_InfHorz_PType.m | 8 +- .../TransitionPath_Case1_MixHorz_PType.m | 518 +++++++++--------- ...nsitionPath_Case1_MixHorz_PType_shooting.m | 22 +- 4 files changed, 314 insertions(+), 354 deletions(-) diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m index e0ccbd3f..f638e10c 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m @@ -258,7 +258,7 @@ % Need to fill in some defaults PTypeStructure.(iistr).vfoptions.parallel=2; % hardcode PTypeStructure.(iistr).simoptions.parallel=2; % hardcode - PTypeStructure.(iistr).simoptions.fastOLG=1; % hardcode + PTypeStructure.(iistr).simoptions.fastOLG=1; % hardcode if ~isfield(PTypeStructure.(iistr).vfoptions,'n_e') PTypeStructure.(iistr).n_e=0; else @@ -376,98 +376,41 @@ PTypeStructure.(iistr).z_grid=gpuArray(z_grid); end - if PTypeStructure.(iistr).vfoptions.experienceasset - % Borrowed from TransitionPaths/InfHorz/TransitionPath_Case1.m - % Split decision variables into the standard ones and the one relevant to the experience asset - if isscalar(PTypeStructure.(iistr).n_d) - PTypeStructure.(iistr).n_d1=0; - else - PTypeStructure.(iistr).n_d1=PTypeStructure.(iistr).n_d(1:end-1); - end - PTypeStructure.(iistr).n_d2=PTypeStructure.(iistr).n_d(end); % n_d2 is the decision variable that influences next period vale of the experience asset - PTypeStructure.(iistr).d1_grid=PTypeStructure.(iistr).d_grid(1:sum(PTypeStructure.(iistr).n_d1)); - PTypeStructure.(iistr).d2_grid=PTypeStructure.(iistr).d_grid(sum(PTypeStructure.(iistr).n_d1)+1:end); - % Split endogenous assets into the standard ones and the experience asset + % to be able to EvalFnsOnAgentDist using fastOLG we also need + PTypeStructure.(iistr).a_gridvals=gpuArray(CreateGridvals(PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).a_grid,1)); % a_grivdals is [N_a,l_a] + % use fine grid for aprime_gridvals + if PTypeStructure.(iistr).vfoptions.gridinterplayer==0 + PTypeStructure.(iistr).aprime_gridvals=PTypeStructure.(iistr).a_gridvals; + elseif PTypeStructure.(iistr).vfoptions.gridinterplayer==1 if isscalar(PTypeStructure.(iistr).n_a) - PTypeStructure.(iistr).n_a1=0; + n_aprime=PTypeStructure.(iistr).n_a+(PTypeStructure.(iistr).n_a-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; + aprime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).N_a)',PTypeStructure.(iistr).a_grid,gpuArray(linspace(1,PTypeStructure.(iistr).N_a,n_aprime))'); + PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); else - PTypeStructure.(iistr).n_a1=PTypeStructure.(iistr).n_a(1:end-1); - end - PTypeStructure.(iistr).n_a2=PTypeStructure.(iistr).n_a(end); % n_a2 is the experience asset - PTypeStructure.(iistr).a1_grid=PTypeStructure.(iistr).a_grid(1:sum(PTypeStructure.(iistr).n_a1)); - PTypeStructure.(iistr).a2_grid=PTypeStructure.(iistr).a_grid(sum(PTypeStructure.(iistr).n_a1)+1:end); - - % aprimeFnParamNames in same fashion - l_d2=length(PTypeStructure.(iistr).n_d2); - l_a2=length(PTypeStructure.(iistr).n_a2); - temp=getAnonymousFnInputNames(PTypeStructure.(iistr).vfoptions.aprimeFn); - if length(temp)>(l_d2+l_a2) - PTypeStructure.(iistr).aprimeFnParamNames={temp{l_d2+l_a2+1:end}}; % the first inputs will always be (d2,a2) - else - PTypeStructure.(iistr).aprimeFnParamNames={}; + a1_grid=PTypeStructure.(iistr).a_grid(1:PTypeStructure.(iistr).n_a(1)); + n_a1prime=PTypeStructure.(iistr).n_a(1)+(PTypeStructure.(iistr).n_a(1)-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; + n_aprime=[n_a1prime,PTypeStructure.(iistr).n_a(2:end)]; + a1prime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).n_a(1))',a1_grid,gpuArray(linspace(1,PTypeStructure.(iistr).n_a(1),n_a1prime))'); + aprime_grid=[a1prime_grid; PTypeStructure.(iistr).a_grid(PTypeStructure.(iistr).n_a(1)+1:end)]; + PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); end - - PTypeStructure.(iistr).N_a1=prod(PTypeStructure.(iistr).n_a1); - - % to be able to EvalFnsOnAgentDist using fastOLG we also need - PTypeStructure.(iistr).a_gridvals=gpuArray(CreateGridvals(PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).a_grid,1)); % a_grivdals is [N_a,l_a] - % use fine grid for aprime_gridvals - if PTypeStructure.(iistr).vfoptions.gridinterplayer==0 - PTypeStructure.(iistr).aprime_gridvals=PTypeStructure.(iistr).a_gridvals; - elseif PTypeStructure.(iistr).vfoptions.gridinterplayer==1 - if isscalar(PTypeStructure.(iistr).n_a) - n_aprime=PTypeStructure.(iistr).n_a+(PTypeStructure.(iistr).n_a-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; - aprime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).N_a)',PTypeStructure.(iistr).a_grid,gpuArray(linspace(1,PTypeStructure.(iistr).N_a,n_aprime))'); - PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); - else - a1_grid=PTypeStructure.(iistr).a_grid(1:PTypeStructure.(iistr).n_a(1)); - n_a1prime=PTypeStructure.(iistr).n_a(1)+(PTypeStructure.(iistr).n_a(1)-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; - n_aprime=[n_a1prime,PTypeStructure.(iistr).n_a(2:end)]; - a1prime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).n_a(1))',a1_grid,gpuArray(linspace(1,PTypeStructure.(iistr).n_a(1),n_a1prime))'); - aprime_grid=[a1prime_grid; PTypeStructure.(iistr).a_grid(PTypeStructure.(iistr).n_a(1)+1:end)]; - PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); - end - end - PTypeStructure.(iistr).d_gridvals=CreateGridvals(PTypeStructure.(iistr).n_d,gpuArray(PTypeStructure.(iistr).d_grid),1); - % if N_d==0 - % PTypeStructure.(iistr).daprime_gridvals=gpuArray(PTypeStructure.(iistr).a_gridvals); - % else - % PTypeStructure.(iistr).daprime_gridvals=gpuArray([kron(ones(N_a,1),CreateGridvals(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).d_grid,1)), kron(PTypeStructure.(iistr).a_gridvals,ones(PTypeStructure.(iistr).N_d,1))]); % daprime_gridvals is [N_d*N_aprime,l_d+l_aprime] - % end - else - % to be able to EvalFnsOnAgentDist using fastOLG we also need - PTypeStructure.(iistr).a_gridvals=gpuArray(CreateGridvals(PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).a_grid,1)); % a_grivdals is [N_a,l_a] - % use fine grid for aprime_gridvals - if PTypeStructure.(iistr).vfoptions.gridinterplayer==0 - PTypeStructure.(iistr).aprime_gridvals=PTypeStructure.(iistr).a_gridvals; - elseif PTypeStructure.(iistr).vfoptions.gridinterplayer==1 - if isscalar(PTypeStructure.(iistr).n_a) - n_aprime=PTypeStructure.(iistr).n_a+(PTypeStructure.(iistr).n_a-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; - aprime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).N_a)',PTypeStructure.(iistr).a_grid,gpuArray(linspace(1,PTypeStructure.(iistr).N_a,n_aprime))'); - PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); - else - a1_grid=PTypeStructure.(iistr).a_grid(1:PTypeStructure.(iistr).n_a(1)); - n_a1prime=PTypeStructure.(iistr).n_a(1)+(PTypeStructure.(iistr).n_a(1)-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; - n_aprime=[n_a1prime,PTypeStructure.(iistr).n_a(2:end)]; - a1prime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).n_a(1))',a1_grid,gpuArray(linspace(1,PTypeStructure.(iistr).n_a(1),n_a1prime))'); - aprime_grid=[a1prime_grid; PTypeStructure.(iistr).a_grid(PTypeStructure.(iistr).n_a(1)+1:end)]; - PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); - end - end - PTypeStructure.(iistr).d_gridvals=CreateGridvals(PTypeStructure.(iistr).n_d,gpuArray(PTypeStructure.(iistr).d_grid),1); - % if N_d==0 - % PTypeStructure.(iistr).daprime_gridvals=gpuArray(PTypeStructure.(iistr).a_gridvals); - % else - % PTypeStructure.(iistr).daprime_gridvals=gpuArray([kron(ones(N_a,1),CreateGridvals(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).d_grid,1)), kron(PTypeStructure.(iistr).a_gridvals,ones(PTypeStructure.(iistr).N_d,1))]); % daprime_gridvals is [N_d*N_aprime,l_d+l_aprime] - % end end + PTypeStructure.(iistr).d_gridvals=CreateGridvals(PTypeStructure.(iistr).n_d,gpuArray(PTypeStructure.(iistr).d_grid),1); + % if N_d==0 + % PTypeStructure.(iistr).daprime_gridvals=gpuArray(PTypeStructure.(iistr).a_gridvals); + % else + % PTypeStructure.(iistr).daprime_gridvals=gpuArray([kron(ones(N_a,1),CreateGridvals(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).d_grid,1)), kron(PTypeStructure.(iistr).a_gridvals,ones(PTypeStructure.(iistr).N_d,1))]); % daprime_gridvals is [N_d*N_aprime,l_d+l_aprime] + % end if isa(pi_z,'struct') PTypeStructure.(iistr).pi_z=pi_z.(iistr); % Different grids by permanent type, but not depending on age. (same as the case just above; this case can occour with or without the existence of vfoptions, as long as there is no vfoptions.agedependentgrids) else PTypeStructure.(iistr).pi_z=pi_z; end - + + % If using any non-standard endogenous states, setup for those (both FHorz and InfHorz btw) + [PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions]=SetupNonStandardEndoStates_FHorz_TPath(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); + PTypeStructure.(iistr).ReturnFn=ReturnFn; if isa(ReturnFn,'struct') PTypeStructure.(iistr).ReturnFn=ReturnFn.(iistr); @@ -517,8 +460,8 @@ l_d=length(PTypeStructure.(iistr).n_d); end if PTypeStructure.(iistr).vfoptions.experienceasset - l_aprime=length(PTypeStructure.(iistr).n_a1); - l_a=l_aprime+length(PTypeStructure.(iistr).n_a2); + l_aprime=length(PTypeStructure.(iistr).vfoptions.setup_experienceasset.n_a1); + l_a=l_aprime+length(PTypeStructure.(iistr).vfoptions.setup_experienceasset.n_a2); else l_aprime=length(PTypeStructure.(iistr).n_a); l_a=l_aprime; @@ -594,13 +537,10 @@ % Convert z and e to age-dependent joint-grids and transtion matrix % output: z_gridvals_J, pi_z_J, e_gridvals_J, pi_e_J, transpathoptions,vfoptions,simoptions - %% If using any non-standard endogenous states, setup for those (both FHorz and InfHorz btw) - [PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions]=SetupNonStandardEndoStates_FHorz_TPath(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); - %% Organise V_final, AgeWeights and AgentDist_init % Reshape V_final + N_j_temp=PTypeStructure.(iistr).N_j; if transpathoptions.fastOLG==0 - N_j_temp=PTypeStructure.(iistr).N_j; if N_z==0 if N_e==0 V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); @@ -615,7 +555,6 @@ end end else % transpathoptions.fastOLG==1 - N_j_temp=PTypeStructure.(iistr).N_j; if N_z==0 if N_e==0 V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); @@ -634,7 +573,6 @@ % Reshape AgentDist_initial and turn AgeWeights_T into appropriate size so that we can always just do AgentDist.*AgeWeights % Note when simoptions.fastOLG==1 we have shapes of [N_a*N_j_temp*whatever,1-or-N_e] instead of [N_a,whatever-and-maybe-N_e,N_j_temp] AgentDist_init=AgentDist_initial.(iistr); - N_j_temp=PTypeStructure.(iistr).N_j; if N_z==0 if N_e==0 AgentDist_init=reshape(AgentDist_init,[N_a,N_j_temp]); % if simoptions.fastOLG==0 diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m index 6393b470..26d2b680 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m @@ -372,8 +372,8 @@ - %% Check if pi_z and z_grid can be precomputed - % Note: cannot handle that whether not not they can be precomputed differs across ptypes + %% Check if pi_z and z_grid can be pre-computed + % Note: cannot handle that whether not not they can be pre-computed differs across ptypes transpathoptions.zpathprecomputed=1; transpathoptions.zpathtrivial=1; % z_grid_J and pi_z_J are not varying over the path if isfield(PTypeStructure.(iistr).vfoptions,'ExogShockFn') @@ -428,8 +428,8 @@ end end end - %% If used, check if pi_e and e_grid can be procomputed - % Note: cannot handle that whether not not they can be precomputed differs across ptypes + %% If used, check if pi_e and e_grid can be pre-computed + % Note: cannot handle that whether not not they can be pre-computed differs across ptypes if using_e_var==1 % Check if e_grid and/or pi_e depend on prices. If not then create pi_e_J and e_grid_J for the entire transition before we start transpathoptions.epathprecomputed=1; diff --git a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m index dede3528..43a25175 100644 --- a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m @@ -165,15 +165,29 @@ end end -%% Get AgeWeights from Parameters +%% Get AgeWeights from Parameters if isstruct(AgeWeightsParamNames) for ii=1:N_i try AgeWeights=Parameters.(AgeWeightsParamNames.(Names_i{ii}){1}); - if length(AgeWeights)~=N_j.(Names_i{ii}) - error('Ageweights does not have age-like length') + if isstruct(N_j) + if length(AgeWeights)~=N_j.(Names_i{ii}) + error('Ageweights does not have age-like length') + else + break + end + elseif isscalar(N_j) + if length(AgeWeights)~=N_j + error('Ageweights does not have age-like length') + else + break + end else - break + if length(AgeWeights)~=N_j(ii) + error('Ageweights does not have age-like length') + else + break + end end catch if ii==N_i @@ -203,11 +217,11 @@ if isstruct(GeneralEqmEqns) if length(PricePathNames)~=length(fieldnames(GeneralEqmEqns)) fprintf('length(PricePathNames)=%i and length(fieldnames(GeneralEqmEqns))=%i (relates to following error) \n', length(PricePathNames), length(fieldnames(GeneralEqmEqns))) - error('Initial PricePath contains fewer variables than GeneralEqmEqns (structure) \n') + error('Initial PricePath contains less variables than GeneralEqmEqns (structure) \n') end else if length(PricePathNames)~=length(GeneralEqmEqns) - error('Initial PricePath contains fewer variables than GeneralEqmEqns') + error('Initial PricePath contains less variables than GeneralEqmEqns') end end @@ -255,10 +269,15 @@ PTypeStructure.(iistr).simoptions=PType_Options(simoptions,Names_i,ii); % some simoptions will differ by permanent type, will clean these up as we go before they are passed end + % The parameter names can be made to depend on the permanent-type + PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames; + if isa(DiscountFactorParamNames,'struct') + PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(Names_i{ii}); + end + % Need to fill in some defaults PTypeStructure.(iistr).vfoptions.parallel=2; % hardcode PTypeStructure.(iistr).simoptions.parallel=2; % hardcode - PTypeStructure.(iistr).simoptions.fastOLG=0; % hardcode TESTING if ~isfield(PTypeStructure.(iistr).vfoptions,'n_e') PTypeStructure.(iistr).n_e=0; else @@ -376,101 +395,44 @@ PTypeStructure.(iistr).z_grid=gpuArray(z_grid); end - if PTypeStructure.(iistr).vfoptions.experienceasset - % Borrowed from TransitionPaths/InfHorz/TransitionPath_Case1.m - % Split decision variables into the standard ones and the one relevant to the experience asset - if isscalar(PTypeStructure.(iistr).n_d) - PTypeStructure.(iistr).n_d1=0; - else - PTypeStructure.(iistr).n_d1=PTypeStructure.(iistr).n_d(1:end-1); - end - PTypeStructure.(iistr).n_d2=PTypeStructure.(iistr).n_d(end); % n_d2 is the decision variable that influences next period vale of the experience asset - PTypeStructure.(iistr).d1_grid=PTypeStructure.(iistr).d_grid(1:sum(PTypeStructure.(iistr).n_d1)); - PTypeStructure.(iistr).d2_grid=PTypeStructure.(iistr).d_grid(sum(PTypeStructure.(iistr).n_d1)+1:end); - % Split endogenous assets into the standard ones and the experience asset + % to be able to EvalFnsOnAgentDist using fastOLG we also need + PTypeStructure.(iistr).a_gridvals=gpuArray(CreateGridvals(PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).a_grid,1)); % a_grivdals is [N_a,l_a] + % use fine grid for aprime_gridvals + if PTypeStructure.(iistr).vfoptions.gridinterplayer==0 + PTypeStructure.(iistr).aprime_gridvals=PTypeStructure.(iistr).a_gridvals; + elseif PTypeStructure.(iistr).vfoptions.gridinterplayer==1 if isscalar(PTypeStructure.(iistr).n_a) - PTypeStructure.(iistr).n_a1=0; - else - PTypeStructure.(iistr).n_a1=PTypeStructure.(iistr).n_a(1:end-1); - end - PTypeStructure.(iistr).n_a2=PTypeStructure.(iistr).n_a(end); % n_a2 is the experience asset - PTypeStructure.(iistr).a1_grid=PTypeStructure.(iistr).a_grid(1:sum(PTypeStructure.(iistr).n_a1)); - PTypeStructure.(iistr).a2_grid=PTypeStructure.(iistr).a_grid(sum(PTypeStructure.(iistr).n_a1)+1:end); - - % aprimeFnParamNames in same fashion - l_d2=length(PTypeStructure.(iistr).n_d2); - l_a2=length(PTypeStructure.(iistr).n_a2); - temp=getAnonymousFnInputNames(PTypeStructure.(iistr).vfoptions.aprimeFn); - if length(temp)>(l_d2+l_a2) - PTypeStructure.(iistr).aprimeFnParamNames={temp{l_d2+l_a2+1:end}}; % the first inputs will always be (d2,a2) + n_aprime=PTypeStructure.(iistr).n_a+(PTypeStructure.(iistr).n_a-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; + aprime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).N_a)',PTypeStructure.(iistr).a_grid,gpuArray(linspace(1,PTypeStructure.(iistr).N_a,n_aprime))'); + PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); else - PTypeStructure.(iistr).aprimeFnParamNames={}; - end - - PTypeStructure.(iistr).N_a1=prod(PTypeStructure.(iistr).n_a1); - - % to be able to EvalFnsOnAgentDist using fastOLG we also need - PTypeStructure.(iistr).a_gridvals=gpuArray(CreateGridvals(PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).a_grid,1)); % a_grivdals is [N_a,l_a] - % use fine grid for aprime_gridvals - if PTypeStructure.(iistr).vfoptions.gridinterplayer==0 - PTypeStructure.(iistr).aprime_gridvals=PTypeStructure.(iistr).a_gridvals; - elseif PTypeStructure.(iistr).vfoptions.gridinterplayer==1 - if isscalar(PTypeStructure.(iistr).n_a) - n_aprime=PTypeStructure.(iistr).n_a+(PTypeStructure.(iistr).n_a-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; - aprime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).N_a)',PTypeStructure.(iistr).a_grid,gpuArray(linspace(1,PTypeStructure.(iistr).N_a,n_aprime))'); - PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); - else - a1_grid=PTypeStructure.(iistr).a_grid(1:PTypeStructure.(iistr).n_a(1)); - n_a1prime=PTypeStructure.(iistr).n_a(1)+(PTypeStructure.(iistr).n_a(1)-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; - n_aprime=[n_a1prime,PTypeStructure.(iistr).n_a(2:end)]; - a1prime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).n_a(1))',a1_grid,gpuArray(linspace(1,PTypeStructure.(iistr).n_a(1),n_a1prime))'); - aprime_grid=[a1prime_grid; PTypeStructure.(iistr).a_grid(PTypeStructure.(iistr).n_a(1)+1:end)]; - PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); - end + a1_grid=PTypeStructure.(iistr).a_grid(1:PTypeStructure.(iistr).n_a(1)); + n_a1prime=PTypeStructure.(iistr).n_a(1)+(PTypeStructure.(iistr).n_a(1)-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; + n_aprime=[n_a1prime,PTypeStructure.(iistr).n_a(2:end)]; + a1prime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).n_a(1))',a1_grid,gpuArray(linspace(1,PTypeStructure.(iistr).n_a(1),n_a1prime))'); + aprime_grid=[a1prime_grid; PTypeStructure.(iistr).a_grid(PTypeStructure.(iistr).n_a(1)+1:end)]; + PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); end - PTypeStructure.(iistr).d_gridvals=CreateGridvals(PTypeStructure.(iistr).n_d,gpuArray(PTypeStructure.(iistr).d_grid),1); - % if N_d==0 - % PTypeStructure.(iistr).daprime_gridvals=gpuArray(PTypeStructure.(iistr).a_gridvals); - % else - % PTypeStructure.(iistr).daprime_gridvals=gpuArray([kron(ones(N_a,1),CreateGridvals(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).d_grid,1)), kron(PTypeStructure.(iistr).a_gridvals,ones(PTypeStructure.(iistr).N_d,1))]); % daprime_gridvals is [N_d*N_aprime,l_d+l_aprime] - % end - else - % to be able to EvalFnsOnAgentDist using fastOLG we also need - PTypeStructure.(iistr).a_gridvals=gpuArray(CreateGridvals(PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).a_grid,1)); % a_grivdals is [N_a,l_a] - % use fine grid for aprime_gridvals - if PTypeStructure.(iistr).vfoptions.gridinterplayer==0 - PTypeStructure.(iistr).aprime_gridvals=PTypeStructure.(iistr).a_gridvals; - elseif PTypeStructure.(iistr).vfoptions.gridinterplayer==1 - if isscalar(PTypeStructure.(iistr).n_a) - n_aprime=PTypeStructure.(iistr).n_a+(PTypeStructure.(iistr).n_a-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; - aprime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).N_a)',PTypeStructure.(iistr).a_grid,gpuArray(linspace(1,PTypeStructure.(iistr).N_a,n_aprime))'); - PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); - else - a1_grid=PTypeStructure.(iistr).a_grid(1:PTypeStructure.(iistr).n_a(1)); - n_a1prime=PTypeStructure.(iistr).n_a(1)+(PTypeStructure.(iistr).n_a(1)-1)*PTypeStructure.(iistr).vfoptions.ngridinterp; - n_aprime=[n_a1prime,PTypeStructure.(iistr).n_a(2:end)]; - a1prime_grid=interp1(gpuArray(1:1:PTypeStructure.(iistr).n_a(1))',a1_grid,gpuArray(linspace(1,PTypeStructure.(iistr).n_a(1),n_a1prime))'); - aprime_grid=[a1prime_grid; PTypeStructure.(iistr).a_grid(PTypeStructure.(iistr).n_a(1)+1:end)]; - PTypeStructure.(iistr).aprime_gridvals=CreateGridvals(n_aprime,aprime_grid,1); - end - end - PTypeStructure.(iistr).d_gridvals=CreateGridvals(PTypeStructure.(iistr).n_d,gpuArray(PTypeStructure.(iistr).d_grid),1); - % if N_d==0 - % PTypeStructure.(iistr).daprime_gridvals=gpuArray(PTypeStructure.(iistr).a_gridvals); - % else - % PTypeStructure.(iistr).daprime_gridvals=gpuArray([kron(ones(N_a,1),CreateGridvals(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).d_grid,1)), kron(PTypeStructure.(iistr).a_gridvals,ones(PTypeStructure.(iistr).N_d,1))]); % daprime_gridvals is [N_d*N_aprime,l_d+l_aprime] - % end end + PTypeStructure.(iistr).d_gridvals=CreateGridvals(PTypeStructure.(iistr).n_d,gpuArray(PTypeStructure.(iistr).d_grid),1); + % if N_d==0 + % PTypeStructure.(iistr).daprime_gridvals=gpuArray(PTypeStructure.(iistr).a_gridvals); + % else + % PTypeStructure.(iistr).daprime_gridvals=gpuArray([kron(ones(N_a,1),CreateGridvals(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).d_grid,1)), kron(PTypeStructure.(iistr).a_gridvals,ones(PTypeStructure.(iistr).N_d,1))]); % daprime_gridvals is [N_d*N_aprime,l_d+l_aprime] + % end if isa(pi_z,'struct') - PTypeStructure.(iistr).pi_z=pi_z.(iistr); % Different grids by permanent type, but not depending on age. (same as the case just above; this case can occour with or without the existence of vfoptions, as long as there is no vfoptions.agedependentgrids) + PTypeStructure.(iistr).pi_z=pi_z.(Names_i{ii}); % Different grids by permanent type, but not depending on age. (same as the case just above; this case can occour with or without the existence of vfoptions, as long as there is no vfoptions.agedependentgrids) else PTypeStructure.(iistr).pi_z=pi_z; end - + + % If using any non-standard endogenous states, setup for those (both FHorz and InfHorz btw) + [PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions]=SetupNonStandardEndoStates_FHorz_TPath(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); + PTypeStructure.(iistr).ReturnFn=ReturnFn; if isa(ReturnFn,'struct') - PTypeStructure.(iistr).ReturnFn=ReturnFn.(iistr); + PTypeStructure.(iistr).ReturnFn=ReturnFn.(Names_i{ii}); end % Parameters are allowed to be given as structure, or as vector/matrix (in terms of their dependence on permanent type). @@ -482,8 +444,8 @@ for kField=1:nFields if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) - if isfield(Parameters.(FullParamNames{kField}),iistr) - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(iistr); + if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); end elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. temp=Parameters.(FullParamNames{kField}); @@ -507,7 +469,7 @@ % The parameter names can be made to depend on the permanent-type PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames; if isa(DiscountFactorParamNames,'struct') - PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(iistr); + PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(Names_i{ii}); end % Implement new way of handling ReturnFn inputs (note l_d, l_a, l_z are just created for this and then not used for anything else later) @@ -517,8 +479,8 @@ l_d=length(PTypeStructure.(iistr).n_d); end if PTypeStructure.(iistr).vfoptions.experienceasset - l_aprime=length(PTypeStructure.(iistr).n_a1); - l_a=l_aprime+length(PTypeStructure.(iistr).n_a2); + l_aprime=length(PTypeStructure.(iistr).vfoptions.setup_experienceasset.n_a1); + l_a=l_aprime+length(PTypeStructure.(iistr).vfoptions.setup_experienceasset.n_a2); else l_aprime=length(PTypeStructure.(iistr).n_a); l_a=l_aprime; @@ -530,12 +492,8 @@ if isfield(PTypeStructure.(iistr).vfoptions,'SemiExoStateFn') l_z=l_z+length(PTypeStructure.(iistr).vfoptions.n_semiz); end - l_e=0; - if isfield(PTypeStructure.(iistr).vfoptions,'n_e') - if PTypeStructure.(iistr).vfoptions.n_e(1)~=0 - l_e=length(PTypeStructure.(iistr).vfoptions.n_e); - end - end + l_e=PTypeStructure.(iistr).l_e; + %% Implement new way of handling ReturnFn inputs ReturnFnParamNames=ReturnFnParamNamesFn(PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).vfoptions,Parameters); PTypeStructure.(iistr).ReturnFnParamNames=ReturnFnParamNames; @@ -544,17 +502,15 @@ %% Figure out which functions are actually relevant to the present PType. And then change to FnsToEvaluate as cell so that it is not being recomputed all the time % Only the relevant ones need to be evaluated. % The dependence of FnsToEvaluateFn and FnsToEvaluateFnParamNames are necessarily the same. - PTypeStructure.(iistr).FnsToEvaluate={}; - PTypeStructure.(iistr).FnsToEvaluateParamNames={}; FnNames=fieldnames(FnsToEvaluate); PTypeStructure.numFnsToEvaluate=length(fieldnames(FnsToEvaluate)); PTypeStructure.(iistr).WhichFnsForCurrentPType=zeros(PTypeStructure.numFnsToEvaluate,1); jj=1; % jj indexes the FnsToEvaluate that are relevant to the current PType for kk=1:PTypeStructure.numFnsToEvaluate - if isstruct(FnsToEvaluate.(FnNames{kk})) - if isfield(FnsToEvaluate.(FnNames{kk}), iistr) - PTypeStructure.(iistr).FnsToEvaluate.(FnNames{kk})=FnsToEvaluate.(FnNames{kk}).(iistr); + if isa(FnsToEvaluate.(FnNames{kk}),'struct') + if isfield(FnsToEvaluate.(FnNames{kk}), Names_i{ii}) + PTypeStructure.(iistr).FnsToEvaluate.(FnNames{kk})=FnsToEvaluate.(FnNames{kk}).(Names_i{ii}); % % Figure out FnsToEvaluateParamNames % temp=getAnonymousFnInputNames(FnsToEvaluate.(FnNames{kk}).(Names_i{ii})); % PTypeStructure.(iistr).FnsToEvaluateParamNames(jj).Names={temp{l_d+l_a+l_a+l_z+l_e+1:end}}; % the first inputs will always be (d,aprime,a,z) @@ -569,12 +525,11 @@ PTypeStructure.(iistr).FnsToEvaluate.(FnNames{kk})=FnsToEvaluate.(FnNames{kk}); % Figure out FnsToEvaluateParamNames temp=getAnonymousFnInputNames(FnsToEvaluate.(FnNames{kk})); - PTypeStructure.(iistr).FnsToEvaluateParamNames(jj).Names={temp{l_d+l_aprime+l_a+l_z+l_e+1:end}}; % the first inputs will always be (d,aprime,a,z) + PTypeStructure.(iistr).FnsToEvaluateParamNames(jj).Names={temp{l_d+l_a+l_a+l_z+l_e+1:end}}; % the first inputs will always be (d,aprime,a,z) PTypeStructure.(iistr).WhichFnsForCurrentPType(kk)=jj; jj=jj+1; PTypeStructure.FnsAndPTypeIndicator(kk,ii)=1; end end - % Now that all the relevant FnsToEvaluate for type ii are in PTypeStructure.(iistr).FnsToEvaluate PTypeStructure.(iistr).l_daprime=PTypeStructure.(iistr).l_d+PTypeStructure.(iistr).l_aprime; PTypeStructure.(iistr).AggVarNames=fieldnames(PTypeStructure.(iistr).FnsToEvaluate); @@ -591,97 +546,204 @@ % Change FnsToEvaluate out of structure form, but want to still create AggVars as a structure PTypeStructure.(iistr).simoptions.outputasstructure=1; + %% Check if pi_z and z_grid can be pre-computed + % Note: cannot handle that whether not not they can be pre-computed differs across ptypes + transpathoptions.zpathprecomputed=1; + transpathoptions.zpathtrivial=1; % z_grid_J and pi_z_J are not varying over the path + if isfield(PTypeStructure.(iistr).vfoptions,'ExogShockFn') + transpathoptions.zpathprecomputed=0; + N_z=prod(PTypeStructure.(iistr).n_z); + % Note: If ExogShockFn depends on the path, it must be done via a parameter + % that depends on the path (i.e., via ParamPath or PricePath) + PTypeStructure.(iistr).vfoptions.ExogShockFnParamNames=getAnonymousFnInputNames(PTypeStructure.(iistr).vfoptions.ExogShockFn); + overlap=0; + for pp=1:length(PTypeStructure.(iistr).vfoptions.ExogShockFnParamNames) + if strcmp(PTypeStructure.(iistr).vfoptions.ExogShockFnParamNames{pp},PricePathNames) + overlap=1; + end + end + if overlap==0 + transpathoptions.zpathprecomputed=1; + % If ExogShockFn does not depend on any of the prices (in PricePath), then + % we can simply create it now rather than within each 'subfn' or 'p_grid' + + % Check if it depends on the ParamPath + transpathoptions.zpathtrivial=1; + for pp=1:length(PTypeStructure.(iistr).vfoptions.ExogShockFnParamNames) + if strcmp(PTypeStructure.(iistr).vfoptions.ExogShockFnParamNames{pp},ParamPathNames) + transpathoptions.zpathtrivial=0; + end + end + if transpathoptions.zpathtrivial==1 + ExogShockFnParamsVec=CreateVectorFromParams(PTypeStructure.(iistr).Parameters, PTypeStructure.(iistr).vfoptions.ExogShockFnParamNames); + ExogShockFnParamsCell=cell(length(ExogShockFnParamsVec),1); + for pp=1:length(ExogShockFnParamsVec) + ExogShockFnParamsCell(pp,1)={ExogShockFnParamsVec(pp)}; + end + [z_grid,pi_z]=vfoptions.ExogShockFn(ExogShockFnParamsCell{:}); + elseif transpathoptions.zpathtrivial==0 + % z_grid and/or pi_z varies along the transition path (but only depending on ParamPath, not PricePath + transpathoptions.(iistr).pi_z_T=zeros(N_z,N_z,T,'gpuArray'); + transpathoptions.(iistr).z_grid_T=zeros(sum(n_z),T,'gpuArray'); + for tt=1:T + for pp=1:length(ParamPathNames) + PTypeStructure.(iistr).Parameters.(ParamPathNames{pp})=ParamPathStruct.(ParamPathNames{pp}); + end + % Note, we know the PricePath is irrelevant for the current purpose + ExogShockFnParamsVec=CreateVectorFromParams(PTypeStructure.(iistr).Parameters, PTypeStructure.(iistr).vfoptions.ExogShockFnParamNames); + ExogShockFnParamsCell=cell(length(ExogShockFnParamsVec),1); + for pp=1:length(ExogShockFnParamsVec) + ExogShockFnParamsCell(pp,1)={ExogShockFnParamsVec(pp)}; + end + [z_grid,pi_z]=PTypeStructure.(iistr).vfoptions.ExogShockFn(ExogShockFnParamsCell{:}); + transpathoptions.(iistr).pi_z_T(:,:,tt)=pi_z; + transpathoptions.(iistr).z_grid_T(:,tt)=z_grid; + end + end + end + end + %% If used, check if pi_e and e_grid can be pre-computed + % Note: cannot handle that whether not not they can be pre-computed differs across ptypes + if l_e>0 + % Check if e_grid and/or pi_e depend on prices. If not then create pi_e_J and e_grid_J for the entire transition before we start + transpathoptions.epathprecomputed=1; + transpathoptions.epathtrivial=1; % e_grid_J and pi_e_J are not varying over the path + if isfield(PTypeStructure.(iistr).vfoptions,'EiidShockFn') + transpathoptions.epathprecomputed=0; + % Note: If EiidShockFn depends on the path, it must be done via a parameter + % that depends on the path (i.e., via ParamPath or PricePath) + PTypeStructure.(iistr).vfoptions.EiidShockFnParamNames=getAnonymousFnInputNames(PTypeStructure.(iistr).vfoptions.EiidShockFn); + overlap=0; + for pp=1:length(PTypeStructure.(iistr).vfoptions.EiidShockFnParamNames) + if strcmp(PTypeStructure.(iistr).vfoptions.EiidShockFnParamNames{pp},PricePathNames) + overlap=1; + end + end + if overlap==0 + transpathoptions.epathprecomputed=1; + % If ExogShockFn does not depend on any of the prices (in PricePath), then + % we can simply create it now rather than within each 'subfn' or 'p_grid' + + % Check if it depends on the ParamPath + transpathoptions.epathtrivial=1; + for pp=1:length(PTypeStructure.(iistr).vfoptions.EiidShockFnParamNames) + if strcmp(PTypeStructure.(iistr).vfoptions.EiidShockFnParamNames{pp},ParamPathNames) + transpathoptions.epathtrivial=0; + end + end + if transpathoptions.epathtrivial==1 + EiidShockFnParamsVec=CreateVectorFromParams(PTypeStructure.(iistr).Parameters, PTypeStructure.(iistr).vfoptions.EiidShockFnParamNames); + EiidShockFnParamsCell=cell(length(EiidShockFnParamsVec),1); + for pp=1:length(EiidShockFnParamsVec) + EiidShockFnParamsCell(pp,1)={EiidShockFnParamsVec(pp)}; + end + [e_grid,pi_e]=PTypeStructure.(iistr).vfoptions.EiidShockFn(EiidShockFnParamsCell{:}); + % Now store them in vfoptions and simoptions + PTypeStructure.(iistr).vfoptions.pi_e=pi_e; + PTypeStructure.(iistr).vfoptions.e_grid=e_grid; + PTypeStructure.(iistr).simoptions.pi_e=pi_e; + PTypeStructure.(iistr).simoptions.e_grid=e_grid; + elseif transpathoptions.epathtrivial==0 + % e_grid_T and/or pi_e_T varies along the transition path (but only depending on ParamPath, not PricePath) + transpathoptions.(iistr).pi_e_T=zeros(N_e,T,'gpuArray'); + transpathoptions.(iistr).e_grid_T=zeros(sum(PTypeStructure.(iistr).n_e),T,'gpuArray'); + for tt=1:T + for pp=1:length(ParamPathNames) + PTypeStructure.(iistr).Parameters.(ParamPathNames{pp})=ParamPathStruct.(ParamPathNames{pp}); + end + % Note, we know the PricePath is irrelevant for the current purpose + EiidShockFnParamsVec=CreateVectorFromParams(PTypeStructure.(iistr).Parameters, vPTypeStructure.(iistr).foptions.EiidShockFnParamNames); + EiidShockFnParamsCell=cell(length(EiidShockFnParamsVec),1); + for pp=1:length(ExogShockFnParamsVec) + EiidShockFnParamsCell(pp,1)={EiidShockFnParamsVec(pp)}; + end + [e_grid,pi_e]=PTypeStructure.(iistr).vfoptions.ExogShockFn(EiidShockFnParamsCell{:}); + + transpathoptions.(iistr).pi_e_T(:,tt)=pi_e; + transpathoptions.(iistr).e_grid_T(:,tt)=e_grid; + end + end + end + end + end - %% Set up exogenous shock processes if isfinite(PTypeStructure.(iistr).N_j) + PTypeStructure.(iistr).simoptions.fastOLG=0; % hardcode + + %% Set up exogenous shock processes [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).pi_z_J_sim, PTypeStructure.(iistr).e_gridvals_J, PTypeStructure.(iistr).pi_e_J, PTypeStructure.(iistr).pi_e_J_sim, PTypeStructure.(iistr).ze_gridvals_J_fastOLG, transpathoptions, PTypeStructure.(iistr).simoptions]=ExogShockSetup_TPath_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); % Convert z and e to age-dependent joint-grids and transtion matrix % output: z_gridvals_J, pi_z_J, e_gridvals_J, pi_e_J, transpathoptions,vfoptions,simoptions - else - [PTypeStructure.(iistr).z_gridvals, PTypeStructure.(iistr).pi_z, transpathoptions, PTypeStructure.(iistr).simoptions]=ExogShockSetup_TPath_InfHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); - end - %% If using any non-standard endogenous states, setup for those (both FHorz and InfHorz btw) - [PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions]=SetupNonStandardEndoStates_FHorz_TPath(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); - - %% Organise V_final and AgentDist_initial - % Reshape V_final - if ~isfinite(PTypeStructure.(iistr).N_j) - % If no z, then N_z=1 here - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z]); - elseif transpathoptions.fastOLG==0 + %% Organise V_final and AgentDist_initial + % Reshape V_final N_j_temp=PTypeStructure.(iistr).N_j; - if N_z==0 - if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); - else - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_e,N_j_temp]); - end - else - if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_j_temp]); - else - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_e,N_j_temp]); - end - end - else % transpathoptions.fastOLG==1 - N_j_temp=PTypeStructure.(iistr).N_j; - if N_z==0 - if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); + if transpathoptions.fastOLG==0 + if N_z==0 + if N_e==0 + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); + else + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_e,N_j_temp]); + end else - V_final.(iistr)=reshape(permute(V_final.(iistr),[1,3,2]),[N_a*N_j_temp,N_e]); + if N_e==0 + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_j_temp]); + else + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_e,N_j_temp]); + end end else - if N_e==0 - V_final.(iistr)=reshape(permute(V_final.(iistr),[1,3,2]),[N_a*N_j_temp,N_z]); + if N_z==0 + if N_e==0 + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); + else + V_final.(iistr)=reshape(permute(V_final.(iistr),[1,3,2]),[N_a*N_j_temp,N_e]); + end else - V_final.(iistr)=reshape(permute(V_final.(iistr),[1,4,2,3]),[N_a*N_j_temp,N_z,N_e]); + if N_e==0 + V_final.(iistr)=reshape(permute(V_final.(iistr),[1,3,2]),[N_a*N_j_temp,N_z]); + else + V_final.(iistr)=reshape(permute(V_final.(iistr),[1,4,2,3]),[N_a*N_j_temp,N_z,N_e]); + end end end - end - % Reshape AgentDist_initial and turn AgeWeights_T into appropriate size so that we can always just do AgentDist.*AgeWeights - % Note when simoptions.fastOLG==1 we have shapes of [N_a*N_j_temp*whatever,1-or-N_e] instead of [N_a,whatever-and-maybe-N_e,N_j_temp] - AgentDist_init=AgentDist_initial.(iistr); - if isfinite(PTypeStructure.(iistr).N_j) - N_j_temp=PTypeStructure.(iistr).N_j; + % Reshape AgentDist_initial if N_z==0 if N_e==0 - AgentDist_init=reshape(AgentDist_init,[N_a,N_j_temp]); % if simoptions.fastOLG==0 - AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] - if PTypeStructure.(iistr).simoptions.fastOLG - AgentDist_init=reshape(AgentDist_init,[N_a*N_j_temp,1]); - AgeWeights_init=repelem(AgeWeights_init',N_a,1); + AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j_ii] + if PTypeStructure.(iistr).simoptions.fastOLG==1 + AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_j_temp,1]); + AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); end else - AgentDist_init=reshape(AgentDist_init,[N_a*N_e,N_j_temp]); % if simoptions.fastOLG==0 - AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] - if PTypeStructure.(iistr).simoptions.fastOLG % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 - AgentDist_init=reshape(permute(reshape(AgentDist_init,[N_a,N_e,N_j_temp]),[1,3,2]),[N_a*N_j_temp,N_e]); - AgeWeights_init=repelem(AgeWeights_init',N_a,1); + AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_e,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j_ii] + if PTypeStructure.(iistr).simoptions.fastOLG==1 % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 + AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_e,N_j_temp]),[1,3,2]),[N_a*N_j_temp*N_e,1]); + AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); end end else if N_e==0 - AgentDist_init=reshape(AgentDist_init,[N_a*N_z,N_j_temp]); % if simoptions.fastOLG==0 - AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] - if PTypeStructure.(iistr).simoptions.fastOLG % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 - AgentDist_init=reshape(permute(reshape(AgentDist_init,[N_a,N_z,N_j_temp]),[1,3,2]),[N_a*N_j_temp*N_z,1]); - AgeWeights_init=repelem(AgeWeights_init',N_a,1); + AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_z,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j_ii] + if PTypeStructure.(iistr).simoptions.fastOLG==1 % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 + AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_z,N_j_temp]),[1,3,2]),[N_a*N_j_temp*N_z,1]); + AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); end else - AgentDist_init=reshape(AgentDist_init,[N_a*N_z*N_e,N_j_temp]); % if simoptions.fastOLG==0 - AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] - if PTypeStructure.(iistr).simoptions.fastOLG % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 - AgentDist_init=reshape(permute(reshape(AgentDist_init,[N_a,N_z,N_e,N_j_temp]),[1,4,2,3]),[N_a*N_j_temp*N_z,N_e]); - AgeWeights_init=repelem(AgeWeights_init',N_a,1); + AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_z*N_e,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j_ii] + if PTypeStructure.(iistr).simoptions.fastOLG==1 % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 + AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_z,N_e,N_j_temp]),[1,4,2,3]),[N_a*N_j_temp*N_z,N_e]); + AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); end end end - % Get AgeWeights and switch into the transpathoptions.ageweightstrivial=0 setup (and this is what subfns hardcode when doing PTypes) % It is assumed there is only one Age Weight Parameter (name)) - % AgeWeights_T is (a,j,z)-by-T (create as N_j-by-T to start, then switch) + % AgeWeights_T is (a,j,z)-by-T (create as j-by-T to start, then switch) if isstruct(AgeWeights) AgeWeights_ii=AgeWeights.(iistr); if all(size(AgeWeights_ii)==[N_j_temp,1]) @@ -691,7 +753,7 @@ % Does not depend on transition path period PTypeStructure.(iistr).AgeWeights_T=gather(AgeWeights_ii'.*ones(1,T)); else - fprintf('Following error applies to agent permanent type: %s \n',iistr) + fprintf('Following error applies to agent permanent type: %s \n',Names_i{ii}) error('The age weights parameter seems to be the wrong size') end else % not a structure, so must apply to all permanent types @@ -705,8 +767,30 @@ error('The age weights parameter seems to be the wrong size') end end + % Check ParamPath to see if the AgeWeights vary over the transition + % (and overwrite PTypeStructure.(iistr).AgeWeights_T if it does) + if isstruct(AgeWeightsParamNames) + temp=strcmp(ParamPathNames,AgeWeightsParamNames.(iistr){1}); + else + temp=strcmp(ParamPathNames,AgeWeightsParamNames{1}); + end + if any(temp) + transpathoptions.ageweightstrivial=0; % AgeWeights vary over the transition + [~,kk]=max(temp); % Get index for the AgeWeightsParamNames in ParamPathNames + % Create AgeWeights_T + PTypeStructure.(iistr).AgeWeights_T=ParamPath(:,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk))'; % This will always be N_j_ii-by-T (as transpose) + % Note: still leave it in ParamPath just in case it is used in AggVars or somesuch + end + % Because ptypes hardcodes transpathoptions.ageweightstrivial=0, we need + if PTypeStructure.(iistr).simoptions.fastOLG==1 + if N_z==0 + PTypeStructure.(iistr).AgeWeights_T=repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1); % simoptions.fastOLG=1 so this is (a,j)-by-1 + else + PTypeStructure.(iistr).AgeWeights_T=repmat(repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1),N_z,1); % simoptions.fastOLG=1 so this is (a,j,z)-by-1 + end + end - %% Set up jequalOneDist_T.(iistr) [hardcodes transpathoptions.trivialjequalonedist=0] + %% Set up jequalOneDist_T.(iistr) [hardcodes transpathoptions.trivialjequalonedist=0 and simoptions.fastOLG=1] if ~isstruct(jequalOneDist) jequalOneDist_temp=gpuArray(jequalOneDist); else % jequalOneDist is a structure @@ -742,42 +826,15 @@ if N_e==0 jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z,1]); % simoptions.fastOLG==1 else - jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z*N_e,1]); % simoptions.fastOLG==1 (how different than simoptions.fastOLG==0?) + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z*N_e,1]); % simoptions.fastOLG==1 end end PTypeStructure.(iistr).jequalOneDist=jequalOneDist_temp; end - - % Check ParamPath to see if the AgeWeights vary over the transition - % (and overwrite PTypeStructure.(iistr).AgeWeights_T if it does) - temp=strcmp(ParamPathNames,AgeWeightsParamNames.(iistr){1}); - if any(temp) - transpathoptions.ageweightstrivial=0; % AgeWeights vary over the transition - [~,kk]=max(temp); % Get index for the AgeWeightsParamNames{1} in ParamPathNames - % Create AgeWeights_T - PTypeStructure.(iistr).AgeWeights_T=ParamPath(:,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk))'; % This will always be N_j-by-T (as transpose) - % Note: still leave it in ParamPath just in case it is used in AggVars or somesuch - end - - % Because ptypes hardcodes transpathoptions.ageweightstrivial=0, we need - if PTypeStructure.(iistr).simoptions.fastOLG==1 - if N_z==0 - PTypeStructure.(iistr).AgeWeights_T=repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1); % simoptions.fastOLG=1 so this is (a,j)-by-1 - else - PTypeStructure.(iistr).AgeWeights_T=repmat(repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1),N_z,1); % simoptions.fastOLG=1 so this is (a,j,z)-by-1 - end - end - else % InfHorz case - if N_z==0 - AgentDist_init=reshape(AgentDist_init,[N_a,1]); - else - AgentDist_init=reshape(AgentDist_init,[N_a*N_z,1]); - end + else + % Presume all InfHorz ExogShock functions happen only with respect to transition paths (i.e., z_grid_T/e_grid_T and pi_z_T/pi_e_T end - AgentDist_initial.(iistr)=AgentDist_init; - clear AgentDist_init - %% Which parts of ParamPath and PricePath relate to ptype ii % Some ParamPath and PricePath parameters may depend on ptype PTypeStructure.(iistr).RelevantPricePath=ones(1,size(PricePath0,2)); % start will all relevant @@ -859,22 +916,16 @@ end if transpathoptions.verbose==1 - fprintf('Completed setup, beginning transition computination \n') + fprintf('Completed setup, beginning transition computation \n') end %% If using a shooting algorithm, set that up transpathoptions=setupGEnewprice3_shooting(transpathoptions,GeneralEqmEqns,PricePathNames,N_i,PricePathSizeVec); -%% Check if using _tminus1 and/or _tplus1 variables, and update PTypeStructure +%% Check if using _tminus1 and/or _tplus1 variables. if isstruct(FnsToEvaluate) && isstruct(GeneralEqmEqns) [tplus1priceNames,tminus1priceNames,tminus1AggVarsNames,tminus1paramNames,tplus1pricePathkk]=inputsFindtplus1tminus1(FnsToEvaluate,GeneralEqmEqns,PricePathNames,ParamPathNames,Names_i); - if isstruct(tminus1AggVarsNames) - AggVarsPTypes=fieldnames(tminus1AggVarsNames); - for ii=1:length(AggVarsPTypes) - PTypeStructure.(AggVarsPTypes{ii}).tminus1AggVarsNames=tminus1AggVarsNames.(AggVarsPTypes{ii}); - end - end else tplus1priceNames=[]; tminus1priceNames=[]; @@ -890,20 +941,9 @@ use_tminus1price=0; if ~isempty(tminus1priceNames) use_tminus1price=1; - if isstruct(tminus1AggVarsNames) - AggVarsPTypes=fieldnames(tminus1AggVarsNames); - for nn=1:length(AggVarsPTypes) - for ii=1:length(tminus1AggVarsNames.(AggVarsPTypes{nn})) - if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames.(AggVarsPTypes{nn}){ii}) - error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames.(AggVarsPTypes{nn}){ii}) - end - end - end - else - for ii=1:length(tminus1AggVarsNames) - if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames{ii}) - error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames{ii}) - end + for ii=1:length(tminus1priceNames) + if ~isfield(transpathoptions.initialvalues,tminus1priceNames{ii}) + error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1priceNames{ii}) end end end @@ -919,20 +959,9 @@ use_tminus1AggVars=0; if ~isempty(tminus1AggVarsNames) use_tminus1AggVars=1; - if isstruct(tminus1AggVarsNames) - AggVarsPTypes=fieldnames(tminus1AggVarsNames); - for nn=1:length(AggVarsPTypes) - for ii=1:length(tminus1AggVarsNames.(AggVarsPTypes{nn})) - if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames.(AggVarsPTypes{nn}){ii}) - error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames.(AggVarsPTypes{nn}){ii}) - end - end - end - else - for ii=1:length(tminus1AggVarsNames) - if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames{ii}) - error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames{ii}) - end + for ii=1:length(tminus1AggVarsNames) + if ~isfield(transpathoptions.initialvalues,tminus1AggVarsNames{ii}) + error('Using %s as an input (to FnsToEvaluate or GeneralEqmEqns) but it is not in transpathoptions.initialvalues \n',tminus1AggVarsNames{ii}) end end end @@ -953,9 +982,9 @@ %% Shooting algorithm if transpathoptions.GEnewprice~=2 - % For permanent types, there is just one shooting command, - % because things like z,e, and fastOLG, as well as ExpAsset are handled on a per-PType basis (to permit that they differ across ptype) - [PricePath,GEcondnPathmatrix]=TransitionPath_Case1_MixHorz_PType_shooting(PricePath0, PricePathNames, ParamPath, ParamPathNames, T, V_final, AgentDist_initial, FnsToEvaluate, GeneralEqmEqns, PricePathSizeVec, ParamPathSizeVec, PricePathSizeVec_ii, ParamPathSizeVec_ii, GEeqnNames,nGeneralEqmEqns,nGeneralEqmEqns_acrossptypes,GeneralEqmEqnsCell,GeneralEqmEqnParamNames, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, transpathoptions, PTypeStructure); + % For permanent type, there is just one shooting command, + % because things like z,e, and fastOLG are handled on a per-PType basis (to permit that they differ across ptype) + [PricePath,GEcondnPath]=TransitionPath_Case1_MixHorz_PType_shooting(PricePath0, PricePathNames, ParamPath, ParamPathNames, T, V_final, AgentDist_initial, FnsToEvaluate, GeneralEqmEqns, PricePathSizeVec, ParamPathSizeVec, PricePathSizeVec_ii, ParamPathSizeVec_ii, GEeqnNames,nGeneralEqmEqns,nGeneralEqmEqns_acrossptypes,GeneralEqmEqnsCell,GeneralEqmEqnParamNames, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, transpathoptions, PTypeStructure); % Switch the solution into structure for output. pp_indexinpricepath=zeros(1,length(PricePathNames)); @@ -984,9 +1013,6 @@ end end end - for gg=1:length(GEeqnNames) - GEcondnPath.(GEeqnNames{gg})=GEcondnPathmatrix(:,gg)'; - end if nargout==1 varargout={PricePathStruct}; diff --git a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType_shooting.m b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType_shooting.m index a12a9a2f..f5539c20 100644 --- a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType_shooting.m +++ b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType_shooting.m @@ -161,7 +161,7 @@ else %% Go from T-1 to 1 calculating the Value function and Optimal policy function at each step. warning("TransitionPath_Case1_MixHorz_PType_shooting not yet hanlding e_gridvals and/or pi_e") - [~,PolicyIndexesPath_ii]=TransitionPath_InfHorz_substeps_Step1_ValueFnIter(T,PolicyIndexesPath_ii,V_final.(iistr),PTypeStructure.(iistr).Parameters,PricePathOld_ii,ParamPath_ii,PricePathSizeVec_ii,ParamPathSizeVec_ii,PricePathNames_ii,ParamPathNames_ii,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).d_gridvals, PTypeStructure.(iistr).a_grid, PTypeStructure.(iistr).z_gridvals,[],PTypeStructure.(iistr).pi_z,[],PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).DiscountFactorParamNames, PTypeStructure.(iistr).ReturnFnParamNames, transpathoptions,PTypeStructure.(iistr).vfoptions); + [~,PolicyIndexesPath_ii]=TransitionPath_InfHorz_substeps_Step1_ValueFnIter(T,PolicyIndexesPath_ii,V_final.(iistr),PTypeStructure.(iistr).Parameters,PricePathOld_ii,ParamPath_ii,PricePathSizeVec_ii,ParamPathSizeVec_ii,PricePathNames_ii,ParamPathNames_ii,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).d_gridvals, PTypeStructure.(iistr).a_grid, PTypeStructure.(iistr).z_grid,[],PTypeStructure.(iistr).pi_z,[],PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).DiscountFactorParamNames, PTypeStructure.(iistr).ReturnFnParamNames, transpathoptions,PTypeStructure.(iistr).vfoptions); %% Modify PolicyIndexesPath into forms needed for forward iteration [PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,PolicyValuesPath_ii]=TransitionPath_InfHorz_substeps_Step2_AdjustPolicy(PolicyIndexesPath_ii,T,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).d_gridvals,PTypeStructure.(iistr).aprime_gridvals,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); @@ -221,6 +221,11 @@ end end + if PTypeStructure.(iistr).N_z==0 && PTypeStructure.(iistr).N_e==0 + PVP_ii_t=PolicyValuesPath_ii(:,:,:,tt); + else + PVP_ii_t=PolicyValuesPath_ii(:,:,:,:,tt); + end if isfinite(PTypeStructure.(iistr).N_j) %% Get the current optimal policy, and iterate the agent dist if transpathoptions.(iistr).trivialjequalonedist==0 @@ -237,21 +242,12 @@ AgentDistnext_ii=TransitionPath_FHorz_substeps_Step3tt_IterAgentDist(AgentDist_ii,PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,tt,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).pi_z_J,PTypeStructure.(iistr).pi_z_J_sim,PTypeStructure.(iistr).pi_e_J,PTypeStructure.(iistr).pi_e_J_sim,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,PTypeStructure.(iistr).exceptlastj,PTypeStructure.(iistr).exceptfirstj,PTypeStructure.(iistr).justfirstj,PTypeStructure.(iistr).jequalOneDist,transpathoptions,PTypeStructure.(iistr).simoptions); %% AggVars - if PTypeStructure.(iistr).N_z==0 && PTypeStructure.(iistr).N_e==0 - PVP_ii_t=PolicyValuesPath_ii(:,:,:,tt); - else - PVP_ii_t=PolicyValuesPath_ii(:,:,:,:,tt); - end AggVars_ii=TransitionPath_FHorz_substeps_Step4tt_AggVars(AgentDist_ii,AgeWeights_ii,PVP_ii_t,tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).l_a,PTypeStructure.(iistr).l_z,PTypeStructure.(iistr).l_e,PTypeStructure.(iistr).N_d,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).ze_gridvals_J_fastOLG,transpathoptions); else warning("TransitionPath_Case1_MixHorz_PType_shooting not hanlding pi_e yet") - AgentDistnext_ii=TransitionPath_InfHorz_substeps_Step3tt_IterAgentDist(AgentDist_ii,PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,tt,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).pi_z,[],PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,transpathoptions,PTypeStructure.(iistr).simoptions); - if PTypeStructure.(iistr).N_z==0 - PVP_ii_t=PolicyValuesPath_ii(:,:,tt); - else - PVP_ii_t=PolicyValuesPath_ii(:,:,:,tt); - end - AggVars_ii=TransitionPath_InfHorz_substeps_Step4tt_AggVars(AgentDist_ii,PVP_ii_t,tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).z_gridvals,transpathoptions); + AgentDist_ii_InfHorz=reshape(AgentDist_ii,[PTypeStructure.(iistr).N_a*PTypeStructure.(iistr).N_z,1]); + AgentDistnext_ii=TransitionPath_InfHorz_substeps_Step3tt_IterAgentDist(AgentDist_ii_InfHorz,PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,tt,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).pi_z,[],PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,transpathoptions,PTypeStructure.(iistr).simoptions); + AggVars_ii=TransitionPath_InfHorz_substeps_Step4tt_AggVars(AgentDist_ii_InfHorz,PVP_ii_t,tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).z_grid,transpathoptions); end for ff=1:length(AggVarNames_ii) From d010a49d2b3eb998edfb9008de4373cc56b3e651 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sun, 10 May 2026 13:14:06 +1200 Subject: [PATCH 59/90] Fix PVP_ii_t logic Missed the fact that PolicyValuePaths have different number of dimensions between FHorz and InfHorz. --- .../TransitionPath_Case1_MixHorz_PType_shooting.m | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType_shooting.m b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType_shooting.m index f5539c20..ea0a1516 100644 --- a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType_shooting.m +++ b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType_shooting.m @@ -221,11 +221,6 @@ end end - if PTypeStructure.(iistr).N_z==0 && PTypeStructure.(iistr).N_e==0 - PVP_ii_t=PolicyValuesPath_ii(:,:,:,tt); - else - PVP_ii_t=PolicyValuesPath_ii(:,:,:,:,tt); - end if isfinite(PTypeStructure.(iistr).N_j) %% Get the current optimal policy, and iterate the agent dist if transpathoptions.(iistr).trivialjequalonedist==0 @@ -242,11 +237,21 @@ AgentDistnext_ii=TransitionPath_FHorz_substeps_Step3tt_IterAgentDist(AgentDist_ii,PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,tt,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).pi_z_J,PTypeStructure.(iistr).pi_z_J_sim,PTypeStructure.(iistr).pi_e_J,PTypeStructure.(iistr).pi_e_J_sim,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,PTypeStructure.(iistr).exceptlastj,PTypeStructure.(iistr).exceptfirstj,PTypeStructure.(iistr).justfirstj,PTypeStructure.(iistr).jequalOneDist,transpathoptions,PTypeStructure.(iistr).simoptions); %% AggVars + if PTypeStructure.(iistr).N_z==0 && PTypeStructure.(iistr).N_e==0 + PVP_ii_t=PolicyValuesPath_ii(:,:,:,tt); + else + PVP_ii_t=PolicyValuesPath_ii(:,:,:,:,tt); + end AggVars_ii=TransitionPath_FHorz_substeps_Step4tt_AggVars(AgentDist_ii,AgeWeights_ii,PVP_ii_t,tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).l_a,PTypeStructure.(iistr).l_z,PTypeStructure.(iistr).l_e,PTypeStructure.(iistr).N_d,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).ze_gridvals_J_fastOLG,transpathoptions); else warning("TransitionPath_Case1_MixHorz_PType_shooting not hanlding pi_e yet") AgentDist_ii_InfHorz=reshape(AgentDist_ii,[PTypeStructure.(iistr).N_a*PTypeStructure.(iistr).N_z,1]); AgentDistnext_ii=TransitionPath_InfHorz_substeps_Step3tt_IterAgentDist(AgentDist_ii_InfHorz,PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,tt,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).pi_z,[],PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,transpathoptions,PTypeStructure.(iistr).simoptions); + if PTypeStructure.(iistr).N_z==0 && PTypeStructure.(iistr).N_e==0 + PVP_ii_t=PolicyValuesPath_ii(:,:,tt); + else + PVP_ii_t=PolicyValuesPath_ii(:,:,:,tt); + end AggVars_ii=TransitionPath_InfHorz_substeps_Step4tt_AggVars(AgentDist_ii_InfHorz,PVP_ii_t,tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).z_grid,transpathoptions); end From e801f7c061055801294a3a841d6281f695f4ca2f Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sun, 10 May 2026 21:47:32 +1200 Subject: [PATCH 60/90] Pre-merge PRs 89 and 90 These have been submitted as PRs. If/when they are merged, they should not conflict. --- ReturnFnMatrix/ReturnFnParamNamesFn.m | 4 ++-- ...TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/ReturnFnMatrix/ReturnFnParamNamesFn.m b/ReturnFnMatrix/ReturnFnParamNamesFn.m index 216fbf8d..5dd695d2 100644 --- a/ReturnFnMatrix/ReturnFnParamNamesFn.m +++ b/ReturnFnMatrix/ReturnFnParamNamesFn.m @@ -19,11 +19,11 @@ if n_z(1)==0 l_z=0; end -if isfield(vfoptions,'n_semiz') +if isfield(vfoptions,'n_semiz') && vfoptions.n_semiz>0 l_z=l_z+length(vfoptions.n_semiz); end l_e=0; -if isfield(vfoptions,'n_e') +if isfield(vfoptions,'n_e') && vfoptions.n_e>0 l_e=length(vfoptions.n_e); end if isfield(vfoptions,'experienceasset') diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m index 93ef9057..5e3ad59c 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m @@ -221,12 +221,17 @@ end PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 + PolicyaprimejzPath=permute(repmat(PolicyaprimejzPath,[1,1,2]),[1,3,2]); if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+a2primeIndexesPath; else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + end + if exist('PolicyProbsPath','var') + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; + else + PolicyProbsPath=a2primeProbsPath; end - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); end end elseif N_z==0 && N_e>0 From 2399d230fba91ca8a2c3ae7ddb6b3df7460c42b0 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Mon, 11 May 2026 12:23:17 +1200 Subject: [PATCH 61/90] Expand fixes for `e` grids --- ...onPath_FHorz_substeps_Step2_AdjustPolicy.m | 55 ++++++++++++++----- ...itionPath_FHorz_substeps_Step4tt_AggVars.m | 2 +- .../TransitionPath_Case1_MixHorz_PType.m | 2 +- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m index 5e3ad59c..a7cbd91a 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m @@ -280,12 +280,17 @@ end PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 + PolicyaprimejPath=permute(repmat(PolicyaprimejPath,[1,1,2]),[1,3,2]); if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejPath=PolicyaprimejPath+a2primeIndexesPath; else - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejPath=PolicyaprimejPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + end + if exist('PolicyProbsPath','var') + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; + else + PolicyProbsPath=a2primeProbsPath; end - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); end end elseif N_z>0 && N_e>0 @@ -334,12 +339,17 @@ end PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 + PolicyaprimejzPath=permute(repmat(PolicyaprimejzPath,[1,1,2]),[1,3,2]); if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+a2primeIndexesPath; else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + end + if exist('PolicyProbsPath','var') + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; + else + PolicyProbsPath=a2primeProbsPath; end - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); end end end @@ -418,12 +428,17 @@ PolicyaprimejzPath=gather(PolicyaprimejzPath); clear PolicyIndexesPath L2index if simoptions.experienceasset==1 + PolicyaprimejzPath=permute(repmat(PolicyaprimejzPath,[1,1,2]),[1,3,2]); if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+a2primeIndexesPath; else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + end + if exist('PolicyProbsPath','var') + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; + else + PolicyProbsPath=a2primeProbsPath; end - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); end elseif N_z==0 && N_e>0 % Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats @@ -457,12 +472,17 @@ PolicyaprimejPath=gather(PolicyaprimejPath); clear PolicyIndexesPath L2index if simoptions.experienceasset==1 + PolicyaprimejPath=permute(repmat(PolicyaprimejPath,[1,1,2]),[1,3,2]); if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejPath=PolicyaprimejPath+a2primeIndexesPath; else - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejPath=PolicyaprimejPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + end + if exist('PolicyProbsPath','var') + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; + else + PolicyProbsPath=a2primeProbsPath; end - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); end elseif N_z>0 && N_e>0 % Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats @@ -496,12 +516,17 @@ PolicyaprimejzPath=gather(PolicyaprimejzPath); clear PolicyIndexesPath L2index if simoptions.experienceasset==1 + PolicyaprimejzPath=permute(repmat(PolicyaprimejzPath,[1,1,2]),[1,3,2]); if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+a2primeIndexesPath; else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + end + if exist('PolicyProbsPath','var') + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; + else + PolicyProbsPath=a2primeProbsPath; end - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); end end end diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step4tt_AggVars.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step4tt_AggVars.m index 63703134..74769ef6 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step4tt_AggVars.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step4tt_AggVars.m @@ -11,7 +11,7 @@ if transpathoptions.fastOLG==0 % What's the right thing here? - WeightedAgentDist=AgentDist.*AgeWeights'; + WeightedAgentDist=AgentDist.*AgeWeights; if N_e==0 if N_z==0 WeightedAgentDist=reshape(WeightedAgentDist,[N_a*N_j,1]); diff --git a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m index 43a25175..0b64245f 100644 --- a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m @@ -668,7 +668,7 @@ end if isfinite(PTypeStructure.(iistr).N_j) - PTypeStructure.(iistr).simoptions.fastOLG=0; % hardcode + PTypeStructure.(iistr).simoptions.fastOLG=1; % hardcode %% Set up exogenous shock processes [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).pi_z_J_sim, PTypeStructure.(iistr).e_gridvals_J, PTypeStructure.(iistr).pi_e_J, PTypeStructure.(iistr).pi_e_J_sim, PTypeStructure.(iistr).ze_gridvals_J_fastOLG, transpathoptions, PTypeStructure.(iistr).simoptions]=ExogShockSetup_TPath_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); From e1f407c3ca72215bafa23da233ef6a5cf62c41af Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 12 May 2026 06:44:28 +1200 Subject: [PATCH 62/90] Reshape V_final regardless of fastOLG We reshape V_final further (and permute it) when fastOLG==1. --- .../TransitionPath_Case1_MixHorz_PType.m | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m index 0b64245f..4e2491cc 100644 --- a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m @@ -678,24 +678,24 @@ %% Organise V_final and AgentDist_initial % Reshape V_final N_j_temp=PTypeStructure.(iistr).N_j; - if transpathoptions.fastOLG==0 - if N_z==0 - if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); - else - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_e,N_j_temp]); - end + if N_z==0 + if N_e==0 + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); else - if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_j_temp]); - else - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_e,N_j_temp]); - end + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_e,N_j_temp]); end else + if N_e==0 + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_j_temp]); + else + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_e,N_j_temp]); + end + end + if transpathoptions.fastOLG==1 if N_z==0 if N_e==0 - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); + % Already reshaped + % V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); else V_final.(iistr)=reshape(permute(V_final.(iistr),[1,3,2]),[N_a*N_j_temp,N_e]); end From ec04d47f0f08aaea57779b75f8815a8b207f8697 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 13 May 2026 16:36:47 +1200 Subject: [PATCH 63/90] Restore fixes to make Electrify complete when fastOLG==0 --- .../AgentDistOnTransPath_Case1_FHorz.m | 16 +++++++++------ .../ExoShocks/ExogShockSetup_TPath_FHorz.m | 14 ++++++------- ...onPath_FHorz_substeps_Step2_AdjustPolicy.m | 20 ++++++++++++++----- ...Horz_TPath_SingleStep_fastOLG_ExpAsset_e.m | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz.m b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz.m index 73ddcb75..bbfdc956 100644 --- a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz.m +++ b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz.m @@ -450,8 +450,12 @@ PolicyaprimezPath=reshape(PolicyaprimezPath,[N_a*N_z*N_e,(N_j-1),1,T])+N_a1*(a2primeIndexesPath-1); PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(N_a1*(a2primeIndexesPath-1),1,2,1); - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyaprimejzPath=repmat(permute(PolicyaprimejzPath,[1,3,2]),1,2,1)+N_a1*(a2primeIndexesPath-1); + if exist('PolicyProbsPath','var') + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + else + PolicyProbsPath=a2primeProbsPath; + end end end end @@ -873,24 +877,24 @@ %% if N_e==0 if N_z==0 - AgentDistPath=reshape(AgentDistPath,[n_a,N_j,T]); + AgentDistPath=reshape(AgentDistPath,[N_a,N_j,T]); else if simoptions.fastOLG==1 AgentDistPath=permute(reshape(AgentDistPath,[N_a,N_j,N_z,T]),[1,3,2,4]); end - AgentDistPath=reshape(AgentDistPath,[n_a,n_z,N_j,T]); + AgentDistPath=reshape(AgentDistPath,[N_a,N_z,N_j,T]); end else if N_z==0 if simoptions.fastOLG==1 AgentDistPath=permute(reshape(AgentDistPath,[N_a,N_j,N_e,T]),[1,3,2,4]); end - AgentDistPath=reshape(AgentDistPath,[n_a,simoptions.n_e,N_j,T]); + AgentDistPath=reshape(AgentDistPath,[N_a,N_e,N_j,T]); else if simoptions.fastOLG==1 AgentDistPath=permute(reshape(AgentDistPath,[N_a,N_j,N_z,N_e,T]),[1,3,4,2,5]); end - AgentDistPath=reshape(AgentDistPath,[n_a,n_z,simoptions.n_e,N_j,T]); + AgentDistPath=reshape(AgentDistPath,[N_a,N_z,N_e,N_j,T]); end end diff --git a/SubCodes/ExoShocks/ExogShockSetup_TPath_FHorz.m b/SubCodes/ExoShocks/ExogShockSetup_TPath_FHorz.m index d27f364f..a32b8dce 100644 --- a/SubCodes/ExoShocks/ExogShockSetup_TPath_FHorz.m +++ b/SubCodes/ExoShocks/ExogShockSetup_TPath_FHorz.m @@ -16,9 +16,9 @@ % % transpathoptions.zepathtrivial=0 when either of zpathtrival and epathtrivial both are zero -% gridpiboth=4: sometimes (trans path GE) we want both grid and transition probabilties, including pi_z_J_sim alternative transition probs -% gridpiboth=3: sometimes (value fn iter) we want both grid and transition probabilties -% gridpiboth=2: sometimes (agent dist) we want just transition probabilties, including pi_z_J_sim alternative transition probs +% gridpiboth=4: sometimes (trans path GE) we want both grid and transition probabilities, including pi_z_J_sim alternative transition probs +% gridpiboth=3: sometimes (value fn iter) we want both grid and transition probabilities +% gridpiboth=2: sometimes (agent dist) we want just transition probabilities, including pi_z_J_sim alternative transition probs % gridpiboth=1: sometimes (FnsToEvaluate) we want just grid @@ -376,7 +376,7 @@ end end - if transpathoptions.fastOLG==1 % Reshape grid and transtion matrix for use with fastOLG + if transpathoptions.fastOLG==1 % Reshape grid and transition matrix for use with fastOLG if N_z>0 if gridpiboth==1 % for most FnsToEvaluate, we don't use pi_e e_gridvals_J=permute(e_gridvals_J,[3,4,1,2]); % Give it the size required for CreateReturnFnMatrix_Case1_Disc_Par2_fastOLGe: (j,1,N_e,l_e) @@ -498,9 +498,9 @@ %% Create ze_gridvals_J_fastOLG, which is used for AggVars. Will be z_gridvals_J_fastOLG or e_gridvals_J_OLG if only one of them is used -% gridpiboth=4: sometimes (trans path GE) we want both grid and transition probabilties, including pi_z_J_sim alternative transition probs -% gridpiboth=3: sometimes (value fn iter) we want both grid and transition probabilties -% gridpiboth=2: sometimes (agent dist) we want just transition probabilties, including pi_z_J_sim alternative transition probs +% gridpiboth=4: sometimes (trans path GE) we want both grid and transition probabilities, including pi_z_J_sim alternative transition probs +% gridpiboth=3: sometimes (value fn iter) we want both grid and transition probabilities +% gridpiboth=2: sometimes (agent dist) we want just transition probabilities, including pi_z_J_sim alternative transition probs % gridpiboth=1: sometimes (FnsToEvaluate) we want just grid if gridpiboth==3 || gridpiboth==2 ze_gridvals_J_fastOLG=[]; diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m index c100afc1..adedbbbe 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m @@ -33,7 +33,12 @@ aprimeFnParamsVec=CreateAgeMatrixFromParams(Parameters,simoptions.setup_experienceasset.aprimeFnParamNames,N_j); % [N_j,number of params] - [a2primeIndexes, a2primeProbs]=CreateaprimePolicyExperienceAsset_J(PolicyIndexesPath(:,:,:,:,tt),simoptions.setup_experienceasset.aprimeFn, whichisdforexpasset, n_d, simoptions.setup_experienceasset.n_a1,simoptions.setup_experienceasset.n_a2, N_ze, N_j, simoptions.setup_experienceasset.d_grid, simoptions.setup_experienceasset.a2_grid, aprimeFnParamsVec,transpathoptions.fastOLG); + if N_z>0 && N_e>0 + PIP_tt=PolicyIndexesPath(:,:,:,:,:,tt); + else + PIP_tt=PolicyIndexesPath(:,:,:,:,tt); + end + [a2primeIndexes, a2primeProbs]=CreateaprimePolicyExperienceAsset_J(PIP_tt,simoptions.setup_experienceasset.aprimeFn, whichisdforexpasset, n_d, simoptions.setup_experienceasset.n_a1,simoptions.setup_experienceasset.n_a2, N_ze, N_j, simoptions.setup_experienceasset.d_grid, simoptions.setup_experienceasset.a2_grid, aprimeFnParamsVec,transpathoptions.fastOLG); % Note: a2primeIndexes and a2primeProbs are both [N_a,N_z,N_j] for fastOLG=0 % Note: a2primeIndexes is always the 'lower' point (the upper points are just aprimeIndexes+1), and the a2primeProbs are the probability of this lower point (prob of upper point is just 1 minus this). a2primeIndexesPath(:,:,:,tt)=a2primeIndexes(:,:,1:end-1); @@ -46,7 +51,12 @@ aprimeFnParamsVec=CreateAgeMatrixFromParams(Parameters,simoptions.setup_experienceasset.aprimeFnParamNames,N_j); % [N_j,number of params] - [a2primeIndexes, a2primeProbs]=CreateaprimePolicyExperienceAsset_J(PolicyIndexesPath(:,:,:,:,tt),simoptions.setup_experienceasset.aprimeFn, whichisdforexpasset, n_d, simoptions.setup_experienceasset.n_a1,simoptions.setup_experienceasset.n_a2, N_ze, N_j, simoptions.setup_experienceasset.d_grid, simoptions.setup_experienceasset.a2_grid, aprimeFnParamsVec,transpathoptions.fastOLG); + if N_z>0 && N_e>0 + PIP_tt=PolicyIndexesPath(:,:,:,:,:,tt); + else + PIP_tt=PolicyIndexesPath(:,:,:,:,tt); + end + [a2primeIndexes, a2primeProbs]=CreateaprimePolicyExperienceAsset_J(PIP_tt,simoptions.setup_experienceasset.aprimeFn, whichisdforexpasset, n_d, simoptions.setup_experienceasset.n_a1,simoptions.setup_experienceasset.n_a2, N_ze, N_j, simoptions.setup_experienceasset.d_grid, simoptions.setup_experienceasset.a2_grid, aprimeFnParamsVec,transpathoptions.fastOLG); % Note: a2primeIndexes and a2primeProbs are both [N_a,N_j,N_z] for fastOLG=1 % Note: a2primeIndexes is always the 'lower' point (the upper points are just aprimeIndexes+1), and the a2primeProbs are the probability of this lower point (prob of upper point is just 1 minus this). a2primeIndexesPath(:,:,:,tt)=a2primeIndexes(:,1:end-1,:); @@ -363,12 +373,12 @@ end elseif simoptions.fastOLG==1 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+a2primeIndexesPath; else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e.m index b8f13f60..69d0b7d3 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e.m @@ -7,7 +7,7 @@ % (fastOLG requires swapping order of j and z) N_d1=prod(n_d1); -N_a=prod(n_a); +N_a=prod(n_a1)*n_a2; N_z=prod(n_z); N_e=prod(n_e); % N_z=0 is handled elsewhere From 327f98effb5a127b2a573f4e89085027c25f175f Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 13 May 2026 19:27:52 +1200 Subject: [PATCH 64/90] Additional fixes when not N_Z>0 && N_e>0 --- .../AgentDistOnTransPath_Case1_FHorz.m | 16 ++++++++++++---- ...itionPath_FHorz_substeps_Step2_AdjustPolicy.m | 12 ++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz.m b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz.m index bbfdc956..67417c66 100644 --- a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz.m +++ b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz.m @@ -372,8 +372,12 @@ PolicyaprimezPath=reshape(PolicyaprimezPath,[N_a*N_z,(N_j-1),1,T])+N_a1*(a2primeIndexesPath-1); PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(N_a1*(a2primeIndexesPath-1),1,2,1); - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyaprimejzPath=repmat(permute(PolicyaprimejzPath,[1,3,2]),1,2,1)+N_a1*(a2primeIndexesPath-1); + if exist('PolicyProbsPath','var') + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + else + PolicyProbsPath=a2primeProbsPath; + end end end end @@ -412,8 +416,12 @@ PolicyaprimePath=reshape(PolicyaprimePath,[N_a*N_e,(N_j-1),1,T])+N_a1*(a2primeIndexesPath-1); PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(N_a1*(a2primeIndexesPath-1),1,2,1); - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyaprimejzPath=repmat(permute(PolicyaprimejzPath,[1,3,2]),1,2,1)+N_a1*(a2primeIndexesPath-1); + if exist('PolicyProbsPath','var') + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + else + PolicyProbsPath=a2primeProbsPath; + end end end else % z, e diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m index adedbbbe..f35d2127 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m @@ -183,10 +183,10 @@ if simoptions.setup_experienceasset.N_a1==0 PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); else - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -247,10 +247,10 @@ if simoptions.setup_experienceasset.N_a1==0 PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -311,10 +311,10 @@ if simoptions.setup_experienceasset.N_a1==0 PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); else - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end From ea6fbe28503614a2acbc8ec3eb91e6c9e196bf40 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 13 May 2026 19:34:54 +1200 Subject: [PATCH 65/90] rename _Case1_MixHorz to _MixHorz Will commit caller changes separately. --- ..._MixHorz_PType.m => EvalFnOnTransPath_AggVars_MixHorz_PType.m} | 0 ...1_MixHorz_PType.m => HeteroAgentStationaryEqm_MixHorz_PType.m} | 0 ...ype_subfn.m => HeteroAgentStationaryEqm_MixHorz_PType_subfn.m} | 0 ...yDist_Case1_MixHorz_PType.m => StationaryDist_MixHorz_PType.m} | 0 ...yDist_Case1_MixHorz_PType.m => StationaryDist_MixHorz_PType.m} | 0 ...Case1_MixHorz_PType.m => AgentDistOnTransPath_MixHorz_PType.m} | 0 ...nPath_Case1_MixHorz_PType.m => TransitionPath_MixHorz_PType.m} | 0 ...z_PType_shooting.m => TransitionPath_MixHorz_PType_shooting.m} | 0 ...ueFnIter_Case1_MixHorz_PType.m => ValueFnIter_MixHorz_PType.m} | 0 ...h_Case1_MixHorz_PType.m => ValueFnOnTransPath_MixHorz_PType.m} | 0 10 files changed, 0 insertions(+), 0 deletions(-) rename EvaluateFnOnAgentDist/TransPathMixHorz/{EvalFnOnTransPath_AggVars_Case1_MixHorz_PType.m => EvalFnOnTransPath_AggVars_MixHorz_PType.m} (100%) rename HeterogeneousAgent/MixHorz/{HeteroAgentStationaryEqm_Case1_MixHorz_PType.m => HeteroAgentStationaryEqm_MixHorz_PType.m} (100%) rename HeterogeneousAgent/MixHorz/{HeteroAgentStationaryEqm_Case1_MixHorz_PType_subfn.m => HeteroAgentStationaryEqm_MixHorz_PType_subfn.m} (100%) rename StationaryDist/MixHorz/{StationaryDist_Case1_MixHorz_PType.m => StationaryDist_MixHorz_PType.m} (100%) rename StationaryDist/MixHorzPType/{StationaryDist_Case1_MixHorz_PType.m => StationaryDist_MixHorz_PType.m} (100%) rename StationaryDist/TransPathMixHorz/{AgentDistOnTransPath_Case1_MixHorz_PType.m => AgentDistOnTransPath_MixHorz_PType.m} (100%) rename TransitionPaths/MixHorz/{TransitionPath_Case1_MixHorz_PType.m => TransitionPath_MixHorz_PType.m} (100%) rename TransitionPaths/MixHorz/{TransitionPath_Case1_MixHorz_PType_shooting.m => TransitionPath_MixHorz_PType_shooting.m} (100%) rename ValueFnIter/MixHorz_PType/{ValueFnIter_Case1_MixHorz_PType.m => ValueFnIter_MixHorz_PType.m} (100%) rename ValueFnIter/TransPathMixHorz/{ValueFnOnTransPath_Case1_MixHorz_PType.m => ValueFnOnTransPath_MixHorz_PType.m} (100%) diff --git a/EvaluateFnOnAgentDist/TransPathMixHorz/EvalFnOnTransPath_AggVars_Case1_MixHorz_PType.m b/EvaluateFnOnAgentDist/TransPathMixHorz/EvalFnOnTransPath_AggVars_MixHorz_PType.m similarity index 100% rename from EvaluateFnOnAgentDist/TransPathMixHorz/EvalFnOnTransPath_AggVars_Case1_MixHorz_PType.m rename to EvaluateFnOnAgentDist/TransPathMixHorz/EvalFnOnTransPath_AggVars_MixHorz_PType.m diff --git a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_Case1_MixHorz_PType.m b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m similarity index 100% rename from HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_Case1_MixHorz_PType.m rename to HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m diff --git a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_Case1_MixHorz_PType_subfn.m b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType_subfn.m similarity index 100% rename from HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_Case1_MixHorz_PType_subfn.m rename to HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType_subfn.m diff --git a/StationaryDist/MixHorz/StationaryDist_Case1_MixHorz_PType.m b/StationaryDist/MixHorz/StationaryDist_MixHorz_PType.m similarity index 100% rename from StationaryDist/MixHorz/StationaryDist_Case1_MixHorz_PType.m rename to StationaryDist/MixHorz/StationaryDist_MixHorz_PType.m diff --git a/StationaryDist/MixHorzPType/StationaryDist_Case1_MixHorz_PType.m b/StationaryDist/MixHorzPType/StationaryDist_MixHorz_PType.m similarity index 100% rename from StationaryDist/MixHorzPType/StationaryDist_Case1_MixHorz_PType.m rename to StationaryDist/MixHorzPType/StationaryDist_MixHorz_PType.m diff --git a/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_Case1_MixHorz_PType.m b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m similarity index 100% rename from StationaryDist/TransPathMixHorz/AgentDistOnTransPath_Case1_MixHorz_PType.m rename to StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m diff --git a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m similarity index 100% rename from TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType.m rename to TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m diff --git a/TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType_shooting.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m similarity index 100% rename from TransitionPaths/MixHorz/TransitionPath_Case1_MixHorz_PType_shooting.m rename to TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m diff --git a/ValueFnIter/MixHorz_PType/ValueFnIter_Case1_MixHorz_PType.m b/ValueFnIter/MixHorz_PType/ValueFnIter_MixHorz_PType.m similarity index 100% rename from ValueFnIter/MixHorz_PType/ValueFnIter_Case1_MixHorz_PType.m rename to ValueFnIter/MixHorz_PType/ValueFnIter_MixHorz_PType.m diff --git a/ValueFnIter/TransPathMixHorz/ValueFnOnTransPath_Case1_MixHorz_PType.m b/ValueFnIter/TransPathMixHorz/ValueFnOnTransPath_MixHorz_PType.m similarity index 100% rename from ValueFnIter/TransPathMixHorz/ValueFnOnTransPath_Case1_MixHorz_PType.m rename to ValueFnIter/TransPathMixHorz/ValueFnOnTransPath_MixHorz_PType.m From 0b103ca5d36ffc4fae5e82eb76fe927cb8d5e67d Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 13 May 2026 19:36:33 +1200 Subject: [PATCH 66/90] Change _Case1_MixHorz interfaces to _MixHorz --- .../EvalFnOnTransPath_AggVars_MixHorz_PType.m | 2 +- .../HeteroAgentStationaryEqm_MixHorz_PType.m | 26 +++++++++---------- ...roAgentStationaryEqm_MixHorz_PType_subfn.m | 2 +- .../MixHorz/StationaryDist_MixHorz_PType.m | 2 +- .../StationaryDist_MixHorz_PType.m | 2 +- .../AgentDistOnTransPath_MixHorz_PType.m | 2 +- .../MixHorz/TransitionPath_MixHorz_PType.m | 4 +-- .../TransitionPath_MixHorz_PType_shooting.m | 6 ++--- .../MixHorz_PType/ValueFnIter_MixHorz_PType.m | 2 +- .../ValueFnOnTransPath_MixHorz_PType.m | 2 +- 10 files changed, 25 insertions(+), 25 deletions(-) diff --git a/EvaluateFnOnAgentDist/TransPathMixHorz/EvalFnOnTransPath_AggVars_MixHorz_PType.m b/EvaluateFnOnAgentDist/TransPathMixHorz/EvalFnOnTransPath_AggVars_MixHorz_PType.m index ff683e91..a393db21 100644 --- a/EvaluateFnOnAgentDist/TransPathMixHorz/EvalFnOnTransPath_AggVars_MixHorz_PType.m +++ b/EvaluateFnOnAgentDist/TransPathMixHorz/EvalFnOnTransPath_AggVars_MixHorz_PType.m @@ -1,4 +1,4 @@ -function AggVarsPath=EvalFnOnTransPath_AggVars_Case1_MixHorz_PType(FnsToEvaluate, AgentDistPath, PolicyPath, PricePath, ParamPath, Parameters, T, n_d, n_a, n_z, N_j, Names_i, d_grid, a_grid,z_grid, transpathoptions, simoptions) +function AggVarsPath=EvalFnOnTransPath_AggVars_MixHorz_PType(FnsToEvaluate, AgentDistPath, PolicyPath, PricePath, ParamPath, Parameters, T, n_d, n_a, n_z, N_j, Names_i, d_grid, a_grid,z_grid, transpathoptions, simoptions) % AggVars is simple in the sense we can just solve to get AggVars for each ptype and then take the weigthed sum over them % This only works because we are just after the mean diff --git a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m index 2cb8c359..da23fad9 100644 --- a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m +++ b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m @@ -1,12 +1,12 @@ -function varargout=HeteroAgentStationaryEqm_Case1_MixHorz_PType(n_d, n_a, n_z, N_j, Names_i, n_p, pi_z, d_grid, a_grid, z_grid,jequaloneDist, ReturnFn, FnsToEvaluate, GeneralEqmEqns, Parameters, DiscountFactorParamNames, AgeWeightParamNames, PTypeDistParamNames, GEPriceParamNames,heteroagentoptions, simoptions, vfoptions) +function varargout=HeteroAgentStationaryEqm_MixHorz_PType(n_d, n_a, n_z, N_j, Names_i, n_p, pi_z, d_grid, a_grid, z_grid,jequaloneDist, ReturnFn, FnsToEvaluate, GeneralEqmEqns, Parameters, DiscountFactorParamNames, AgeWeightParamNames, PTypeDistParamNames, GEPriceParamNames,heteroagentoptions, simoptions, vfoptions) % Outputs: [p_eqm, GeneralEqmConditions] % Unless you use n_p and p_grid, in which case [p_eq, p_eqm_index, GeneralEqmConditions] % % Allows for different permanent (fixed) types of agent. -% See ValueFnIter_Case1_MixHorz_PType for general idea. +% See ValueFnIter_MixHorz_PType for general idea. % % Rest of this description describes how those inputs not used for -% ValueFnIter_Case1_MixHorz_PType should be set up. +% ValueFnIter_MixHorz_PType should be set up. % % jequaloneDist can either be same for all permanent types, or must be passed as a structure. % AgeWeightParamNames is either same for all permanent types, or must be passed as a structure. @@ -47,7 +47,7 @@ heteroagentoptions.fminalgo=heteroagentoptions.fminalgo(1:end-1); heteroagentoptions.toleranceGEcondns=heteroagentoptions.toleranceGEcondns(1:end-1); heteroagentoptions.toleranceGEprices=heteroagentoptions.toleranceGEprices(1:end-1); - p_eqm_previous=HeteroAgentStationaryEqm_Case1_MixHorz_PType(n_d, n_a, n_z, N_j, Names_i, n_p, pi_z, d_grid, a_grid, z_grid,jequaloneDist, ReturnFn, FnsToEvaluate, GeneralEqmEqns, Parameters, DiscountFactorParamNames, AgeWeightParamNames, PTypeDistParamNames, GEPriceParamNames,heteroagentoptions, simoptions, vfoptions); + p_eqm_previous=HeteroAgentStationaryEqm_MixHorz_PType(n_d, n_a, n_z, N_j, Names_i, n_p, pi_z, d_grid, a_grid, z_grid,jequaloneDist, ReturnFn, FnsToEvaluate, GeneralEqmEqns, Parameters, DiscountFactorParamNames, AgeWeightParamNames, PTypeDistParamNames, GEPriceParamNames,heteroagentoptions, simoptions, vfoptions); for pp=1:length(GEPriceParamNames) Parameters.(GEPriceParamNames{pp})=p_eqm_previous.(GEPriceParamNames{pp}); end @@ -314,7 +314,7 @@ % If jequaloneDist is not a structure, then we deal with it here (as it may be dependent on PType, and we will turn it into a structure if it is) if ~isstruct(jequaloneDist) if ~exist("simoptions","var") - error('You must input simoptions to HeteroAgentStationaryEqm_Case1_MixHorz_PType when using jequaloneDist as a structure') + error('You must input simoptions to HeteroAgentStationaryEqm_MixHorz_PType when using jequaloneDist as a structure') end [jequaloneDist,~,Parameters]=jequaloneDist_PType(jequaloneDist,Parameters,simoptions,n_a,n_z,PTypeStructure.N_i,PTypeStructure.Names_i,PTypeDistParamNames,1); end @@ -827,28 +827,28 @@ %% Otherwise, use fminsearch to find the general equilibrium if all(heteroagentoptions.GEptype==0) if heteroagentoptions.fminalgo~=8 && heteroagentoptions.fminalgo~=3 - GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_Case1_MixHorz_PType_subfn(p, PTypeStructure, Parameters, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames, GEeqnNames, AggVarNames, nGEprices,heteroagentoptions); + GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_MixHorz_PType_subfn(p, PTypeStructure, Parameters, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames, GEeqnNames, AggVarNames, nGEprices,heteroagentoptions); elseif heteroagentoptions.fminalgo==3 heteroagentoptions.outputGEform=1; % vector - GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_Case1_MixHorz_PType_subfn(p, PTypeStructure, Parameters, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames, GEeqnNames, AggVarNames, nGEprices,heteroagentoptions); + GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_MixHorz_PType_subfn(p, PTypeStructure, Parameters, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames, GEeqnNames, AggVarNames, nGEprices,heteroagentoptions); elseif heteroagentoptions.fminalgo==8 heteroagentoptions.outputGEform=1; % vector weightsbackup=heteroagentoptions.multiGEweights; heteroagentoptions.multiGEweights=sqrt(heteroagentoptions.multiGEweights); % To use a weighting matrix in lsqnonlin(), we work with the square-roots of the weights - GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_Case1_MixHorz_PType_subfn(p, PTypeStructure, Parameters, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames, GEeqnNames, AggVarNames, nGEprices,heteroagentoptions); + GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_MixHorz_PType_subfn(p, PTypeStructure, Parameters, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames, GEeqnNames, AggVarNames, nGEprices,heteroagentoptions); heteroagentoptions.multiGEweights=weightsbackup; % change it back now that we have set up CalibrateLifeCycleModel_objectivefn() end else % some GE condns depend on ptype if heteroagentoptions.fminalgo~=8 && heteroagentoptions.fminalgo~=3 - GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_Case1_MixHorz_PType_GEptype_subfn(p, PTypeStructure, Parameters, GeneralEqmEqns, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames,AggVarNames,nGEprices,GEpriceindexes,GEprice_ptype,heteroagentoptions); + GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_MixHorz_PType_GEptype_subfn(p, PTypeStructure, Parameters, GeneralEqmEqns, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames,AggVarNames,nGEprices,GEpriceindexes,GEprice_ptype,heteroagentoptions); elseif heteroagentoptions.fminalgo==3 heteroagentoptions.outputGEform=1; % vector - GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_Case1_MixHorz_PType_GEptype_subfn(p, PTypeStructure, Parameters, GeneralEqmEqns, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames,AggVarNames,nGEprices,GEpriceindexes,GEprice_ptype,heteroagentoptions); + GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_MixHorz_PType_GEptype_subfn(p, PTypeStructure, Parameters, GeneralEqmEqns, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames,AggVarNames,nGEprices,GEpriceindexes,GEprice_ptype,heteroagentoptions); elseif heteroagentoptions.fminalgo==8 heteroagentoptions.outputGEform=1; % vector weightsbackup=heteroagentoptions.multiGEweights; heteroagentoptions.multiGEweights=sqrt(heteroagentoptions.multiGEweights); % To use a weighting matrix in lsqnonlin(), we work with the square-roots of the weights - GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_Case1_MixHorz_PType_GEptype_subfn(p, PTypeStructure, Parameters, GeneralEqmEqns, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames,AggVarNames,nGEprices,GEpriceindexes,GEprice_ptype,heteroagentoptions); + GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_MixHorz_PType_GEptype_subfn(p, PTypeStructure, Parameters, GeneralEqmEqns, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames,AggVarNames,nGEprices,GEpriceindexes,GEprice_ptype,heteroagentoptions); heteroagentoptions.multiGEweights=weightsbackup; % change it back now that we have set up CalibrateLifeCycleModel_objectivefn() end end @@ -1018,9 +1018,9 @@ heteroagentoptions.constrainAtoB=zeros(length(p_eqm_vec),1); end if all(heteroagentoptions.GEptype==0) - GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_Case1_MixHorz_PType_subfn(p, PTypeStructure, Parameters, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames, GEeqnNames, AggVarNames, nGEprices,heteroagentoptions); + GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_MixHorz_PType_subfn(p, PTypeStructure, Parameters, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames, GEeqnNames, AggVarNames, nGEprices,heteroagentoptions); else - GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_Case1_MixHorz_PType_GEptype_subfn(p, PTypeStructure, Parameters, GeneralEqmEqns, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames,AggVarNames,nGEprices,GEpriceindexes,GEprice_ptype,heteroagentoptions); + GeneralEqmConditionsFnOpt=@(p) HeteroAgentStationaryEqm_MixHorz_PType_GEptype_subfn(p, PTypeStructure, Parameters, GeneralEqmEqns, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames,AggVarNames,nGEprices,GEpriceindexes,GEprice_ptype,heteroagentoptions); end GeneralEqmConditions=GeneralEqmConditionsFnOpt(p_eqm_vec); end diff --git a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType_subfn.m b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType_subfn.m index a913a43a..de2e6a85 100644 --- a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType_subfn.m +++ b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType_subfn.m @@ -1,4 +1,4 @@ -function GeneralEqmConditions=HeteroAgentStationaryEqm_Case1_MixHorz_PType_subfn(GEpricesvec, PTypeStructure, Parameters, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames, GEeqnNames, AggVarNames, nGEprices, heteroagentoptions) +function GeneralEqmConditions=HeteroAgentStationaryEqm_MixHorz_PType_subfn(GEpricesvec, PTypeStructure, Parameters, GeneralEqmEqnsCell, GeneralEqmEqnParamNames, GEPriceParamNames, GEeqnNames, AggVarNames, nGEprices, heteroagentoptions) heteroagentparamsvecindex=0:1:length(GEpricesvec); [GEpricesvec,penalty]=ParameterConstraints_TransformParamsToOriginal(GEpricesvec,heteroagentparamsvecindex,GEPriceParamNames,heteroagentoptions); diff --git a/StationaryDist/MixHorz/StationaryDist_MixHorz_PType.m b/StationaryDist/MixHorz/StationaryDist_MixHorz_PType.m index 11a0921a..e6cb4887 100644 --- a/StationaryDist/MixHorz/StationaryDist_MixHorz_PType.m +++ b/StationaryDist/MixHorz/StationaryDist_MixHorz_PType.m @@ -1,4 +1,4 @@ -function StationaryDist=StationaryDist_Case1_MixHorz_PType(jequaloneDist,AgeWeightsParamNames,PTypeDistParamNames,Policy,n_d,n_a,n_z,N_j,Names_i,pi_z,Parameters,simoptions) +function StationaryDist=StationaryDist_MixHorz_PType(jequaloneDist,AgeWeightsParamNames,PTypeDistParamNames,Policy,n_d,n_a,n_z,N_j,Names_i,pi_z,Parameters,simoptions) % Allows for different permanent (fixed) types of agent. % See ValueFnIter_Case1_FHorz_PType for general idea. % diff --git a/StationaryDist/MixHorzPType/StationaryDist_MixHorz_PType.m b/StationaryDist/MixHorzPType/StationaryDist_MixHorz_PType.m index 2e4df061..fb30afbe 100644 --- a/StationaryDist/MixHorzPType/StationaryDist_MixHorz_PType.m +++ b/StationaryDist/MixHorzPType/StationaryDist_MixHorz_PType.m @@ -1,4 +1,4 @@ -function StationaryDist=StationaryDist_Case1_MixHorz_PType(jequaloneDist,AgeWeightsParamNames,PTypeDistParamNames,Policy,n_d,n_a,n_z,N_j,Names_i,pi_z,Parameters,simoptions) +function StationaryDist=StationaryDist_MixHorz_PType(jequaloneDist,AgeWeightsParamNames,PTypeDistParamNames,Policy,n_d,n_a,n_z,N_j,Names_i,pi_z,Parameters,simoptions) % Allows for different permanent (fixed) types of agent. % See ValueFnIter_Case1_FHorz_PType for general idea. % diff --git a/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m index 5722df21..2cd8f06f 100644 --- a/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m +++ b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m @@ -1,4 +1,4 @@ -function AgentDistPath=AgentDistOnTransPath_Case1_MixHorz_PType(AgentDist_initial, jequalOneDist, PricePath, ParamPath, PolicyPath, AgeWeightsParamNames,n_d,n_a,n_z,N_j,Names_i,pi_z, T,Parameters, transpathoptions, simoptions) +function AgentDistPath=AgentDistOnTransPath_MixHorz_PType(AgentDist_initial, jequalOneDist, PricePath, ParamPath, PolicyPath, AgeWeightsParamNames,n_d,n_a,n_z,N_j,Names_i,pi_z, T,Parameters, transpathoptions, simoptions) % Remark to self: No real need for T as input, as this is anyway the length of PricePath AgentDistPath=struct(); diff --git a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m index be42e1a4..83af9de0 100644 --- a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m @@ -1,4 +1,4 @@ -function varargout=TransitionPath_Case1_MixHorz_PType(PricePath0, ParamPath, T, V_final, AgentDist_initial, jequalOneDist, n_d, n_a, n_z, N_j, Names_i, d_grid,a_grid,z_grid, pi_z, ReturnFn, FnsToEvaluate, GeneralEqmEqns, Parameters, DiscountFactorParamNames, AgeWeightsParamNames, PTypeDistParamNames, transpathoptions, simoptions, vfoptions) +function varargout=TransitionPath_MixHorz_PType(PricePath0, ParamPath, T, V_final, AgentDist_initial, jequalOneDist, n_d, n_a, n_z, N_j, Names_i, d_grid,a_grid,z_grid, pi_z, ReturnFn, FnsToEvaluate, GeneralEqmEqns, Parameters, DiscountFactorParamNames, AgeWeightsParamNames, PTypeDistParamNames, transpathoptions, simoptions, vfoptions) % This code will work for all transition paths except those that involve at % change in the transition matrix pi_z (can handle a change in pi_z, but % only if it is a 'surprise', not anticipated changes) @@ -1009,7 +1009,7 @@ if transpathoptions.GEnewprice~=2 % For permanent type, there is just one shooting command, % because things like z,e, and fastOLG are handled on a per-PType basis (to permit that they differ across ptype) - [PricePath,GEcondnPath]=TransitionPath_Case1_MixHorz_PType_shooting(PricePath0, PricePathNames, ParamPath, ParamPathNames, T, V_final, AgentDist_initial, FnsToEvaluate, GeneralEqmEqns, PricePathSizeVec, ParamPathSizeVec, PricePathSizeVec_ii, ParamPathSizeVec_ii, GEeqnNames,nGeneralEqmEqns,nGeneralEqmEqns_acrossptypes,GeneralEqmEqnsCell,GeneralEqmEqnParamNames, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, transpathoptions, PTypeStructure); + [PricePath,GEcondnPath]=TransitionPath_MixHorz_PType_shooting(PricePath0, PricePathNames, ParamPath, ParamPathNames, T, V_final, AgentDist_initial, FnsToEvaluate, GeneralEqmEqns, PricePathSizeVec, ParamPathSizeVec, PricePathSizeVec_ii, ParamPathSizeVec_ii, GEeqnNames,nGeneralEqmEqns,nGeneralEqmEqns_acrossptypes,GeneralEqmEqnsCell,GeneralEqmEqnParamNames, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, transpathoptions, PTypeStructure); % Switch the solution into structure for output. pp_indexinpricepath=zeros(1,length(PricePathNames)); diff --git a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m index ea0a1516..a7b56604 100644 --- a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m +++ b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m @@ -1,4 +1,4 @@ -function [PricePathOld,GEcondnPath]=TransitionPath_Case1_MixHorz_PType_shooting(PricePathOld, PricePathNames, ParamPath, ParamPathNames, T, V_final, AgentDist_initial, FullFnsToEvaluate, GeneralEqmEqns, PricePathSizeVec, ParamPathSizeVec, PricePathSizeVec_ii, ParamPathSizeVec_ii, GEeqnNames,nGeneralEqmEqns,nGeneralEqmEqns_acrossptypes,GeneralEqmEqnsCell,GeneralEqmEqnParamNames, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, transpathoptions, PTypeStructure) +function [PricePathOld,GEcondnPath]=TransitionPath_MixHorz_PType_shooting(PricePathOld, PricePathNames, ParamPath, ParamPathNames, T, V_final, AgentDist_initial, FullFnsToEvaluate, GeneralEqmEqns, PricePathSizeVec, ParamPathSizeVec, PricePathSizeVec_ii, ParamPathSizeVec_ii, GEeqnNames,nGeneralEqmEqns,nGeneralEqmEqns_acrossptypes,GeneralEqmEqnsCell,GeneralEqmEqnParamNames, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, transpathoptions, PTypeStructure) % This code will work for all transition paths except those that involve at % change in the transition matrix pi_z (can handle a change in pi_z, but % only if it is a 'surprise', not anticipated changes) @@ -160,7 +160,7 @@ [PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,PolicyValuesPath_ii]=TransitionPath_FHorz_substeps_Step2_AdjustPolicy(PolicyIndexesPath_ii,T,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).d_gridvals,PTypeStructure.(iistr).aprime_gridvals,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); else %% Go from T-1 to 1 calculating the Value function and Optimal policy function at each step. - warning("TransitionPath_Case1_MixHorz_PType_shooting not yet hanlding e_gridvals and/or pi_e") + warning("TransitionPath_MixHorz_PType_shooting not yet hanlding e_gridvals and/or pi_e") [~,PolicyIndexesPath_ii]=TransitionPath_InfHorz_substeps_Step1_ValueFnIter(T,PolicyIndexesPath_ii,V_final.(iistr),PTypeStructure.(iistr).Parameters,PricePathOld_ii,ParamPath_ii,PricePathSizeVec_ii,ParamPathSizeVec_ii,PricePathNames_ii,ParamPathNames_ii,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).d_gridvals, PTypeStructure.(iistr).a_grid, PTypeStructure.(iistr).z_grid,[],PTypeStructure.(iistr).pi_z,[],PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).DiscountFactorParamNames, PTypeStructure.(iistr).ReturnFnParamNames, transpathoptions,PTypeStructure.(iistr).vfoptions); %% Modify PolicyIndexesPath into forms needed for forward iteration @@ -244,7 +244,7 @@ end AggVars_ii=TransitionPath_FHorz_substeps_Step4tt_AggVars(AgentDist_ii,AgeWeights_ii,PVP_ii_t,tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).l_a,PTypeStructure.(iistr).l_z,PTypeStructure.(iistr).l_e,PTypeStructure.(iistr).N_d,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).ze_gridvals_J_fastOLG,transpathoptions); else - warning("TransitionPath_Case1_MixHorz_PType_shooting not hanlding pi_e yet") + warning("TransitionPath_MixHorz_PType_shooting not hanlding pi_e yet") AgentDist_ii_InfHorz=reshape(AgentDist_ii,[PTypeStructure.(iistr).N_a*PTypeStructure.(iistr).N_z,1]); AgentDistnext_ii=TransitionPath_InfHorz_substeps_Step3tt_IterAgentDist(AgentDist_ii_InfHorz,PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,tt,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).pi_z,[],PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,transpathoptions,PTypeStructure.(iistr).simoptions); if PTypeStructure.(iistr).N_z==0 && PTypeStructure.(iistr).N_e==0 diff --git a/ValueFnIter/MixHorz_PType/ValueFnIter_MixHorz_PType.m b/ValueFnIter/MixHorz_PType/ValueFnIter_MixHorz_PType.m index 309a85a1..c2019f58 100644 --- a/ValueFnIter/MixHorz_PType/ValueFnIter_MixHorz_PType.m +++ b/ValueFnIter/MixHorz_PType/ValueFnIter_MixHorz_PType.m @@ -1,4 +1,4 @@ -function [V, Policy]=ValueFnIter_Case1_MixHorz_PType(n_d,n_a,n_z, N_j,Names_i,d_grid, a_grid, z_grid, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, vfoptions) +function [V, Policy]=ValueFnIter_MixHorz_PType(n_d,n_a,n_z, N_j,Names_i,d_grid, a_grid, z_grid, pi_z, ReturnFn, Parameters, DiscountFactorParamNames, vfoptions) % % vfoptions.verbose=1 will give feedback diff --git a/ValueFnIter/TransPathMixHorz/ValueFnOnTransPath_MixHorz_PType.m b/ValueFnIter/TransPathMixHorz/ValueFnOnTransPath_MixHorz_PType.m index ad48b1c3..25cf814a 100644 --- a/ValueFnIter/TransPathMixHorz/ValueFnOnTransPath_MixHorz_PType.m +++ b/ValueFnIter/TransPathMixHorz/ValueFnOnTransPath_MixHorz_PType.m @@ -1,4 +1,4 @@ -function [VPath,PolicyPath]=ValueFnOnTransPath_Case1_MixHorz_PType(PricePath, ParamPath, T, V_final, Policy_final, Parameters, n_d, n_a, n_z, N_j, Names_i, d_grid, a_grid,z_grid, pi_z, DiscountFactorParamNames, ReturnFn, transpathoptions, vfoptions) +function [VPath,PolicyPath]=ValueFnOnTransPath_MixHorz_PType(PricePath, ParamPath, T, V_final, Policy_final, Parameters, n_d, n_a, n_z, N_j, Names_i, d_grid, a_grid,z_grid, pi_z, DiscountFactorParamNames, ReturnFn, transpathoptions, vfoptions) VPath=struct(); PolicyPath=struct(); From d98979493e48ade486d86afb3b645afae72c8464 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 13 May 2026 19:44:25 +1200 Subject: [PATCH 67/90] Adjust parameter order based on new AgentDistOnTransPath_InfHorz --- .../TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m index 2cd8f06f..bfdc3983 100644 --- a/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m +++ b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m @@ -182,7 +182,7 @@ if isfinite(N_j_temp) AgentDistPath_ii=AgentDistOnTransPath_Case1_FHorz(AgentDist_initial_temp, jequalOneDist_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, AgeWeightsParamNames_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,pi_z_temp, T,Parameters_temp, transpathoptions_temp, simoptions_temp); else - AgentDistPath_ii=AgentDistOnTransPath_InfHorz(AgentDist_initial_temp, PolicyPath_temp, n_d_temp,n_a_temp,n_z_temp,pi_z_temp,T,simoptions_temp, Parameters_temp, PricePath_temp, ParamPath_temp); + AgentDistPath_ii=AgentDistOnTransPath_InfHorz(AgentDist_initial_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, n_d_temp,n_a_temp,n_z_temp,pi_z_temp,T,simoptions_temp, Parameters_temp); end % Note: T cannot depend on ptype, nor can PricePath depend on ptype From e28f34c87f8fa6c132a0c9b8c5637e7dee5bb4bf Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Thu, 14 May 2026 09:28:11 +1200 Subject: [PATCH 68/90] Fixes from stash --- .../SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m index f35d2127..e4f8f404 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m @@ -311,7 +311,7 @@ if simoptions.setup_experienceasset.N_a1==0 PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; From 64b8270b97df9be110681dc70138d74dea9e1847 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 15 May 2026 07:47:47 +1200 Subject: [PATCH 69/90] Add missing call to SetupNonStandardEndoStates_InfHorz_TPath --- TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m index daf5120e..fdfb3a49 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m @@ -316,6 +316,9 @@ PTypeStructure.(iistr).pi_z=pi_z; end + %% If using any non-standard endogenous states, setup for those + [PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions]=SetupNonStandardEndoStates_InfHorz_TPath(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); + PTypeStructure.(iistr).ReturnFn=ReturnFn; if isa(ReturnFn,'struct') PTypeStructure.(iistr).ReturnFn=ReturnFn.(Names_i{ii}); From cbaf27344b37353e8f069477338f50cef6fc6cfe Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 15 May 2026 09:16:43 +1200 Subject: [PATCH 70/90] Fix bad merge into TransitionPath_MixHorz_PType --- .../MixHorz/TransitionPath_MixHorz_PType.m | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m index 36f0ce73..600c6a29 100644 --- a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m @@ -782,48 +782,48 @@ error('The age weights parameter seems to be the wrong size') end end - % Check ParamPath to see if the AgeWeights vary over the transition - % (and overwrite PTypeStructure.(iistr).AgeWeights_T if it does) - temp=strcmp(ParamPathNames,AgeWeightsParamNames.(iistr){1}); - if any(temp) - transpathoptions.ageweightstrivial=0; % AgeWeights vary over the transition - [~,kk]=max(temp); % Get index for the AgeWeightsParamNames{1} in ParamPathNames - % Create AgeWeights_T - PTypeStructure.(iistr).AgeWeights_T=ParamPath(:,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk))'; % This will always be N_j-by-T (as transpose) - % Note: still leave it in ParamPath just in case it is used in AggVars or somesuch - end - - % Because ptypes hardcodes transpathoptions.ageweightstrivial=0, we need - if PTypeStructure.(iistr).simoptions.fastOLG==1 - if N_z==0 - PTypeStructure.(iistr).AgeWeights_T=repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1); % simoptions.fastOLG=1 so this is (a,j)-by-1 - else - PTypeStructure.(iistr).AgeWeights_T=repmat(repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1),N_z,1); % simoptions.fastOLG=1 so this is (a,j,z)-by-1 + % Check ParamPath to see if the AgeWeights vary over the transition + % (and overwrite PTypeStructure.(iistr).AgeWeights_T if it does) + temp=strcmp(ParamPathNames,AgeWeightsParamNames.(iistr){1}); + if any(temp) + transpathoptions.ageweightstrivial=0; % AgeWeights vary over the transition + [~,kk]=max(temp); % Get index for the AgeWeightsParamNames{1} in ParamPathNames + % Create AgeWeights_T + PTypeStructure.(iistr).AgeWeights_T=ParamPath(:,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk))'; % This will always be N_j-by-T (as transpose) + % Note: still leave it in ParamPath just in case it is used in AggVars or somesuch end - PTypeStructure.(iistr).jequalOneDist_T=jequalOneDist_temp; - else - transpathoptions.(iistr).trivialjequalonedist=1; - if N_z==0 - if N_e==0 - AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a,1]); % if simoptions.fastOLG==0 - if PTypeStructure.(iistr).simoptions.fastOLG==1 - AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a,1]); - end + + % Because ptypes hardcodes transpathoptions.ageweightstrivial=0, we need + if PTypeStructure.(iistr).simoptions.fastOLG==1 + if N_z==0 + PTypeStructure.(iistr).AgeWeights_T=repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1); % simoptions.fastOLG=1 so this is (a,j)-by-1 + else + PTypeStructure.(iistr).AgeWeights_T=repmat(repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1),N_z,1); % simoptions.fastOLG=1 so this is (a,j,z)-by-1 end else - if N_e==0 - AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z,1]); - elseif simoptions.fastOLG==0 - AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z*N_e,1]); + transpathoptions.(iistr).trivialjequalonedist=1; + if N_z==0 + if N_e==0 + AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a,1]); % if simoptions.fastOLG==0 + if PTypeStructure.(iistr).simoptions.fastOLG==1 + AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a,1]); + end + end else - AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z,N_e]); + if N_e==0 + AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z,1]); + elseif simoptions.fastOLG==0 + AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z*N_e,1]); + else + AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z,N_e]); + end end end - PTypeStructure.(iistr).jequalOneDist=jequalOneDist_temp; - end + PTypeStructure.(iistr).jequalOneDist_T=jequalOneDist; - AgentDist_initial.(iistr)=AgentDist_init; - clear AgentDist_init + AgentDist_initial.(iistr)=AgentDist_init; + clear AgentDist_init + end %% Which parts of ParamPath and PricePath relate to ptype ii % Some ParamPath and PricePath parameters may depend on ptype From 6b4b336427877af7573014f4d1ba9d55c63b8540 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 15 May 2026 09:19:07 +1200 Subject: [PATCH 71/90] Check existence of Idependsonptype --- .../FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m | 2 +- .../InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m | 2 +- TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m index 97aa1071..4b1dde32 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m @@ -32,7 +32,7 @@ pp_indexinpricepath=zeros(1,length(PricePathNames)); pp_c=0; for pp=1:length(PricePathNames) - if PTypeStructure.PricePath_Idependsonptype(pp)==0 + if isfield(PTypeStructure, 'PricePath_Idependsonptype') && PTypeStructure.PricePath_Idependsonptype(pp)==0 pp_c=pp_c+1; pp_indexinpricepath(pp)=pp_c; else diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m index 2e847389..1ca896a5 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m @@ -34,7 +34,7 @@ pp_indexinpricepath=zeros(1,length(PricePathNames)); pp_c=0; for pp=1:length(PricePathNames) - if PTypeStructure.PricePath_Idependsonptype(pp)==0 + if isfield(PTypeStructure, 'PricePath_Idependsonptype') && PTypeStructure.PricePath_Idependsonptype(pp)==0 pp_c=pp_c+1; pp_indexinpricepath(pp)=pp_c; else diff --git a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m index 56f897c5..e9709ae3 100644 --- a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m +++ b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m @@ -34,7 +34,7 @@ pp_indexinpricepath=zeros(1,length(PricePathNames)); pp_c=0; for pp=1:length(PricePathNames) - if PTypeStructure.PricePath_Idependsonptype(pp)==0 + if isfield(PTypeStructure, 'PricePath_Idependsonptype') && PTypeStructure.PricePath_Idependsonptype(pp)==0 pp_c=pp_c+1; pp_indexinpricepath(pp)=pp_c; else From 25c06824ee6da3e844b930b5bc003f48a8557ac4 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 15 May 2026 16:07:04 +1200 Subject: [PATCH 72/90] Absorb e_grid and pi_e changes Now working for TPath (I think)! --- ...Path_InfHorz_substeps_Step2_AdjustPolicy.m | 51 +++++++++---------- .../MixHorz/TransitionPath_MixHorz_PType.m | 9 ---- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m index ffc6b2e7..5ea68bec 100644 --- a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step2_AdjustPolicy.m @@ -111,10 +111,10 @@ if simoptions.setup_experienceasset.N_a1==0 PolicyaprimePath=repmat(PolicyaprimePath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); else - PolicyaprimePath=repmat(PolicyaprimePath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimePath=repmat(PolicyaprimePath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -130,19 +130,18 @@ PolicyValuesPath=PolicyInd2Val_InfHorz_TPath(PolicyIndexesPath,n_d,n_a,n_z,T-1,d_gridvals,aprime_gridvals,vfoptions,1); PolicyValuesPath=permute(reshape(PolicyValuesPath,[size(PolicyValuesPath,1),N_a,N_z,T-1]),[2,3,1,4]); %[N_a,N_z,l_d+l_a,T-1] % Modify PolicyIndexesPath into forms needed for forward iteration - % Create version of PolicyIndexesPath called PolicyaprimePath, which only tracks aprime - % If there is z then PolicyaprimezPath + % Create version of PolicyIndexesPath called PolicyaprimePath, which only tracks aprime,z % When using grid interpolation layer also PolicyProbsPath if isscalar(n_a1) - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:),[N_a*N_z,T-1]); % aprime index + PolicyaprimezPath=reshape(PolicyIndexesPath(l_d+1,:,:,:),[N_a*N_z,T-1]); % aprime index elseif length(n_a1)==2 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1),[N_a*N_z,T-1]); + PolicyaprimezPath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1),[N_a*N_z,T-1]); elseif length(n_a1)==3 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,:)-1),[N_a*N_z,T-1]); + PolicyaprimezPath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,:)-1),[N_a*N_z,T-1]); elseif length(n_a1)==4 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,:)-1)+n_a1(1)*n_a1(2)*n_a1(3)*(PolicyIndexesPath(l_d+4,:,:,:)-1),[N_a*N_z,T-1]); + PolicyaprimezPath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,:)-1)+n_a1(1)*n_a1(2)*n_a1(3)*(PolicyIndexesPath(l_d+4,:,:,:)-1),[N_a*N_z,T-1]); end - PolicyaprimezPath=gather(PolicyaprimePath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1)); + PolicyaprimezPath=gather(PolicyaprimezPath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1)); if simoptions.gridinterplayer==1 L2index=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,:,:),[N_a*N_z,1,T-1]); % PolicyIndexesPath is of size [l_d+l_aprime+1,N_a,N_z,T] @@ -158,15 +157,15 @@ PolicyaprimezPath=reshape(PolicyaprimezPath,[N_a*N_z,1,T-1]); % so can assume this size later end - clear PolicyIndexesPath PolicyaprimePath L2index + clear PolicyIndexesPath L2index if simoptions.experienceasset==1 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimePath=repmat(PolicyaprimezPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimezPath=repmat(PolicyaprimezPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); else - PolicyaprimePath=repmat(PolicyaprimezPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimezPath=repmat(PolicyaprimezPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -183,7 +182,6 @@ PolicyValuesPath=permute(reshape(PolicyValuesPath,[size(PolicyValuesPath,1),N_a,N_ze,T-1]),[2,3,1,4]); %[N_a,N_e,l_d+l_a,T-1] % Modify PolicyIndexesPath into forms needed for forward iteration % Create version of PolicyIndexesPath called PolicyaprimePath, which only tracks aprime - % For fastOLG we use PolicyaprimejPath, if there is z then PolicyaprimejzPath % When using grid interpolation layer also PolicyProbsPath if isscalar(n_a1) PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:),[N_a*N_e,T-1]); % aprime index @@ -212,10 +210,10 @@ if simoptions.setup_experienceasset.N_a1==0 PolicyaprimePath=repmat(PolicyaprimePath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); else - PolicyaprimePath=repmat(PolicyaprimePath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimePath=repmat(PolicyaprimePath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -231,19 +229,18 @@ PolicyValuesPath=PolicyInd2Val_InfHorz_TPath(PolicyIndexesPath,n_d,n_a,[n_z,n_e],T-1,d_gridvals,aprime_gridvals,vfoptions,1); PolicyValuesPath=permute(reshape(PolicyValuesPath,[size(PolicyValuesPath,1),N_a,N_z*N_e,T-1]),[2,3,1,4]); %[N_a,N_z*N_e,l_d+l_a,T-1] % Modify PolicyIndexesPath into forms needed for forward iteration - % Create version of PolicyIndexesPath called PolicyaprimePath, which only tracks aprime - % For fastOLG we use PolicyaprimejPath, if there is z then PolicyaprimejzPath + % Create version of PolicyIndexesPath called PolicyaprimezPath, which only tracks aprime,z % When using grid interpolation layer also PolicyProbsPath if isscalar(n_a1) - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,:),[N_a*N_z*N_e,T-1]); % aprime index + PolicyaprimezPath=reshape(PolicyIndexesPath(l_d+1,:,:,:,:),[N_a*N_z*N_e,T-1]); % aprime index elseif length(n_a1)==2 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,:)-1),[N_a*N_z*N_e,T-1]); + PolicyaprimezPath=reshape(PolicyIndexesPath(l_d+1,:,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,:)-1),[N_a*N_z*N_e,T-1]); elseif length(n_a1)==3 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,:,:)-1),[N_a*N_z*N_e,T-1]); + PolicyaprimezPath=reshape(PolicyIndexesPath(l_d+1,:,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,:,:)-1),[N_a*N_z*N_e,T-1]); elseif length(n_a1)==4 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,:,:)-1)+n_a1(1)*n_a1(2)*n_a1(3)*(PolicyIndexesPath(l_d+4,:,:,:,:)-1),[N_a*N_z*N_e,T-1]); + PolicyaprimezPath=reshape(PolicyIndexesPath(l_d+1,:,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,:,:)-1)+n_a1(1)*n_a1(2)*n_a1(3)*(PolicyIndexesPath(l_d+4,:,:,:,:)-1),[N_a*N_z*N_e,T-1]); end - PolicyaprimezPath=gather(PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1),N_e,1)); + PolicyaprimezPath=gather(PolicyaprimezPath+repmat(repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1),N_e,1)); if simoptions.gridinterplayer==1 L2index=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,:,:,:),[N_a*N_z*N_e,1,T-1]); % PolicyIndexesPath is of size [l_d+l_aprime+1,N_a,N_z,N_e,T] PolicyaprimezPath=reshape(PolicyaprimezPath,[N_a*N_z*N_e,1,T-1]); % reinterpret this as lower grid index @@ -258,15 +255,15 @@ PolicyaprimezPath=reshape(PolicyaprimezPath,[N_a*N_z*N_e,1,T-1]); % so can assume this size later end - clear PolicyIndexesPath PolicyaprimePath L2index + clear PolicyIndexesPath L2index if simoptions.experienceasset==1 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimePath=repmat(PolicyaprimezPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimezPath=repmat(PolicyaprimezPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); else - PolicyaprimePath=repmat(PolicyaprimezPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimezPath=repmat(PolicyaprimezPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end diff --git a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m index 88c2f443..82692031 100644 --- a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m @@ -551,21 +551,12 @@ %% Organise V_final and AgentDist_initial - % Reshape V_final N_j_temp=PTypeStructure.(iistr).N_j; if ~isfinite(N_j_temp) % Reshape V_final % If no z, then N_z=1 here V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z]); - - % Presume all InfHorz ExogShock functions happen only with respect to transition paths (i.e., z_grid_T/e_grid_T and pi_z_T/pi_e_T else - %% Set up exogenous shock processes - [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).pi_z_J_sim, PTypeStructure.(iistr).e_gridvals_J, PTypeStructure.(iistr).pi_e_J, PTypeStructure.(iistr).pi_e_J_sim, PTypeStructure.(iistr).ze_gridvals_J_fastOLG, transpathoptions, PTypeStructure.(iistr).simoptions]=ExogShockSetup_TPath_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); - % Convert z and e to age-dependent joint-grids and transtion matrix - % output: z_gridvals_J, pi_z_J, e_gridvals_J, pi_e_J, transpathoptions,vfoptions,simoptions - - %% Organise V_final and AgentDist_initial % Reshape V_final if N_z==0 if N_e==0 From a8022f4eabf4e3b3d42ec8b69d1cdcf409bdab80 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 15 May 2026 16:08:45 +1200 Subject: [PATCH 73/90] Potential lowmemory improvements A variety of potentially helpful ways to better use lowmemory with and without lowmemory==3. --- .../ValueFnIter_FHorz_ExpAssetu_DC1_GI_e_raw.m | 10 +++++----- .../ValueFnIter_FHorz_ExpAssetu_DC1_GI_nod1_e_raw.m | 10 +++++----- .../ValueFnIter_FHorz_ExpAssetu_DC1_GI_nod1_raw.m | 8 ++++---- .../ValueFnIter_FHorz_ExpAssetu_DC1_GI_raw.m | 8 ++++---- .../DC1/ValueFnIter_FHorz_ExpAssetu_DC1_e_raw.m | 8 ++++---- .../DC1/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_e_raw.m | 8 ++++---- .../DC1/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_raw.m | 6 +++--- .../DC1/ValueFnIter_FHorz_ExpAssetu_DC1_raw.m | 6 +++--- .../ValueFnIter_FHorz_ExpAssetu_GI_e_raw.m | 8 ++++---- .../ValueFnIter_FHorz_ExpAssetu_GI_nod1_e_raw.m | 8 ++++---- .../ValueFnIter_FHorz_ExpAssetu_GI_nod1_raw.m | 6 +++--- .../ValueFnIter_FHorz_ExpAssetu_GI_raw.m | 8 ++++---- .../ValueFnIter_InfHorz_ExpAsset_Refine_GI_raw.m | 2 +- 13 files changed, 48 insertions(+), 48 deletions(-) diff --git a/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_e_raw.m index d8915234..3ccb7ce7 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_e_raw.m @@ -22,7 +22,7 @@ midpoint=zeros(N_d,1,N_a1,N_a2,N_z,N_e,'gpuArray'); elseif vfoptions.lowmemory==1 midpoint=zeros(N_d,1,N_a1,N_a2,N_z,'gpuArray'); -elseif vfoptions.lowmemory==2 +elseif vfoptions.lowmemory>=2 midpoint=zeros(N_d,1,N_a1,N_a2,'gpuArray'); end @@ -32,7 +32,7 @@ % precompute eind=shiftdim((0:1:N_e-1),-4); % already includes -1 end -if vfoptions.lowmemory==2 +if vfoptions.lowmemory>=2 special_n_z=ones(1,length(n_z)); else % precompute @@ -150,7 +150,7 @@ Policy3(3,:,:,e_c,N_j)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); for e_c=1:N_e @@ -337,7 +337,7 @@ Policy3(3,:,:,e_c,N_j)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -548,7 +548,7 @@ Policy3(3,:,:,e_c,jj)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_nod1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_nod1_e_raw.m index dbe4ba2e..85624add 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_nod1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_nod1_e_raw.m @@ -20,7 +20,7 @@ midpoint=zeros(N_d2,1,N_a1,N_a2,N_z,N_e,'gpuArray'); elseif vfoptions.lowmemory==1 midpoint=zeros(N_d2,1,N_a1,N_a2,N_z,'gpuArray'); -elseif vfoptions.lowmemory==2 +elseif vfoptions.lowmemory>=2 midpoint=zeros(N_d2,1,N_a1,N_a2,'gpuArray'); end @@ -30,7 +30,7 @@ % precompute eind=shiftdim((0:1:N_e-1),-4); % already includes -1 end -if vfoptions.lowmemory==2 +if vfoptions.lowmemory>=2 special_n_z=ones(1,length(n_z)); else % precompute @@ -149,7 +149,7 @@ Policy3(3,:,:,e_c,N_j)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); for e_c=1:N_e @@ -338,7 +338,7 @@ Policy3(3,:,:,e_c,N_j)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -545,7 +545,7 @@ Policy3(3,:,:,e_c,jj)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_nod1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_nod1_raw.m index 82a31781..a1d10453 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_nod1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_nod1_raw.m @@ -25,7 +25,7 @@ % Preallocate if vfoptions.lowmemory==0 midpoint=zeros(N_d2,1,N_a1,N_a2,N_z,'gpuArray'); -elseif vfoptions.lowmemory==1 +elseif vfoptions.lowmemory>=1 midpoint=zeros(N_d2,1,N_a1,N_a2,'gpuArray'); end @@ -94,7 +94,7 @@ Policy3(2,:,:,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,N_j)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); % n-Monotonicity @@ -223,7 +223,7 @@ Policy3(2,:,:,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,N_j)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -375,7 +375,7 @@ Policy3(3,:,:,jj)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_raw.m index c0fe69c2..0c958d2d 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DC1/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI_raw.m @@ -17,7 +17,7 @@ pi_u=shiftdim(pi_u,-2); % put it into third dimension -if vfoptions.lowmemory==1 +if vfoptions.lowmemory>=1 special_n_z=ones(1,length(n_z)); % Preallocate midpoint=zeros(N_d,1,N_a1,N_a2,'gpuArray'); @@ -93,7 +93,7 @@ Policy3(2,:,:,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,N_j)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); % n-Monotonicity @@ -224,7 +224,7 @@ Policy3(2,:,:,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,N_j)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -382,7 +382,7 @@ save tempB.mat midpointB DiscountedEVB level1ii end - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_e_raw.m index db82fd01..9b41bed7 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_e_raw.m @@ -28,7 +28,7 @@ % precompute zind=shiftdim((0:1:N_z-1),-3); % already includes -1 zBind=shiftdim((0:1:N_z-1),-1); % already includes -1 -elseif vfoptions.lowmemory==2 +elseif vfoptions.lowmemory>=2 special_n_e=ones(1,length(n_e)); special_n_z=ones(1,length(n_z)); end @@ -140,7 +140,7 @@ end end end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); for e_c=1:N_e @@ -336,7 +336,7 @@ end end end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); DiscountedEV_z=DiscountedEV_z(:,:,:,:,z_c); @@ -552,7 +552,7 @@ end end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); DiscountedEV_z=DiscountedEV(:,:,:,:,z_c); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_e_raw.m index 7be357f7..557cd67d 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_e_raw.m @@ -27,7 +27,7 @@ % precompute zind=shiftdim((0:1:N_z-1),-3); % already includes -1 zBind=shiftdim((0:1:N_z-1),-1); % already includes -1 -elseif vfoptions.lowmemory==2 +elseif vfoptions.lowmemory>=2 special_n_e=ones(1,length(n_e)); special_n_z=ones(1,length(n_z)); end @@ -139,7 +139,7 @@ end end end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); for z_c=1:N_z @@ -336,7 +336,7 @@ end end end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); @@ -551,7 +551,7 @@ end end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_raw.m index d96188de..4167bb2c 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_raw.m @@ -80,7 +80,7 @@ Policy(curraindex,:,N_j)=shiftdim(maxindex+N_d2*(loweredge(allind)-1),1); end end - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); % n-Monotonicity @@ -217,7 +217,7 @@ end end - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -375,7 +375,7 @@ end end - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_raw.m index 246b11bc..cbd27199 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DC1/ValueFnIter_FHorz_ExpAssetu_DC1_raw.m @@ -84,7 +84,7 @@ end end - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -218,7 +218,7 @@ end end - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -372,7 +372,7 @@ end end - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_e_raw.m index fb664fd5..557d74dc 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_e_raw.m @@ -23,7 +23,7 @@ if vfoptions.lowmemory>=1 special_n_e=ones(1,length(n_e),'gpuArray'); end -if vfoptions.lowmemory==2 +if vfoptions.lowmemory>=2 special_n_z=ones(1,length(n_z)); else zind=shiftdim((0:1:N_z-1),-3); % already includes -1 @@ -88,7 +88,7 @@ Policy3(2,:,:,e_c,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,e_c,N_j)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); for e_c=1:N_e @@ -202,7 +202,7 @@ Policy3(2,:,:,e_c,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,e_c,N_j)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); DiscountedEV_z=DiscountedEV(:,:,:,:,z_c); @@ -335,7 +335,7 @@ Policy3(2,:,:,e_c,jj)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,e_c,jj)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); DiscountedEV_z=DiscountedEV(:,:,:,:,z_c); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_nod1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_nod1_e_raw.m index 4d07a3dd..4beb01cb 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_nod1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_nod1_e_raw.m @@ -21,7 +21,7 @@ if vfoptions.lowmemory>0 special_n_e=ones(1,length(n_e)); end -if vfoptions.lowmemory==2 +if vfoptions.lowmemory>=2 special_n_z=ones(1,length(n_z)); else zind=shiftdim((0:1:N_z-1),-3); @@ -88,7 +88,7 @@ Policy3(3,:,:,e_c,N_j)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -200,7 +200,7 @@ Policy3(2,:,:,e_c,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,e_c,N_j)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); DiscountedEV_z=DiscountedEV(:,:,:,:,z_c); @@ -328,7 +328,7 @@ Policy3(2,:,:,e_c,jj)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,e_c,jj)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind end - elseif vfoptions.lowmemory==2 + elseif vfoptions.lowmemory>=2 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); DiscountedEV_z=DiscountedEV(:,:,:,:,z_c); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_nod1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_nod1_raw.m index a5586626..e5747959 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_nod1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_nod1_raw.m @@ -59,7 +59,7 @@ Policy3(1,:,:,N_j)=d_ind; % d2 Policy3(2,:,:,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,N_j)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, 0, n_d2, n_a1, n_a1,n_a2, special_n_z, d2_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,1); @@ -140,7 +140,7 @@ Policy3(2,:,:,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,N_j)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); @@ -243,7 +243,7 @@ Policy3(2,:,:,jj)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,jj)=shiftdim(ceil(maxindexL2/N_d2),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); diff --git a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_raw.m index e89fc38b..39ab17ea 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI_raw.m @@ -19,7 +19,7 @@ pi_u=shiftdim(pi_u,-2); % put it into third dimension -if vfoptions.lowmemory==1 +if vfoptions.lowmemory>=1 special_n_z=ones(1,length(n_z)); else zind=shiftdim((0:1:N_z-1),-3); % already includes -1 @@ -64,7 +64,7 @@ Policy3(2,:,:,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,N_j)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); ReturnMatrix_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,n_a2,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, a2_gridvals, z_val, ReturnFnParamsVec,1); @@ -147,7 +147,7 @@ Policy3(2,:,:,N_j)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,N_j)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 DiscountedEV=repelem(DiscountedEV,N_d1,1); % precompute this as we need in a loop for z_c=1:N_z @@ -252,7 +252,7 @@ Policy3(2,:,:,jj)=shiftdim(squeeze(midpoint(allind)),-1); % a1prime midpoint Policy3(3,:,:,jj)=shiftdim(ceil(maxindexL2/N_d),-1); % a1primeL2ind - elseif vfoptions.lowmemory==1 + elseif vfoptions.lowmemory>=1 for z_c=1:N_z z_val=z_gridvals_J(z_c,:,jj); diff --git a/ValueFnIter/InfHorz/ExpAsset/GridInterpLayer/ValueFnIter_InfHorz_ExpAsset_Refine_GI_raw.m b/ValueFnIter/InfHorz/ExpAsset/GridInterpLayer/ValueFnIter_InfHorz_ExpAsset_Refine_GI_raw.m index ecc5cf17..3b737b98 100644 --- a/ValueFnIter/InfHorz/ExpAsset/GridInterpLayer/ValueFnIter_InfHorz_ExpAsset_Refine_GI_raw.m +++ b/ValueFnIter/InfHorz/ExpAsset/GridInterpLayer/ValueFnIter_InfHorz_ExpAsset_Refine_GI_raw.m @@ -32,7 +32,7 @@ % For refinement, now we solve for d*(aprime,a,z) that maximizes the ReturnFn [ReturnMatrixfine,dstar]=max(ReturnMatrixfine,[],1); ReturnMatrixfine=shiftdim(ReturnMatrixfine,1); -elseif vfoptions.lowmemory==1 % loop over z +elseif vfoptions.lowmemory>=1 % loop over z %% Refinement: calculate ReturnMatrix and 'remove' the d dimension ReturnMatrixfine=zeros(N_d2*N_a1prime,N_a,N_z,'gpuArray'); % 'refined' return matrix dstar=zeros(N_d2*N_a1prime,N_a,N_z,'gpuArray'); From bb8d397cbd919683fd461429c7a7916759f2eb5a Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 15 May 2026 16:39:55 +1200 Subject: [PATCH 74/90] Initialize transpathoptions.trivialjequalonedist=0 --- .../MixHorz/TransitionPath_MixHorz_PType.m | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m index 82692031..5122f348 100644 --- a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m @@ -641,6 +641,7 @@ end end end + % Get AgeWeights and switch into the transpathoptions.ageweightstrivial=0 setup (and this is what subfns hardcode when doing PTypes) % It is assumed there is only one Age Weight Parameter (name)) % AgeWeights_T is (a,j,z)-by-T (create as j-by-T to start, then switch) @@ -667,6 +668,49 @@ error('The age weights parameter seems to be the wrong size') end end + + %% Set up jequalOneDist_T.(iistr) [hardcodes transpathoptions.trivialjequalonedist=0] + if ~isstruct(jequalOneDist) + jequalOneDist_temp=gpuArray(jequalOneDist); + else % jequalOneDist is a structure + jequalOneDist_temp=gpuArray(jequalOneDist.(iistr)); + end + % Check if jequalOneDistPath is a path or not (and reshape appropriately) + temp=size(jequalOneDist_temp); + if temp(end)==T % jequalOneDist depends on T + transpathoptions.(iistr).trivialjequalonedist=0; + if N_z==0 + if N_e==0 + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a,T]); + else + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_e,T]); % simoptions.fastOLG==1 + end + else + if N_e==0 + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z,T]); % simoptions.fastOLG==1 + else + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z*N_e,T]); % simoptions.fastOLG==1 + end + end + PTypeStructure.(iistr).jequalOneDist_T=jequalOneDist_temp; + else + transpathoptions.(iistr).trivialjequalonedist=1; + if N_z==0 + if N_e==0 + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a,1]); + else + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_e,1]); % simoptions.fastOLG==1 + end + else + if N_e==0 + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z,1]); % simoptions.fastOLG==1 + else + jequalOneDist_temp=reshape(jequalOneDist_temp,[N_a*N_z*N_e,1]); % simoptions.fastOLG==1 (how different than simoptions.fastOLG==0?) + end + end + PTypeStructure.(iistr).jequalOneDist=jequalOneDist_temp; + end + % Check ParamPath to see if the AgeWeights vary over the transition % (and overwrite PTypeStructure.(iistr).AgeWeights_T if it does) temp=strcmp(ParamPathNames,AgeWeightsParamNames.(iistr){1}); From 539e5d33fa8c60c7624a51d69d47351ae4b18800 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sat, 16 May 2026 05:13:05 +1200 Subject: [PATCH 75/90] Fix more merge problems This makes it through! --- .../MixHorz/TransitionPath_MixHorz_PType.m | 92 +++++++------------ 1 file changed, 33 insertions(+), 59 deletions(-) diff --git a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m index 5122f348..fdf64e44 100644 --- a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m @@ -551,12 +551,8 @@ %% Organise V_final and AgentDist_initial - N_j_temp=PTypeStructure.(iistr).N_j; - if ~isfinite(N_j_temp) - % Reshape V_final - % If no z, then N_z=1 here - V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z]); - else + if isfinite(PTypeStructure.(iistr).N_j) + N_j_temp=PTypeStructure.(iistr).N_j; % Reshape V_final if N_z==0 if N_e==0 @@ -600,44 +596,27 @@ AgeWeights_init=repelem(AgeWeights_init',N_a,1); end else - if N_e==0 - V_final.(iistr)=reshape(permute(V_final.(iistr),[1,3,2]),[N_a*N_j_temp,N_z]); - else - V_final.(iistr)=reshape(permute(V_final.(iistr),[1,4,2,3]),[N_a*N_j_temp,N_z,N_e]); - end - end - end - % Reshape AgentDist_initial - if N_z==0 - if N_e==0 - AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a,N_j_temp]); % if simoptions.fastOLG==0 - AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j_ii] - if PTypeStructure.(iistr).simoptions.fastOLG==1 - AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_j_temp,1]); - AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); - end - else - AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_e,N_j_temp]); % if simoptions.fastOLG==0 - AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j_ii] - if PTypeStructure.(iistr).simoptions.fastOLG==1 % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 - AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_e,N_j_temp]),[1,3,2]),[N_a*N_j_temp*N_e,1]); - AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); + AgentDist_init=reshape(AgentDist_init,[N_a*N_e,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] + if PTypeStructure.(iistr).simoptions.fastOLG % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 + AgentDist_init=reshape(permute(reshape(AgentDist_init,[N_a,N_e,N_j_temp]),[1,3,2]),[N_a*N_j_temp,N_e]); + AgeWeights_init=repelem(AgeWeights_init',N_a,1); end end else if N_e==0 - AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_z,N_j_temp]); % if simoptions.fastOLG==0 - AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j_ii] - if PTypeStructure.(iistr).simoptions.fastOLG==1 % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 - AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_z,N_j_temp]),[1,3,2]),[N_a*N_j_temp*N_z,1]); - AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); + AgentDist_init=reshape(AgentDist_init,[N_a*N_z,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] + if PTypeStructure.(iistr).simoptions.fastOLG % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 + AgentDist_init=reshape(permute(reshape(AgentDist_init,[N_a,N_z,N_j_temp]),[1,3,2]),[N_a*N_j_temp*N_z,1]); + AgeWeights_init=repelem(AgeWeights_init',N_a,1); end else - AgentDist_initial.(iistr)=reshape(AgentDist_initial.(iistr),[N_a*N_z*N_e,N_j_temp]); % if simoptions.fastOLG==0 - AgeWeights_init.(iistr)=sum(AgentDist_initial.(iistr),1); % [1,N_j_ii] - if PTypeStructure.(iistr).simoptions.fastOLG==1 % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 - AgentDist_initial.(iistr)=reshape(permute(reshape(AgentDist_initial.(iistr),[N_a,N_z,N_e,N_j_temp]),[1,4,2,3]),[N_a*N_j_temp*N_z,N_e]); - AgeWeights_init.(iistr)=repelem(AgeWeights_init.(iistr)',N_a,1); + AgentDist_init=reshape(AgentDist_init,[N_a*N_z*N_e,N_j_temp]); % if simoptions.fastOLG==0 + AgeWeights_init=sum(AgentDist_init,1); % [1,N_j] + if PTypeStructure.(iistr).simoptions.fastOLG % simoptions.fastOLG==1, so AgentDist is treated as : (a,j,z)-by-1 + AgentDist_init=reshape(permute(reshape(AgentDist_init,[N_a,N_z,N_e,N_j_temp]),[1,4,2,3]),[N_a*N_j_temp*N_z,N_e]); + AgeWeights_init=repelem(AgeWeights_init',N_a,1); end end end @@ -729,31 +708,23 @@ else PTypeStructure.(iistr).AgeWeights_T=repmat(repelem(PTypeStructure.(iistr).AgeWeights_T,N_a,1),N_z,1); % simoptions.fastOLG=1 so this is (a,j,z)-by-1 end - else - transpathoptions.(iistr).trivialjequalonedist=1; - if N_z==0 - if N_e==0 - AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a,1]); % if simoptions.fastOLG==0 - if PTypeStructure.(iistr).simoptions.fastOLG==1 - AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a,1]); - end - end - else - if N_e==0 - AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z,1]); - elseif simoptions.fastOLG==0 - AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z*N_e,1]); - else - AgentDist_init.(iistr)=reshape(AgentDist_init.(iistr),[N_a*N_z,N_e]); - end - end end - PTypeStructure.(iistr).jequalOneDist_T=jequalOneDist; + else + % Reshape V_final + % If no z, then N_z=1 here + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z]); - AgentDist_initial.(iistr)=AgentDist_init; - clear AgentDist_init + % Reshape AgentDist_initial + if N_z==0 + AgentDist_init=reshape(AgentDist_initial.(iistr),[N_a,1]); + else + AgentDist_init=reshape(AgentDist_initial.(iistr),[N_a*N_z,1]); + end end + AgentDist_initial.(iistr)=AgentDist_init; + clear AgentDist_init + %% Which parts of ParamPath and PricePath relate to ptype ii % Some ParamPath and PricePath parameters may depend on ptype PTypeStructure.(iistr).RelevantPricePath=ones(1,size(PricePath0,2)); % start will all relevant @@ -938,6 +909,9 @@ end end end + for gg=1:length(GEeqnNames) + GEcondnPath.(GEeqnNames{gg})=GEcondnPathmatrix(:,gg)'; + end if nargout==1 varargout={PricePathStruct}; From 7fb704d2bfdfdb905b680276bead325b6492077e Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sat, 16 May 2026 06:32:11 +1200 Subject: [PATCH 76/90] Merge fixes and prep End-to-end success! --- .../EvalFnOnAgentDist_ProbDensityFn_InfHorz.m | 169 ++++++++++++++++ ...teroAgentStationaryEqm_Case1_FHorz_PType.m | 182 ++++++------------ .../HeteroAgentStationaryEqm_InfHorz_PType.m | 30 +-- .../HeteroAgentStationaryEqm_MixHorz_PType.m | 87 ++------- .../TransPath/AgentDistOnTransPath_InfHorz.m | 4 +- .../AgentDistOnTransPath_MixHorz_PType.m | 2 +- 6 files changed, 260 insertions(+), 214 deletions(-) create mode 100644 EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_ProbDensityFn_InfHorz.m diff --git a/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_ProbDensityFn_InfHorz.m b/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_ProbDensityFn_InfHorz.m new file mode 100644 index 00000000..7d6f7e5e --- /dev/null +++ b/EvaluateFnOnAgentDist/InfHorz/EvalFnOnAgentDist_ProbDensityFn_InfHorz.m @@ -0,0 +1,169 @@ +function ProbDensityFns=EvalFnOnAgentDist_ProbDensityFn_InfHorz(StationaryDist, Policy, FnsToEvaluate, Parameters, FnsToEvaluateParamNames, n_d, n_a, n_z, d_grid, a_grid, z_grid,simoptions,EntryExitParamNames) +% Evaluates the aggregate value (weighted sum/integral) for each element of FnsToEvaluate +% +% Parallel, simoptions and EntryExitParamNames are optional inputs, only needed when using endogenous entry + +%% +Parallel=1+(gpuDeviceCount>0); +if ~isfield(simoptions,'gridinterplayer') + simoptions.gridinterplayer=0; +end + +if n_d(1)==0 + l_d=0; +else + l_d=length(n_d); +end +l_a=length(n_a); +l_z=length(n_z); + +N_a=prod(n_a); +N_z=prod(n_z); + +l_daprime=size(Policy,1); +if simoptions.gridinterplayer==1 + l_daprime=l_daprime-1; +end + +%% +if isstruct(StationaryDist) + % Even though Mass is unimportant, still need to deal with 'exit' in PolicyIndexes. + ProbDensityFns=EvalFnOnAgentDist_ProbDensityFn_InfHorz_Mass(StationaryDist.pdf,StationaryDist.mass, Policy, FnsToEvaluate, Parameters, FnsToEvaluateParamNames,EntryExitParamNames, n_d, n_a, n_z, d_grid, a_grid, z_grid, Parallel,simoptions); + return +end + +%% Implement new way of handling FnsToEvaluate +if isstruct(FnsToEvaluate) + FnsToEvaluateStruct=1; + clear FnsToEvaluateParamNames + AggVarNames=fieldnames(FnsToEvaluate); + for ff=1:length(AggVarNames) + temp=getAnonymousFnInputNames(FnsToEvaluate.(AggVarNames{ff})); + if length(temp)>(l_daprime+l_a+l_z) + FnsToEvaluateParamNames(ff).Names={temp{l_daprime+l_a+l_z+1:end}}; % the first inputs will always be (d,aprime,a,z) + else + FnsToEvaluateParamNames(ff).Names={}; + end + FnsToEvaluate2{ff}=FnsToEvaluate.(AggVarNames{ff}); + end + FnsToEvaluate=FnsToEvaluate2; +else + FnsToEvaluateStruct=0; +end + +%% +if Parallel==2 + StationaryDist=gpuArray(StationaryDist); + Policy=gpuArray(Policy); + n_d=gpuArray(n_d); + n_a=gpuArray(n_a); + n_z=gpuArray(n_z); + d_grid=gpuArray(d_grid); + a_gridvals=CreateGridvals(n_z,gpuArray(a_grid),1); + z_gridvals=CreateGridvals(n_z,gpuArray(z_grid),1); + + StationaryDistVec=reshape(StationaryDist,[N_a*N_z,1]); + + ProbDensityFns=zeros(N_a*N_z,length(FnsToEvaluate),'gpuArray'); + + l_daprime=size(Policy,1); + PolicyValues=PolicyInd2Val_InfHorz(Policy,n_d,n_a,n_z,d_grid,a_grid); + permuteindexes=[1+(1:1:(l_a+l_z)),1]; + PolicyValuesPermute=permute(PolicyValues,permuteindexes); %[n_a,n_s,l_d+l_a] + + for ff=1:length(FnsToEvaluate) + FnToEvaluateParamsCell=CreateCellFromParams(Parameters,FnsToEvaluateParamNames(ff).Names); + Values=EvalFnOnAgentDist_Grid(FnsToEvaluate{ff}, FnToEvaluateParamsCell,PolicyValuesPermute,l_daprime,n_a,n_z,a_gridvals,z_gridvals); + Values=reshape(Values,[N_a*N_z,1]); + ProbDensityFns(:,ff)=Values.*StationaryDistVec; + end + +else + [d_gridvals, aprime_gridvals]=CreateGridvals_Policy(Policy,n_d,n_a,n_a,n_z,d_grid,a_grid,1, 2); + a_gridvals=CreateGridvals(n_a,a_grid,2); + z_gridvals=CreateGridvals(n_z,z_grid,2); + + StationaryDistVec=reshape(StationaryDist,[N_a*N_z,1]); + + StationaryDistVec=gather(StationaryDistVec); + + ProbDensityFns=zeros(N_a*N_z,length(FnsToEvaluate)); + + if l_d>0 + for ff=1:length(FnsToEvaluate) + % Includes check for cases in which no parameters are actually required + if isempty(FnsToEvaluateParamNames(ff).Names) % check for 'SSvalueParamNames={}' + Values=zeros(N_a*N_z,1); + for ii=1:N_a*N_z + j1=rem(ii-1,N_a)+1; + j2=ceil(ii/N_a); + Values(ii)=FnsToEvaluate{ff}(d_gridvals{j1+(j2-1)*N_a,:},aprime_gridvals{j1+(j2-1)*N_a,:},a_gridvals{j1,:},z_gridvals{j2,:}); + end + ProbDensityFns(:,ff)=Values.*StationaryDistVec; + else + FnToEvaluateParamsCell=num2cell(CreateVectorFromParams(Parameters,FnsToEvaluateParamNames(ff).Names)); + Values=zeros(N_a*N_z,1); + for ii=1:N_a*N_z + j1=rem(ii-1,N_a)+1; + j2=ceil(ii/N_a); + Values(ii)=FnsToEvaluate{ff}(d_gridvals{j1+(j2-1)*N_a,:},aprime_gridvals{j1+(j2-1)*N_a,:},a_gridvals{j1,:},z_gridvals{j2,:},FnToEvaluateParamsCell{:}); + end + ProbDensityFns(:,ff)=Values.*StationaryDistVec; + end + end + + else %l_d=0 + + for ff=1:length(FnsToEvaluate) + % Includes check for cases in which no parameters are actually required + if isempty(FnsToEvaluateParamNames(ff).Names) % check for 'SSvalueParamNames={}' + Values=zeros(N_a*N_z,1); + for ii=1:N_a*N_z + j1=rem(ii-1,N_a)+1; + j2=ceil(ii/N_a); + Values(ii)=FnsToEvaluate{ff}(aprime_gridvals{j1+(j2-1)*N_a,:},a_gridvals{j1,:},z_gridvals{j2,:}); + end + % When evaluating value function (which may sometimes give -Inf values) on StationaryDistVec (which at those points will be + % 0) we get 'NaN'. Use temp as intermediate variable just eliminate those. + ProbDensityFns(:,ff)=Values.*StationaryDistVec; + else + FnToEvaluateParamsCell=num2cell(CreateVectorFromParams(Parameters,FnsToEvaluateParamNames(ff).Names)); + Values=zeros(N_a*N_z,1); + for ii=1:N_a*N_z + j1=rem(ii-1,N_a)+1; + j2=ceil(ii/N_a); + Values(ii)=FnsToEvaluate{ff}(aprime_gridvals{j1+(j2-1)*N_a,:},a_gridvals{j1,:},z_gridvals{j2,:},FnToEvaluateParamsCell{:}); + end + ProbDensityFns(:,ff)=Values.*StationaryDistVec; + end + end + end +end + +% Normalize to 1 (to make it a pdf) +for ff=1:length(FnsToEvaluate) + ProbDensityFns(:,ff)=ProbDensityFns(:,ff)/sum(ProbDensityFns(:,ff)); +end + +% When evaluating value function (which may sometimes give -Inf values) on StationaryDistVec (which at those points will be +% 0) we get 'NaN'. Just eliminate those. +ProbDensityFns(isnan(ProbDensityFns))=0; + +%% +if FnsToEvaluateStruct==1 + % Change the output into a structure + ProbDensityFns2=ProbDensityFns'; % Note the transpose + clear ProbDensityFns + ProbDensityFns=struct(); +% AggVarNames=fieldnames(FnsToEvaluate); + for ff=1:length(AggVarNames) + ProbDensityFns.(AggVarNames{ff})=reshape(ProbDensityFns2(ff,:),[n_a,n_z]); + end +else + % Change the ordering and size so that ProbDensityFns has same kind of + % shape as StationaryDist, except first dimension indexes the 'FnsToEvaluate'. + ProbDensityFns=ProbDensityFns'; + ProbDensityFns=reshape(ProbDensityFns,[length(FnsToEvaluate),n_a,n_z]); +end + +end diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m index 6537a9a2..ec5d46f2 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m @@ -274,7 +274,7 @@ try if isstruct(Parameters.(paramnames{pp})) % parameter depends on ptype as a structure for ii=1:N_i - Parameters.([paramnames{pp},'_',Names_i{ii}])=Parameters.(paramnames{pp}).(Names_i{ii}); + Parameters.([paramnames{pp},'_',Names_i{ii}])=Parameters.(paramnames{pp}).(iistr); end paramthatdependsonptype(pp)=1; paramnamesptype{sum(paramthatdependsonptype)}=paramnames{pp}; @@ -368,7 +368,7 @@ % Horizon is determined via N_j if isstruct(N_j) - PTypeStructure.(iistr).N_j=N_j.(Names_i{ii}); + PTypeStructure.(iistr).N_j=N_j.(iistr); elseif isscalar(N_j) PTypeStructure.(iistr).N_j=N_j; else @@ -376,17 +376,17 @@ end if isstruct(n_d) - PTypeStructure.(iistr).n_d=n_d.(Names_i{ii}); + PTypeStructure.(iistr).n_d=n_d.(iistr); else PTypeStructure.(iistr).n_d=n_d; end if isstruct(n_a) - PTypeStructure.(iistr).n_a=n_a.(Names_i{ii}); + PTypeStructure.(iistr).n_a=n_a.(iistr); else PTypeStructure.(iistr).n_a=n_a; end if isstruct(n_z) - PTypeStructure.(iistr).n_z=n_z.(Names_i{ii}); + PTypeStructure.(iistr).n_z=n_z.(iistr); else PTypeStructure.(iistr).n_z=n_z; end @@ -410,66 +410,16 @@ end if isstruct(d_grid) - PTypeStructure.(iistr).d_grid=d_grid.(Names_i{ii}); + PTypeStructure.(iistr).d_grid=d_grid.(iistr); else PTypeStructure.(iistr).d_grid=d_grid; end if isstruct(a_grid) - PTypeStructure.(iistr).a_grid=a_grid.(Names_i{ii}); + PTypeStructure.(iistr).a_grid=a_grid.(iistr); else PTypeStructure.(iistr).a_grid=a_grid; end - %% Parameter Structure - % Parameters are allowed to be given as structure, or as vector/matrix - % (in terms of their dependence on permanent type). So go through each of - % these in term. - % ie. Parameters.alpha=[0;1]; or Parameters.alpha.ptype1=0; Parameters.alpha.ptype2=1; - FullParamNames=fieldnames(Parameters); % all the different parameters - nFields=length(FullParamNames); - for kField=1:nFields - if isstruct(Parameters.(FullParamNames{kField})) % Check the current parameter for permanent type in structure form - % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) - if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); - end - elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. - temp=Parameters.(FullParamNames{kField}); - [~,ptypedim]=max(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i); % Parameters as vector/matrix can be at most two dimensional, figure out which relates to PType, it should be the row dimension, if it is not then give a warning. - if ptypedim==1 - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=temp(ii,:); - elseif ptypedim==2 - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=temp(:,ii); - end - end - end - - %% Set up exogenous shock grids now (so they can then just be reused every time) - % CHECK IF THE FOLLOWING IS REDUNDANT/OBSOLETE down to next %% - if isstruct(z_grid) - PTypeStructure.(iistr).z_grid=z_grid.(Names_i{ii}); - else - % If the last dimension is of length N_i, this indicates dependence on ptype - nn=size(z_grid,ndims(z_grid)); - if nn==N_i - otherdims = repmat({':'},1,ndims(z_grid)-1); - PTypeStructure.(iistr).z_grid=z_grid(otherdims{:},ii); - else - PTypeStructure.(iistr).z_grid=z_grid; - end - end - if isstruct(pi_z) - PTypeStructure.(iistr).pi_z=pi_z.(Names_i{ii}); - else - % If the last dimension is of length N_i, this indicates dependence on ptype - nn=size(pi_z,ndims(pi_z)); - if nn==N_i - otherdims = repmat({':'},1,ndims(pi_z)-1); - PTypeStructure.(iistr).pi_z=pi_z(otherdims{:},ii); - else - PTypeStructure.(iistr).pi_z=pi_z; - end - end %% Parameter Structure % Parameters are allowed to be given as structure, or as vector/matrix @@ -481,10 +431,10 @@ FullParamNames=fieldnames(Parameters); % all the different parameters nFields=length(FullParamNames); for kField=1:nFields - if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form + if isstruct(Parameters.(FullParamNames{kField})) % Check the current parameter for permanent type in structure form % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) - if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); + if isfield(Parameters.(FullParamNames{kField}),iistr) + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(iistr); end elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. temp=Parameters.(FullParamNames{kField}); @@ -519,46 +469,28 @@ end % Switch over to joint-grids - if isfinite(PTypeStructure.(iistr).N_j) % FHorz - % If z (and e) are not determined in GE, then compute z_gridvals_J and pi_z_J now (and e_gridvals_J and pi_e_J) - if heteroagentoptions.gridsinGE(ii)==0 - [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).vfoptions]=ExogShockSetup_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,3); - % Note: these are actually z_gridvals_J and pi_z_J - PTypeStructure.(iistr).simoptions.e_gridvals_J=PTypeStructure.(iistr).vfoptions.e_gridvals_J; % Note, will be [] if no e - PTypeStructure.(iistr).simoptions.pi_e_J=PTypeStructure.(iistr).vfoptions.pi_e_J; % Note, will be [] if no e - else - % % Create placeholders, as these will need to be created in general eqm since they depend on General eqm parameters - % PTypeStructure.(iistr).z_gridvals_J=[]; - % PTypeStructure.(iistr).pi_z_J=[]; - end - PTypeStructure.(iistr)=rmfield(PTypeStructure.(iistr),'z_grid'); % Should not be used, as now have z_gridvals_J - PTypeStructure.(iistr)=rmfield(PTypeStructure.(iistr),'pi_z'); % Should not be used, as now have pi_z_J - if isfield(PTypeStructure.(iistr).simoptions,'ExogShockFn') % Note: ExogShockSetup_FHorz() removed ExogShockFn from vfoptions but not from simoptions - if heteroagentoptions.useCustomModelStats==1 - heteroagentoptions.CustomModelStatsInputs.z_grid=PTypeStructure.(iistr).z_gridvals_J; - heteroagentoptions.CustomModelStatsInputs.pi_z=PTypeStructure.(iistr).pi_z_J; - end - PTypeStructure.(iistr).simoptions=rmfield(simoptions,'ExogShockFn'); - end - else % InfHorz - if heteroagentoptions.gridsinGE(ii)==0 - [PTypeStructure.(iistr).z_gridvals, PTypeStructure.(iistr).pi_z, PTypeStructure.(iistr).vfoptions]=ExogShockSetup_InfHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,3); - PTypeStructure.(iistr).simoptions.e_gridvals=PTypeStructure.(iistr).vfoptions.e_gridvals; % Note, will be [] if no e - PTypeStructure.(iistr).simoptions.pi_e=PTypeStructure.(iistr).vfoptions.pi_e; % Note, will be [] if no e - else - % % Create placeholders, as these will need to be created in general eqm since they depend on General eqm parameters - % PTypeStructure.(iistr).z_gridvals=[]; - % PTypeStructure.(iistr).pi_z=[]; - end - if isfield(PTypeStructure.(iistr).simoptions,'ExogShockFn') % Note: ExogShockSetup_InfHorz() removed ExogShockFn from vfoptions but not from simoptions - if heteroagentoptions.useCustomModelStats==1 - heteroagentoptions.CustomModelStatsInputs.z_grid=PTypeStructure.(iistr).z_gridvals_J; - heteroagentoptions.CustomModelStatsInputs.pi_z=PTypeStructure.(iistr).pi_z_J; - end - PTypeStructure.(iistr).simoptions=rmfield(PTypeStructure.(iistr).simoptions,'ExogShockFn'); + % If z (and e) are not determined in GE, then compute z_gridvals_J and pi_z_J now (and e_gridvals_J and pi_e_J) + if heteroagentoptions.gridsinGE(ii)==0 + [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).vfoptions]=ExogShockSetup_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,3); + % Note: these are actually z_gridvals_J and pi_z_J + PTypeStructure.(iistr).simoptions.e_gridvals_J=PTypeStructure.(iistr).vfoptions.e_gridvals_J; % Note, will be [] if no e + PTypeStructure.(iistr).simoptions.pi_e_J=PTypeStructure.(iistr).vfoptions.pi_e_J; % Note, will be [] if no e + else + % % Create placeholders, as these will need to be created in general eqm since they depend on General eqm parameters + % PTypeStructure.(iistr).z_gridvals_J=[]; + % PTypeStructure.(iistr).pi_z_J=[]; + end + PTypeStructure.(iistr)=rmfield(PTypeStructure.(iistr),'z_grid'); % Should not be used, as now have z_gridvals_J + PTypeStructure.(iistr)=rmfield(PTypeStructure.(iistr),'pi_z'); % Should not be used, as now have pi_z_J + if isfield(PTypeStructure.(iistr).simoptions,'ExogShockFn') % Note: ExogShockSetup_FHorz() removed ExogShockFn from vfoptions but not from simoptions + if heteroagentoptions.useCustomModelStats==1 + heteroagentoptions.CustomModelStatsInputs.z_grid=PTypeStructure.(iistr).z_gridvals_J; + heteroagentoptions.CustomModelStatsInputs.pi_z=PTypeStructure.(iistr).pi_z_J; end + PTypeStructure.(iistr).simoptions=rmfield(simoptions,'ExogShockFn'); end - % Regardless of whether they are done here of in _subfn, they will be precomputed by the time we get to the value fn, stationary dist, etc. So + + % Regardless of whether they are done here of in _subfn, they will be pre-computed by the time we get to the value fn, stationary dist, etc. So PTypeStructure.(iistr).vfoptions.alreadygridvals=1; PTypeStructure.(iistr).simoptions.alreadygridvals=1; @@ -586,56 +518,52 @@ end end - if isfinite(PTypeStructure.(iistr).N_j) % FHorz - PTypeStructure.(iistr).vfoptions=SemiExogShockSetup_FHorz(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,2,3); - PTypeStructure.(iistr).simoptions.semiz_gridvals_J=PTypeStructure.(iistr).vfoptions.semiz_gridvals_J; - PTypeStructure.(iistr).simoptions.pi_semiz_J=PTypeStructure.(iistr).vfoptions.pi_semiz_J; - else - error('Semi-exogenous state not yet implemented for InfHorz') - end + PTypeStructure.(iistr).vfoptions=SemiExogShockSetup_FHorz(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,2,3); + PTypeStructure.(iistr).simoptions.semiz_gridvals_J=PTypeStructure.(iistr).vfoptions.semiz_gridvals_J; + PTypeStructure.(iistr).simoptions.pi_semiz_J=PTypeStructure.(iistr).vfoptions.pi_semiz_J; + % Regardless of whether they are done here of in _subfn, they will be precomputed by the time we get to the value fn, stationary dist, etc. So PTypeStructure.(iistr).vfoptions.alreadygridvals_semiexo=1; PTypeStructure.(iistr).simoptions.alreadygridvals_semiexo=1; end + %% DiscountFactor and ReturnFn % The parameter names can be made to depend on the permanent-type if isstruct(DiscountFactorParamNames) - PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(Names_i{ii}); + PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(iistr); else PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames; end PTypeStructure.(iistr).ReturnFn=ReturnFn; if isa(ReturnFn,'struct') - PTypeStructure.(iistr).ReturnFn=ReturnFn.(Names_i{ii}); + PTypeStructure.(iistr).ReturnFn=ReturnFn.(iistr); end PTypeStructure.(iistr).ReturnFnParamNames=ReturnFnParamNamesFn(PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).Parameters); %% jequaloneDist and AgeWeightsParamNames - if isfinite(PTypeStructure.(iistr).N_j) % FHorz - if isstruct(jequaloneDist) - if isfield(jequaloneDist,PTypeStructure.Names_i{ii}) - if isa(jequaloneDist, 'function_handle') - [PTypeStructure.(iistr).jequaloneDist,~,PTypeStructure.(iistr).Parameters]=jequaloneDist_PType(jequaloneDist.(iistr),PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).simoptions,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_i,PTypeStructure.(iistr).PTypeDistParamNames,0); - else - PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(PTypeStructure.Names_i{ii}); - end + if isstruct(jequaloneDist) + if isfield(jequaloneDist,PTypeStructure.(iistr)) + if isa(jequaloneDist, 'function_handle') + [PTypeStructure.(iistr).jequaloneDist,~,PTypeStructure.(iistr).Parameters]=jequaloneDist_PType(jequaloneDist.(iistr),PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).simoptions,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_i,PTypeStructure.(iistr).PTypeDistParamNames,0); else - error(['You must input jequaloneDist for permanent type ', PTypeStructure.Names_i{ii}, ' \n']) + PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(PTypeStructure.(iistr)); end else - PTypeStructure.(iistr).jequaloneDist=jequaloneDist; + error(['You must input jequaloneDist for permanent type ', PTypeStructure.(iistr), ' \n']) end + else + PTypeStructure.(iistr).jequaloneDist=jequaloneDist; + end - PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames; - if isstruct(AgeWeightParamNames) - if isfield(AgeWeightParamNames,Names_i{ii}) - PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(Names_i{ii}); - else - error(['You must input AgeWeightParamNames for permanent type ', Names_i{ii}, ' \n']) - end + PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames; + if isstruct(AgeWeightParamNames) + if isfield(AgeWeightParamNames,iistr) + PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(iistr); + else + error(['You must input AgeWeightParamNames for permanent type ', iistr, ' \n']) end end @@ -725,7 +653,7 @@ % check if it is an _name, in which case need to put it into AggVarNames_mod so that it gets handled correctly if it is used as an input later checkunderscorename=0; for ii=1:N_i - lname=length(Names_i{ii}); + lname=length(iistr); if length(intEqnnames_gg)>lname+1 % only check if intEqnnames_gg is long enough to be possible if strcmp(intEqnnames_gg(end-lname:end),['_',Names_i{ii}]) % E.g., creates Parameters.r.ptype001 from Parameters.r_ptype001 @@ -999,7 +927,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=p_eqm_vec(GEpriceindexes(pp,1):GEpriceindexes(pp,2)); for ii=1:N_i - p_eqm.(GEPriceParamNames{pp}).(PTypeStructure.Names_i{ii})=temp(ii); + p_eqm.(GEPriceParamNames{pp}).(PTypeStructure.(iistr))=temp(ii); end end end @@ -1074,7 +1002,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=GEpricepath(GEpriceindexes(pp,1):GEpriceindexes(pp,2),:); for ii=1:N_i - PriceHistory.(GEPriceParamNames{pp}).(Names_i{ii})=temp(ii,:); + PriceHistory.(GEPriceParamNames{pp}).(iistr)=temp(ii,:); end end end diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m index 89a9b6d3..bfaa2b7d 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m @@ -328,18 +328,18 @@ PTypeStructure.(iistr).n_d=n_d; if isstruct(n_d) - PTypeStructure.(iistr).n_d=n_d.(Names_i{ii}); + PTypeStructure.(iistr).n_d=n_d.(iistr); else PTypeStructure.(iistr).n_d=n_d; end PTypeStructure.(iistr).n_a=n_a; if isstruct(n_a) - PTypeStructure.(iistr).n_a=n_a.(Names_i{ii}); + PTypeStructure.(iistr).n_a=n_a.(iistr); else PTypeStructure.(iistr).n_a=n_a; end if isstruct(n_z) - PTypeStructure.(iistr).n_z=n_z.(Names_i{ii}); + PTypeStructure.(iistr).n_z=n_z.(iistr); else PTypeStructure.(iistr).n_z=n_z; end @@ -363,12 +363,12 @@ end if isstruct(d_grid) - PTypeStructure.(iistr).d_grid=d_grid.(Names_i{ii}); + PTypeStructure.(iistr).d_grid=d_grid.(iistr); else PTypeStructure.(iistr).d_grid=d_grid; end if isstruct(a_grid) - PTypeStructure.(iistr).a_grid=a_grid.(Names_i{ii}); + PTypeStructure.(iistr).a_grid=a_grid.(iistr); else PTypeStructure.(iistr).a_grid=a_grid; end @@ -386,7 +386,7 @@ if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(iistr); end elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. temp=Parameters.(FullParamNames{kField}); @@ -403,12 +403,12 @@ %% Set up exogenous shock grids now (so they can then just be reused every time) if isstruct(z_grid) - PTypeStructure.(iistr).z_grid=z_grid.(Names_i{ii}); + PTypeStructure.(iistr).z_grid=z_grid.(iistr); else PTypeStructure.(iistr).z_grid=z_grid; end if isstruct(pi_z) - PTypeStructure.(iistr).pi_z=pi_z.(Names_i{ii}); + PTypeStructure.(iistr).pi_z=pi_z.(iistr); else PTypeStructure.(iistr).pi_z=pi_z; end @@ -455,13 +455,13 @@ %% DiscountFactor and ReturnFn % The parameter names can be made to depend on the permanent-type if isstruct(DiscountFactorParamNames) - PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(Names_i{ii}); + PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(iistr); else PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames; end if isstruct(ReturnFn) - PTypeStructure.(iistr).ReturnFn=ReturnFn.(Names_i{ii}); + PTypeStructure.(iistr).ReturnFn=ReturnFn.(iistr); else PTypeStructure.(iistr).ReturnFn=ReturnFn; end @@ -491,8 +491,8 @@ PTypeStructure.(iistr).PTypeWeight=PTypeStructure.(iistr).Parameters.(PTypeDistParamNames{1}); % Don't need '.(Names_i{ii}' as this was already done when putting it into PTypeStrucutre, and here I take it straight from PTypeStructure.(iistr).Parameters rather than from Parameters itself. end - % Ptype masses; Copied from FHorz/PType version for consistency - PTypeStructure.ptweights(1,ii)=PTypeStructure.(iistr).Parameters.(PTypeDistParamNames{1}); + % Ptype masses + PTypeStructure.ptweights(ii,1)=PTypeStructure.(iistr).Parameters.(PTypeDistParamNames{1}); end @@ -559,7 +559,7 @@ % check if it is an _name, in which case need to put it into AggVarNames_mod so that it gets handled correctly if it is used as an input later checkunderscorename=0; for ii=1:N_i - lname=length(Names_i{ii}); + lname=length(iistr); if length(intEqnnames_gg)>lname+1 % only check if intEqnnames_gg is long enough to be possible if strcmp(intEqnnames_gg(end-lname:end),['_',Names_i{ii}]) % E.g., creates Parameters.r.ptype001 from Parameters.r_ptype001 @@ -820,7 +820,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=p_eqm_vec(GEpriceindexes(pp,1):GEpriceindexes(pp,2)); for ii=1:N_i - p_eqm.(GEPriceParamNames{pp}).(Names_i{ii})=temp(ii); + p_eqm.(GEPriceParamNames{pp}).(iistr)=temp(ii); end end end @@ -894,7 +894,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=GEpricepath(GEpriceindexes(pp,1):GEpriceindexes(pp,2),:); for ii=1:N_i - PriceHistory.(GEPriceParamNames{pp}).(Names_i{ii})=temp(ii,:); + PriceHistory.(GEPriceParamNames{pp}).(iistr)=temp(ii,:); end end end diff --git a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m index 7cd3323e..73344284 100644 --- a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m +++ b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m @@ -274,7 +274,7 @@ try if isstruct(Parameters.(paramnames{pp})) % parameter depends on ptype as a structure for ii=1:N_i - Parameters.([paramnames{pp},'_',Names_i{ii}])=Parameters.(paramnames{pp}).(Names_i{ii}); + Parameters.([paramnames{pp},'_',Names_i{ii}])=Parameters.(paramnames{pp}).(iistr); end paramthatdependsonptype(pp)=1; paramnamesptype{sum(paramthatdependsonptype)}=paramnames{pp}; @@ -368,7 +368,7 @@ % Horizon is determined via N_j if isstruct(N_j) - PTypeStructure.(iistr).N_j=N_j.(Names_i{ii}); + PTypeStructure.(iistr).N_j=N_j.(iistr); elseif isscalar(N_j) PTypeStructure.(iistr).N_j=N_j; else @@ -376,17 +376,17 @@ end if isstruct(n_d) - PTypeStructure.(iistr).n_d=n_d.(Names_i{ii}); + PTypeStructure.(iistr).n_d=n_d.(iistr); else PTypeStructure.(iistr).n_d=n_d; end if isstruct(n_a) - PTypeStructure.(iistr).n_a=n_a.(Names_i{ii}); + PTypeStructure.(iistr).n_a=n_a.(iistr); else PTypeStructure.(iistr).n_a=n_a; end if isstruct(n_z) - PTypeStructure.(iistr).n_z=n_z.(Names_i{ii}); + PTypeStructure.(iistr).n_z=n_z.(iistr); else PTypeStructure.(iistr).n_z=n_z; end @@ -410,67 +410,16 @@ end if isstruct(d_grid) - PTypeStructure.(iistr).d_grid=d_grid.(Names_i{ii}); + PTypeStructure.(iistr).d_grid=d_grid.(iistr); else PTypeStructure.(iistr).d_grid=d_grid; end if isstruct(a_grid) - PTypeStructure.(iistr).a_grid=a_grid.(Names_i{ii}); + PTypeStructure.(iistr).a_grid=a_grid.(iistr); else PTypeStructure.(iistr).a_grid=a_grid; end - %% Parameter Structure - % Parameters are allowed to be given as structure, or as vector/matrix - % (in terms of their dependence on permanent type). So go through each of - % these in term. - % ie. Parameters.alpha=[0;1]; or Parameters.alpha.ptype1=0; Parameters.alpha.ptype2=1; - PTypeStructure.(iistr).Parameters=Parameters; - FullParamNames=fieldnames(Parameters); % all the different parameters - nFields=length(FullParamNames); - for kField=1:nFields - if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form - % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) - if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); - end - elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. - temp=Parameters.(FullParamNames{kField}); - [~,ptypedim]=max(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i); % Parameters as vector/matrix can be at most two dimensional, figure out which relates to PType, it should be the row dimension, if it is not then give a warning. - if ptypedim==1 - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=temp(ii,:); - elseif ptypedim==2 - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=temp(:,ii); - end - end - end - - %% Set up exogenous shock grids now (so they can then just be reused every time) - - if isstruct(z_grid) - PTypeStructure.(iistr).z_grid=z_grid.(Names_i{ii}); - else - % If the last dimension is of length N_i, this indicates dependence on ptype - nn=size(z_grid,ndims(z_grid)); - if nn==N_i - otherdims = repmat({':'},1,ndims(z_grid)-1); - PTypeStructure.(iistr).z_grid=z_grid(otherdims{:},ii); - else - PTypeStructure.(iistr).z_grid=z_grid; - end - end - if isstruct(pi_z) - PTypeStructure.(iistr).pi_z=pi_z.(Names_i{ii}); - else - % If the last dimension is of length N_i, this indicates dependence on ptype - nn=size(pi_z,ndims(pi_z)); - if nn==N_i - otherdims = repmat({':'},1,ndims(pi_z)-1); - PTypeStructure.(iistr).pi_z=pi_z(otherdims{:},ii); - else - PTypeStructure.(iistr).pi_z=pi_z; - end - end %% Parameter Structure % Parameters are allowed to be given as structure, or as vector/matrix @@ -482,10 +431,10 @@ FullParamNames=fieldnames(Parameters); % all the different parameters nFields=length(FullParamNames); for kField=1:nFields - if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form + if isstruct(Parameters.(FullParamNames{kField})) % Check the current parameter for permanent type in structure form % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(iistr); end elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. temp=Parameters.(FullParamNames{kField}); @@ -585,28 +534,28 @@ %% DiscountFactor and ReturnFn % The parameter names can be made to depend on the permanent-type if isstruct(DiscountFactorParamNames) - PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(Names_i{ii}); + PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(iistr); else PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames; end PTypeStructure.(iistr).ReturnFn=ReturnFn; if isa(ReturnFn,'struct') - PTypeStructure.(iistr).ReturnFn=ReturnFn.(Names_i{ii}); + PTypeStructure.(iistr).ReturnFn=ReturnFn.(iistr); end PTypeStructure.(iistr).ReturnFnParamNames=ReturnFnParamNamesFn(PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).Parameters); %% jequaloneDist and AgeWeightsParamNames if isfinite(PTypeStructure.(iistr).N_j) % FHorz if isstruct(jequaloneDist) - if isfield(jequaloneDist,PTypeStructure.Names_i{ii}) + if isfield(jequaloneDist,PTypeStructure.(iistr)) if isa(jequaloneDist, 'function_handle') [PTypeStructure.(iistr).jequaloneDist,~,PTypeStructure.(iistr).Parameters]=jequaloneDist_PType(jequaloneDist.(iistr),PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).simoptions,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_i,PTypeStructure.(iistr).PTypeDistParamNames,0); else - PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(PTypeStructure.Names_i{ii}); + PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(PTypeStructure.(iistr)); end else - error(['You must input jequaloneDist for permanent type ', PTypeStructure.Names_i{ii}, ' \n']) + error(['You must input jequaloneDist for permanent type ', PTypeStructure.(iistr), ' \n']) end else PTypeStructure.(iistr).jequaloneDist=jequaloneDist; @@ -615,7 +564,7 @@ PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames; if isstruct(AgeWeightParamNames) if isfield(AgeWeightParamNames,Names_i{ii}) - PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(Names_i{ii}); + PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(iistr); else error(['You must input AgeWeightParamNames for permanent type ', Names_i{ii}, ' \n']) end @@ -708,7 +657,7 @@ % check if it is an _name, in which case need to put it into AggVarNames_mod so that it gets handled correctly if it is used as an input later checkunderscorename=0; for ii=1:N_i - lname=length(Names_i{ii}); + lname=length(iistr); if length(intEqnnames_gg)>lname+1 % only check if intEqnnames_gg is long enough to be possible if strcmp(intEqnnames_gg(end-lname:end),['_',Names_i{ii}]) % E.g., creates Parameters.r.ptype001 from Parameters.r_ptype001 @@ -979,7 +928,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=p_eqm_vec(GEpriceindexes(pp,1):GEpriceindexes(pp,2)); for ii=1:N_i - p_eqm.(GEPriceParamNames{pp}).(PTypeStructure.Names_i{ii})=temp(ii); + p_eqm.(GEPriceParamNames{pp}).(PTypeStructure.(iistr))=temp(ii); end end end @@ -1054,7 +1003,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=GEpricepath(GEpriceindexes(pp,1):GEpriceindexes(pp,2),:); for ii=1:N_i - PriceHistory.(GEPriceParamNames{pp}).(Names_i{ii})=temp(ii,:); + PriceHistory.(GEPriceParamNames{pp}).(iistr)=temp(ii,:); end end end diff --git a/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m b/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m index 75e139c8..13481914 100644 --- a/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m +++ b/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m @@ -173,8 +173,8 @@ Policy_aprime(:,:,2)=reshape(Policy(ndvars+1,:,:),[N_a,N_z,1])+n_a1*Policy_a2prime(:,:,1); % Note: upper grid point minus 1 is anyway just lower grid point end PolicyaprimezPath=reshape(Policy_aprime+N_a*(0:1:N_z-1),[N_a*N_z,2]); - % PolicyProbs_2D=reshape(PolicyProbs,[N_a*N_z,2]); % sparse matrices are limited to 2D; match PolicyaprimezPath shape - AgentDist=AgentDist_InfHorz_TPath_SingleStep_nProbs_raw(AgentDist,PolicyaprimezPath,II2,PolicyProbs,N_a,N_z,pi_z_sparse); + PolicyProbs_2D=reshape(PolicyProbs,[N_a*N_z,2]); % sparse matrices are limited to 2D; match PolicyaprimezPath shape + AgentDist=AgentDist_InfHorz_TPath_SingleStep_nProbs_raw(AgentDist,PolicyaprimezPath,II2,PolicyProbs_2D,N_a,N_z,pi_z_sparse); AgentDistPath(:,tt+1)=gpuArray(full(AgentDist)); end diff --git a/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m index 17acd046..10394ef3 100644 --- a/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m +++ b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m @@ -182,7 +182,7 @@ if isfinite(N_j_temp) AgentDistPath_ii=AgentDistOnTransPath_Case1_FHorz(AgentDist_initial_temp, jequalOneDist_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, AgeWeightsParamNames_temp,n_d_temp,n_a_temp,n_z_temp,N_j_temp,pi_z_temp, T,Parameters_temp, transpathoptions_temp, simoptions_temp); else - AgentDistPath_ii=AgentDistOnTransPath_InfHorz(AgentDist_initial_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, n_d_temp,n_a_temp,n_z_temp,pi_z_temp,T,simoptions_temp, Parameters_temp); + AgentDistPath_ii=AgentDistOnTransPath_InfHorz(AgentDist_initial_temp, PricePath_temp, ParamPath_temp, PolicyPath_temp, n_d_temp,n_a_temp,n_z_temp,pi_z_temp,T,Parameters_temp, simoptions_temp); end % Note: T cannot depend on ptype, nor can PricePath depend on ptype From 604f3675f5b9a62de90af2e2736cd9ab2573c74a Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sat, 16 May 2026 07:09:08 +1200 Subject: [PATCH 77/90] More merge cleanups and fixups --- .../subcodes/EvalFnOnAgentDist_Grid.m | 3 + ...onaryEqm_Case1_FHorz_PType_GEptype_subfn.m | 1 - .../HeteroAgentStationaryEqm_InfHorz_PType.m | 1 - ...roAgentStationaryEqm_InfHorz_PType_subfn.m | 1 - ...onPath_InfHorz_PType_ExpAsset_singlepath.m | 177 ------------------ .../TransitionPath_InfHorz_PType_shooting.m | 1 + .../TransitionPath_InfHorz_PType_singlepath.m | 168 ----------------- .../MixHorz/TransitionPath_MixHorz_PType.m | 2 +- .../TransitionPath_MixHorz_PType_shooting.m | 6 +- 9 files changed, 7 insertions(+), 353 deletions(-) delete mode 100644 TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m delete mode 100644 TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_singlepath.m diff --git a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m index 48367f86..f6103336 100644 --- a/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m +++ b/EvaluateFnOnAgentDist/subcodes/EvalFnOnAgentDist_Grid.m @@ -26,6 +26,9 @@ daprime5vals=PolicyValuesPermute(:,5); if l_daprime>=6 daprime6vals=PolicyValuesPermute(:,6); + if l_daprime>=7 + daprime7vals=PolicyValuesPermute(:,7); + end end end end diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m index 3e647180..90b0daea 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_GEptype_subfn.m @@ -185,7 +185,6 @@ end - %% Evaluate the General Eqm Eqns % use of real() is a hack that could disguise errors, but I couldn't find why matlab was treating output as complex GEeqnnames=fieldnames(GeneralEqmEqns); diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m index bfaa2b7d..fba8d042 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m @@ -490,7 +490,6 @@ else PTypeStructure.(iistr).PTypeWeight=PTypeStructure.(iistr).Parameters.(PTypeDistParamNames{1}); % Don't need '.(Names_i{ii}' as this was already done when putting it into PTypeStrucutre, and here I take it straight from PTypeStructure.(iistr).Parameters rather than from Parameters itself. end - % Ptype masses PTypeStructure.ptweights(ii,1)=PTypeStructure.(iistr).Parameters.(PTypeDistParamNames{1}); end diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m index 0f7c767d..9efcd15f 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m @@ -115,7 +115,6 @@ end end - %% Evaluate General Eqm Eqns % use of real() is a hack that could disguise errors, but I couldn't find why matlab was treating output as complex GeneralEqmConditionsVec=zeros(1,length(GEeqnNames)); diff --git a/TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m b/TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m deleted file mode 100644 index bf94c1f9..00000000 --- a/TransitionPaths/InfHorz/PType/ExpAsset/TransitionPath_InfHorz_PType_ExpAsset_singlepath.m +++ /dev/null @@ -1,177 +0,0 @@ -function AggVarsPath=TransitionPath_InfHorz_PType_ExpAsset_singlepath(PricePathOld, ParamPath, PricePathNames, ParamPathNames, T, V_final, AgentDist_initial, ... - l_d,n_d1,n_d2,n_a1,n_a2,N_z,n_z,d2_grid,a1_gridvals,a2_grid,d_gridvals,a_gridvals,z_grid,pi_z, ... - ReturnFn, aprimeFn, FnsToEvaluate, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, FnsToEvaluateParamNames, AggVarNames, ... - PricePathSizeVec, ParamPathSizeVec, use_tminus1price, use_tminus1params, use_tplus1price, use_tminus1AggVars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, ... - transpathoptions, vfoptions, simoptions) -% PricePathOld is matrix of size T-by-'number of prices' -% ParamPath is matrix of size T-by-'number of parameters that change over path' - -% Remark to self: No real need for T as input, as this is anyway the length of PricePathOld - -% For this agent type, first go back through the value & policy fns. -% Then forwards through agent dist and agg vars. - -N_d1=prod(n_d1); -N_a1=prod(n_a1); -N_a2=prod(n_a2); -N_a=N_a1*N_a2; - -l_aprime=length(n_a1); -l_a=l_aprime+length(n_a2); - -PolicyIndexesPath=zeros(l_d+l_aprime,N_a,N_z,T-1,'gpuArray'); %Periods 1 to T-1 - -% This and much of the rest of this code borrowed from TransitionPath_InfHorz_shooting.m -if simoptions.gridinterplayer==0 - II1=(1:1:N_a*N_z); % Index for this period (a,z) - IIones=ones(N_a*N_z,1); % Next period 'probabilities' -elseif simoptions.gridinterplayer==1 - PolicyProbsPath=zeros(N_a*N_z,2,T-1,'gpuArray'); % preallocate - II2=([1:1:N_a*N_z; 1:1:N_a*N_z]'); % Index for this period (a,z), note the 2 copies -end - -if size(V_final)~=[N_a,N_z] - error("V_final wrong shape") - V_final=reshape(V_final,[N_a,N_z]); -end - -%First, go from T-1 to 1 calculating the Value function and Optimal -%policy function at each step. Since we won't need to keep the value -%functions for anything later we just store the next period one in -%Vnext, and the current period one to be calculated in V -Vnext=V_final; -for ttr=1:T-1 %so t=T-i - % The following digs deeper into PricePathOld and ParamPath in - % FHorz case--check it - for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(T-ttr,kk); - end - for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(T-ttr,kk); - end - [V, Policy]=ValueFnIter_InfHorz_TPath_SingleStep_ExpAsset(Vnext,n_d1,n_d2,n_a1,n_a2,n_z,d_gridvals, d2_grid, a1_gridvals, a2_grid, z_grid, pi_z, ReturnFn, aprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, aprimeFnParamNames, vfoptions); - % The VKron input is next period value fn, the VKron output is this period. - % Policy is kept in the form where it is just a single-value in (d,a') - - PolicyIndexesPath(:,:,:,T-ttr)=Policy; - Vnext=V; - -end -% Free up space on GPU by deleting things no longer needed -clear V Vnext - -%% Modify PolicyIndexesPath into forms needed for forward iteration -% Create version of PolicyIndexesPath in form we want for the agent distribution iteration -% Creates PolicyaprimezPath, and when using grid interpolation layer also PolicyProbsPath -if isscalar(n_a1) - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:),[N_a*N_z,T-1]); % aprime index -elseif length(n_a1)==2 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1),[N_a*N_z,T-1]); -elseif length(n_a1)==3 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,:)-1),[N_a*N_z,T-1]); -elseif length(n_a1)==41 - PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:)-1)+prod(n_a1(1:2))*(PolicyIndexesPath(l_d+3,:,:,:)-1)+prod(n_a1(1:3))*(PolicyIndexesPath(l_d+4,:,:,:)-1),[N_a*N_z,T-1]); -end -PolicyaprimezPath=PolicyaprimePath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1); -if simoptions.gridinterplayer==1 - PolicyaprimezPath=reshape(PolicyaprimezPath,[N_a*N_z,1,T-1]); % reinterpret this as lower grid index - PolicyaprimezPath=repelem(PolicyaprimezPath,1,2,1); % create copy that will be the upper grid index - PolicyaprimezPath(:,2,:)=PolicyaprimezPath(:,2,:)+1; % upper grid index - PolicyProbsPath(:,2,:)=reshape(PolicyIndexesPath(l_d+l_aprime+1,:,:),[N_a*N_z,1,T-1]); % L2 index - PolicyProbsPath(:,2,:)=(PolicyProbsPath(:,2,:)-1)/(1+simoptions.ngridinterp); % probability of upper grid point - PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point -end -% Create PolicyValuesPath from PolicyIndexesPath for use in calculating model stats -if N_d1==0 - PolicyValuesPath=PolicyInd2Val_InfHorz_TPath(PolicyIndexesPath,n_d2,[n_a1,n_a2],n_z,T-1,d_gridvals,a_gridvals,vfoptions,1); -else - PolicyValuesPath=PolicyInd2Val_InfHorz_TPath(PolicyIndexesPath,[n_d1,n_d2],[n_a1,n_a2],n_z,T-1,d_gridvals,a_gridvals,vfoptions,1); -end -PolicyValuesPath=permute(reshape(PolicyValuesPath,[size(PolicyValuesPath,1),N_a,N_z,T-1]),[2,3,1,4]); %[N_a,N_z,l_d+l_a,T-1] - -%Now we have the full PolicyIndexesPath, we go forward in time from 1 -%to T using the policies to update the agents distribution generating a -%new price path -%Call AgentDist the current periods distn - -AgentDist=sparse(gather(reshape(AgentDist_initial,[N_a*N_z,1]))); -AggVarsPath=zeros(length(FnsToEvaluate),T-1); -pi_z_sparse=sparse(gather(pi_z)); % Need full pi_z for value fn, and sparse for agent dist - -for tt=1:T-1 - %% Setup the Parameters for period tt - - % Get t-1 PricePath and ParamPath before we update them - if use_tminus1price==1 - for pp=1:length(tminus1priceNames) - if tt>1 - Parameters.([tminus1priceNames{pp},'_tminus1'])=Parameters.(tminus1priceNames{pp}); - else - Parameters.([tminus1priceNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1priceNames{pp}); - end - end - end - if use_tminus1params==1 - for pp=1:length(tminus1paramNames) - if tt>1 - Parameters.([tminus1paramNames{pp},'_tminus1'])=Parameters.(tminus1paramNames{pp}); - else - Parameters.([tminus1paramNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1paramNames{pp}); - end - end - end - % Get t-1 AggVars before we update them - if use_tminus1AggVars==1 - for pp=1:length(tminus1AggVarsNames) - if tt>1 - % The AggVars have not yet been updated, so they still contain previous period values - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); - else - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); - end - end - end - - % Update current PricePath and ParamPath - for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(tt,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); - end - for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); - end - - % Get t+1 PricePath - if use_tplus1price==1 - for pp=1:length(tplus1priceNames) - kk=tplus1pricePathkk(pp); - Parameters.([tplus1priceNames{pp},'_tplus1'])=PricePathOld(tt+1,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); % Make is so that the time t+1 variables can be used - end - end - - %% Get the current optimal policy, and iterate the agent dist - if simoptions.gridinterplayer==0 - AgentDistnext=AgentDist_InfHorz_TPath_SingleStep(AgentDist,PolicyaprimezPath(:,tt),II1,IIones,N_a,N_z,pi_z_sparse); - elseif simoptions.gridinterplayer==1 - AgentDistnext=AgentDist_InfHorz_TPath_SingleStep_nProbs(AgentDist,PolicyaprimezPath(:,:,tt),II2,PolicyProbsPath(:,:,tt),N_a,N_z,pi_z_sparse); - end - - %% AggVars - AggVars_Means=EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars(gpuArray(full(AgentDist)), PolicyValuesPath(:,:,:,tt), FnsToEvaluate, Parameters, FnsToEvaluateParamNames, AggVarNames, [n_a1,n_a2], n_z, a_gridvals, z_grid,1); - AggVars=zeros(length(AggVars_Means),1); - if length(fieldnames(AggVars_Means))~=length(AggVarNames) - error(["AggVar length disparity:";"---------";AggVarNames;"--- vs ---";fieldnames(AggVars_Means)]); - end - for ii=1:length(AggVarNames) - AggVars(ii)=AggVars_Means.(AggVarNames{ii}).Mean; - Parameters.(AggVarNames{ii})=AggVars(ii); - end - - % Do nothing with IntermediateEqns and GeneralEqmEqns as they are outside PType scope (they are where all the PTypes meet). - - AgentDist=AgentDistnext; - - AggVarsPath(:,tt)=AggVars; -end - - -end diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m index 1ca896a5..24265520 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m @@ -114,6 +114,7 @@ PricePathNew=zeros(size(PricePathOld),'gpuArray'); PricePathNew(T,:)=PricePathOld(T,:); + %% Iterate on the transition path while PricePathDist>transpathoptions.tolerance && pathcounter1 - Parameters.([tminus1priceNames{pp},'_tminus1'])=Parameters.(tminus1priceNames{pp}); - else - Parameters.([tminus1priceNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1priceNames{pp}); - end - end - end - if use_tminus1params==1 - for pp=1:length(tminus1paramNames) - if tt>1 - Parameters.([tminus1paramNames{pp},'_tminus1'])=Parameters.(tminus1paramNames{pp}); - else - Parameters.([tminus1paramNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1paramNames{pp}); - end - end - end - % Get t-1 AggVars before we update them - if use_tminus1AggVars==1 - for pp=1:length(tminus1AggVarsNames) - if tt>1 - % The AggVars have not yet been updated, so they still contain previous period values - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=Parameters.(tminus1AggVarsNames{pp}); - else - Parameters.([tminus1AggVarsNames{pp},'_tminus1'])=transpathoptions.initialvalues.(tminus1AggVarsNames{pp}); - end - end - end - - % Update current PricePath and ParamPath - for kk=1:length(PricePathNames) - Parameters.(PricePathNames{kk})=PricePathOld(tt,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); - end - for kk=1:length(ParamPathNames) - Parameters.(ParamPathNames{kk})=ParamPath(tt,ParamPathSizeVec(1,kk):ParamPathSizeVec(2,kk)); - end - - % Get t+1 PricePath - if use_tplus1price==1 - for pp=1:length(tplus1priceNames) - kk=tplus1pricePathkk(pp); - Parameters.([tplus1priceNames{pp},'_tplus1'])=PricePathOld(tt+1,PricePathSizeVec(1,kk):PricePathSizeVec(2,kk)); % Make is so that the time t+1 variables can be used - end - end - - %% Get the current optimal policy, and iterate the agent dist - if simoptions.gridinterplayer==0 - AgentDistnext=AgentDist_InfHorz_TPath_SingleStep(AgentDist,PolicyaprimezPath(:,tt),II1,IIones,N_a,N_z,pi_z_sparse); - elseif simoptions.gridinterplayer==1 - AgentDistnext=AgentDist_InfHorz_TPath_SingleStep_nProbs(AgentDist,PolicyaprimezPath(:,:,tt),II2,PolicyProbsPath(:,:,tt),N_a,N_z,pi_z_sparse); - end - - %% AggVars - AggVars_Means=EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars(gpuArray(full(AgentDist)), PolicyValuesPath(:,:,:,tt), FnsToEvaluate, Parameters, FnsToEvaluateParamNames, AggVarNames, n_a, n_z, a_gridvals, z_grid,1); - AggVars=zeros(length(AggVars_Means),1); - if length(fieldnames(AggVars_Means))~=length(AggVarNames) - error(["AggVar length disparity:";"---------";AggVarNames;"--- vs ---";fieldnames(AggVars_Means)]); - end - for ii=1:length(AggVarNames) - AggVars(ii)=AggVars_Means.(AggVarNames{ii}).Mean; - Parameters.(AggVarNames{ii})=AggVars(ii); - end - - % Do nothing with IntermediateEqns and GeneralEqmEqns as they are outside PType scope (they are where all the PTypes meet). - - AgentDist=AgentDistnext; - - AggVarsPath(:,tt)=AggVars; -end - - -end diff --git a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m index fdf64e44..7aa707e3 100644 --- a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m @@ -152,7 +152,7 @@ end end -%% Get AgeWeights from Parameters +%% Get AgeWeights from Parameters if isstruct(AgeWeightsParamNames) for ii=1:N_i try diff --git a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m index e9709ae3..2d77f246 100644 --- a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m +++ b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m @@ -153,8 +153,7 @@ [PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,PolicyValuesPath_ii]=TransitionPath_FHorz_substeps_Step2_AdjustPolicy(PolicyIndexesPath_ii,T,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).d_gridvals,PTypeStructure.(iistr).aprime_gridvals,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); else %% Go from T-1 to 1 calculating the Value function and Optimal policy function at each step. - warning("TransitionPath_MixHorz_PType_shooting not yet hanlding e_gridvals and/or pi_e") - [~,PolicyIndexesPath_ii]=TransitionPath_InfHorz_substeps_Step1_ValueFnIter(T,PolicyIndexesPath_ii,V_final.(iistr),PTypeStructure.(iistr).Parameters,PricePathOld_ii,ParamPath_ii,PricePathSizeVec_ii,ParamPathSizeVec_ii,PricePathNames_ii,ParamPathNames_ii,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).d_gridvals, PTypeStructure.(iistr).a_grid, PTypeStructure.(iistr).z_grid,[],PTypeStructure.(iistr).pi_z,[],PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).DiscountFactorParamNames, PTypeStructure.(iistr).ReturnFnParamNames, transpathoptions,PTypeStructure.(iistr).vfoptions); + [~,PolicyIndexesPath_ii]=TransitionPath_InfHorz_substeps_Step1_ValueFnIter(T,PolicyIndexesPath_ii,V_final.(iistr),PTypeStructure.(iistr).Parameters,PricePathOld_ii,ParamPath_ii,PricePathSizeVec_ii,ParamPathSizeVec_ii,PricePathNames_ii,ParamPathNames_ii,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).d_gridvals, PTypeStructure.(iistr).a_grid,PTypeStructure.(iistr).z_gridvals,PTypeStructure.(iistr).e_gridvals,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).pi_e,PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).DiscountFactorParamNames, PTypeStructure.(iistr).ReturnFnParamNames, transpathoptions,PTypeStructure.(iistr).vfoptions); %% Modify PolicyIndexesPath into forms needed for forward iteration [PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,PolicyValuesPath_ii]=TransitionPath_InfHorz_substeps_Step2_AdjustPolicy(PolicyIndexesPath_ii,T,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).n_e,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).d_gridvals,PTypeStructure.(iistr).aprime_gridvals,transpathoptions,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).simoptions); @@ -254,9 +253,8 @@ end AggVars_ii=TransitionPath_FHorz_substeps_Step4tt_AggVars(AgentDist_ii,AgeWeights_ii,PVP_ii_t,tt,PTypeStructure.(iistr).FnsToEvaluateCell,PTypeStructure.(iistr).FnsToEvaluateParamNames,AggVarNames_ii,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).l_d,PTypeStructure.(iistr).l_aprime,PTypeStructure.(iistr).l_a,PTypeStructure.(iistr).l_z,PTypeStructure.(iistr).l_e,PTypeStructure.(iistr).N_d,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).a_gridvals,PTypeStructure.(iistr).ze_gridvals_J_fastOLG,transpathoptions); else - warning("TransitionPath_MixHorz_PType_shooting not hanlding pi_e yet") AgentDist_ii_InfHorz=reshape(AgentDist_ii,[PTypeStructure.(iistr).N_a*PTypeStructure.(iistr).N_z,1]); - AgentDistnext_ii=TransitionPath_InfHorz_substeps_Step3tt_IterAgentDist(AgentDist_ii_InfHorz,PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,tt,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).pi_z,[],PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,transpathoptions,PTypeStructure.(iistr).simoptions); + AgentDistnext_ii=TransitionPath_InfHorz_substeps_Step3tt_IterAgentDist(AgentDist_ii_InfHorz,PolicyPath_ForAgentDistIter_ii,PolicyProbsPath_ii,tt,PTypeStructure.(iistr).N_a,PTypeStructure.(iistr).N_z,PTypeStructure.(iistr).N_e,PTypeStructure.(iistr).N_probs,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).pi_e,PTypeStructure.(iistr).II1,PTypeStructure.(iistr).II2,transpathoptions,PTypeStructure.(iistr).simoptions); if PTypeStructure.(iistr).N_z==0 && PTypeStructure.(iistr).N_e==0 PVP_ii_t=PolicyValuesPath_ii(:,:,tt); else From 953f15da06e9309217210f146dbfde4f361d9f60 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sat, 16 May 2026 07:15:44 +1200 Subject: [PATCH 78/90] Copy in verbose logic from FHorz case --- ...roAgentStationaryEqm_InfHorz_PType_subfn.m | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m index 9efcd15f..6d2e624d 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m @@ -3,6 +3,31 @@ heteroagentparamsvecindex=0:1:length(GEpricesvec); [GEpricesvec,penalty]=ParameterConstraints_TransformParamsToOriginal(GEpricesvec,heteroagentparamsvecindex,GEPriceParamNames,heteroagentoptions); +if heteroagentoptions.verbose>0 + GEpricesvec_tminus1=zeros(nGEprices,1); + AggVars_tminus1=NaN(length(AggVarNames),1); + + for pp=1:nGEprices + GEpricesvec_tminus1(pp)=Parameters.(GEPriceParamNames{pp}); + end + GEpricesvec_delta=GEpricesvec-GEpricesvec_tminus1; + for aa=1:length(AggVarNames) + if isfield(Parameters,AggVarNames{aa}) + AggVars_tminus1(aa)=Parameters.(AggVarNames{aa}); + end + end + if heteroagentoptions.useintermediateEqns==1 + intEqnnames=fieldnames(heteroagentoptions.intermediateEqns); + intermediateEqns_tminus1=zeros(length(intEqnnames),1); + for aa=1:length(intEqnnames) + if isfield(Parameters,intEqnnames{aa}) + intEqns_tminus1(aa)=Parameters.(intEqnnames{aa}); + end + end + end + % We don't do anything special for CustomModelStats, which are not as easily done as others above. +end + if heteroagentoptions.verbose==2 fprintf(' \n') fprintf('Current GE prices: \n') From ef8dd99a6fa1775ec6a4a24f3b5f021da219462e Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Sat, 16 May 2026 07:39:07 +1200 Subject: [PATCH 79/90] More merge fixes and simplifications --- ...ath_FHorz_substeps_Step3tt_IterAgentDist.m | 4 --- ...ionPath_InfHorz_substeps_Step4tt_AggVars.m | 1 + .../PricePathParamPath_FHorz_StructToMatrix.m | 29 +++++++++++++++++-- .../ValueFnIter_InfHorz_EpsteinZin.m | 2 -- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step3tt_IterAgentDist.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step3tt_IterAgentDist.m index be387211..d99319d4 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step3tt_IterAgentDist.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step3tt_IterAgentDist.m @@ -15,10 +15,6 @@ end end -if isempty(N_probs) - N_probs=0; -end - if N_z==0 && N_e==0 if simoptions.fastOLG==0 % PolicyaprimePath_slowOLG diff --git a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step4tt_AggVars.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step4tt_AggVars.m index 3581dbdd..f745d2e2 100644 --- a/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step4tt_AggVars.m +++ b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step4tt_AggVars.m @@ -28,4 +28,5 @@ + end diff --git a/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m b/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m index bf6a88d5..5bcefaa9 100644 --- a/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m +++ b/TransitionPaths/Subcodes/PricePathParamPath_FHorz_StructToMatrix.m @@ -71,10 +71,35 @@ % Note: Internally PricePathOld is matrix of size T-by-'number of prices'. % ParamPath is matrix of size T-by-'number of parameters that change over the transition path'. % Actually, some of those prices are 1-by-N_j or N_i or both, so is more subtle than this. - Names_i=fieldnames(N_j); PricePathNames=fieldnames(PricePathStruct); PricePathSizeVec=zeros(1,length(PricePathNames)); % Allows for a given price param to depend on age (or permanent type) - for pp=1:length(PricePathNames) + if isstruct(N_j) + Names_i=fieldnames(N_j); + for pp=1:length(PricePathNames) + if isstruct(PricePathStruct.(PricePathNames{pp})) + tempptypenames=fieldnames(PricePathStruct.(PricePathNames{pp})); + temp=PricePathStruct.(PricePathNames{pp}).(tempptypenames{1}); + tempsize=size(temp); + PricePathSizeVec(pp)=length(tempptypenames)*tempsize(tempsize~=T); % Get the dimension which is not T + for ii=1:N_i + N_j_temp=N_j.(Names_i{ii}); + if ~any(PricePathSizeVec(pp)==[1,N_i,N_j_temp,N_i*N_j_temp]) + error(['PricePath for ', PricePathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) + end + end + else + temp=PricePathStruct.(PricePathNames{pp}); + tempsize=size(temp); + PricePathSizeVec(pp)=tempsize(tempsize~=T); % Get the dimension which is not T + for ii=1:N_i + N_j_temp=N_j.(Names_i{ii}); + if ~any(PricePathSizeVec(pp)==[1,N_i,N_j_temp]) + error(['PricePath for ', PricePathNames{pp}, ' appears to be the wrong size (should be 1-by-T or N_j-by-T or N_i-by-T)']) + end + end + end + end + else for pp=1:length(PricePathNames) if isstruct(PricePathStruct.(PricePathNames{pp})) tempptypenames=fieldnames(PricePathStruct.(PricePathNames{pp})); diff --git a/ValueFnIter/InfHorz/ExoticPrefs/ValueFnIter_InfHorz_EpsteinZin.m b/ValueFnIter/InfHorz/ExoticPrefs/ValueFnIter_InfHorz_EpsteinZin.m index 8be1197b..26b27ea9 100644 --- a/ValueFnIter/InfHorz/ExoticPrefs/ValueFnIter_InfHorz_EpsteinZin.m +++ b/ValueFnIter/InfHorz/ExoticPrefs/ValueFnIter_InfHorz_EpsteinZin.m @@ -124,6 +124,4 @@ V=reshape(VKron,[n_a,n_z]); Policy=UnKronPolicyIndexes_Case1(Policy, n_d, n_a, n_z,vfoptions); - end - From d9ae43d8110e124eaeb2cf8e35e9d970de57f3b9 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 19 May 2026 21:17:02 +1200 Subject: [PATCH 80/90] Resync PolicyInd2Val_FHorz_TPath.m with master. The fixes I needed are now in master. --- PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m | 6 ------ 1 file changed, 6 deletions(-) diff --git a/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m b/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m index 1443bc08..cb373805 100644 --- a/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m +++ b/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m @@ -28,9 +28,6 @@ l_aprime=length(n_a); if vfoptions.experienceasset==1 l_aprime=l_aprime-1; - if n_d(1)==0 - n_d=n_d(2:end); - end end PolicyPath=reshape(PolicyPath,[size(PolicyPath,1),N_a,N_z,N_j,T]); @@ -92,9 +89,6 @@ % If using vfoptions.gridinterplayer=1, it should be aprime_gridvals for the fine grid, not the rough grid l_aprime=size(a_grid,2);% it is aprime_gridvals, not actually a_grid - if vfoptions.experienceasset==1 && n_d(1)==0 - n_d=n_d(2:end); - end if n_d(1)==0 PolicyValuesPath=zeros(l_aprime,N_a*N_j*N_z*T,'gpuArray'); % N_a*N_j*N_z*T is because of how indexing gridvals will work From 09c8ce59f13a13839a3b627da755281d17cab322 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 19 May 2026 21:18:49 +1200 Subject: [PATCH 81/90] Resync EvalFnOnTransPath_AggVars_InfHorz.m with master --- .../TransPath/EvalFnOnTransPath_AggVars_InfHorz.m | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m b/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m index fe5bf9ee..79990871 100644 --- a/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m +++ b/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m @@ -1,17 +1,6 @@ function AggVarsPath=EvalFnOnTransPath_AggVars_InfHorz(FnsToEvaluate,AgentDistPath,PolicyPath,PricePath,ParamPath, Parameters, T, n_d, n_a, n_z, d_grid, a_grid,z_grid,simoptions) % AggVarsPath is T periods long (periods 0 (before the reforms are announced) & T are the initial and final values). -%% Check which transpathoptions have been used, set all others to defaults -if exist('transpathoptions','var')==0 - disp('No transpathoptions given, using defaults') - %If transpathoptions is not given, just use all the defaults - transpathoptions.verbose=0; -else - %Check transpathoptions for missing fields, if there are some fill them with the defaults - if ~isfield(transpathoptions,'verbose') - transpathoptions.verbose=0; - end -end if ~exist('simoptions','var') % If simoptions is not given, just use all the defaults simoptions.experienceasset=0; From e1f8f00c818bfa52ac1488f5de6088fd6d73811e Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Tue, 19 May 2026 21:20:51 +1200 Subject: [PATCH 82/90] Resync with master If there's a problem (such as `temp` being sparse), it's the caller's fault. --- .../EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m | 3 --- 1 file changed, 3 deletions(-) diff --git a/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m b/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m index 2e2edaa1..f26ebabb 100644 --- a/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m +++ b/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m @@ -20,9 +20,6 @@ Values=reshape(Values,[N_a*N_z,1]); % When evaluating value function (which may sometimes give -Inf values) on StationaryDistVec (which at those points will be 0) we get 'NaN'. Use temp as intermediate variable just eliminate those. temp=Values.*AgentDist; - if issparse(temp) - temp=gather(temp); - end val=sum(temp(~isnan(temp))); if outputastruct==0 From abfccc7527308e00eb5a26a02ee9202b83a91d46 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 20 May 2026 20:34:06 +1200 Subject: [PATCH 83/90] Integrate PR 94 --- StationaryDist/InfHorz/StationaryDist_InfHorz.m | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/StationaryDist/InfHorz/StationaryDist_InfHorz.m b/StationaryDist/InfHorz/StationaryDist_InfHorz.m index 78dbe489..5bdcb044 100644 --- a/StationaryDist/InfHorz/StationaryDist_InfHorz.m +++ b/StationaryDist/InfHorz/StationaryDist_InfHorz.m @@ -121,6 +121,22 @@ end end +%% +if simoptions.parallel==2 + % If using GPU make sure all the relevant inputs are GPU arrays (not standard arrays) + % Some things require simoptions.d_grid or simoptions.a_grid, make sure + % they are on GPU if they are used + if isfield(simoptions,'d_grid') + simoptions.d_grid=gpuArray(simoptions.d_grid); + end + if isfield(simoptions,'a_grid') + simoptions.a_grid=gpuArray(simoptions.a_grid); + end +else + error("no CPU support for computing InfHorz Stationary Distribution") + return +end + %% Setup for Exogenous Shocks if simoptions.alreadygridvals==0 if isfield(simoptions,'ExogShockFn') From b0c476becdbaa682a1c60acd498fc5d80170bbba Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 20 May 2026 20:34:24 +1200 Subject: [PATCH 84/90] Add missing lowmemory==3 --- .../ValueFnIter_FHorz_ExpAsset_raw.m | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m index ef066d5d..13b3c272 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m @@ -43,6 +43,19 @@ V(:,z_c,N_j)=Vtemp; Policy(:,z_c,N_j)=maxindex; end + % there is no `e` to iterate + elseif vfoptions.lowmemory==3 + for ea_c=1:N_a2 + ea_val=a2_gridvals(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(z_c,:,N_j); + ReturnMatrix_ea_z=CreateReturnFnMatrix_ExpAsset_Disc(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); % Level=0, Refine=0 + % Calc the max and its index + [Vtemp,maxindex]=max(ReturnMatrix_ea_z,[],1); + V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=shiftdim(Vtemp,1); + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=shiftdim(maxindex,1); + end + end end else DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,N_j); From 52dc6270e15d4ddc11a6b598ed3254e65bf603c4 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 20 May 2026 21:08:03 +1200 Subject: [PATCH 85/90] Update from HeteroAgentStationaryEqm_merge --- ...teroAgentStationaryEqm_Case1_FHorz_PType.m | 83 ++++++++++++------- .../HeteroAgentStationaryEqm_InfHorz_PType.m | 48 +++++++---- .../HeteroAgentStationaryEqm_MixHorz_PType.m | 81 +++++++++++++----- 3 files changed, 143 insertions(+), 69 deletions(-) diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m index 75f045b7..02e34481 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m @@ -167,7 +167,6 @@ heteroagentoptions.useCustomModelStats=1; % Stash some of the inputs so they can be passed to CustomModelStats later (only things we otherwise override). % So that user gets exactly what they input, not any internally reworked things - % In this (PType) context, these assignments are typically structs that each have all the PType fields within them heteroagentoptions.CustomModelStatsInputs.FnsToEvaluate=FnsToEvaluate; heteroagentoptions.CustomModelStatsInputs.n_d=n_d; heteroagentoptions.CustomModelStatsInputs.n_a=n_a; @@ -274,7 +273,7 @@ try if isstruct(Parameters.(paramnames{pp})) % parameter depends on ptype as a structure for ii=1:N_i - Parameters.([paramnames{pp},'_',Names_i{ii}])=Parameters.(paramnames{pp}).(iistr); + Parameters.([paramnames{pp},'_',Names_i{ii}])=Parameters.(paramnames{pp}).(Names_i{ii}); end paramthatdependsonptype(pp)=1; paramnamesptype{sum(paramthatdependsonptype)}=paramnames{pp}; @@ -368,7 +367,7 @@ % Horizon is determined via N_j if isstruct(N_j) - PTypeStructure.(iistr).N_j=N_j.(iistr); + PTypeStructure.(iistr).N_j=N_j.(Names_i{ii}); elseif isscalar(N_j) PTypeStructure.(iistr).N_j=N_j; else @@ -376,17 +375,17 @@ end if isstruct(n_d) - PTypeStructure.(iistr).n_d=n_d.(iistr); + PTypeStructure.(iistr).n_d=n_d.(Names_i{ii}); else PTypeStructure.(iistr).n_d=n_d; end if isstruct(n_a) - PTypeStructure.(iistr).n_a=n_a.(iistr); + PTypeStructure.(iistr).n_a=n_a.(Names_i{ii}); else PTypeStructure.(iistr).n_a=n_a; end if isstruct(n_z) - PTypeStructure.(iistr).n_z=n_z.(iistr); + PTypeStructure.(iistr).n_z=n_z.(Names_i{ii}); else PTypeStructure.(iistr).n_z=n_z; end @@ -410,31 +409,29 @@ end if isstruct(d_grid) - PTypeStructure.(iistr).d_grid=d_grid.(iistr); + PTypeStructure.(iistr).d_grid=d_grid.(Names_i{ii}); else PTypeStructure.(iistr).d_grid=d_grid; end if isstruct(a_grid) - PTypeStructure.(iistr).a_grid=a_grid.(iistr); + PTypeStructure.(iistr).a_grid=a_grid.(Names_i{ii}); else PTypeStructure.(iistr).a_grid=a_grid; end - %% Parameter Structure % Parameters are allowed to be given as structure, or as vector/matrix % (in terms of their dependence on permanent type). So go through each of - % these in term. + % these in turn. % ie. Parameters.alpha=[0;1]; or Parameters.alpha.ptype1=0; Parameters.alpha.ptype2=1; - % Need to establish this in PTypeStructure before we use it below. PTypeStructure.(iistr).Parameters=Parameters; FullParamNames=fieldnames(Parameters); % all the different parameters nFields=length(FullParamNames); for kField=1:nFields - if isstruct(Parameters.(FullParamNames{kField})) % Check the current parameter for permanent type in structure form + if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) - if isfield(Parameters.(FullParamNames{kField}),iistr) - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(iistr); + if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); end elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. temp=Parameters.(FullParamNames{kField}); @@ -447,12 +444,36 @@ end end - % Check if using ExogShockFn or EiidShockFn, and if so, do these use a parameter that is being determined in general eqm - heteroagentoptions.gridsinGE(ii)=0; %% Set up exogenous shock grids now (so they can then just be reused every time) - % Check if using ExogShockFn or EiidShockFn, and if so, do these use a - % parameter that is being determined in general eqm + + if isstruct(z_grid) + PTypeStructure.(iistr).z_grid=z_grid.(Names_i{ii}); + else + % If the last dimension is of length N_i, this indicates dependence on ptype + nn=size(z_grid,ndims(z_grid)); + if nn==N_i + otherdims = repmat({':'},1,ndims(z_grid)-1); + PTypeStructure.(iistr).z_grid=z_grid(otherdims{:},ii); + else + PTypeStructure.(iistr).z_grid=z_grid; + end + end + if isstruct(pi_z) + PTypeStructure.(iistr).pi_z=pi_z.(Names_i{ii}); + else + % If the last dimension is of length N_i, this indicates dependence on ptype + nn=size(pi_z,ndims(pi_z)); + if nn==N_i + otherdims = repmat({':'},1,ndims(pi_z)-1); + PTypeStructure.(iistr).pi_z=pi_z(otherdims{:},ii); + else + PTypeStructure.(iistr).pi_z=pi_z; + end + end + + % Check if using ExogShockFn or EiidShockFn, and if so, do these use a parameter that is being determined in general eqm + heteroagentoptions.gridsinGE(ii)=0; if isfield(PTypeStructure.(iistr).vfoptions,'ExogShockFn') tempExogShockFnParamNames=getAnonymousFnInputNames(PTypeStructure.(iistr).vfoptions.ExogShockFn); % can just leave action space in here as we only use it to see if GEPriceParamNames is part of it @@ -489,8 +510,7 @@ end PTypeStructure.(iistr).simoptions=rmfield(simoptions,'ExogShockFn'); end - - % Regardless of whether they are done here of in _subfn, they will be pre-computed by the time we get to the value fn, stationary dist, etc. So + % Regardless of whether they are done here of in _subfn, they will be precomputed by the time we get to the value fn, stationary dist, etc. So PTypeStructure.(iistr).vfoptions.alreadygridvals=1; PTypeStructure.(iistr).simoptions.alreadygridvals=1; @@ -530,28 +550,27 @@ %% DiscountFactor and ReturnFn % The parameter names can be made to depend on the permanent-type if isstruct(DiscountFactorParamNames) - PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(iistr); + PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(Names_i{ii}); else PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames; end PTypeStructure.(iistr).ReturnFn=ReturnFn; if isa(ReturnFn,'struct') - PTypeStructure.(iistr).ReturnFn=ReturnFn.(iistr); + PTypeStructure.(iistr).ReturnFn=ReturnFn.(Names_i{ii}); end - PTypeStructure.(iistr).ReturnFnParamNames=ReturnFnParamNamesFn(PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).Parameters); %% jequaloneDist and AgeWeightsParamNames if isstruct(jequaloneDist) - if isfield(jequaloneDist,PTypeStructure.(iistr)) + if isfield(jequaloneDist,PTypeStructure.Names_i{ii}) if isa(jequaloneDist, 'function_handle') [PTypeStructure.(iistr).jequaloneDist,~,PTypeStructure.(iistr).Parameters]=jequaloneDist_PType(jequaloneDist.(iistr),PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).simoptions,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_i,PTypeStructure.(iistr).PTypeDistParamNames,0); else - PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(PTypeStructure.(iistr)); + PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(PTypeStructure.Names_i{ii}); end else - error(['You must input jequaloneDist for permanent type ', PTypeStructure.(iistr), ' \n']) + error(['You must input jequaloneDist for permanent type ', PTypeStructure.Names_i{ii}, ' \n']) end else PTypeStructure.(iistr).jequaloneDist=jequaloneDist; @@ -559,10 +578,10 @@ PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames; if isstruct(AgeWeightParamNames) - if isfield(AgeWeightParamNames,iistr) - PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(iistr); + if isfield(AgeWeightParamNames,Names_i{ii}) + PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(Names_i{ii}); else - error(['You must input AgeWeightParamNames for permanent type ', iistr, ' \n']) + error(['You must input AgeWeightParamNames for permanent type ', Names_i{ii}, ' \n']) end end @@ -652,7 +671,7 @@ % check if it is an _name, in which case need to put it into AggVarNames_mod so that it gets handled correctly if it is used as an input later checkunderscorename=0; for ii=1:N_i - lname=length(iistr); + lname=length(Names_i{ii}); if length(intEqnnames_gg)>lname+1 % only check if intEqnnames_gg is long enough to be possible if strcmp(intEqnnames_gg(end-lname:end),['_',Names_i{ii}]) % E.g., creates Parameters.r.ptype001 from Parameters.r_ptype001 @@ -926,7 +945,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=p_eqm_vec(GEpriceindexes(pp,1):GEpriceindexes(pp,2)); for ii=1:N_i - p_eqm.(GEPriceParamNames{pp}).(PTypeStructure.(iistr))=temp(ii); + p_eqm.(GEPriceParamNames{pp}).(PTypeStructure.Names_i{ii})=temp(ii); end end end @@ -1001,7 +1020,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=GEpricepath(GEpriceindexes(pp,1):GEpriceindexes(pp,2),:); for ii=1:N_i - PriceHistory.(GEPriceParamNames{pp}).(iistr)=temp(ii,:); + PriceHistory.(GEPriceParamNames{pp}).(Names_i{ii})=temp(ii,:); end end end diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m index fba8d042..ebcfdd96 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m @@ -328,18 +328,18 @@ PTypeStructure.(iistr).n_d=n_d; if isstruct(n_d) - PTypeStructure.(iistr).n_d=n_d.(iistr); + PTypeStructure.(iistr).n_d=n_d.(Names_i{ii}); else PTypeStructure.(iistr).n_d=n_d; end PTypeStructure.(iistr).n_a=n_a; if isstruct(n_a) - PTypeStructure.(iistr).n_a=n_a.(iistr); + PTypeStructure.(iistr).n_a=n_a.(Names_i{ii}); else PTypeStructure.(iistr).n_a=n_a; end if isstruct(n_z) - PTypeStructure.(iistr).n_z=n_z.(iistr); + PTypeStructure.(iistr).n_z=n_z.(Names_i{ii}); else PTypeStructure.(iistr).n_z=n_z; end @@ -363,12 +363,12 @@ end if isstruct(d_grid) - PTypeStructure.(iistr).d_grid=d_grid.(iistr); + PTypeStructure.(iistr).d_grid=d_grid.(Names_i{ii}); else PTypeStructure.(iistr).d_grid=d_grid; end if isstruct(a_grid) - PTypeStructure.(iistr).a_grid=a_grid.(iistr); + PTypeStructure.(iistr).a_grid=a_grid.(Names_i{ii}); else PTypeStructure.(iistr).a_grid=a_grid; end @@ -377,7 +377,7 @@ %% Parameter Structure % Parameters are allowed to be given as structure, or as vector/matrix % (in terms of their dependence on permanent type). So go through each of - % these in term. + % these in turn. % ie. Parameters.alpha=[0;1]; or Parameters.alpha.ptype1=0; Parameters.alpha.ptype2=1; PTypeStructure.(iistr).Parameters=Parameters; FullParamNames=fieldnames(Parameters); % all the different parameters @@ -386,7 +386,7 @@ if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(iistr); + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); end elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. temp=Parameters.(FullParamNames{kField}); @@ -403,14 +403,28 @@ %% Set up exogenous shock grids now (so they can then just be reused every time) if isstruct(z_grid) - PTypeStructure.(iistr).z_grid=z_grid.(iistr); + PTypeStructure.(iistr).z_grid=z_grid.(Names_i{ii}); else - PTypeStructure.(iistr).z_grid=z_grid; + % If the last dimension is of length N_i, this indicates dependence on ptype + nn=size(z_grid,ndims(z_grid)); + if nn==N_i + otherdims = repmat({':'},1,ndims(z_grid)-1); + PTypeStructure.(iistr).z_grid=z_grid(otherdims{:},ii); + else + PTypeStructure.(iistr).z_grid=z_grid; + end end if isstruct(pi_z) - PTypeStructure.(iistr).pi_z=pi_z.(iistr); + PTypeStructure.(iistr).pi_z=pi_z.(Names_i{ii}); else - PTypeStructure.(iistr).pi_z=pi_z; + % If the last dimension is of length N_i, this indicates dependence on ptype + nn=size(pi_z,ndims(pi_z)); + if nn==N_i + otherdims = repmat({':'},1,ndims(pi_z)-1); + PTypeStructure.(iistr).pi_z=pi_z(otherdims{:},ii); + else + PTypeStructure.(iistr).pi_z=pi_z; + end end % Check if using ExogShockFn or EiidShockFn, and if so, do these use a @@ -430,7 +444,7 @@ heteroagentoptions.gridsinGE(ii)=1; end end - % If z (and e) are not determined in GE, then compute z_gridvals_J and pi_z_J now (and e_gridvals_J and pi_e_J) + % If z (and e) are not determined in GE, then compute z_gridvals and pi_z now (and e_gridvals and pi_e) if heteroagentoptions.gridsinGE(ii)==0 % Some of the shock grids depend on parameters that are determined in general eqm [PTypeStructure.(iistr).z_grid, PTypeStructure.(iistr).pi_z, PTypeStructure.(iistr).vfoptions]=ExogShockSetup_InfHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,3); @@ -455,13 +469,13 @@ %% DiscountFactor and ReturnFn % The parameter names can be made to depend on the permanent-type if isstruct(DiscountFactorParamNames) - PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(iistr); + PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(Names_i{ii}); else PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames; end if isstruct(ReturnFn) - PTypeStructure.(iistr).ReturnFn=ReturnFn.(iistr); + PTypeStructure.(iistr).ReturnFn=ReturnFn.(Names_i{ii}); else PTypeStructure.(iistr).ReturnFn=ReturnFn; end @@ -558,7 +572,7 @@ % check if it is an _name, in which case need to put it into AggVarNames_mod so that it gets handled correctly if it is used as an input later checkunderscorename=0; for ii=1:N_i - lname=length(iistr); + lname=length(Names_i{ii}); if length(intEqnnames_gg)>lname+1 % only check if intEqnnames_gg is long enough to be possible if strcmp(intEqnnames_gg(end-lname:end),['_',Names_i{ii}]) % E.g., creates Parameters.r.ptype001 from Parameters.r_ptype001 @@ -819,7 +833,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=p_eqm_vec(GEpriceindexes(pp,1):GEpriceindexes(pp,2)); for ii=1:N_i - p_eqm.(GEPriceParamNames{pp}).(iistr)=temp(ii); + p_eqm.(GEPriceParamNames{pp}).(Names_i{ii})=temp(ii); end end end @@ -893,7 +907,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=GEpricepath(GEpriceindexes(pp,1):GEpriceindexes(pp,2),:); for ii=1:N_i - PriceHistory.(GEPriceParamNames{pp}).(iistr)=temp(ii,:); + PriceHistory.(GEPriceParamNames{pp}).(Names_i{ii})=temp(ii,:); end end end diff --git a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m index e15f3230..1e3eada7 100644 --- a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m +++ b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m @@ -274,7 +274,7 @@ try if isstruct(Parameters.(paramnames{pp})) % parameter depends on ptype as a structure for ii=1:N_i - Parameters.([paramnames{pp},'_',Names_i{ii}])=Parameters.(paramnames{pp}).(iistr); + Parameters.([paramnames{pp},'_',Names_i{ii}])=Parameters.(paramnames{pp}).(Names_i{ii}); end paramthatdependsonptype(pp)=1; paramnamesptype{sum(paramthatdependsonptype)}=paramnames{pp}; @@ -368,7 +368,7 @@ % Horizon is determined via N_j if isstruct(N_j) - PTypeStructure.(iistr).N_j=N_j.(iistr); + PTypeStructure.(iistr).N_j=N_j.(Names_i{ii}); elseif isscalar(N_j) PTypeStructure.(iistr).N_j=N_j; else @@ -376,17 +376,17 @@ end if isstruct(n_d) - PTypeStructure.(iistr).n_d=n_d.(iistr); + PTypeStructure.(iistr).n_d=n_d.(Names_i{ii}); else PTypeStructure.(iistr).n_d=n_d; end if isstruct(n_a) - PTypeStructure.(iistr).n_a=n_a.(iistr); + PTypeStructure.(iistr).n_a=n_a.(Names_i{ii}); else PTypeStructure.(iistr).n_a=n_a; end if isstruct(n_z) - PTypeStructure.(iistr).n_z=n_z.(iistr); + PTypeStructure.(iistr).n_z=n_z.(Names_i{ii}); else PTypeStructure.(iistr).n_z=n_z; end @@ -410,31 +410,30 @@ end if isstruct(d_grid) - PTypeStructure.(iistr).d_grid=d_grid.(iistr); + PTypeStructure.(iistr).d_grid=d_grid.(Names_i{ii}); else PTypeStructure.(iistr).d_grid=d_grid; end if isstruct(a_grid) - PTypeStructure.(iistr).a_grid=a_grid.(iistr); + PTypeStructure.(iistr).a_grid=a_grid.(Names_i{ii}); else PTypeStructure.(iistr).a_grid=a_grid; end - + %% Parameter Structure % Parameters are allowed to be given as structure, or as vector/matrix % (in terms of their dependence on permanent type). So go through each of % these in term. % ie. Parameters.alpha=[0;1]; or Parameters.alpha.ptype1=0; Parameters.alpha.ptype2=1; - % Need to establish this in PTypeStructure before we use it below. PTypeStructure.(iistr).Parameters=Parameters; FullParamNames=fieldnames(Parameters); % all the different parameters nFields=length(FullParamNames); for kField=1:nFields - if isstruct(Parameters.(FullParamNames{kField})) % Check the current parameter for permanent type in structure form + if isa(Parameters.(FullParamNames{kField}), 'struct') % Check the current parameter for permanent type in structure form % Check if this parameter is used for the current permanent type (it may or may not be, some parameters are only used be a subset of permanent types) if isfield(Parameters.(FullParamNames{kField}),Names_i{ii}) - PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(iistr); + PTypeStructure.(iistr).Parameters.(FullParamNames{kField})=Parameters.(FullParamNames{kField}).(Names_i{ii}); end elseif sum(size(Parameters.(FullParamNames{kField}))==PTypeStructure.N_i)>=1 % Check for permanent type in vector/matrix form. temp=Parameters.(FullParamNames{kField}); @@ -447,6 +446,34 @@ end end + + %% Set up exogenous shock grids now (so they can then just be reused every time) + + if isstruct(z_grid) + PTypeStructure.(iistr).z_grid=z_grid.(Names_i{ii}); + else + % If the last dimension is of length N_i, this indicates dependence on ptype + nn=size(z_grid,ndims(z_grid)); + if nn==N_i + otherdims = repmat({':'},1,ndims(z_grid)-1); + PTypeStructure.(iistr).z_grid=z_grid(otherdims{:},ii); + else + PTypeStructure.(iistr).z_grid=z_grid; + end + end + if isstruct(pi_z) + PTypeStructure.(iistr).pi_z=pi_z.(Names_i{ii}); + else + % If the last dimension is of length N_i, this indicates dependence on ptype + nn=size(pi_z,ndims(pi_z)); + if nn==N_i + otherdims = repmat({':'},1,ndims(pi_z)-1); + PTypeStructure.(iistr).pi_z=pi_z(otherdims{:},ii); + else + PTypeStructure.(iistr).pi_z=pi_z; + end + end + % Check if using ExogShockFn or EiidShockFn, and if so, do these use a parameter that is being determined in general eqm heteroagentoptions.gridsinGE(ii)=0; if isfield(PTypeStructure.(iistr).vfoptions,'ExogShockFn') @@ -479,11 +506,25 @@ end PTypeStructure.(iistr)=rmfield(PTypeStructure.(iistr),'z_grid'); % Should not be used, as now have z_gridvals_J PTypeStructure.(iistr)=rmfield(PTypeStructure.(iistr),'pi_z'); % Should not be used, as now have pi_z_J + if isfield(PTypeStructure.(iistr).simoptions,'ExogShockFn') % Note: ExogShockSetup_FHorz() removed ExogShockFn from vfoptions but not from simoptions + if heteroagentoptions.useCustomModelStats==1 + heteroagentoptions.CustomModelStatsInputs.z_grid=PTypeStructure.(iistr).z_gridvals_J; + heteroagentoptions.CustomModelStatsInputs.pi_z=PTypeStructure.(iistr).pi_z_J; + end + PTypeStructure.(iistr).simoptions=rmfield(simoptions,'ExogShockFn'); + end else % InfHorz if heteroagentoptions.gridsinGE(ii)==0 [PTypeStructure.(iistr).z_gridvals, PTypeStructure.(iistr).pi_z, PTypeStructure.(iistr).vfoptions]=ExogShockSetup_InfHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,3); PTypeStructure.(iistr).simoptions.e_gridvals=PTypeStructure.(iistr).vfoptions.e_gridvals; % Note, will be [] if no e PTypeStructure.(iistr).simoptions.pi_e=PTypeStructure.(iistr).vfoptions.pi_e; % Note, will be [] if no e + if isfield(PTypeStructure.(iistr).simoptions,'ExogShockFn') % Note: ExogShockSetup_InfHorz() removed ExogShockFn from vfoptions but not from simoptions + if heteroagentoptions.useCustomModelStats==1 + heteroagentoptions.CustomModelStatsInputs.z_grid=PTypeStructure.(iistr).z_grid; + heteroagentoptions.CustomModelStatsInputs.pi_z=PTypeStructure.(iistr).pi_z; + end + PTypeStructure.(iistr).simoptions=rmfield(PTypeStructure.(iistr).simoptions,'ExogShockFn'); + end else % % Create placeholders, as these will need to be created in general eqm since they depend on General eqm parameters % PTypeStructure.(iistr).z_gridvals=[]; @@ -534,28 +575,28 @@ %% DiscountFactor and ReturnFn % The parameter names can be made to depend on the permanent-type if isstruct(DiscountFactorParamNames) - PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(iistr); + PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames.(Names_i{ii}); else PTypeStructure.(iistr).DiscountFactorParamNames=DiscountFactorParamNames; end PTypeStructure.(iistr).ReturnFn=ReturnFn; if isa(ReturnFn,'struct') - PTypeStructure.(iistr).ReturnFn=ReturnFn.(iistr); + PTypeStructure.(iistr).ReturnFn=ReturnFn.(Names_i{ii}); end PTypeStructure.(iistr).ReturnFnParamNames=ReturnFnParamNamesFn(PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).Parameters); %% jequaloneDist and AgeWeightsParamNames if isfinite(PTypeStructure.(iistr).N_j) % FHorz if isstruct(jequaloneDist) - if isfield(jequaloneDist,PTypeStructure.(iistr)) + if isfield(jequaloneDist,iistr) if isa(jequaloneDist, 'function_handle') [PTypeStructure.(iistr).jequaloneDist,~,PTypeStructure.(iistr).Parameters]=jequaloneDist_PType(jequaloneDist.(iistr),PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).simoptions,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_i,PTypeStructure.(iistr).PTypeDistParamNames,0); else - PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(PTypeStructure.(iistr)); + PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(iistr); end else - error(['You must input jequaloneDist for permanent type ', PTypeStructure.(iistr), ' \n']) + error(['You must input jequaloneDist for permanent type ', PTypeStructure.Names_i{ii}, ' \n']) end else PTypeStructure.(iistr).jequaloneDist=jequaloneDist; @@ -564,7 +605,7 @@ PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames; if isstruct(AgeWeightParamNames) if isfield(AgeWeightParamNames,Names_i{ii}) - PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(iistr); + PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(Names_i{ii}); else error(['You must input AgeWeightParamNames for permanent type ', Names_i{ii}, ' \n']) end @@ -657,7 +698,7 @@ % check if it is an _name, in which case need to put it into AggVarNames_mod so that it gets handled correctly if it is used as an input later checkunderscorename=0; for ii=1:N_i - lname=length(iistr); + lname=length(Names_i{ii}); if length(intEqnnames_gg)>lname+1 % only check if intEqnnames_gg is long enough to be possible if strcmp(intEqnnames_gg(end-lname:end),['_',Names_i{ii}]) % E.g., creates Parameters.r.ptype001 from Parameters.r_ptype001 @@ -928,7 +969,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=p_eqm_vec(GEpriceindexes(pp,1):GEpriceindexes(pp,2)); for ii=1:N_i - p_eqm.(GEPriceParamNames{pp}).(PTypeStructure.(iistr))=temp(ii); + p_eqm.(GEPriceParamNames{pp}).(PTypeStructure.Names_i{ii})=temp(ii); end end end @@ -1003,7 +1044,7 @@ elseif heteroagentoptions.GEptype_vectoroutput==0 temp=GEpricepath(GEpriceindexes(pp,1):GEpriceindexes(pp,2),:); for ii=1:N_i - PriceHistory.(GEPriceParamNames{pp}).(iistr)=temp(ii,:); + PriceHistory.(GEPriceParamNames{pp}).(Names_i{ii})=temp(ii,:); end end end From ac6ff21c0d39c4a131a476c79ce4711748f031ac Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 20 May 2026 21:54:28 +1200 Subject: [PATCH 86/90] More merge fixes We now complete end-to-end for small model, small_z_no_e (with AgentDistOnTransPath_MixHorz_PType change coming). --- .../slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m | 2 +- ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz.m | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m index 55e34ffc..5387ca99 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_raw.m @@ -49,7 +49,7 @@ ea_val=a2_gridvals(ea_c); for z_c=1:N_z z_val=z_gridvals_J(z_c,:,N_j); - ReturnMatrix_ea_z=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); + ReturnMatrix_ea_z=CreateReturnFnMatrix_ExpAsset_Disc(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsVec,0,0); % Level=0, Refine=0 % Calc the max and its index [Vtemp,maxindex]=max(ReturnMatrix_ea_z,[],1); V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=shiftdim(Vtemp,1); diff --git a/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz.m b/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz.m index 9746f3fe..7315cd91 100644 --- a/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz.m +++ b/ValueFnIter/TransPathFHorz/ValueFnOnTransPath_Case1_FHorz.m @@ -77,7 +77,7 @@ vfoptions.EVpre=0; % =1 is used by 'Matched Expecations Path', for TPath we want =0 (this relates to details of fastOLG=1 value fn code) if transpathoptions.fastOLG==0 && vfoptions.lowmemory>0 - error('On transtion paths you can only use vfoptions.lowmemory>0 when using transpathoptions.fastOLG=1, because otherwise the runtimes will anyway be so slow as to be essentially unusable') + warning('On transtion paths you can only use vfoptions.lowmemory>0 when using transpathoptions.fastOLG=1, because otherwise the runtimes will anyway be so slow as to be essentially unusable') end From 6852e69accecb38b3f3a74bf7d109398795173b6 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 20 May 2026 21:55:09 +1200 Subject: [PATCH 87/90] Move d_grid and a_grid to GPU --- .../TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m index 10394ef3..30db466f 100644 --- a/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m +++ b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m @@ -119,6 +119,14 @@ end end + % Move d_grid and a_grid to GPU + if isfield(simoptions_temp,'d_grid') && iscolumn(simoptions_temp.d_grid) + simoptions_temp.d_grid=gpuArray(simoptions_temp.d_grid); + end + if isfield(simoptions_temp,'a_grid') && iscolumn(simoptions_temp.a_grid) + simoptions_temp.a_grid=gpuArray(simoptions_temp.a_grid); + end + if simoptions_temp.verboseparams==1 sprintf('Parameter values for the current permanent type') Parameters_temp From c795d268bb0e30f6a336bf5a6c78220ed0a4e985 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Wed, 20 May 2026 21:59:14 +1200 Subject: [PATCH 88/90] Fixes needed for full z and e end-to-end --- .../ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m index e1af69ca..c5e983f2 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/slowOLG/ValueFnIter_FHorz_TPath_SingleStep_ExpAsset_e_raw.m @@ -65,7 +65,7 @@ z_val=z_gridvals_J(z_c,:,N_j); for e_c=1:N_e e_val=e_gridvals_J(e_c,:,N_j); - ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1, special_n_ea, special_n_z, special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_ExpAsset_Disc_e(ReturnFn, n_d1,n_d2,n_a1,n_a1, special_n_ea, special_n_z, special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); % Level=0, Refine=0 % Calc the max and its index [Vtemp,maxindex]=max(ReturnMatrix_ea_ze); V(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,e_c,N_j)=Vtemp; @@ -171,7 +171,7 @@ for e_c=1:N_e e_val=e_gridvals_J(e_c,:,jj); - ReturnMatrix_ea_ze=CreateReturnFnMatrix_Case1_ExpAsset_Disc_Par2e(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); + ReturnMatrix_ea_ze=CreateReturnFnMatrix_ExpAsset_Disc_e(ReturnFn, n_d1,n_d2,n_a1,n_a1,special_n_ea,special_n_z,special_n_e, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsVec,0,0); % Level=0, Refine=0 entireRHS_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; From f43bb3d2adc4fecb44eaa7c5f046ef66c08d5d72 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 22 May 2026 07:10:37 +1200 Subject: [PATCH 89/90] Changes needed for fastOLG Scenario 3 We test Scenario=3 because Scenario=4 is too large for fastOLG. --- .../EvalFnOnTransPath_AggVars_Case1_FHorz.m | 1 + PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m | 3 + ...onPath_FHorz_substeps_Step2_AdjustPolicy.m | 118 +++++++++--------- ..._TPath_SingleStep_fastOLG_ExpAsset_e_raw.m | 40 ++++-- ...rz_TPath_SingleStep_fastOLG_ExpAsset_raw.m | 36 ++++-- ...entDist_InfHorz_TPath_SingleStep_AggVars.m | 3 +- 6 files changed, 125 insertions(+), 76 deletions(-) diff --git a/EvaluateFnOnAgentDist/TransPathFHorz/EvalFnOnTransPath_AggVars_Case1_FHorz.m b/EvaluateFnOnAgentDist/TransPathFHorz/EvalFnOnTransPath_AggVars_Case1_FHorz.m index ea3f831e..7438c164 100644 --- a/EvaluateFnOnAgentDist/TransPathFHorz/EvalFnOnTransPath_AggVars_Case1_FHorz.m +++ b/EvaluateFnOnAgentDist/TransPathFHorz/EvalFnOnTransPath_AggVars_Case1_FHorz.m @@ -143,6 +143,7 @@ if simoptions.alreadygridvals==0 % gridpiboth=1: only need z_gridvals_J (and e_gridvals_J if N_e>0) [z_gridvals_J, ~, ~, e_gridvals_J, ~, ~, ~, transpathoptions, simoptions]=ExogShockSetup_FHorz_TPath(n_z,z_grid,[],N_a,N_j,Parameters,PricePathNames,ParamPathNames,transpathoptions,simoptions,1); + simoptions.alreadygridvals=1; % we call down with z_gridvals_J, so those are already gridvals elseif simoptions.alreadygridvals==1 z_gridvals_J=z_grid; e_gridvals_J=simoptions.e_gridvals_J; diff --git a/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m b/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m index cb373805..7a82eaeb 100644 --- a/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m +++ b/PolicyInd2Val/PolicyInd2Val_FHorz_TPath.m @@ -89,6 +89,9 @@ % If using vfoptions.gridinterplayer=1, it should be aprime_gridvals for the fine grid, not the rough grid l_aprime=size(a_grid,2);% it is aprime_gridvals, not actually a_grid + if vfoptions.experienceasset==1 + l_aprime=l_aprime-1; + end if n_d(1)==0 PolicyValuesPath=zeros(l_aprime,N_a*N_j*N_z*T,'gpuArray'); % N_a*N_j*N_z*T is because of how indexing gridvals will work diff --git a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m index 0b66a202..22ae8169 100644 --- a/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m +++ b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m @@ -3,7 +3,7 @@ %% if simoptions.experienceasset==1 - % Note: Have not yet implemented to permite the aprimeFn parameters to vary over time path tt + % Note: Have not yet implemented to permit the aprimeFn parameters to vary over time path tt whichisdforexpasset=length(n_d)-simoptions.setup_experienceasset.l_dexperienceasset+1:length(n_d); % is just saying which is the decision variable that influences the experience asset (it is the 'last' decision variable) if N_e==0 && N_z==0 @@ -147,7 +147,10 @@ PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,1:N_j-1,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,1:N_j-1,:)-1)+n_a1(1)*n_a1(2)*n_a1(3)*(PolicyIndexesPath(l_d+4,:,1:N_j-1,:)-1),[N_a,N_j-1,T-1]); end if simoptions.fastOLG==0 - PolicyaprimePath_slowOLG=gather(PolicyaprimePath); + if simoptions.experienceasset==1 + PolicyaprimePath=reshape(PolicyaprimePath,[N_a,(N_j-1),1,T]); + end + PolicyaprimePath_slowOLG=gather(repmat(PolicyaprimePath,1,2,1)); elseif simoptions.fastOLG==1 PolicyaprimePath=reshape(permute(reshape(PolicyaprimePath,[N_a,N_j-1,T-1]),[1,2,3]),[N_a*(N_j-1),T-1]); PolicyaprimejPath=PolicyaprimePath+repelem(N_a*gpuArray(0:1:(N_j-1)-1)',N_a,1); @@ -163,7 +166,7 @@ PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point PolicyProbsPath=gather(PolicyProbsPath); elseif N_probs>1 % for a reason other than gridinterplayer - PolicyaprimejPath=reshape(PolicyaprimejPath,[N_a*(N_j-1),1,T-1]); % so can assume this size later + PolicyaprimejPath=repmat(reshape(PolicyaprimejPath,[N_a*(N_j-1),1,T-1]),1,2,1); % so can assume this size later end PolicyaprimejPath=gather(PolicyaprimejPath); end @@ -171,23 +174,19 @@ if simoptions.experienceasset==1 if simoptions.fastOLG==0 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimePath_slowOLG=reshape(PolicyaprimePath_slowOLG,[N_a,(N_j-1),1,T])+a2primeIndexesPath; + PolicyaprimePath_slowOLG=PolicyaprimePath_slowOLG+a2primeIndexesPath; else - PolicyaprimePath_slowOLG=reshape(PolicyaprimePath_slowOLG,[N_a,(N_j-1),1,T])+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); - end - if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); - else - PolicyProbsPath=a2primeProbsPath; + PolicyaprimePath_slowOLG=PolicyaprimePath_slowOLG+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end + PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejPath=PolicyaprimejPath+a2primeIndexesPath; else - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + PolicyaprimejPath=PolicyaprimejPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -211,7 +210,10 @@ PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,1:N_j-1,:)-1)+n_a1(1)*n_a1(2)*n_a1(3)*(PolicyIndexesPath(l_d+4,:,:,1:N_j-1,:)-1),[N_a*N_z,N_j-1,T-1]); end if simoptions.fastOLG==0 - PolicyaprimezPath_slowOLG=gather(PolicyaprimePath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1)); + if simoptions.experienceasset==1 + PolicyaprimezPath=reshape(PolicyaprimePath+repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1),[N_a*N_z,(N_j-1),1,T]); + end + PolicyaprimezPath_slowOLG=gather(repmat(PolicyaprimezPath,1,2,1)); elseif simoptions.fastOLG==1 PolicyaprimePath=reshape(permute(reshape(PolicyaprimePath,[N_a,N_z,N_j-1,T-1]),[1,3,2,4]),[N_a*(N_j-1)*N_z,T-1]); PolicyaprimejzPath=PolicyaprimePath+repelem(N_a*gpuArray(0:1:(N_j-1)*N_z-1)',N_a,1); @@ -227,7 +229,7 @@ PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point PolicyProbsPath=gather(PolicyProbsPath); elseif N_probs>1 % for a reason other than gridinterplayer - PolicyaprimejzPath=reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z,1,T-1]); % so can assume this size later + PolicyaprimejzPath=repmat(reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z,1,T-1]),1,2,1); % so can assume this size later end PolicyaprimejzPath=gather(PolicyaprimejzPath); end @@ -235,23 +237,19 @@ if simoptions.experienceasset==1 if simoptions.fastOLG==0 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z,(N_j-1),1,T])+a2primeIndexesPath; + PolicyaprimezPath_slowOLG=PolicyaprimezPath_slowOLG+a2primeIndexesPath; else - PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z,(N_j-1),1,T])+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); - end - if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); - else - PolicyProbsPath=a2primeProbsPath; + PolicyaprimezPath_slowOLG=PolicyaprimezPath_slowOLG+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end + PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+a2primeIndexesPath; else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + PolicyaprimejzPath=PolicyaprimejzPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -275,7 +273,10 @@ PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,1:N_j-1,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,1:N_j-1,:)-1)+n_a1(1)*n_a1(2)*n_a1(3)*(PolicyIndexesPath(l_d+4,:,:,1:N_j-1,:)-1),[N_a*N_e,N_j-1,T-1]); end if simoptions.fastOLG==0 - PolicyaprimePath_slowOLG=gather(PolicyaprimePath); + if simoptions.experienceasset==1 + PolicyaprimePath=reshape(PolicyaprimePath,[N_a*N_e,(N_j-1),1,T]); + end + PolicyaprimePath_slowOLG=gather(repmat(PolicyaprimePath,1,2,1)); elseif simoptions.fastOLG==1 PolicyaprimePath=reshape(permute(reshape(PolicyaprimePath,[N_a,N_e,N_j-1,T-1]),[1,3,2,4]),[N_a*(N_j-1)*N_e,T-1]); PolicyaprimejPath=PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:(N_j-1)-1)',N_a,1),N_e,1); @@ -291,7 +292,7 @@ PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point PolicyProbsPath=gather(PolicyProbsPath); elseif N_probs>1 % for a reason other than gridinterplayer - PolicyaprimejPath=reshape(PolicyaprimejPath,[N_a*(N_j-1)*N_e,1,T-1]); % so can assume this size later + PolicyaprimejPath=repmat(reshape(PolicyaprimejPath,[N_a*(N_j-1)*N_e,1,T-1]),1,2,1); % so can assume this size later end PolicyaprimejPath=gather(PolicyaprimejPath); end @@ -299,15 +300,11 @@ if simoptions.experienceasset==1 if simoptions.fastOLG==0 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimePath_slowOLG=reshape(PolicyaprimePath_slowOLG,[N_a*N_e,(N_j-1),1,T])+a2primeIndexesPath; + PolicyaprimePath_slowOLG=PolicyaprimePath_slowOLG+a2primeIndexesPath; else - PolicyaprimePath_slowOLG=reshape(PolicyaprimePath_slowOLG,[N_a*N_e,(N_j-1),1,T])+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); - end - if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); - else - PolicyProbsPath=a2primeProbsPath; + PolicyaprimePath_slowOLG=PolicyaprimePath_slowOLG+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end + PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 if simoptions.setup_experienceasset.N_a1==0 PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); @@ -339,7 +336,10 @@ PolicyaprimePath=reshape(PolicyIndexesPath(l_d+1,:,:,:,1:N_j-1,:)+n_a1(1)*(PolicyIndexesPath(l_d+2,:,:,:,1:N_j-1,:)-1)+n_a1(1)*n_a1(2)*(PolicyIndexesPath(l_d+3,:,:,:,1:N_j-1,:)-1)+n_a1(1)*n_a1(2)*n_a1(3)*(PolicyIndexesPath(l_d+4,:,:,:,1:N_j-1,:)-1),[N_a*N_z*N_e,N_j-1,T-1]); end if simoptions.fastOLG==0 - PolicyaprimezPath_slowOLG=gather(PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1),N_e,1)); + if simoptions.experienceasset==1 + PolicyaprimezPath=reshape(PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:N_z-1)',N_a,1),N_e,1),[N_a*N_z*N_e,(N_j-1),1,T]); + end + PolicyaprimezPath_slowOLG=gather(repmat(PolicyaprimezPath,1,2,1)); elseif simoptions.fastOLG==1 PolicyaprimePath=reshape(permute(reshape(PolicyaprimePath,[N_a,N_z*N_e,N_j-1,T-1]),[1,3,2,4]),[N_a*(N_j-1)*N_z*N_e,T-1]); PolicyaprimejzPath=PolicyaprimePath+repmat(repelem(N_a*gpuArray(0:1:(N_j-1)*N_z-1)',N_a,1),N_e,1); @@ -355,7 +355,7 @@ PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point PolicyProbsPath=gather(PolicyProbsPath); elseif N_probs>1 % for a reason other than gridinterplayer - PolicyaprimejzPath=reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z*N_e,1,T-1]); % so can assume this size later + PolicyaprimejzPath=repmat(reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z*N_e,1,T-1]),1,2,1); % so can assume this size later end PolicyaprimejzPath=gather(PolicyaprimejzPath); end @@ -363,20 +363,16 @@ if simoptions.experienceasset==1 if simoptions.fastOLG==0 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z*N_e,(N_j-1),1,T])+a2primeIndexesPath; + PolicyaprimezPath_slowOLG=PolicyaprimezPath_slowOLG+a2primeIndexesPath; else - PolicyaprimezPath_slowOLG=reshape(PolicyaprimezPath_slowOLG,[N_a*N_z*N_e,(N_j-1),1,T])+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); - end - if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); - else - PolicyProbsPath=a2primeProbsPath; + PolicyaprimezPath_slowOLG=PolicyaprimezPath_slowOLG+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end + PolicyProbsPath=a2primeProbsPath; elseif simoptions.fastOLG==1 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+a2primeIndexesPath; + PolicyaprimejzPath=PolicyaprimejzPath+a2primeIndexesPath; else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); + PolicyaprimejzPath=PolicyaprimejzPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; @@ -419,18 +415,18 @@ PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point PolicyProbsPath=gather(PolicyProbsPath); elseif N_probs>1 % for a reason other than gridinterplayer - PolicyaprimejPath=reshape(PolicyaprimejPath,[N_a*(N_j-1),1,T-1]); % so can assume this size later + PolicyaprimejPath=repmat(reshape(PolicyaprimejPath,[N_a*(N_j-1),1,T-1]),1,2,1); % so can assume this size later end PolicyaprimejPath=gather(PolicyaprimejPath); clear PolicyIndexesPath L2index if simoptions.experienceasset==1 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejPath=PolicyaprimejPath+a2primeIndexesPath; else - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejPath=PolicyaprimejPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -464,18 +460,18 @@ PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point PolicyProbsPath=gather(PolicyProbsPath); elseif N_probs>1 % for a reason other than gridinterplayer - PolicyaprimejzPath=reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z,1,T-1]); % so can assume this size later + PolicyaprimejzPath=repmat(reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z,1,T-1]),1,2,1); % so can assume this size later end PolicyaprimejzPath=gather(PolicyaprimejzPath); clear PolicyIndexesPath L2index if simoptions.experienceasset==1 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+a2primeIndexesPath; else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -509,18 +505,18 @@ PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point PolicyProbsPath=gather(PolicyProbsPath); elseif N_probs>1 % for a reason other than gridinterplayer - PolicyaprimejPath=reshape(PolicyaprimejPath,[N_a*(N_j-1)*N_e,1,T-1]); % so can assume this size later + PolicyaprimejPath=repmat(reshape(PolicyaprimejPath,[N_a*(N_j-1)*N_e,1,T-1]),1,2,1); % so can assume this size later end PolicyaprimejPath=gather(PolicyaprimejPath); clear PolicyIndexesPath L2index if simoptions.experienceasset==1 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejPath=PolicyaprimejPath+a2primeIndexesPath; else - PolicyaprimejPath=repmat(PolicyaprimejPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejPath=PolicyaprimejPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -554,18 +550,18 @@ PolicyProbsPath(:,1,:)=1-PolicyProbsPath(:,2,:); % probability of lower grid point PolicyProbsPath=gather(PolicyProbsPath); elseif N_probs>1 % for a reason other than gridinterplayer - PolicyaprimejzPath=reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z*N_e,1,T-1]); % so can assume this size later + PolicyaprimejzPath=repmat(reshape(PolicyaprimejzPath,[N_a*(N_j-1)*N_z*N_e,1,T-1]),1,2,1); % so can assume this size later end PolicyaprimejzPath=gather(PolicyaprimejzPath); clear PolicyIndexesPath L2index if simoptions.experienceasset==1 if simoptions.setup_experienceasset.N_a1==0 - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(a2primeIndexesPath,1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+a2primeIndexesPath; else - PolicyaprimejzPath=repmat(PolicyaprimejzPath,1,2,1)+repelem(simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1),1,2,1); + PolicyaprimejzPath=PolicyaprimejzPath+simoptions.setup_experienceasset.N_a1*(a2primeIndexesPath-1); end if exist('PolicyProbsPath','var') - PolicyProbsPath=repmat(PolicyProbsPath,1,2,1).*repelem(a2primeProbsPath,1,2,1); + PolicyProbsPath=PolicyProbsPath.*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e_raw.m index 25a2da67..b4b78114 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e_raw.m @@ -39,7 +39,7 @@ aprimeplus1Index=repelem((1:1:N_a1)',N_d2,1,1)+N_a1*repmat(a2primeIndex,N_a1,1,1); % [N_d2*N_a1,N_a2,N_j], autofill the [1,N_a1,N_j] dimensions for the first part aprimeProbs=repmat(a2primeProbs,N_a1,1,1,N_z); % [N_d2*N_a1,N_a2,N_j,N_z] - EVpre=[sum(V(N_a+1:end,:).*replem(reshape(pi_e_J,[N_j,1,N_e]),N_a-1,1,1),3); zeros(N_a,N_z,'gpuArray')]; % I use zeros in j=N_j so that can just use pi_z_J to create expectations + EVpre=[sum(V(N_a+1:end,:,:).*pi_e_J(N_a+1:end,:,:),3); zeros(N_a,N_z,'gpuArray')]; % I use zeros in j=N_j so that can just use pi_z_J to create expectations % Need to add the indexes for j to the aprimeIndex, remember fastOLG so V is (a,j)-by-z Vlower=reshape(EVpre(aprimeIndex+shiftdim(N_a*gpuArray(0:1:N_j-1),-1),:),[N_d2*N_a1,N_a2,N_j,N_z]); @@ -83,12 +83,10 @@ EV=reshape(sum(EV,4),[N_d2*N_a1,N_a2,N_j,N_z]); % (aprime,1,j,z), 2nd dim will be autofilled with a end -DiscountedEV=DiscountFactorParamsVec.*repelem(EV,N_d1,N_a1,1,1); - if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc_e(ReturnFn, n_d1, n_d2, n_a1, n_a1,n_a2, n_z,n_e,N_j, d_gridvals, a1_gridvals, a1_gridvals, a2_grid, z_gridvals_J, e_gridvals_J, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 - entireRHS=ReturnMatrix+DiscountedEV; + entireRHS=ReturnMatrix+DiscountFactorParamsVec.*repelem(EV,N_d1,N_a1,1,1); % Calc the max and it's index [Vtemp,maxindex]=max(entireRHS,[],1); @@ -100,6 +98,7 @@ special_n_e=ones(1,length(n_e),'gpuArray'); V=zeros(N_a*N_j,N_z,N_e,'gpuArray'); Policy=zeros(N_a*N_j,N_z,N_e,'gpuArray'); + DiscountedEV=DiscountFactorParamsVec.*repelem(EV,N_d1,N_a1,1,1); for e_c=1:N_e e_val=e_gridvals_J(:,e_c,:); @@ -121,7 +120,7 @@ for z_c=1:N_z z_val=z_gridvals_J(:,z_c,:); - DiscountedEV_z=DiscountedEV(:,:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec.*repelem(EV(:,:,:,z_c),N_d1,N_a1,1,1); for e_c=1:N_e e_val=e_gridvals_J(:,e_c,:); @@ -136,12 +135,39 @@ Policy(:,z_c,e_c)=maxindex; end end +elseif vfoptions.lowmemory==3 + special_n_ea=ones(1,length(n_a2),'gpuArray'); + special_n_z=ones(1,length(n_z),'gpuArray'); + special_n_e=ones(1,length(n_e),'gpuArray'); + V=zeros(N_a*N_j,N_z,N_e,'gpuArray'); + Policy=zeros(N_a*N_j,N_z,N_e,'gpuArray'); + + for ea_c=1:N_a2 + ea_val=a2_grid(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(:,z_c,:); + DiscountedEV_z=DiscountFactorParamsVec.*repelem(EV(:,:,:,z_c),N_d1,N_a1,1,1); + + for e_c=1:N_e + e_val=e_gridvals_J(:,e_c,:); + + ReturnMatrix_ze=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc_e(ReturnFn, n_d1, n_d2, n_a1, n_a1,n_a2, special_n_z,special_n_e,N_j, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 + + entireRHS_ze=ReturnMatrix_ze+DiscountedEV_z; + + % Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_ze,[],1); + V(:,z_c,e_c)=Vtemp; + Policy(:,z_c,e_c)=maxindex; + end + end + end end %% fastOLG with z, so need to output to take certain shapes -% V=reshape(V,[N_a*N_j,N_z,N_e]); -% Policy=reshape(Policy,[N_a,N_j,N_z,N_e]); +V=reshape(V,[N_a*N_j,N_z,N_e]); +Policy=reshape(Policy,[N_a,N_j,N_z,N_e]); %% Output shape for policy Policy=shiftdim(Policy,-1); % so first dim is just one point diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_raw.m index 6376919d..cc6af3ca 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_raw.m @@ -79,11 +79,11 @@ EV=reshape(sum(EV,4),[N_d2*N_a1,N_a2,N_j,N_z]); % (aprime,1,j,z), 2nd dim will be autofilled with a end -DiscountedEV=DiscountFactorParamsVec.*repelem(EV,N_d1,N_a1,1,1); if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc(ReturnFn, n_d1, n_d2, n_a1, n_a1,n_a2, n_z,N_j, d_gridvals, a1_gridvals, a1_gridvals, a2_grid, z_gridvals_J, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 + DiscountedEV=DiscountFactorParamsVec.*repelem(EV,N_d1,N_a1,1,1); entireRHS=ReturnMatrix+DiscountedEV; % Calc the max and it's index @@ -99,7 +99,7 @@ for z_c=1:N_z z_val=z_gridvals_J(:,z_c,:); - DiscountedEV_z=DiscountedEV(:,:,:,z_c); + DiscountedEV_z=DiscountFactorParamsVec.*repelem(EV(:,:,:,z_c),N_d1,N_a1,1,1); ReturnMatrix_z=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc(ReturnFn, n_d1, n_d2, n_a1, n_a1,n_a2, special_n_z,N_j, d_gridvals, a1_gridvals, a1_gridvals, a2_grid, z_val, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 @@ -107,15 +107,37 @@ % Calc the max and it's index [Vtemp,maxindex]=max(entireRHS_z,[],1); - V(:,z_c)=Vtemp; - Policy(:,z_c)=maxindex; + V(:,z_c)=Vtemp(:); + Policy(:,z_c)=maxindex(:); end -end +elseif vfoptions.lowmemory==3 + special_n_ea=ones(1,length(n_a2),'gpuArray'); + special_n_z=ones(1,length(n_z),'gpuArray'); + V=zeros(N_a*N_j,N_z,'gpuArray'); + Policy=zeros(N_a*N_j,N_z,'gpuArray'); + + for ea_c=1:N_a2 + ea_val=a2_grid(ea_c); + for z_c=1:N_z + z_val=z_gridvals_J(:,z_c,:); + DiscountedEV_z=DiscountFactorParamsVec.*repelem(EV(:,:,:,z_c),N_d1,N_a1,1,1); + + ReturnMatrix_z=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc(ReturnFn, n_d1, n_d2, n_a1, n_a1, special_n_ea, special_n_z,N_j, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 + + entireRHS_z=ReturnMatrix_z+DiscountedEV_z; + + % Calc the max and it's index + [Vtemp,maxindex]=max(entireRHS_z,[],1); + V(:,z_c)=Vtemp(:); + Policy(:,z_c)=maxindex(:); + end + end +end %% fastOLG with z, so need to output to take certain shapes -% V=reshape(V,[N_a*N_j,N_z]); -% Policy=reshape(Policy,[N_a,N_j,N_z]); +V=reshape(V,[N_a*N_j,N_z]); +Policy=reshape(Policy,[N_a,N_j,N_z]); %% Output shape for policy Policy=shiftdim(Policy,-1); % so first dim is just one point diff --git a/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m b/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m index f26ebabb..cd53dc31 100644 --- a/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m +++ b/TransitionPaths/InfHorz/subcodes/EvalFnOnAgentDist_TPath_SingleStep/EvalFnOnAgentDist_InfHorz_TPath_SingleStep_AggVars.m @@ -17,9 +17,10 @@ for ff=1:length(FnsToEvaluateNames) FnToEvaluateParamsCell=CreateCellFromParams(Parameters,FnsToEvaluateParamNames(ff).Names); Values=EvalFnOnAgentDist_Grid(FnsToEvaluateCell{ff}, FnToEvaluateParamsCell,PolicyValuesPermute,l_daprime,n_a,n_z,a_gridvals,z_gridvals); + % Values comes back as a gpuArray Values=reshape(Values,[N_a*N_z,1]); % When evaluating value function (which may sometimes give -Inf values) on StationaryDistVec (which at those points will be 0) we get 'NaN'. Use temp as intermediate variable just eliminate those. - temp=Values.*AgentDist; + temp=gather(Values.*AgentDist); val=sum(temp(~isnan(temp))); if outputastruct==0 From 03253844b92a8cf0d0bf9e54997783ed7ee10eb3 Mon Sep 17 00:00:00 2001 From: Michael Tiemann Date: Fri, 22 May 2026 11:27:26 +1200 Subject: [PATCH 90/90] e_gridvals_J and lowmemory==3 issues Address some of these issues. Not out of the woods yet. --- ...teReturnFnMatrix_fastOLG_ExpAsset_Disc_e.m | 11 ++++---- SubCodes/ExoShocks/ExogShockSetup_FHorz.m | 2 +- ..._TPath_SingleStep_fastOLG_ExpAsset_e_raw.m | 26 +++++++++---------- ...rz_TPath_SingleStep_fastOLG_ExpAsset_raw.m | 22 ++++++++-------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/ReturnFnMatrix/ExperienceAsset/CreateReturnFnMatrix_fastOLG_ExpAsset_Disc_e.m b/ReturnFnMatrix/ExperienceAsset/CreateReturnFnMatrix_fastOLG_ExpAsset_Disc_e.m index 8fb5aa85..59ca082d 100644 --- a/ReturnFnMatrix/ExperienceAsset/CreateReturnFnMatrix_fastOLG_ExpAsset_Disc_e.m +++ b/ReturnFnMatrix/ExperienceAsset/CreateReturnFnMatrix_fastOLG_ExpAsset_Disc_e.m @@ -104,16 +104,17 @@ end end end +% fastOLG: e_gridvals_J is (1,1,1,1,1,j,N_e,l_e) for fastOLG with ExpAsset if l_e>=1 - e1vals=shiftdim(e_gridvals_J(:,:,1),-5); + e1vals=e_gridvals_J(1,1,1,1,1,:,:,1); if l_e>=2 - e2vals=shiftdim(e_gridvals_J(:,:,2),-5); + e2vals=e_gridvals_J(1,1,1,1,1,:,:,2); if l_e>=3 - e3vals=shiftdim(e_gridvals_J(:,:,3),-5); + e3vals=e_gridvals_J(1,1,1,1,1,:,:,3); if l_e>=4 - e4vals=shiftdim(e_gridvals_J(:,:,4),-5); + e4vals=e_gridvals_J(1,1,1,1,1,:,:,4); if l_e>=5 - e5vals=shiftdim(e_gridvals_J(:,:,5),-5); + e5vals=e_gridvals_J(1,1,1,1,1,:,:,5); end end end diff --git a/SubCodes/ExoShocks/ExogShockSetup_FHorz.m b/SubCodes/ExoShocks/ExogShockSetup_FHorz.m index bb86186f..7202ce9b 100644 --- a/SubCodes/ExoShocks/ExogShockSetup_FHorz.m +++ b/SubCodes/ExoShocks/ExogShockSetup_FHorz.m @@ -204,7 +204,7 @@ error('You are using an e (iid) variable, and so need to declare options.pi_e (options refers to either vfoptions or simoptions)') end - if gridpiboth==1 % for most FnsToEvaluate, we don't use pi_z + if gridpiboth==1 % for most FnsToEvaluate, we don't use pi_e options.pi_e_J=[]; % Now just do e_gridvals_J options.e_gridvals_J=zeros(prod(options.n_e),length(options.n_e),N_j,'gpuArray'); diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e_raw.m index b4b78114..fac15cfb 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e_raw.m @@ -101,7 +101,7 @@ DiscountedEV=DiscountFactorParamsVec.*repelem(EV,N_d1,N_a1,1,1); for e_c=1:N_e - e_val=e_gridvals_J(:,e_c,:); + e_val=e_gridvals_J(:,:,:,:,:,:,:,e_c,:); ReturnMatrix_e=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc_e(ReturnFn, n_d1, n_d2, n_a1, n_a1,n_a2, n_z,special_n_e,N_j, d_gridvals, a1_gridvals, a1_gridvals, a2_grid, z_gridvals_J, e_val, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 @@ -119,11 +119,11 @@ Policy=zeros(N_a*N_j,N_z,N_e,'gpuArray'); for z_c=1:N_z - z_val=z_gridvals_J(:,z_c,:); + z_val=z_gridvals_J(:,:,:,:,:,z_c,:); DiscountedEV_z=DiscountFactorParamsVec.*repelem(EV(:,:,:,z_c),N_d1,N_a1,1,1); for e_c=1:N_e - e_val=e_gridvals_J(:,e_c,:); + e_val=e_gridvals_J(:,:,:,:,:,:,:,e_c,:); ReturnMatrix_ze=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc_e(ReturnFn, n_d1, n_d2, n_a1, n_a1,n_a2, special_n_z,special_n_e,N_j, d_gridvals, a1_gridvals, a1_gridvals, a2_grid, z_val, e_val, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 @@ -139,26 +139,26 @@ special_n_ea=ones(1,length(n_a2),'gpuArray'); special_n_z=ones(1,length(n_z),'gpuArray'); special_n_e=ones(1,length(n_e),'gpuArray'); - V=zeros(N_a*N_j,N_z,N_e,'gpuArray'); - Policy=zeros(N_a*N_j,N_z,N_e,'gpuArray'); + V=zeros(N_a1,N_a2,N_j,N_z,N_e,'gpuArray'); + Policy=zeros(N_a1,N_a2,N_j,N_z,N_e,'gpuArray'); for ea_c=1:N_a2 ea_val=a2_grid(ea_c); for z_c=1:N_z - z_val=z_gridvals_J(:,z_c,:); - DiscountedEV_z=DiscountFactorParamsVec.*repelem(EV(:,:,:,z_c),N_d1,N_a1,1,1); + z_val=z_gridvals_J(:,:,:,:,:,z_c,:); + DiscountedEV_ea_z=DiscountFactorParamsVec.*repelem(EV(:,ea_c,:,z_c),N_d1,N_a1,1,1); for e_c=1:N_e - e_val=e_gridvals_J(:,e_c,:); + e_val=e_gridvals_J(:,:,:,:,:,:,:,e_c,:); - ReturnMatrix_ze=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc_e(ReturnFn, n_d1, n_d2, n_a1, n_a1,n_a2, special_n_z,special_n_e,N_j, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 + ReturnMatrix_ea_ze=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc_e(ReturnFn, n_d1, n_d2, n_a1, n_a1,special_n_ea, special_n_z,special_n_e,N_j, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, e_val, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 - entireRHS_ze=ReturnMatrix_ze+DiscountedEV_z; + entireRHS_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; % Calc the max and it's index - [Vtemp,maxindex]=max(entireRHS_ze,[],1); - V(:,z_c,e_c)=Vtemp; - Policy(:,z_c,e_c)=maxindex; + [Vtemp,maxindex]=max(entireRHS_ea_ze,[],1); + V(:,ea_c,:,z_c,e_c)=shiftdim(Vtemp,1); + Policy(:,ea_c,:,z_c,e_c)=shiftdim(maxindex,1); end end end diff --git a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_raw.m index cc6af3ca..6f9d3fee 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/fastOLG/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_raw.m @@ -98,7 +98,7 @@ Policy=zeros(N_a*N_j,N_z,'gpuArray'); for z_c=1:N_z - z_val=z_gridvals_J(:,z_c,:); + z_val=z_gridvals_J(:,:,:,:,:,z_c,:); DiscountedEV_z=DiscountFactorParamsVec.*repelem(EV(:,:,:,z_c),N_d1,N_a1,1,1); ReturnMatrix_z=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc(ReturnFn, n_d1, n_d2, n_a1, n_a1,n_a2, special_n_z,N_j, d_gridvals, a1_gridvals, a1_gridvals, a2_grid, z_val, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 @@ -114,23 +114,23 @@ elseif vfoptions.lowmemory==3 special_n_ea=ones(1,length(n_a2),'gpuArray'); special_n_z=ones(1,length(n_z),'gpuArray'); - V=zeros(N_a*N_j,N_z,'gpuArray'); - Policy=zeros(N_a*N_j,N_z,'gpuArray'); + V=zeros(N_a1,N_a2,N_j,N_z,'gpuArray'); + Policy=zeros(N_a1,N_a2,N_j,N_z,'gpuArray'); for ea_c=1:N_a2 ea_val=a2_grid(ea_c); for z_c=1:N_z - z_val=z_gridvals_J(:,z_c,:); - DiscountedEV_z=DiscountFactorParamsVec.*repelem(EV(:,:,:,z_c),N_d1,N_a1,1,1); + z_val=z_gridvals_J(:,:,:,:,:,z_c,:); + DiscountedEV_ea_z=DiscountFactorParamsVec.*repelem(EV(:,ea_c,:,z_c),N_d1,N_a1,1,1); - ReturnMatrix_z=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc(ReturnFn, n_d1, n_d2, n_a1, n_a1, special_n_ea, special_n_z,N_j, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 - - entireRHS_z=ReturnMatrix_z+DiscountedEV_z; + ReturnMatrix_ea_z=CreateReturnFnMatrix_fastOLG_ExpAsset_Disc(ReturnFn, n_d1, n_d2, n_a1, n_a1, special_n_ea, special_n_z,N_j, d_gridvals, a1_gridvals, a1_gridvals, ea_val, z_val, ReturnFnParamsAgeMatrix,0,0); % Level=0, Refine=0 + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountedEV_ea_z; + % Calc the max and it's index - [Vtemp,maxindex]=max(entireRHS_z,[],1); - V(:,z_c)=Vtemp(:); - Policy(:,z_c)=maxindex(:); + [Vtemp,maxindex]=max(entireRHS_ea_z,[],1); + V(:,ea_c,:,z_c)=shiftdim(Vtemp,1); + Policy(:,ea_c,:,z_c)=shiftdim(maxindex,1); end end end