diff --git a/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m b/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m index 01ce0348..76ff9dcb 100644 --- a/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m +++ b/EvaluateFnOnAgentDist/FHorz/LifeCycleProfiles_FHorz_Case1.m @@ -467,7 +467,7 @@ for jj=j1:jend % Includes check for cases in which no parameters are actually required FnToEvaluateParamsCell=CreateCellFromParams(Parameters,FnsToEvaluateParamNames(ff).Names,jj); - Values(:,:,jj-j1+1)=EvalFnOnAgentDist_Grid(FnsToEvaluate{ff}, FnToEvaluateParamsCell,PolicyValuesPermuteJ(:,:,:,jj),l_daprime,n_a,n_z,a_gridvals,z_gridvals_J(:,:,jj)); + Values(:,:,jj-j1+1)=EvalFnOnAgentDist_Grid(FnsToEvaluate{ff}, FnToEvaluateParamsCell,PolicyValuesPermuteJ(:,:,:,jj),l_daprime,n_a,n_z,a_gridvals,z_gridvals_J(:,:,jj)); end Values=reshape(Values,[N_a*N_z*(jend-j1+1),1]); diff --git a/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m b/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m index 828ba34d..ce4684be 100644 --- a/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m +++ b/EvaluateFnOnAgentDist/FHorz/PType/EvalFnOnAgentDist_AllStats_FHorz_Case1_PType.m @@ -411,7 +411,13 @@ if FnsAndPTypeIndicator_ii(ff)==1 % If this function is relevant to this ptype % Get parameter names for current FnsToEvaluate functions - tempnames=getAnonymousFnInputNames(FnsToEvaluate.(FnsToEvalNames{ff})); + % Get parameter names for current FnsToEvaluate functions + if isstruct(FnsToEvaluate.(FnsToEvalNames{ff})) + tempfn=FnsToEvaluate.(FnsToEvalNames{ff}).(Names_i{ii}); + else + tempfn=FnsToEvaluate.(FnsToEvalNames{ff}); + end + tempnames=getAnonymousFnInputNames(tempfn); if length(tempnames)>(l_daprime_temp+l_a_temp+l_z_temp) FnsToEvaluateParamNames={tempnames{l_daprime_temp+l_a_temp+l_z_temp+1:end}}; % the first inputs will always be (d,aprime,a,z) else @@ -425,9 +431,9 @@ %% We have set up the current PType, now do some calculations for it. simoptions_temp.keepoutputasmatrix=1; - ValuesOnGrid_ii=EvalFnOnAgentDist_Grid_J(FnsToEvaluate.(FnsToEvalNames{ff}),CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_J_temp); + ValuesOnGrid_ii=EvalFnOnAgentDist_Grid_J(tempfn,CellOverAgeOfParamValues,PolicyValuesPermute_temp,l_daprime_temp,n_a_temp,n_z_temp,a_gridvals_temp,z_gridvals_J_temp); ValuesOnGrid_ii=reshape(ValuesOnGrid_ii,[N_a_temp*N_z_temp*N_j_temp,1]); - + % StationaryDist_ii=reshape(StationaryDist.(Names_i{ii}),[N_a_temp*N_z_temp*N_j_temp,1]); % Note: does not impose *StationaryDist.ptweights(ii) % Eliminate all the zero-weighted points (this doesn't really save runtime for the exact calculation and often can increase it, but diff --git a/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m b/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m index e280a1f6..f4bfdeef 100644 --- a/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m +++ b/EvaluateFnOnAgentDist/TransPath/EvalFnOnTransPath_AggVars_InfHorz.m @@ -1,6 +1,17 @@ function AggVarsPath=EvalFnOnTransPath_AggVars_InfHorz(FnsToEvaluate,AgentDistPath,PolicyPath,PricePath,ParamPath, Parameters, T, n_d, n_a, n_z, d_grid, a_grid,z_grid,simoptions) % AggVarsPath is T periods long (periods 0 (before the reforms are announced) & T are the initial and final values). +%% Check which transpathoptions have been used, set all others to defaults +if exist('transpathoptions','var')==0 + disp('No transpathoptions given, using defaults') + % If transpathoptions is not given, just use all the defaults + transpathoptions.verbose=0; +else + % Check transpathoptions for missing fields, if there are some fill them with the defaults + if ~isfield(transpathoptions,'verbose') + transpathoptions.verbose=0; + end +end if ~exist('simoptions','var') % If simoptions is not given, just use all the defaults simoptions.experienceasset=0; diff --git a/HeterogeneousAgent/FHorz/HeteroAgentStationaryEqm_Case1_FHorz.m b/HeterogeneousAgent/FHorz/HeteroAgentStationaryEqm_Case1_FHorz.m index da7c807d..08bbb163 100644 --- a/HeterogeneousAgent/FHorz/HeteroAgentStationaryEqm_Case1_FHorz.m +++ b/HeterogeneousAgent/FHorz/HeteroAgentStationaryEqm_Case1_FHorz.m @@ -133,6 +133,7 @@ heteroagentoptions.useCustomModelStats=1; % Stash some of the inputs so they can be passed to CustomModelStats later (only things we otherwise overright). % So that user gets exactly what they input, not any internally reworked things + % In this (non-PType) context, these assignments are arrays or other simple types heteroagentoptions.CustomModelStatsInputs.z_grid=z_grid; heteroagentoptions.CustomModelStatsInputs.pi_z=pi_z; % Need the following two as otherwise they would contain alreadygridvals=1 diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m index 4fa198c2..c1627bd5 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType.m @@ -165,8 +165,9 @@ heteroagentoptions.useCustomModelStats=0; if isfield(heteroagentoptions,'CustomModelStats') heteroagentoptions.useCustomModelStats=1; - % Stash some of the inputs so they can be passed to CustomModelStats later (only things we otherwise overright). + % Stash some of the inputs so they can be passed to CustomModelStats later (only things we otherwise overwrite). % So that user gets exactly what they input, not any internally reworked things + % In this (PType) context, these assignments are typically structs that each have all the PType fields within them heteroagentoptions.CustomModelStatsInputs.FnsToEvaluate=FnsToEvaluate; heteroagentoptions.CustomModelStatsInputs.n_d=n_d; heteroagentoptions.CustomModelStatsInputs.n_a=n_a; @@ -490,45 +491,27 @@ end % Switch over to joint-grids - if isfinite(PTypeStructure.(iistr).N_j) % FHorz - % If z (and e) are not determined in GE, then compute z_gridvals_J and pi_z_J now (and e_gridvals_J and pi_e_J) - if heteroagentoptions.gridsinGE(ii)==0 - [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).vfoptions]=ExogShockSetup_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,3); - % Note: these are actually z_gridvals_J and pi_z_J - PTypeStructure.(iistr).simoptions.e_gridvals_J=PTypeStructure.(iistr).vfoptions.e_gridvals_J; % Note, will be [] if no e - PTypeStructure.(iistr).simoptions.pi_e_J=PTypeStructure.(iistr).vfoptions.pi_e_J; % Note, will be [] if no e - else - % % Create placeholders, as these will need to be created in general eqm since they depend on General eqm parameters - % PTypeStructure.(iistr).z_gridvals_J=[]; - % PTypeStructure.(iistr).pi_z_J=[]; - end - PTypeStructure.(iistr)=rmfield(PTypeStructure.(iistr),'z_grid'); % Should not be used, as now have z_gridvals_J - PTypeStructure.(iistr)=rmfield(PTypeStructure.(iistr),'pi_z'); % Should not be used, as now have pi_z_J - if isfield(PTypeStructure.(iistr).simoptions,'ExogShockFn') % Note: ExogShockSetup_FHorz() removed ExogShockFn from vfoptions but not from simoptions - if heteroagentoptions.useCustomModelStats==1 - heteroagentoptions.CustomModelStatsInputs.z_grid=PTypeStructure.(iistr).z_gridvals_J; - heteroagentoptions.CustomModelStatsInputs.pi_z=PTypeStructure.(iistr).pi_z_J; - end - PTypeStructure.(iistr).simoptions=rmfield(simoptions,'ExogShockFn'); - end - else % InfHorz - if heteroagentoptions.gridsinGE(ii)==0 - [PTypeStructure.(iistr).z_gridvals, PTypeStructure.(iistr).pi_z, PTypeStructure.(iistr).vfoptions]=ExogShockSetup_InfHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,3); - PTypeStructure.(iistr).simoptions.e_gridvals=PTypeStructure.(iistr).vfoptions.e_gridvals; % Note, will be [] if no e - PTypeStructure.(iistr).simoptions.pi_e=PTypeStructure.(iistr).vfoptions.pi_e; % Note, will be [] if no e - else - % % Create placeholders, as these will need to be created in general eqm since they depend on General eqm parameters - % PTypeStructure.(iistr).z_gridvals=[]; - % PTypeStructure.(iistr).pi_z=[]; - end - if isfield(PTypeStructure.(iistr).simoptions,'ExogShockFn') % Note: ExogShockSetup_InfHorz() removed ExogShockFn from vfoptions but not from simoptions - if heteroagentoptions.useCustomModelStats==1 - heteroagentoptions.CustomModelStatsInputs.z_grid=PTypeStructure.(iistr).z_gridvals_J; - heteroagentoptions.CustomModelStatsInputs.pi_z=PTypeStructure.(iistr).pi_z_J; - end - PTypeStructure.(iistr).simoptions=rmfield(PTypeStructure.(iistr).simoptions,'ExogShockFn'); + % If z (and e) are not determined in GE, then compute z_gridvals_J and pi_z_J now (and e_gridvals_J and pi_e_J) + if heteroagentoptions.gridsinGE(ii)==0 + [PTypeStructure.(iistr).z_gridvals_J, PTypeStructure.(iistr).pi_z_J, PTypeStructure.(iistr).vfoptions]=ExogShockSetup_FHorz(PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).z_grid,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,3); + % Note: these are actually z_gridvals_J and pi_z_J + PTypeStructure.(iistr).simoptions.e_gridvals_J=PTypeStructure.(iistr).vfoptions.e_gridvals_J; % Note, will be [] if no e + PTypeStructure.(iistr).simoptions.pi_e_J=PTypeStructure.(iistr).vfoptions.pi_e_J; % Note, will be [] if no e + else + % % Create placeholders, as these will need to be created in general eqm since they depend on General eqm parameters + % PTypeStructure.(iistr).z_gridvals_J=[]; + % PTypeStructure.(iistr).pi_z_J=[]; + end + PTypeStructure.(iistr)=rmfield(PTypeStructure.(iistr),'z_grid'); % Should not be used, as now have z_gridvals_J + PTypeStructure.(iistr)=rmfield(PTypeStructure.(iistr),'pi_z'); % Should not be used, as now have pi_z_J + if isfield(PTypeStructure.(iistr).simoptions,'ExogShockFn') % Note: ExogShockSetup_FHorz() removed ExogShockFn from vfoptions but not from simoptions + if heteroagentoptions.useCustomModelStats==1 + heteroagentoptions.CustomModelStatsInputs.z_grid=PTypeStructure.(iistr).z_gridvals_J; + heteroagentoptions.CustomModelStatsInputs.pi_z=PTypeStructure.(iistr).pi_z_J; end + PTypeStructure.(iistr).simoptions=rmfield(simoptions,'ExogShockFn'); end + % Regardless of whether they are done here of in _subfn, they will be precomputed by the time we get to the value fn, staty dist, etc. So PTypeStructure.(iistr).vfoptions.alreadygridvals=1; PTypeStructure.(iistr).simoptions.alreadygridvals=1; @@ -557,13 +540,10 @@ end end - if isfinite(PTypeStructure.(iistr).N_j) % FHorz - PTypeStructure.(iistr).vfoptions=SemiExogShockSetup_FHorz(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,2,3); - PTypeStructure.(iistr).simoptions.semiz_gridvals_J=PTypeStructure.(iistr).vfoptions.semiz_gridvals_J; - PTypeStructure.(iistr).simoptions.pi_semiz_J=PTypeStructure.(iistr).vfoptions.pi_semiz_J; - else - error('Semi-exogenous state not yet implemented for InfHorz') - end + PTypeStructure.(iistr).vfoptions=SemiExogShockSetup_FHorz(PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).d_grid,PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).vfoptions,2,3); + PTypeStructure.(iistr).simoptions.semiz_gridvals_J=PTypeStructure.(iistr).vfoptions.semiz_gridvals_J; + PTypeStructure.(iistr).simoptions.pi_semiz_J=PTypeStructure.(iistr).vfoptions.pi_semiz_J; + % Regardless of whether they are done here of in _subfn, they will be precomputed by the time we get to the value fn, staty dist, etc. So PTypeStructure.(iistr).vfoptions.alreadygridvals_semiexo=1; PTypeStructure.(iistr).simoptions.alreadygridvals_semiexo=1; @@ -585,33 +565,26 @@ PTypeStructure.(iistr).ReturnFnParamNames=ReturnFnParamNamesFn(PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_j,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).Parameters); %% jequaloneDist and AgeWeightsParamNames - if isfinite(PTypeStructure.(iistr).N_j) % FHorz - - if isstruct(jequaloneDist) - if isfield(jequaloneDist,PTypeStructure.Names_i{ii}) - if isa(jequaloneDist, 'function_handle') - [PTypeStructure.(iistr).jequaloneDist,~,PTypeStructure.(iistr).Parameters]=jequaloneDist_PType(jequaloneDist.(iistr),PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).simoptions,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_i,PTypeStructure.(iistr).PTypeDistParamNames,0); - else - PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(PTypeStructure.Names_i{ii}); - end + if isstruct(jequaloneDist) + if isfield(jequaloneDist,PTypeStructure.Names_i{ii}) + if isa(jequaloneDist, 'function_handle') + [PTypeStructure.(iistr).jequaloneDist,~,PTypeStructure.(iistr).Parameters]=jequaloneDist_PType(jequaloneDist.(iistr),PTypeStructure.(iistr).Parameters,PTypeStructure.(iistr).simoptions,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).N_i,PTypeStructure.(iistr).PTypeDistParamNames,0); else - if isfinite(PTypeStructure.(iistr).N_j) - error(['You must input jequaloneDist for permanent type ', PTypeStructure.Names_i{ii}, ' \n']) - end + PTypeStructure.(iistr).jequaloneDist=jequaloneDist.(PTypeStructure.Names_i{ii}); end else - PTypeStructure.(iistr).jequaloneDist=jequaloneDist; + error(['You must input jequaloneDist for permanent type ', PTypeStructure.Names_i{ii}, ' \n']) end + else + PTypeStructure.(iistr).jequaloneDist=jequaloneDist; + end - PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames; - if isstruct(AgeWeightParamNames) - if isfield(AgeWeightParamNames,Names_i{ii}) - PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(Names_i{ii}); - else - if isfinite(PTypeStructure.(iistr).N_j) - error(['You must input AgeWeightParamNames for permanent type ', Names_i{ii}, ' \n']) - end - end + PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames; + if isstruct(AgeWeightParamNames) + if isfield(AgeWeightParamNames,Names_i{ii}) + PTypeStructure.(iistr).AgeWeightParamNames=AgeWeightParamNames.(Names_i{ii}); + else + error(['You must input AgeWeightParamNames for permanent type ', Names_i{ii}, ' \n']) end end diff --git a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m index 4837b3df..242f8a93 100644 --- a/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_PType_subfn.m +++ b/HeterogeneousAgent/FHorz/PType/HeteroAgentStationaryEqm_Case1_FHorz_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; % Compute this to show max change per round + 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') @@ -18,7 +43,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 @@ -78,12 +103,12 @@ 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); @@ -172,8 +197,8 @@ 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})) end end fprintf('Current GeneralEqmEqns: \n') diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m index 16335c4a..45679fc9 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType.m @@ -466,7 +466,7 @@ PTypeStructure.(iistr).ReturnFn=ReturnFn; end PTypeStructure.(iistr).ReturnFnParamNames=ReturnFnParamNamesFn(PTypeStructure.(iistr).ReturnFn,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,1,PTypeStructure.(iistr).vfoptions,PTypeStructure.(iistr).Parameters); - + %% FnsToEvaluate % Figure out which functions are actually relevant to the present PType. Only the relevant ones need to be evaluated. % The dependence of FnsToEvaluate and FnsToEvaluateFnParamNames are necessarily the same. @@ -482,6 +482,8 @@ PTypeStructure.(iistr).FnsToEvaluate=FnsToEvaluate_temp; PTypeStructure.(iistr).FnsToEvaluateParamNames=FnsToEvaluateParamNames_temp; PTypeStructure.(iistr).WhichFnsForCurrentPType=WhichFnsForCurrentPType; + % Copied from FHorz/PType version for consistency + PTypeStructure.(iistr).FnsAndPTypeIndicator_ii=FnsAndPTypeIndicator_ii; %% PType masses if isa(PTypeDistParamNames, 'array') diff --git a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m index 68b99909..46827634 100644 --- a/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m +++ b/HeterogeneousAgent/InfHorz/PType/HeteroAgentStationaryEqm_InfHorz_PType_subfn.m @@ -32,6 +32,7 @@ %% +AggVars_ConditionalOnPType=zeros(PTypeStructure.numFnsToEvaluate,PTypeStructure.N_i); % Create AggVars conditional on ptype. AggVars=zeros(PTypeStructure.numFnsToEvaluate,1,'gpuArray'); % numFnsToEvaluate is independent of the ptype for ii=1:PTypeStructure.N_i @@ -55,11 +56,13 @@ StationaryDist_ii=StationaryDist_InfHorz(Policy_ii,PTypeStructure.(iistr).n_d,PTypeStructure.(iistr).n_a,PTypeStructure.(iistr).n_z,PTypeStructure.(iistr).pi_z,PTypeStructure.(iistr).simoptions,PTypeStructure.(iistr).Parameters); % PTypeStructure.(iistr).simoptions.outputasstructure=0; % Want AggVars_ii as matrix to make it easier to add them across the PTypes (is set outside this script) AggVars_ii=EvalFnOnAgentDist_AggVars_InfHorz(StationaryDist_ii, Policy_ii, PTypeStructure.(iistr).FnsToEvaluate, PTypeStructure.(iistr).Parameters, PTypeStructure.(iistr).FnsToEvaluateParamNames, PTypeStructure.(iistr).n_d, PTypeStructure.(iistr).n_a, PTypeStructure.(iistr).n_z, PTypeStructure.(iistr).d_grid, PTypeStructure.(iistr).a_grid, PTypeStructure.(iistr).z_grid, PTypeStructure.(iistr).simoptions); - - for kk=1:PTypeStructure.numFnsToEvaluate - jj=PTypeStructure.(iistr).WhichFnsForCurrentPType(kk); - if jj>0 - AggVars(kk)=AggVars(kk)+PTypeStructure.(iistr).PTypeWeight*AggVars_ii(jj); + AggVars_ConditionalOnPType(PTypeStructure.(iistr).FnsAndPTypeIndicator_ii,ii)=AggVars_ii; + % 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 @@ -69,7 +72,8 @@ StationaryDist.(iistr)=StationaryDist_ii; 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 diff --git a/Optimization/setupGEnewprice3_shooting.m b/Optimization/setupGEnewprice3_shooting.m index 8cd3929a..c3ca2ea7 100644 --- a/Optimization/setupGEnewprice3_shooting.m +++ b/Optimization/setupGEnewprice3_shooting.m @@ -93,13 +93,14 @@ for gg2=1:size(options.GEnewprice3.howtoupdate,1) if strcmp(options.GEnewprice3.howtoupdate{gg2,1},GEeqnNames{gg}) pricename_gg=options.GEnewprice3.howtoupdate{gg2,2}; - end - end - for pp=1:length(GEPriceParamNames) - if strcmp(pricename_gg,GEPriceParamNames{pp}) - if PricePathSizeVec(2,pp)-PricePathSizeVec(1,pp)+1~=N_i - fprintf('Following error relates to GE condition %s and to price %s \n',GEeqnNames{gg},GEPriceParamNames{pp}) - error('You declared a GE condition to depend on permenent type, but the price that relates to it (in options.GEnewprice3.howtoupdate) does not depend on ptype') + for pp=1:length(GEPriceParamNames) + if strcmp(pricename_gg,GEPriceParamNames{pp}) + if PricePathSizeVec(2,pp)-PricePathSizeVec(1,pp)+1~=N_i + fprintf('Following error relates to GE condition %s and to price %s \n',GEeqnNames{gg},GEPriceParamNames{pp}) + error('You declared a GE condition to depend on permenent type, but the price that relates to it (in options.GEnewprice3.howtoupdate) does not depend on ptype') + end + break + end end end end diff --git a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m index d1851e16..4513e910 100644 --- a/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m +++ b/StationaryDist/TransPathFHorz/AgentDistOnTransPath_Case1_FHorz_PType.m @@ -135,7 +135,7 @@ AgeWeightsParamNames_temp=AgeWeightsParamNames; end if isstruct(jequalOneDist) - jequalOneDist_temp=jequalOneDist.(iistr); + jequalOneDist_temp=jequalOneDist.(Names_i{ii}); else jequalOneDist_temp=jequalOneDist; end diff --git a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m index 188a369c..aac267d2 100644 --- a/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m +++ b/TransitionPaths/FHorz/PType/TransitionPath_Case1_FHorz_PType_shooting.m @@ -127,9 +127,10 @@ AggVarsFullPath=zeros(PTypeStructure.numFnsToEvaluate,T-1,N_i); % Does not include period T for ii=1:N_i iistr=PTypeStructure.Names_i{ii}; - + % Following few lines I would normally do outside of the while loop, but have to set them for each ptype % AgentDist=AgentDist_initial.(iistr); + % WARNING: The following would overwrite themselves next iteration % V_final=V_final.(iistr); % AgeWeights_T=AgeWeights_T.(iistr); % jequalOneDist_T=jequalOneDist_T.(iistr); @@ -251,8 +252,24 @@ %% Do the general eqm conditions and create PricePathNew based on these if all(transpathoptions.GEptype==0) - GEcondnPath=zeros(T,length(GEeqnNames)); - + GECondnPath=zeros(T,length(GEeqnNames)); + + % Restore all AggVarNames and tminus1AggVarsNames for GEeqns + warning("do we need this restore here?") + 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; @@ -330,7 +347,23 @@ end % Done loop over tt, evaluating the GE conditions else % Some GE conditions depend on PType - GEcondnPath=zeros(T,nGeneralEqmEqns_acrossptypes); + GECondnPath=zeros(T,nGeneralEqmEqns_acrossptypes); + error("need to fit these loops together") + + % Restore AggVarNames and tminus1AggVarsNames by PType for GEeqns + AggVarNames=cell(1,N_i); + tminus1AggVarsNames=cell(1,N_i); + use_tminus1AggVars=0; + for ii=1:N_i + iistr=PTypeStructure.Names_i{ii}; + AggVarNames{ii}=PTypeStructure.(iistr).AggVarNames; + tminus1AggVarsNames{ii}=PTypeStructure.(iistr).tminus1AggVarsNames; + if ~isempty(tminus1AggVarsNames{ii}) + use_tminus1AggVars=1; + end + end + AggVarNames=vertcat(AggVarNames{:}); + tminus1AggVarsNames=vertcat(tminus1AggVarsNames{:}); % Parameters that may be relevant to General Eqm Parameters=PTypeStructure.ParametersRaw; diff --git a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m index 17b1e5c6..a615e11d 100644 --- a/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m +++ b/TransitionPaths/InfHorz/PType/TransitionPath_InfHorz_PType_shooting.m @@ -119,7 +119,6 @@ PricePathNew=zeros(size(PricePathOld),'gpuArray'); PricePathNew(T,:)=PricePathOld(T,:); - %% Iterate on the transition path while PricePathDist>transpathoptions.tolerance && pathcounter=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