Skip to content

Commit c659405

Browse files
committed
mostly field exp
1 parent b854d6c commit c659405

9 files changed

+269
-536
lines changed

EvaluateFnOnAgentDist/FHorz/EvalFnOnAgentDist_AggVars_FHorz_Case1.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
end
8585
end
8686

87+
88+
8789
% If using e variable, do same for this
8890
if isfield(simoptions,'n_e')
8991
n_e=simoptions.n_e;
@@ -126,7 +128,7 @@
126128
if all(size(simoptions.e_grid)==[sum(simoptions.n_e),1])
127129
e_gridvals_J(:,:,jj)=gpuArray(CreateGridvals(simoptions.n_e,simoptions.e_grid,1));
128130
else % already joint-grid
129-
e_gridvals_J(:,:,jj)=gpuArray(simoptions.e_grid,1);
131+
e_gridvals_J(:,:,jj)=gpuArray(simoptions.e_grid);
130132
end
131133
end
132134
else
@@ -135,7 +137,7 @@
135137
if all(size(simoptions.e_grid)==[sum(simoptions.n_e),1])
136138
e_gridvals_J(:,:,jj)=gpuArray(CreateGridvals(simoptions.n_e,simoptions.e_grid,1));
137139
else % already joint-grid
138-
e_gridvals_J(:,:,jj)=gpuArray(simoptions.e_grid,1);
140+
e_gridvals_J(:,:,jj)=gpuArray(simoptions.e_grid);
139141
end
140142
end
141143
end
@@ -151,6 +153,8 @@
151153
n_z=[n_z,n_e];
152154
N_z=prod(n_z);
153155
end
156+
157+
simoptions=rmfield(simoptions,'n_e'); % Remove for subcommands as e is now in z
154158
end
155159
end
156160

EvaluateFnOnAgentDist/FHorz/FieldExp/EvalFnOnAgentDist_AggVars_FHorz_Case1_FieldExp_PType.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@
279279
end
280280
z_grid_J(:,jj)=gather(z_grid);
281281
end
282+
simoptions_temp=rmfield(simoptions_temp,'ExogShockFn');
282283
else
283284
% This is just so that it makes things easier below because I can
284285
% take it as given that pi_z_J and z_grid_J exist
@@ -312,6 +313,7 @@
312313
end
313314
e_grid_J(:,jj)=gather(e_grid);
314315
end
316+
simoptions_temp=rmfield(simoptions_temp,'EiidShockFn');
315317
else
316318
% This is just so that it makes things easier below because I can
317319
% take it as given that pi_e_J and e_grid_J exist
@@ -325,9 +327,10 @@
325327
simoptions_control_temp=simoptions_temp;
326328
simoptions_control_temp.z_grid_J=z_grid_J(:,TreatmentAgeRange(1):TreatmentAgeRange(2)+TreatmentDuration-1);
327329
if isfield(simoptions_temp,'n_e')
328-
simoptions_control_temp.e_grid_J=e_grid_J(:,TreatmentAgeRange(1):TreatmentAgeRange(2)+TreatmentDuration-1);
330+
simoptions_control_temp.e_grid=e_grid_J(:,TreatmentAgeRange(1):TreatmentAgeRange(2)+TreatmentDuration-1);
329331
end
330332

333+
331334
%% Get the age weights out of control group as we need the later for treatment group, then reweight control group ages to reflect treatment duration
332335

333336
% Find the age weights for the treatment group (get them from the marginal distribution of the control group over ages)
@@ -358,7 +361,6 @@
358361
StationaryDist_control_temp=reshape(StationaryDist_control_temp,[numel(StationaryDist_control_temp)/N_j_temp_FieldExp,N_j_temp_FieldExp]);
359362
StationaryDist_control_temp=StationaryDist_control_temp.*agereweight';
360363
StationaryDist_control_temp=StationaryDist_control_temp/sum(StationaryDist_control_temp(:));
361-
362364

363365
%% Calculate the aggregate variables for the control-group
364366
simoptions_control_temp.outputasstructure=0;

EvaluateFnOnAgentDist/FHorz/FieldExp/EvalFnOnAgentDist_AggVars_FHorz_Case1_FieldExp_Treatment.m

Lines changed: 87 additions & 165 deletions
Large diffs are not rendered by default.

StationaryDist/FHorz/FieldExp/StationaryDist_FHorz_Case1_FieldExp_Treatment.m

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
simoptions.iterate=1;
1818
simoptions.tolerance=10^(-9);
1919
simoptions.outputkron=0; % If 1 then leave output in Kron form
20+
simoptions.loopovere=0; % default is parallel over e, 1 will loop over e, 2 will parfor loop over e
2021
else
2122
%Check simoptions for missing fields, if there are some fill them with
2223
%the defaults
@@ -52,6 +53,9 @@
5253
if isfield(simoptions,'outputkron')==0
5354
simoptions.outputkron=0; % If 1 then leave output in Kron form
5455
end
56+
if ~isfield(simoptions,'loopovere')
57+
simoptions.loopovere=0; % default is parallel over e, 1 will loop over e, 2 will parfor loop over e
58+
end
5559
end
5660

5761
%%
@@ -82,7 +86,7 @@
8286
end
8387

8488
if simoptions.parallel~=2 && simoptions.parallel~=4
85-
Policy=gather(Policy);
89+
PolicyKron=gather(PolicyKron);
8690
StationaryDist_Control=gather(StationaryDist_Control);
8791
pi_z=gather(pi_z);
8892
end
@@ -158,10 +162,10 @@
158162
for j_p=TreatmentAgeRange(1):TreatmentAgeRange(2)
159163
% Pull the appropraite initial distribution of agents
160164
if N_ze==0
161-
jequaloneDistKron=StationaryDist_Control(:,j_p);
165+
jequaloneDistKron=reshape(StationaryDist_Control(:,j_p),[N_a,1]);
162166
PolicyKron_treat=PolicyKron(:,:,j_p:j_p+TreatmentDuration-1);
163167
else
164-
jequaloneDistKron=StationaryDist_Control(:,:,j_p);
168+
jequaloneDistKron=reshape(StationaryDist_Control(:,:,j_p),[N_a*N_ze,1]);
165169
if N_z==0 % just e
166170
PolicyKron_treat=PolicyKron(:,:,:,j_p:j_p+TreatmentDuration-1);
167171
elseif N_e==0 % Just z
@@ -192,11 +196,11 @@
192196
simoptions.pi_e_J=pi_e_J(:,j_p:j_p+TreatmentDuration);
193197
end
194198
if N_e==0
195-
StationaryDist_treatment(:,:,:,j_p)=reshape(StationaryDist_FHorz_Case1_Iteration_raw(jequaloneDistKron,AgeWeightParamNames,PolicyKron_treat,N_d,N_a,N_z,TreatmentDuration,pi_z,Parameters,simoptions),[N_a,N_z,TreatmentDuration]);
199+
StationaryDist_treatment(:,:,:,j_p)=reshape(StationaryDist_FHorz_Case1_Iteration_raw(jequaloneDistKron,AgeWeightParamNames,PolicyKron_treat,N_d,N_a,N_z,TreatmentDuration,pi_z_J,Parameters,simoptions),[N_a,N_z,TreatmentDuration]);
196200
elseif N_z==0
197-
StationaryDist_treatment(:,:,:,j_p)=reshape(StationaryDist_FHorz_Case1_Iteration_noz_e_raw(jequaloneDistKron,AgeWeightParamNames,PolicyKron_treat,N_d,N_a,N_e,TreatmentDuration,simoptions.pi_e,Parameters,simoptions),[N_a,N_e,TreatmentDuration]); % e but no z
201+
StationaryDist_treatment(:,:,:,j_p)=reshape(StationaryDist_FHorz_Case1_Iteration_noz_e_raw(jequaloneDistKron,AgeWeightParamNames,PolicyKron_treat,N_d,N_a,N_e,TreatmentDuration,pi_e_J,Parameters,simoptions),[N_a,N_e,TreatmentDuration]); % e but no z
198202
else
199-
StationaryDist_treatment(:,:,:,j_p)=reshape(StationaryDist_FHorz_Case1_Iteration_e_raw(jequaloneDistKron,AgeWeightParamNames,PolicyKron_treat,N_d,N_a,N_z,N_e,TreatmentDuration,pi_z,simoptions.pi_e,Parameters,simoptions),[N_a,N_z*N_e,TreatmentDuration]);
203+
StationaryDist_treatment(:,:,:,j_p)=reshape(StationaryDist_FHorz_Case1_Iteration_e_raw(jequaloneDistKron,AgeWeightParamNames,PolicyKron_treat,N_d,N_a,N_z,N_e,TreatmentDuration,pi_z_J,pi_e_J,Parameters,simoptions),[N_a,N_z*N_e,TreatmentDuration]);
200204
end
201205
end
202206

StationaryDist/FHorz/StationaryDist_FHorz_Case1.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
simoptions.residualasset=0;
3030
simoptions.tolerance=10^(-9);
3131
simoptions.outputkron=0; % If 1 then leave output in Kron form
32+
simoptions.loopovere=0; % default is parallel over e, 1 will loop over e, 2 will parfor loop over e
3233
else
3334
%Check simoptions for missing fields, if there are some fill them with
3435
%the defaults
@@ -80,6 +81,9 @@
8081
if ~isfield(simoptions,'outputkron')
8182
simoptions.outputkron=0; % If 1 then leave output in Kron form
8283
end
84+
if ~isfield(simoptions,'loopovere')
85+
simoptions.loopovere=0; % default is parallel over e, 1 will loop over e, 2 will parfor loop over e
86+
end
8387
end
8488

8589

StationaryDist/FHorz/StationaryDist_FHorz_Case1_Iteration_noz_raw.m

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -36,108 +36,6 @@
3636
StationaryDistKron=gpuArray(StationaryDistKron);
3737
end
3838

39-
%% THIS COMMENTED OUT SECTION IS LEGACY CODE
40-
% Tried out doing this using cpu matrices, gpu matrices, sparse cpu
41-
% matrices, and sparce gpu matrices.
42-
% The sparse cpu matrices seem to be the fastest, and since they also use
43-
% least memory I have hardcoded them above.
44-
45-
46-
% if simoptions.parallel<2 || simoptions.parallel==3
47-
% optaprime=gather(optaprime);
48-
% jequaloneDistKron=gather(jequaloneDistKron);
49-
% end
50-
%
51-
% if simoptions.parallel<2 % Full CPU
52-
%
53-
% StationaryDistKron=zeros(N_a,N_j);
54-
% StationaryDistKron(:,1)=jequaloneDistKron;
55-
%
56-
% for jj=1:(N_j-1)
57-
%
58-
% if simoptions.verbose==1
59-
% fprintf('Stationary Distribution iteration horizon: %i of %i \n',jj, N_j)
60-
% end
61-
%
62-
% optaprime_jj=optaprime(1,:,jj);
63-
%
64-
% Gammatranspose=zeros(N_a,N_a); %probability of going to (a') given in (a)
65-
% Gammatranspose(optaprime_jj+N_a*(0:1:N_a-1))=1;
66-
%
67-
% StationaryDistKron(:,jj+1)=Gammatranspose*StationaryDistKron(:,jj);
68-
% end
69-
%
70-
% elseif simoptions.parallel==2 % Full GPU
71-
%
72-
% StationaryDistKron=zeros(N_a,N_j,'gpuArray');
73-
% StationaryDistKron(:,1)=jequaloneDistKron;
74-
%
75-
% for jj=1:(N_j-1)
76-
%
77-
% if simoptions.verbose==1
78-
% fprintf('Stationary Distribution iteration horizon: %i of %i \n',jj, N_j)
79-
% end
80-
%
81-
% optaprime_jj=optaprime(1,:,jj);
82-
%
83-
% Gammatranspose=zeros(N_a,N_a,'gpuArray');
84-
% Gammatranspose(optaprime_jj+N_a*(gpuArray(0:1:N_a-1)))=1;
85-
%
86-
% StationaryDistKron(:,jj+1)=Gammatranspose*StationaryDistKron(:,jj);
87-
% end
88-
%
89-
% elseif simoptions.parallel==3 % Sparse CPU
90-
%
91-
% StationaryDistKron=zeros(N_a,N_j);
92-
% StationaryDistKron(:,1)=jequaloneDistKron;
93-
%
94-
% StationaryDistKron_jj=sparse(jequaloneDistKron);
95-
%
96-
% for jj=1:(N_j-1)
97-
%
98-
% if simoptions.verbose==1
99-
% fprintf('Stationary Distribution iteration horizon: %i of %i \n',jj, N_j)
100-
% end
101-
%
102-
% optaprime_jj=optaprime(1,:,jj);
103-
%
104-
% Gammatranspose=sparse(N_a,N_a);
105-
% Gammatranspose(optaprime_jj+N_a*(0:1:N_a-1))=1;
106-
%
107-
% StationaryDistKron_jj=Gammatranspose*StationaryDistKron_jj;
108-
% StationaryDistKron(:,jj+1)=full(StationaryDistKron_jj);
109-
% end
110-
%
111-
% if gpuDeviceCount>0 % Move the solution to the gpu if there is one
112-
% StationaryDistKron=gpuArray(StationaryDistKron);
113-
% end
114-
%
115-
% elseif simoptions.parallel==4 % Sparse matrix instead of a standard matrix for P, on gpu
116-
%
117-
% StationaryDistKron=zeros(N_a,N_j,'gpuArray');
118-
% StationaryDistKron(:,1)=jequaloneDistKron;
119-
%
120-
% StationaryDistKron_jj=sparse(StationaryDistKron(:,1));
121-
% for jj=1:(N_j-1)
122-
%
123-
% if simoptions.verbose==1
124-
% fprintf('Stationary Distribution iteration horizon: %i of %i \n',jj, N_j)
125-
% end
126-
%
127-
% optaprime_jj=optaprime(1,:,jj);
128-
%
129-
% Gammatranspose=sparse(N_a,N_a);
130-
% Gammatranspose(optaprime_jj+N_a*(0:1:N_a-1))=1;
131-
%
132-
% Gammatranspose=gpuArray(Gammatranspose);
133-
%
134-
% % StationaryDistKron(:,jj+1)=Gammatranspose*StationaryDistKron(:,jj); % Cannot index sparse gpuArray, so have to use StationaryDistKron_jj instead
135-
% StationaryDistKron_jj=Gammatranspose*StationaryDistKron_jj;
136-
% StationaryDistKron(:,jj+1)=full(StationaryDistKron_jj);
137-
% end
138-
%
139-
% end
140-
14139
%%
14240

14341
% Reweight the different ages based on 'AgeWeightParamNames'. (it is assumed there is only one Age Weight Parameter (name))

0 commit comments

Comments
 (0)