Skip to content

Commit aa823d6

Browse files
committed
improve treatment of shocks
1 parent 35a47b6 commit aa823d6

15 files changed

+330
-453
lines changed

Estimation/Calibration/CalibrateLifeCycleModel.m

Lines changed: 21 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -188,125 +188,34 @@
188188
ReturnFnParamNames=[];
189189
FnsToEvaluateParamNames=[];
190190

191-
192-
% Internally, only ever use age-dependent joint-grids (makes all the code much easier to write)
193-
% Gradually rolling these out so that all the commands build off of these
194-
z_gridvals_J=zeros(prod(n_z),length(n_z),'gpuArray');
195-
pi_z_J=zeros(prod(n_z),prod(n_z),'gpuArray');
191+
%% Set up exogenous shock grids now (so they can then just be reused every time)
192+
% Check if using ExogShockFn or EiidShockFn, and if so, do these use a
193+
% parameter that is being calibrated
194+
caliboptions.calibrateshocks=0; % set to one if need to redo shocks for each new calib parameter vector
196195
if isfield(vfoptions,'ExogShockFn')
197-
if isfield(vfoptions,'ExogShockFnParamNames')
198-
for jj=1:N_j
199-
ExogShockFnParamsVec=CreateVectorFromParams(Parameters, vfoptions.ExogShockFnParamNames,jj);
200-
ExogShockFnParamsCell=cell(length(ExogShockFnParamsVec),1);
201-
for ii=1:length(ExogShockFnParamsVec)
202-
ExogShockFnParamsCell(ii,1)={ExogShockFnParamsVec(ii)};
203-
end
204-
[z_grid,pi_z]=vfoptions.ExogShockFn(ExogShockFnParamsCell{:});
205-
pi_z_J(:,:,jj)=gpuArray(pi_z);
206-
if all(size(z_grid)==[sum(n_z),1])
207-
z_gridvals_J(:,:,jj)=gpuArray(CreateGridvals(n_z,z_grid,1));
208-
else % already joint-grid
209-
z_gridvals_J(:,:,jj)=gpuArray(z_grid,1);
210-
end
211-
end
212-
else
213-
for jj=1:N_j
214-
[z_grid,pi_z]=vfoptions.ExogShockFn(N_j);
215-
pi_z_J(:,:,jj)=gpuArray(pi_z);
216-
if all(size(z_grid)==[sum(n_z),1])
217-
z_gridvals_J(:,:,jj)=gpuArray(CreateGridvals(n_z,z_grid,1));
218-
else % already joint-grid
219-
z_gridvals_J(:,:,jj)=gpuArray(z_grid,1);
220-
end
221-
end
222-
end
223-
elseif prod(n_z)==0 % no z
224-
z_gridvals_J=[];
225-
elseif ndims(z_grid)==3 % already an age-dependent joint-grid
226-
if all(size(z_grid)==[prod(n_z),length(n_z),N_j])
227-
z_gridvals_J=z_grid;
196+
temp=getAnonymousFnInputNames(vfoptions.ExogShockFn);
197+
% can just leave action space in here as we only use it to see if CalibParamNames is part of it
198+
if ~isempty(intersect(temp,CalibParamNames))
199+
caliboptions.calibrateshocks=1;
228200
end
229-
pi_z_J=pi_z;
230-
elseif all(size(z_grid)==[sum(n_z),N_j]) % age-dependent grid
231-
for jj=1:N_j
232-
z_gridvals_J(:,:,jj)=CreateGridvals(n_z,z_grid(:,jj),1);
201+
elseif isfield(vfoptions,'EiidShockFn')
202+
temp=getAnonymousFnInputNames(vfoptions.EiidShockFn);
203+
% can just leave action space in here as we only use it to see if CalibParamNames is part of it
204+
if ~isempty(intersect(temp,CalibParamNames))
205+
caliboptions.calibrateshocks=1;
233206
end
234-
pi_z_J=pi_z;
235-
elseif all(size(z_grid)==[prod(n_z),length(n_z)]) % joint grid
236-
z_gridvals_J=z_grid.*ones(1,1,N_j,'gpuArray');
237-
pi_z_J=pi_z.*ones(1,1,N_j,'gpuArray');
238-
elseif all(size(z_grid)==[sum(n_z),1]) % basic grid
239-
z_gridvals_J=CreateGridvals(n_z,z_grid,1).*ones(1,1,N_j,'gpuArray');
240-
pi_z_J=pi_z.*ones(1,1,N_j,'gpuArray');
241207
end
242-
243-
% If using e variable, do same for this
244-
if isfield(vfoptions,'n_e')
245-
if prod(vfoptions.n_e)==0
246-
vfoptions=rmfield(vfoptions,'n_e');
247-
else
248-
if isfield(vfoptions,'e_grid_J')
249-
error('No longer use vfoptions.e_grid_J, instead just put the age-dependent grid in vfoptions.e_grid (functionality of VFI Toolkit has changed to make it easier to use)')
250-
end
251-
if ~isfield(vfoptions,'e_grid') % && ~isfield(vfoptions,'e_grid_J')
252-
error('You are using an e (iid) variable, and so need to declare vfoptions.e_grid')
253-
elseif ~isfield(vfoptions,'pi_e')
254-
error('You are using an e (iid) variable, and so need to declare vfoptions.pi_e')
255-
end
256-
257-
vfoptions.e_gridvals_J=zeros(prod(vfoptions.n_e),length(vfoptions.n_e),'gpuArray');
258-
vfoptions.pi_e_J=zeros(prod(vfoptions.n_e),prod(vfoptions.n_e),'gpuArray');
259-
260-
if isfield(vfoptions,'EiidShockFn')
261-
if isfield(vfoptions,'EiidShockFnParamNames')
262-
for jj=1:N_j
263-
EiidShockFnParamsVec=CreateVectorFromParams(Parameters, vfoptions.EiidShockFnParamNames,jj);
264-
EiidShockFnParamsCell=cell(length(EiidShockFnParamsVec),1);
265-
for ii=1:length(EiidShockFnParamsVec)
266-
EiidShockFnParamsCell(ii,1)={EiidShockFnParamsVec(ii)};
267-
end
268-
[vfoptions.e_grid,vfoptions.pi_e]=vfoptions.EiidShockFn(EiidShockFnParamsCell{:});
269-
vfoptions.pi_e_J(:,jj)=gpuArray(vfoptions.pi_e);
270-
if all(size(vfoptions.e_grid)==[sum(vfoptions.n_e),1])
271-
vfoptions.e_gridvals_J(:,:,jj)=gpuArray(CreateGridvals(vfoptions.n_e,vfoptions.e_grid,1));
272-
else % already joint-grid
273-
vfoptions.e_gridvals_J(:,:,jj)=gpuArray(vfoptions.e_grid,1);
274-
end
275-
end
276-
else
277-
for jj=1:N_j
278-
[vfoptions.e_grid,vfoptions.pi_e]=vfoptions.EiidShockFn(N_j);
279-
vfoptions.pi_e_J(:,jj)=gpuArray(vfoptions.pi_e);
280-
if all(size(vfoptions.e_grid)==[sum(vfoptions.n_e),1])
281-
vfoptions.e_gridvals_J(:,:,jj)=gpuArray(CreateGridvals(vfoptions.n_e,vfoptions.e_grid,1));
282-
else % already joint-grid
283-
vfoptions.e_gridvals_J(:,:,jj)=gpuArray(vfoptions.e_grid,1);
284-
end
285-
end
286-
end
287-
elseif ndims(vfoptions.e_grid)==3 % already an age-dependent joint-grid
288-
if all(size(vfoptions.e_grid)==[prod(vfoptions.n_e),length(vfoptions.n_e),N_j])
289-
vfoptions.e_gridvals_J=vfoptions.e_grid;
290-
end
291-
vfoptions.pi_e_J=vfoptions.pi_e;
292-
elseif all(size(vfoptions.e_grid)==[sum(vfoptions.n_e),N_j]) % age-dependent stacked-grid
293-
for jj=1:N_j
294-
vfoptions.e_gridvals_J(:,:,jj)=CreateGridvals(vfoptions.n_e,vfoptions.e_grid(:,jj),1);
295-
end
296-
vfoptions.pi_e_J=vfoptions.pi_e;
297-
elseif all(size(vfoptions.e_grid)==[prod(vfoptions.n_e),length(vfoptions.n_e)]) % joint grid
298-
vfoptions.e_gridvals_J=vfoptions.e_grid.*ones(1,1,N_j,'gpuArray');
299-
vfoptions.pi_e_J=vfoptions.pi_e.*ones(1,N_j,'gpuArray');
300-
elseif all(size(vfoptions.e_grid)==[sum(vfoptions.n_e),1]) % basic grid
301-
vfoptions.e_gridvals_J=CreateGridvals(vfoptions.n_e,vfoptions.e_grid,1).*ones(1,1,N_j,'gpuArray');
302-
vfoptions.pi_e_J=vfoptions.pi_e.*ones(1,N_j,'gpuArray');
303-
end
304-
end
208+
if caliboptions.calibrateshocks==0
209+
% Internally, only ever use age-dependent joint-grids (makes all the code much easier to write)
210+
[z_gridvals_J, pi_z_J, vfoptions]=ExogShockSetup_FHorz(n_z,z_grid,pi_z,N_j,Parameters,vfoptions,3);
211+
% output: z_gridvals_J, pi_z_J, vfoptions.e_gridvals_J, vfoptions.pi_e_J
305212
simoptions.e_gridvals_J=vfoptions.e_gridvals_J;
306213
simoptions.pi_e_J=vfoptions.pi_e_J;
307214
end
308-
309-
215+
% Regardless of whether they are done here of in _objectivefn, they will be
216+
% precomputed by the time we get to the value fn, staty dist, etc. So
217+
vfoptions.alreadygridvals=1;
218+
simoptions.alreadygridvals=1;
310219

311220

312221
%%

Estimation/Calibration/CalibrateLifeCycleModel_PType.m

Lines changed: 21 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -233,124 +233,34 @@
233233
ReturnFnParamNames=[];
234234
FnsToEvaluateParamNames=[];
235235

236-
237-
% Internally, only ever use age-dependent joint-grids (makes all the code much easier to write)
238-
% Gradually rolling these out so that all the commands build off of these
239-
z_gridvals_J=zeros(prod(n_z),length(n_z),'gpuArray');
240-
pi_z_J=zeros(prod(n_z),prod(n_z),'gpuArray');
236+
%% Set up exogenous shock grids now (so they can then just be reused every time)
237+
% Check if using ExogShockFn or EiidShockFn, and if so, do these use a
238+
% parameter that is being calibrated
239+
caliboptions.calibrateshocks=0; % set to one if need to redo shocks for each new calib parameter vector
241240
if isfield(vfoptions,'ExogShockFn')
242-
if isfield(vfoptions,'ExogShockFnParamNames')
243-
for jj=1:N_j
244-
ExogShockFnParamsVec=CreateVectorFromParams(Parameters, vfoptions.ExogShockFnParamNames,jj);
245-
ExogShockFnParamsCell=cell(length(ExogShockFnParamsVec),1);
246-
for ii=1:length(ExogShockFnParamsVec)
247-
ExogShockFnParamsCell(ii,1)={ExogShockFnParamsVec(ii)};
248-
end
249-
[z_grid,pi_z]=vfoptions.ExogShockFn(ExogShockFnParamsCell{:});
250-
pi_z_J(:,:,jj)=gpuArray(pi_z);
251-
if all(size(z_grid)==[sum(n_z),1])
252-
z_gridvals_J(:,:,jj)=gpuArray(CreateGridvals(n_z,z_grid,1));
253-
else % already joint-grid
254-
z_gridvals_J(:,:,jj)=gpuArray(z_grid,1);
255-
end
256-
end
257-
else
258-
for jj=1:N_j
259-
[z_grid,pi_z]=vfoptions.ExogShockFn(N_j);
260-
pi_z_J(:,:,jj)=gpuArray(pi_z);
261-
if all(size(z_grid)==[sum(n_z),1])
262-
z_gridvals_J(:,:,jj)=gpuArray(CreateGridvals(n_z,z_grid,1));
263-
else % already joint-grid
264-
z_gridvals_J(:,:,jj)=gpuArray(z_grid,1);
265-
end
266-
end
267-
end
268-
elseif prod(n_z)==0 % no z
269-
z_gridvals_J=[];
270-
elseif ndims(z_grid)==3 % already an age-dependent joint-grid
271-
if all(size(z_grid)==[prod(n_z),length(n_z),N_j])
272-
z_gridvals_J=z_grid;
241+
temp=getAnonymousFnInputNames(vfoptions.ExogShockFn);
242+
% can just leave action space in here as we only use it to see if CalibParamNames is part of it
243+
if ~isempty(intersect(temp,CalibParamNames))
244+
caliboptions.calibrateshocks=1;
273245
end
274-
pi_z_J=pi_z;
275-
elseif all(size(z_grid)==[sum(n_z),N_j]) % age-dependent grid
276-
for jj=1:N_j
277-
z_gridvals_J(:,:,jj)=CreateGridvals(n_z,z_grid(:,jj),1);
246+
elseif isfield(vfoptions,'EiidShockFn')
247+
temp=getAnonymousFnInputNames(vfoptions.EiidShockFn);
248+
% can just leave action space in here as we only use it to see if CalibParamNames is part of it
249+
if ~isempty(intersect(temp,CalibParamNames))
250+
caliboptions.calibrateshocks=1;
278251
end
279-
pi_z_J=pi_z;
280-
elseif all(size(z_grid)==[prod(n_z),length(n_z)]) % joint grid
281-
z_gridvals_J=z_grid.*ones(1,1,N_j,'gpuArray');
282-
pi_z_J=pi_z.*ones(1,1,N_j,'gpuArray');
283-
elseif all(size(z_grid)==[sum(n_z),1]) % basic grid
284-
z_gridvals_J=CreateGridvals(n_z,z_grid,1).*ones(1,1,N_j,'gpuArray');
285-
pi_z_J=pi_z.*ones(1,1,N_j,'gpuArray');
286252
end
287-
288-
% If using e variable, do same for this
289-
if isfield(vfoptions,'n_e')
290-
if prod(vfoptions.n_e)==0
291-
vfoptions=rmfield(vfoptions,'n_e');
292-
else
293-
if isfield(vfoptions,'e_grid_J')
294-
error('No longer use vfoptions.e_grid_J, instead just put the age-dependent grid in vfoptions.e_grid (functionality of VFI Toolkit has changed to make it easier to use)')
295-
end
296-
if ~isfield(vfoptions,'e_grid') % && ~isfield(vfoptions,'e_grid_J')
297-
error('You are using an e (iid) variable, and so need to declare vfoptions.e_grid')
298-
elseif ~isfield(vfoptions,'pi_e')
299-
error('You are using an e (iid) variable, and so need to declare vfoptions.pi_e')
300-
end
301-
302-
vfoptions.e_gridvals_J=zeros(prod(vfoptions.n_e),length(vfoptions.n_e),'gpuArray');
303-
vfoptions.pi_e_J=zeros(prod(vfoptions.n_e),prod(vfoptions.n_e),'gpuArray');
304-
305-
if isfield(vfoptions,'EiidShockFn')
306-
if isfield(vfoptions,'EiidShockFnParamNames')
307-
for jj=1:N_j
308-
EiidShockFnParamsVec=CreateVectorFromParams(Parameters, vfoptions.EiidShockFnParamNames,jj);
309-
EiidShockFnParamsCell=cell(length(EiidShockFnParamsVec),1);
310-
for ii=1:length(EiidShockFnParamsVec)
311-
EiidShockFnParamsCell(ii,1)={EiidShockFnParamsVec(ii)};
312-
end
313-
[vfoptions.e_grid,vfoptions.pi_e]=vfoptions.EiidShockFn(EiidShockFnParamsCell{:});
314-
vfoptions.pi_e_J(:,jj)=gpuArray(vfoptions.pi_e);
315-
if all(size(vfoptions.e_grid)==[sum(vfoptions.n_e),1])
316-
vfoptions.e_gridvals_J(:,:,jj)=gpuArray(CreateGridvals(vfoptions.n_e,vfoptions.e_grid,1));
317-
else % already joint-grid
318-
vfoptions.e_gridvals_J(:,:,jj)=gpuArray(vfoptions.e_grid,1);
319-
end
320-
end
321-
else
322-
for jj=1:N_j
323-
[vfoptions.e_grid,vfoptions.pi_e]=vfoptions.EiidShockFn(N_j);
324-
vfoptions.pi_e_J(:,jj)=gpuArray(vfoptions.pi_e);
325-
if all(size(vfoptions.e_grid)==[sum(vfoptions.n_e),1])
326-
vfoptions.e_gridvals_J(:,:,jj)=gpuArray(CreateGridvals(vfoptions.n_e,vfoptions.e_grid,1));
327-
else % already joint-grid
328-
vfoptions.e_gridvals_J(:,:,jj)=gpuArray(vfoptions.e_grid,1);
329-
end
330-
end
331-
end
332-
elseif ndims(vfoptions.e_grid)==3 % already an age-dependent joint-grid
333-
if all(size(vfoptions.e_grid)==[prod(vfoptions.n_e),length(vfoptions.n_e),N_j])
334-
vfoptions.e_gridvals_J=vfoptions.e_grid;
335-
end
336-
vfoptions.pi_e_J=vfoptions.pi_e;
337-
elseif all(size(vfoptions.e_grid)==[sum(vfoptions.n_e),N_j]) % age-dependent stacked-grid
338-
for jj=1:N_j
339-
vfoptions.e_gridvals_J(:,:,jj)=CreateGridvals(vfoptions.n_e,vfoptions.e_grid(:,jj),1);
340-
end
341-
vfoptions.pi_e_J=vfoptions.pi_e;
342-
elseif all(size(vfoptions.e_grid)==[prod(vfoptions.n_e),length(vfoptions.n_e)]) % joint grid
343-
vfoptions.e_gridvals_J=vfoptions.e_grid.*ones(1,1,N_j,'gpuArray');
344-
vfoptions.pi_e_J=vfoptions.pi_e.*ones(1,N_j,'gpuArray');
345-
elseif all(size(vfoptions.e_grid)==[sum(vfoptions.n_e),1]) % basic grid
346-
vfoptions.e_gridvals_J=CreateGridvals(vfoptions.n_e,vfoptions.e_grid,1).*ones(1,1,N_j,'gpuArray');
347-
vfoptions.pi_e_J=vfoptions.pi_e.*ones(1,N_j,'gpuArray');
348-
end
349-
end
253+
if caliboptions.calibrateshocks==0
254+
% Internally, only ever use age-dependent joint-grids (makes all the code much easier to write)
255+
[z_gridvals_J, pi_z_J, vfoptions]=ExogShockSetup_FHorz(n_z,z_grid,pi_z,N_j,Parameters,vfoptions,3);
256+
% output: z_gridvals_J, pi_z_J, vfoptions.e_gridvals_J, vfoptions.pi_e_J
350257
simoptions.e_gridvals_J=vfoptions.e_gridvals_J;
351258
simoptions.pi_e_J=vfoptions.pi_e_J;
352259
end
353-
260+
% Regardless of whether they are done here of in _objectivefn, they will be
261+
% precomputed by the time we get to the value fn, staty dist, etc. So
262+
vfoptions.alreadygridvals=1;
263+
simoptions.alreadygridvals=1;
354264

355265

356266
%%

0 commit comments

Comments
 (0)