Skip to content

Commit 1c38231

Browse files
committed
minor
1 parent 917339d commit 1c38231

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

ValueFnIter/FHorz/ResidualAsset/ValueFnIter_Case1_FHorz_ResidAsset.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,29 @@
6565
%%
6666
if vfoptions.outputkron==0
6767
if n_d(1)==0
68-
n_d=n_a1;
68+
n_dmod=n_a1;
6969
else
70-
n_d=[n_d,n_a1];
70+
n_dmod=[n_d,n_a1];
7171
end
7272
n_a=[n_a1,n_r];
73-
PolicyKron=reshape(PolicyKron,[prod(n_a),prod(n_z),N_j]);
73+
if n_d==0
74+
PolicyKron=reshape(PolicyKron,[prod(n_a),prod(n_z),N_j]);
75+
else
76+
PolicyKron=reshape(PolicyKron,[2,prod(n_a),prod(n_z),N_j]);
77+
PolicyKron=shiftdim(PolicyKron(1,:,:,:)+prod(n_d)*(PolicyKron(2,:,:,:)-1),1);
78+
end
7479
%Transforming Value Fn and Optimal Policy Indexes matrices back out of Kronecker Form
7580
if isfield(vfoptions,'n_e')
7681
if N_z==0
7782
V=reshape(VKron,[n_a,vfoptions.n_e,N_j]);
78-
Policy=UnKronPolicyIndexes_Case2_FHorz(PolicyKron, n_d, n_a, vfoptions.n_e, N_j, vfoptions); % Treat e as z (because no z)
83+
Policy=UnKronPolicyIndexes_Case2_FHorz(PolicyKron, n_dmod, n_a, vfoptions.n_e, N_j, vfoptions); % Treat e as z (because no z)
7984
else
8085
V=reshape(VKron,[n_a,n_z,vfoptions.n_e,N_j]);
81-
Policy=UnKronPolicyIndexes_Case2_FHorz_e(PolicyKron, n_d, n_a, n_z, vfoptions.n_e, N_j, vfoptions);
86+
Policy=UnKronPolicyIndexes_Case2_FHorz_e(PolicyKron, n_dmod, n_a, n_z, vfoptions.n_e, N_j, vfoptions);
8287
end
8388
else
8489
V=reshape(VKron,[n_a,n_z,N_j]);
85-
Policy=UnKronPolicyIndexes_Case2_FHorz(PolicyKron, n_d, n_a, n_z, N_j, vfoptions);
90+
Policy=UnKronPolicyIndexes_Case2_FHorz(PolicyKron, n_dmod, n_a, n_z, N_j, vfoptions);
8691
end
8792
else
8893
V=VKron;

ValueFnIter/FHorz/ResidualAsset/ValueFnIter_Case1_FHorz_ResidAsset_raw.m

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function [V,Policy2]=ValueFnIter_Case1_FHorz_ResidAsset_raw(n_d,n_a,n_r,n_z,N_j, d_grid, a_grid, r_grid, z_gridvals_J, pi_z_J, ReturnFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, vfoptions)
1+
function [V,Policy2]=ValueFnIter_Case1_FHorz_ResidAsset_raw(n_d,n_a,n_r,n_z,N_j, d_grid, a_grid, r_grid, z_gridvals_J, pi_z_J, ReturnFn, rprimeFn, Parameters, DiscountFactorParamNames, ReturnFnParamNames, rprimeFnParamNames, vfoptions)
22

33
N_d=prod(n_d);
44
N_a=prod(n_a);
@@ -70,8 +70,8 @@
7070
rprimeFnParamsVec=CreateVectorFromParams(Parameters, rprimeFnParamNames,N_j);
7171
[rprimeIndexes,rprimeProbs]=CreateResidualAssetFnMatrix_Case1(rprimeFn, n_d, n_a, n_r, n_z, d_grid, a_grid, r_grid, z_gridvals_J(:,:,N_j), rprimeFnParamsVec); % Note, is actually rprime_grid (but r_grid is anyway same for all ages)
7272
% Note: rprimeIndex is [N_d*N_a*N_a*N_z,1], and rprimeProbs is [N_d*N_a*N_a*N_z,1]
73-
aprimeIndexes=kron(ones(N_a*N_z,1),(1:1:N_a)'); % aprime over (d,aprime,a,z)
74-
zprimeIndexes=kron((1:1:N_z)',ones(N_a*N_a,1)); % zprime over (d,aprime,a,z)
73+
aprimeIndexes=repelem(repmat((1:1:N_a)',N_a*N_z,1),N_d,1); % aprime over (d,aprime,a,z)
74+
zprimeIndexes=repelem((1:1:N_z)',N_d*N_a*N_a,1); % zprime over (d,aprime,a,z)
7575

7676
% lower r index (size is N_d*N_a*N_a*N_z)
7777
fullindex=aprimeIndexes+N_a*(rprimeIndexes-1)+N_a*N_r*(zprimeIndexes-1); % index for (a',r',z'), as function of (d,a',a,z)
@@ -98,8 +98,8 @@
9898
EV(isnan(EV))=0; %multilications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites)
9999
EV=sum(EV,3); % sum over z', leaving a singular second dimension
100100

101-
entireRHS=ReturnMatrix+DiscountFactorParamsVec*(reshape(EV,[N_d*N_a,N_a,1,N_z]); %.*ones(1,1,N_r,1));
102-
101+
entireRHS=ReturnMatrix+DiscountFactorParamsVec*(reshape(EV,[N_d*N_a,N_a,1,N_z]));
102+
103103
%Calc the max and it's index
104104
[Vtemp,maxindex]=max(entireRHS,[],1);
105105

@@ -117,8 +117,8 @@
117117
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)
118118
EV_z=sum(EV_z,3); % sum over z', leaving a singular second dimension
119119

120-
entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*(reshape(EV_z,[N_d*N_a,N_a,1]); %.*ones(1,1,N_r));
121-
120+
entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*(reshape(EV_z,[N_d*N_a,N_a,1]));
121+
122122
%Calc the max and it's index
123123
[Vtemp,maxindex]=max(entireRHS_z,[],1);
124124
V(:,:,z_c,N_j)=Vtemp;
@@ -135,8 +135,8 @@
135135
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)
136136
EV_z=sum(EV_z,3); % sum over z', leaving a singular second dimension
137137

138-
entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*(reshape(EV_z,[N_d*N_a,N_a,1]); %.*ones(1,1,N_r));
139-
138+
entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*(reshape(EV_z,[N_d*N_a,N_a,1]));
139+
140140
%Calc the max and it's index
141141
[Vtemp,maxindex]=max(entireRHS_z,[],1);
142142
V(:,:,z_c,N_j)=Vtemp;
@@ -178,16 +178,16 @@
178178
DiscountFactorParamsVec=CreateVectorFromParams(Parameters, DiscountFactorParamNames,jj);
179179
DiscountFactorParamsVec=prod(DiscountFactorParamsVec);
180180

181-
VKronNext_j=V(:,:,jj+1);
181+
VKronNext_j=V(:,:,:,jj+1);
182182

183183
% Residual asset:
184184
% VKronNext_j is over (aprime,r,z)
185185
% Need to convert to be over (d,aprime,a,z)
186186
rprimeFnParamsVec=CreateVectorFromParams(Parameters, rprimeFnParamNames,jj);
187187
[rprimeIndexes,rprimeProbs]=CreateResidualAssetFnMatrix_Case1(rprimeFn, n_d, n_a, n_r, n_z, d_grid, a_grid, r_grid, z_gridvals_J(:,:,jj), rprimeFnParamsVec); % Note, is actually rprime_grid (but r_grid is anyway same for all ages)
188188
% Note: rprimeIndex is [N_d*N_a*N_a*N_z,1], and rprimeProbs is [N_d*N_a*N_a*N_z,1]
189-
aprimeIndexes=kron(ones(N_a*N_z,1),(1:1:N_a)'); % aprime over (d,aprime,a,z)
190-
zprimeIndexes=kron((1:1:N_z)',ones(N_a*N_a,1)); % zprime over (d,aprime,a,z)
189+
aprimeIndexes=repelem(repmat((1:1:N_a)',N_a*N_z,1),N_d,1); % aprime over (d,aprime,a,z)
190+
zprimeIndexes=repelem((1:1:N_z)',N_d*N_a*N_a,1); % zprime over (d,aprime,a,z)
191191

192192
% lower r index (size is N_d*N_a*N_a*N_z)
193193
fullindex=aprimeIndexes+N_a*(rprimeIndexes-1)+N_a*N_r*(zprimeIndexes-1); % index for (a',r',z'), as function of (d,a',a,z)
@@ -211,8 +211,8 @@
211211
EV(isnan(EV))=0; %multilications of -Inf with 0 gives NaN, this replaces them with zeros (as the zeros come from the transition probabilites)
212212
EV=sum(EV,3); % sum over z', leaving a singular second dimension
213213

214-
entireRHS=ReturnMatrix+DiscountFactorParamsVec*(reshape(EV,[N_d*N_a,N_a,1,N_z]); %.*ones(1,1,N_r,1));
215-
214+
entireRHS=ReturnMatrix+DiscountFactorParamsVec*(reshape(EV,[N_d*N_a,N_a,1,N_z]));
215+
216216
%Calc the max and it's index
217217
[Vtemp,maxindex]=max(entireRHS,[],1);
218218

@@ -229,8 +229,8 @@
229229
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)
230230
EV_z=sum(EV_z,3); % sum over z', leaving a singular second dimension
231231

232-
entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*(reshape(EV_z,[N_d*N_a,N_a,1]); %.*ones(1,1,N_r));
233-
232+
entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*(reshape(EV_z,[N_d*N_a,N_a,1]));
233+
234234
%Calc the max and it's index
235235
[Vtemp,maxindex]=max(entireRHS_z,[],1);
236236
V(:,:,z_c,jj)=Vtemp;
@@ -247,8 +247,8 @@
247247
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)
248248
EV_z=sum(EV_z,3); % sum over z', leaving a singular second dimension
249249

250-
entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*(reshape(EV_z,[N_d*N_a,N_a,1]); %.*ones(1,1,N_r));
251-
250+
entireRHS_z=ReturnMatrix_z+DiscountFactorParamsVec*(reshape(EV_z,[N_d*N_a,N_a,1]));
251+
252252
%Calc the max and it's index
253253
[Vtemp,maxindex]=max(entireRHS_z,[],1);
254254
V(:,:,z_c,jj)=Vtemp;

0 commit comments

Comments
 (0)