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/HeterogeneousAgent/FHorz/HeteroAgentStationaryEqm_Case1_FHorz.m b/HeterogeneousAgent/FHorz/HeteroAgentStationaryEqm_Case1_FHorz.m index a9b9147e..f939ac81 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 override). % 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 8a976d07..91a96eb2 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m @@ -422,7 +422,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 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 5d9bbec7..7c3540df 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,7 @@ 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. +AggVars_ConditionalOnPType=zeros(PTypeStructure.numFnsToEvaluate,PTypeStructure.N_i); % Create AggVars conditional on ptype. for ii=1:PTypeStructure.N_i @@ -76,17 +76,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 diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m index 2707a19e..80755534 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 @@ -18,7 +48,7 @@ %% -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 @@ -56,17 +86,36 @@ 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 + + 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=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 @@ -132,32 +181,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:length(customstatnames) - fprintf(heteroagentoptions.verboseaccuracy1,customstatnames{ii},CustomStats.(customstatnames{ii})) + 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 diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m index d157c7e9..3df6b246 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m @@ -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 @@ -405,12 +405,26 @@ if isstruct(z_grid) PTypeStructure.(iistr).z_grid=z_grid.(iistr); 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); 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); diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m index a3a02d5f..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') @@ -60,6 +85,10 @@ 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.(PTypeStructure.(iistr).FnsToEvaluate{kk})=AggVars_ii(jj); end end @@ -71,12 +100,13 @@ 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 % 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/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m index c9923056..8543ca9d 100644 --- a/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m +++ b/HeterogeneousAgent/MixHorz/HeteroAgentStationaryEqm_MixHorz_PType.m @@ -420,10 +420,11 @@ 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; PTypeStructure.(iistr).Parameters=Parameters; FullParamNames=fieldnames(Parameters); % all the different parameters @@ -445,6 +446,7 @@ end end + %% Set up exogenous shock grids now (so they can then just be reused every time) if isstruct(z_grid) @@ -472,32 +474,6 @@ 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}).(iistr); - 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; if isfield(PTypeStructure.(iistr).vfoptions,'ExogShockFn') @@ -530,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=[]; @@ -599,7 +589,7 @@ %% jequaloneDist and AgeWeightsParamNames if isfinite(PTypeStructure.(iistr).N_j) % FHorz if isstruct(jequaloneDist) - if isfield(jequaloneDist,PTypeStructure.Names_i{ii}) + 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 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/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/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') diff --git a/StationaryDist/MixHorz/StationaryDist_MixHorz_PType.m b/StationaryDist/MixHorz/StationaryDist_MixHorz_PType.m new file mode 100644 index 00000000..e6cb4887 --- /dev/null +++ b/StationaryDist/MixHorz/StationaryDist_MixHorz_PType.m @@ -0,0 +1,253 @@ +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. +% +% 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_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 + 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/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m b/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m index 340a0960..13481914 100644 --- a/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m +++ b/StationaryDist/TransPath/AgentDistOnTransPath_InfHorz.m @@ -173,8 +173,9 @@ 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_2D,N_a,N_z,pi_z_sparse); - AgentDist=AgentDist_InfHorz_TPath_SingleStep_nProbs_raw(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.m b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz.m index 03ecfbe8..39bfeab6 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 @@ -450,8 +458,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 +885,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/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m index 13692688..2e9ab72b 100644 --- a/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m +++ b/StationaryDist/TransPathMixHorz/AgentDistOnTransPath_MixHorz_PType.m @@ -120,6 +120,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 @@ -183,7 +191,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,Parameters_temp, simoptions_temp); end % Note: T cannot depend on ptype, nor can PricePath depend on ptype 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/SubCodes/ExoShocks/ExogShockSetup_FHorz_TPath.m b/SubCodes/ExoShocks/ExogShockSetup_FHorz_TPath.m index ee22c562..09851658 100644 --- a/SubCodes/ExoShocks/ExogShockSetup_FHorz_TPath.m +++ b/SubCodes/ExoShocks/ExogShockSetup_FHorz_TPath.m @@ -809,7 +809,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_Disc_fastOLGe: (j,1,N_e,l_e) diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m index c3189e5a..4a2dad4c 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType.m @@ -120,15 +120,60 @@ %% 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,'verbose') + vfoptions.verbose=0; +end +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.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 +end if ~isfield(vfoptions,'lowmemory') - vfoptions.lowmemory=0; + 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 %% 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 + if ii==N_i + error(['Failed to find parameter ', AgeWeightsParamNames.(Names_i{ii}){1}]) + end + 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. @@ -145,11 +190,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 @@ -205,12 +250,15 @@ 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') 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 @@ -261,7 +309,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); @@ -292,7 +340,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') @@ -310,6 +358,7 @@ 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 @@ -336,12 +385,15 @@ % 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') + if isstruct(pi_z) 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 occur 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); @@ -356,7 +408,7 @@ 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}) + 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. @@ -385,7 +437,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).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; + end l_z=length(PTypeStructure.(iistr).n_z); if PTypeStructure.(iistr).N_z==0 l_z=0; @@ -399,13 +457,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; @@ -418,7 +471,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}).(iistr); % % Figure out FnsToEvaluateParamNames @@ -435,7 +488,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 @@ -462,120 +515,108 @@ % 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 [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 + 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]); + 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]); + 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]); + 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]); + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z,N_e,N_j_temp]); end end - else + else % transpathoptions.fastOLG==1 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 - % 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); 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 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 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) + % 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,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]) + 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',Names_i{ii}) + fprintf('Following error applies to agent permanent type: %s \n',iistr) 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]) + 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]) + 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 - % 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 - 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 @@ -611,12 +652,55 @@ 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 + 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; + 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 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 @@ -700,6 +784,7 @@ fprintf('Completed setup, beginning transition computation \n') end + %% Check if using _tminus1 and/or _tplus1 variables. [tplus1priceNames,tminus1priceNames,tminus1AggVarsNames,tminus1paramNames,tplus1pricePathkk,use_tplus1price,use_tminus1price,use_tminus1params,use_tminus1AggVars]=inputsFindtplus1tminus1(FnsToEvaluate,GeneralEqmEqns,PricePathNames,ParamPathNames,PTypeStructure.Names_i,transpathoptions); @@ -755,7 +840,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, use_stockvars, tminus1priceNames, tminus1paramNames, tplus1priceNames, tminus1AggVarsNames, stockvarsNames, stockvarsInPricePathNames, transpathoptions, PTypeStructure); diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m index 80d00db9..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 @@ -123,6 +123,7 @@ % 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); @@ -218,7 +219,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); @@ -228,10 +231,22 @@ %% 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 + 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); 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; @@ -248,7 +263,7 @@ end - AggVarsFullPath(PTypeStructure.(iistr).WhichFnsForCurrentPType,:,ii)=AggVarsPath_ii; + AggVarsFullPath(logical(PTypeStructure.(iistr).WhichFnsForCurrentPType),:,ii)=AggVarsPath_ii; end % done loop over ii @@ -261,7 +276,23 @@ %% 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; @@ -356,7 +387,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/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step2_AdjustPolicy.m index 29e1a04c..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 @@ -34,7 +34,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); @@ -47,7 +52,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,:); @@ -137,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); @@ -153,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 @@ -161,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)+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 @@ -201,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); @@ -217,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 @@ -225,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)+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 @@ -265,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); @@ -281,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 @@ -289,23 +300,19 @@ 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); 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 @@ -329,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); @@ -345,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 @@ -353,23 +363,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_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)+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=repmat(PolicyProbsPath,1,2,1).*a2primeProbsPath; else PolicyProbsPath=a2primeProbsPath; end @@ -409,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 @@ -454,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 @@ -499,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 @@ -544,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/SubSteps/TransitionPath_FHorz_substeps_Step4tt_AggVars.m b/TransitionPaths/FHorz/SubSteps/TransitionPath_FHorz_substeps_Step4tt_AggVars.m index 0eb689e8..74769ef6 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/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/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/ExpAsset/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_ExpAsset_e.m index 05ab213a..c2053d62 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 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..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 @@ -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,9 +98,10 @@ 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,:); + 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 @@ -120,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,:); - DiscountedEV_z=DiscountedEV(:,:,:,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 @@ -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_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_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,:); + + 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_ea_ze=ReturnMatrix_ea_ze+DiscountedEV_ea_z; + + % Calc the max and it's index + [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 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..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 @@ -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 @@ -98,8 +98,8 @@ Policy=zeros(N_a*N_j,N_z,'gpuArray'); for z_c=1:N_z - z_val=z_gridvals_J(:,z_c,:); - DiscountedEV_z=DiscountedEV(:,:,:,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 @@ -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_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_ea_z=DiscountFactorParamsVec.*repelem(EV(:,ea_c,:,z_c),N_d1,N_a1,1,1); + + 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_ea_z,[],1); + V(:,ea_c,:,z_c)=shiftdim(Vtemp,1); + Policy(:,ea_c,:,z_c)=shiftdim(maxindex,1); + 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/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 afc61d7b..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 @@ -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); @@ -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_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; + 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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=0 @@ -136,15 +153,36 @@ ReturnMatrix_ze=CreateReturnFnMatrix_ExpAsset_Disc_e(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); % Level=0, Refine=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_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; + + %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 28434197..6a23d674 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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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 1bd14ac5..5cfdf77d 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 @@ -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 probabilities) 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_ExpAsset_Disc(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); % Level=0, Refine=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_ExpAsset_Disc(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); % Level=0, Refine=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 e44d5452..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 @@ -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); @@ -13,7 +13,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),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -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_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 @@ -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_ExpAsset_Disc(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); % Level=0, Refine=0 % (d,aprime,a,z) @@ -99,8 +115,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_ExpAsset_Disc(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); % Level=0, Refine=0 entireRHS_z=ReturnMatrix_z+DiscountedEV_z; @@ -110,6 +125,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_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 + + 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_GI1_e_raw.m b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/fastOLG/DivideConquer/GridInterpLayer/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_DC1_GI1_e_raw.m index 21efc2aa..1561aa05 100644 --- a/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/fastOLG/DivideConquer/GridInterpLayer/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_DC1_GI1_e_raw.m +++ b/TransitionPaths/FHorz/subcodes/ValueFnSingleStep/fastOLG/DivideConquer/GridInterpLayer/ValueFnIter_FHorz_TPath_SingleStep_fastOLG_DC1_GI1_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 @@ -68,19 +69,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_fastOLG_Disc_DC1_e(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) @@ -109,6 +104,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 @@ -127,9 +126,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 @@ -183,14 +189,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/InfHorz/PType/TransitionPath_InfHorz_PType.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m index 40f5461c..d3ff6379 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType.m @@ -324,6 +324,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.(iistr); @@ -421,6 +424,7 @@ 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); diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m index 4d45be69..24265520 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/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m b/TransitionPaths/InfHorz/SubSteps/TransitionPath_InfHorz_substeps_Step1_ValueFnIter.m index fe3d18a0..24036086 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 3614f66d..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,19 @@ 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] PolicyaprimezPath=reshape(PolicyaprimezPath,[N_a*N_z,1,T-1]); % reinterpret this as lower grid index @@ -157,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 @@ -182,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 @@ -211,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 @@ -230,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 @@ -257,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 @@ -300,6 +298,4 @@ - - end diff --git a/TransitionPaths/InfHorz/TransitionPath_InfHorz.m b/TransitionPaths/InfHorz/TransitionPath_InfHorz.m index 71c62f2e..24123f8f 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/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 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 4bad4a9f..f739f606 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 @@ -116,7 +116,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 165e6e90..33acc883 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 d2ind=repelem((1:1:N_d2)',N_d1,1); % d2 part of each (d1,d2) combo, [N_d1*N_d2,1] @@ -113,7 +113,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 c2efdcb7..d32bf9f1 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) @@ -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 dcb2805c..eb7b2ecf 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) @@ -64,20 +69,41 @@ elseif vfoptions.lowmemory==1 for z_c=1:N_z - ReturnMatrix_z=CreateReturnFnMatrix_ExpAsset_Disc(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); % Level=0, Refine=0 + z_val=z_gridvals(z_c,:); + ReturnMatrix_z=CreateReturnFnMatrix_ExpAsset_Disc(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); % Level=0, Refine=0 % 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 probabilities) EV_z=sum(EV_z,3); - entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*repelem(EV_z,N_d1,N_a1,1); + 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_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 + + entireRHS_ea_z=ReturnMatrix_ea_z+DiscountFactorParamsVec*repelem(EV_z,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)=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/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m index 3f91b368..f4722518 100644 --- a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m +++ b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType.m @@ -157,10 +157,24 @@ 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 @@ -190,11 +204,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 @@ -245,7 +259,6 @@ % 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 @@ -363,91 +376,31 @@ 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); + 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_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={}; - 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 occur with or without the existence of vfoptions, as long as there is no vfoptions.agedependentgrids) @@ -455,9 +408,16 @@ PTypeStructure.(iistr).pi_z=pi_z; end + % If using any non-standard endogenous states, setup for those + if isfinite(PTypeStructure.(iistr).N_j) + [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); + else + [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); + end + 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). @@ -469,8 +429,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}); @@ -494,7 +454,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) @@ -504,8 +464,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; @@ -517,12 +477,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; @@ -531,17 +487,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}).(iistr)); % 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) @@ -556,12 +510,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); @@ -588,6 +541,7 @@ [PTypeStructure.(iistr).z_gridvals, PTypeStructure.(iistr).pi_z, PTypeStructure.(iistr).pi_z_sparse, PTypeStructure.(iistr).e_gridvals, PTypeStructure.(iistr).pi_e, PTypeStructure.(iistr).pi_e_sparse, PTypeStructure.(iistr).ze_gridvals, transpathoptions, PTypeStructure.(iistr).simoptions]=ExogShockSetup_InfHorz_TPath(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).Parameters,PricePathNames,ParamPathNames,transpathoptions,PTypeStructure.(iistr).simoptions,4); end + %% If using any non-standard endogenous states, setup for those if isfinite(PTypeStructure.(iistr).N_j) [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); @@ -595,13 +549,11 @@ [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); end + %% Organise V_final and AgentDist_initial - % Reshape V_final - N_j_temp=PTypeStructure.(iistr).N_j; - if ~isfinite(N_j_temp) - % 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 V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_j_temp]); @@ -631,12 +583,10 @@ 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 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 N_z==0 if N_e==0 AgentDist_init=reshape(AgentDist_init,[N_a,N_j_temp]); % if simoptions.fastOLG==0 @@ -673,7 +623,7 @@ % 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]) @@ -683,7 +633,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 @@ -750,7 +700,7 @@ 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 @@ -759,11 +709,16 @@ 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 + else + % Reshape V_final + % If no z, then N_z=1 here + V_final.(iistr)=reshape(V_final.(iistr),[N_a,N_z]); + + % Reshape AgentDist_initial if N_z==0 - AgentDist_init=reshape(AgentDist_init,[N_a,1]); + AgentDist_init=reshape(AgentDist_initial.(iistr),[N_a,1]); else - AgentDist_init=reshape(AgentDist_init,[N_a*N_z,1]); + AgentDist_init=reshape(AgentDist_initial.(iistr),[N_a*N_z,1]); end end @@ -853,6 +808,7 @@ %% 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 [tplus1priceNames,tminus1priceNames,tminus1AggVarsNames,tminus1paramNames,tplus1pricePathkk,use_tplus1price,use_tminus1price,use_tminus1params,use_tminus1AggVars]=inputsFindtplus1tminus1(FnsToEvaluate,GeneralEqmEqns,PricePathNames,ParamPathNames,Names_i,transpathoptions); if isstruct(tminus1AggVarsNames) @@ -957,7 +913,6 @@ GEcondnPath.(GEeqnNames{gg})=GEcondnPathmatrix(:,gg)'; end - if nargout==1 varargout={PricePathStruct}; elseif nargout==2 diff --git a/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m b/TransitionPaths/MixHorz/TransitionPath_MixHorz_PType_shooting.m index ce62bf5a..2d77f246 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 @@ -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_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_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,15 +253,14 @@ 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 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); + 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) 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/FHorz/DivideConquer/ValueFnIter_FHorz_DC1_raw.m b/ValueFnIter/FHorz/DivideConquer/ValueFnIter_FHorz_DC1_raw.m index 9cd82416..c08f408e 100644 --- a/ValueFnIter/FHorz/DivideConquer/ValueFnIter_FHorz_DC1_raw.m +++ b/ValueFnIter/FHorz/DivideConquer/ValueFnIter_FHorz_DC1_raw.m @@ -272,21 +272,21 @@ ReturnMatrix_ii=CreateReturnFnMatrix_Disc_DC1(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_Disc_DC1(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 diff --git a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m index c3608740..0b9d2c33 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_e_raw.m @@ -15,10 +15,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'); 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'); +elseif vfoptions.lowmemory==3 + special_n_e=ones(1,length(n_e),'gpuArray'); + special_n_z=ones(1,length(n_z),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -54,6 +57,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_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; + 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); @@ -84,13 +102,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_ExpAsset_Disc_e(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); % Level=0, Refine=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); @@ -99,7 +115,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_ExpAsset_Disc_e(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); % Level=0, Refine=0 @@ -120,15 +136,35 @@ e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_ze=CreateReturnFnMatrix_ExpAsset_Disc_e(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); % Level=0, Refine=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); 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); + for e_c=1:N_e + e_val=e_gridvals_J(e_c,:,N_j); + 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_z=ReturnMatrix_ea_ze+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,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 @@ -171,13 +207,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_ExpAsset_Disc_e(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); % Level=0, Refine=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); @@ -186,7 +220,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_ExpAsset_Disc_e(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); % Level=0, Refine=0 @@ -217,6 +251,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_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_z=ReturnMatrix_ea_ze+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,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/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_noa1_e_raw.m index 06c98a08..72906c89 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 @@ -93,12 +95,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_e(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 +111,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_e(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); @@ -175,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_e(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); @@ -191,14 +192,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_e(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 a91989dc..3fb34969 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 terms 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(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(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 5c5648cc..16d75a83 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 @@ -64,13 +68,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(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 +83,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(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_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_e_raw.m index 79327df8..59306400 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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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 4e65584b..b013a1a7 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 @@ -90,12 +92,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_e(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); @@ -173,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_e(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); @@ -186,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_noz_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_noz_e_raw.m index 0a5aadec..778b3de8 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 terms 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(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(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_noa1_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noa1_raw.m index 14f3b32b..46ca3132 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_noz_e_raw.m b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_nod1_noz_e_raw.m index b3a19e36..aa64694e 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 @@ -61,11 +61,13 @@ % 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,u,zprime) % Already applied the probabilities from interpolating onto grid + + DiscountedEV=DiscountFactorParamsVec*repelem(EV,1,N_a1,1); if vfoptions.lowmemory==0 ReturnMatrix=CreateReturnFnMatrix_ExpAsset_Disc(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); % Level=0, Refine=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 +79,7 @@ e_val=e_gridvals_J(e_c,:,N_j); ReturnMatrix_e=CreateReturnFnMatrix_ExpAsset_Disc(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); % Level=0, Refine=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 57bd9bb4..8bd825e1 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 @@ -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); @@ -68,12 +85,10 @@ EV(isnan(EV))=0; %multiplications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilities) 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_ExpAsset_Disc(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); % Level=0, Refine=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 +100,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_ExpAsset_Disc(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); % Level=0, Refine=0 @@ -96,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 @@ -136,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 probabilities) 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_ExpAsset_Disc(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); % Level=0, Refine=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); @@ -153,7 +184,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_ExpAsset_Disc(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); % Level=0, Refine=0 @@ -164,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 3651c99e..13b3c272 100644 --- a/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m +++ b/ValueFnIter/FHorz/ExperienceAsset/ValueFnIter_FHorz_ExpAsset_raw.m @@ -14,7 +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),'gpuArray'); + special_n_ea=ones(1,length(n_a2),'gpuArray'); end %% j=N_j @@ -38,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); @@ -68,13 +86,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_ExpAsset_Disc(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); % Level=0, Refine=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); @@ -85,11 +101,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_ExpAsset_Disc(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); % Level=0, Refine=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); @@ -97,6 +111,19 @@ 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_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 + 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,N_j)=Vtemp; + Policy(1+(ea_c-1)*N_a1:ea_c*N_a1,z_c,N_j)=maxindex; + end + end end end @@ -138,13 +165,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_ExpAsset_Disc(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); % Level=0, Refine=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); @@ -155,7 +180,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_ExpAsset_Disc(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); % Level=0, Refine=0 @@ -166,6 +191,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_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 + + 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/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_e_raw.m index a84cd782..8e5cbcc9 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_e_raw.m @@ -29,7 +29,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 @@ -141,7 +141,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 @@ -337,7 +337,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_c); @@ -553,7 +553,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/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_e_raw.m index 23cc0d78..ee4a30e1 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/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 @@ -138,7 +138,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 @@ -335,7 +335,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); @@ -550,7 +550,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/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_raw.m index 0e7bac00..bae95868 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_nod1_raw.m @@ -79,7 +79,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 @@ -216,7 +216,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); @@ -374,7 +374,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/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_raw.m index 2a8ded02..64ed869d 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquer/ValueFnIter_FHorz_ExpAssetu_DC1_raw.m @@ -85,7 +85,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); @@ -219,7 +219,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); @@ -373,7 +373,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/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_e_raw.m index c66b4c1c..c406f77c 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_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 @@ -336,7 +336,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); @@ -546,7 +546,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/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_nod1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_nod1_e_raw.m index 6e00fad3..c2c66c25 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_nod1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_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 @@ -148,7 +148,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 @@ -337,7 +337,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); @@ -544,7 +544,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/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_nod1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_nod1_raw.m index 04cf32c8..e46be5bc 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_nod1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_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 @@ -93,7 +93,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 @@ -222,7 +222,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); @@ -374,7 +374,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/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_raw.m index 93ec5e20..a1f5b34f 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/DivideConquerGridInterpLayer/ValueFnIter_FHorz_ExpAssetu_DC1_GI1_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 @@ -223,7 +223,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); @@ -380,7 +380,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/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_e_raw.m index 8afe5615..2cb0782c 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_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_GI1_nod1_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_nod1_e_raw.m index 6fb42f18..56e99b62 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_nod1_e_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_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_GI1_nod1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_nod1_raw.m index ee79e71c..5918823c 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_nod1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_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_ExpAsset_Disc(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,0); % Level=1, Refine=0 @@ -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_GI1_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_raw.m index a14a7a4d..fab85b8d 100644 --- a/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_raw.m +++ b/ValueFnIter/FHorz/ExperienceAssetu/GridInterpLayer/ValueFnIter_FHorz_ExpAssetu_GI1_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_ExpAsset_Disc(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,0); % Level=1, Refine=0 @@ -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/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_e_raw.m b/ValueFnIter/FHorz/ExperienceAssetu/ValueFnIter_FHorz_ExpAssetu_e_raw.m index cc71bb78..e5e28ca4 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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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 964c7bee..1450a1db 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_e(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_e(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 19799c8e..f1912944 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(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(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(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(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(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 101a1032..3e9b1468 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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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_ExpAsset_Disc_e(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); % Level=0, Refine=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 3f9a300b..874ba517 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 3fd520a2..61ecf4ba 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 77f8acff..2e7fb286 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_ExpAsset_Disc(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); % Level=0, Refine=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_ExpAsset_Disc(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); % Level=0, Refine=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 dad5db74..126aef93 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_ExpAsset_Disc(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); % Level=0, Refine=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_ExpAsset_Disc(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); % Level=0, Refine=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_ExpAsset_Disc(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); % Level=0, Refine=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_ExpAsset_Disc(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); % Level=0, Refine=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 diff --git a/ValueFnIter/FHorz/ValueFnIter_Case1_FHorz.m b/ValueFnIter/FHorz/ValueFnIter_Case1_FHorz.m index 9bb022e6..7279413d 100644 --- a/ValueFnIter/FHorz/ValueFnIter_Case1_FHorz.m +++ b/ValueFnIter/FHorz/ValueFnIter_Case1_FHorz.m @@ -341,7 +341,7 @@ if prod(vfoptions.n_semiz)>0 [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 prod(vfoptions.n_semiz)>0 diff --git a/ValueFnIter/FHorz/ValueFnIter_FHorz_e_raw.m b/ValueFnIter/FHorz/ValueFnIter_FHorz_e_raw.m index 7ba9318b..e79643aa 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 probabilities) 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_Disc_e(ReturnFn, n_d, n_a, n_z, n_e, d_gridvals, a_grid, z_gridvals_J(:,:,jj), e_gridvals_J(:,:,jj), ReturnFnParamsVec,0); % (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 @@ -163,7 +162,8 @@ e_val=e_gridvals_J(e_c,:,jj); ReturnMatrix_e=CreateReturnFnMatrix_Disc_e(ReturnFn, n_d, n_a, n_z, special_n_e, d_gridvals, a_grid, z_gridvals_J(:,:,jj), e_val, ReturnFnParamsVec,0); % (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); diff --git a/ValueFnIter/InfHorz/ExpAsset/GridInterpLayer/ValueFnIter_InfHorz_ExpAsset_Refine_GI1_raw.m b/ValueFnIter/InfHorz/ExpAsset/GridInterpLayer/ValueFnIter_InfHorz_ExpAsset_Refine_GI1_raw.m index 624c9746..1040b23e 100644 --- a/ValueFnIter/InfHorz/ExpAsset/GridInterpLayer/ValueFnIter_InfHorz_ExpAsset_Refine_GI1_raw.m +++ b/ValueFnIter/InfHorz/ExpAsset/GridInterpLayer/ValueFnIter_InfHorz_ExpAsset_Refine_GI1_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'); diff --git a/ValueFnIter/InfHorz/ExpAsset/ValueFnIter_InfHorz_ExpAsset_Refine_raw.m b/ValueFnIter/InfHorz/ExpAsset/ValueFnIter_InfHorz_ExpAsset_Refine_raw.m index 1853a357..c1dcabe6 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/ValueFnIter/InfHorz/ValueFnIter_InfHorz_HowardGreedy_nod_raw.m b/ValueFnIter/InfHorz/ValueFnIter_InfHorz_HowardGreedy_nod_raw.m index 27e6ceb9..772cd224 100644 --- a/ValueFnIter/InfHorz/ValueFnIter_InfHorz_HowardGreedy_nod_raw.m +++ b/ValueFnIter/InfHorz/ValueFnIter_InfHorz_HowardGreedy_nod_raw.m @@ -56,9 +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 -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 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