Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')
Expand Down
Loading