|
| 1 | +function StationaryDist=StationaryDist_FHorz_Case1_ResidAsset(jequaloneDist,AgeWeightParamNames,Policy,n_d,n_a,n_z,N_j,pi_z,Parameters,simoptions) |
| 2 | + |
| 3 | +%% Check for the age weights parameter, and make sure it is a row vector |
| 4 | +if size(Parameters.(AgeWeightParamNames{1}),2)==1 % Seems like column vector |
| 5 | + Parameters.(AgeWeightParamNames{1})=Parameters.(AgeWeightParamNames{1})'; |
| 6 | + % Note: assumed there is only one AgeWeightParamNames |
| 7 | +end |
| 8 | + |
| 9 | +%% Check that the age one distribution is of mass one |
| 10 | +if abs(sum(jequaloneDist(:))-1)>10^(-9) |
| 11 | + error('The jequaloneDist must be of mass one') |
| 12 | +end |
| 13 | + |
| 14 | +%% Setup related to experience asset |
| 15 | +if isfield(simoptions,'rprimeFn') |
| 16 | + rprimeFn=simoptions.rprimeFn; |
| 17 | +else |
| 18 | + error('To use an residual asset you must define simoptions.rprimeFn') |
| 19 | +end |
| 20 | +if ~isfield(simoptions,'a_grid') |
| 21 | + error('To use an residual asset you must define simoptions.a_grid') |
| 22 | +end |
| 23 | + |
| 24 | +n_r=n_a(end); |
| 25 | +n_a=n_a(1:end-1); |
| 26 | + |
| 27 | +a_grid=gpuArray(simoptions.a_grid(1:sum(n_a))); |
| 28 | +r_grid=gpuArray(simoptions.a_grid(sum(n_a)+1:end)); |
| 29 | + |
| 30 | +% rprimeFnParamNames in same fashion |
| 31 | +if n_d(1)==0 |
| 32 | + l_d=0; |
| 33 | +else |
| 34 | + l_d=length(n_d); |
| 35 | +end |
| 36 | +l_a=length(n_a); |
| 37 | +l_z=length(n_z); |
| 38 | +temp=getAnonymousFnInputNames(rprimeFn); |
| 39 | +if length(temp)>(l_d+l_a+l_a+l_z) |
| 40 | + rprimeFnParamNames={temp{l_d+l_a+l_a+l_z+1:end}}; % the first inputs will always be (d2,a2) |
| 41 | +else |
| 42 | + rprimeFnParamNames={}; |
| 43 | +end |
| 44 | + |
| 45 | +rprimeFnParamNames |
| 46 | + |
| 47 | +%% |
| 48 | +if isfield(simoptions,'n_e') |
| 49 | + if n_z(1)==0 |
| 50 | + error('Not yet implemented n_z=0 with n_e and residualasset, email me and I will do it (or you can just pretend by using n_z=1 and pi_z=1, not using the value of z anywhere)') |
| 51 | + else |
| 52 | +% StationaryDist=StationaryDist_FHorz_Case1_ResidAsset_e(jequaloneDist,AgeWeightParamNames,Policy,n_d1,n_d2,n_a,n_z,N_j,pi_z,rprimeFn,Parameters,simoptions); |
| 53 | + end |
| 54 | + return |
| 55 | +end |
| 56 | + |
| 57 | +if n_z(1)==0 |
| 58 | + error('Not yet implemented n_z=0 with residualasset, (you can just pretend by using n_z=1 and pi_z=1, not using the value of z anywhere)') |
| 59 | +end |
| 60 | + |
| 61 | +N_a=prod(n_a); |
| 62 | +N_r=prod(n_r); |
| 63 | +N_z=prod(n_z); |
| 64 | + |
| 65 | +if exist('simoptions','var')==0 |
| 66 | + simoptions.nsims=10^4; |
| 67 | + simoptions.parallel=3-(gpuDeviceCount>0); % 3 (sparse) if cpu, 2 if gpu |
| 68 | + simoptions.verbose=0; |
| 69 | + try |
| 70 | + PoolDetails=gcp; |
| 71 | + simoptions.ncores=PoolDetails.NumWorkers; |
| 72 | + catch |
| 73 | + simoptions.ncores=1; |
| 74 | + end |
| 75 | + simoptions.iterate=1; |
| 76 | + simoptions.tolerance=10^(-9); |
| 77 | + simoptions.outputkron=0; % If 1 then leave output in Kron form |
| 78 | +else |
| 79 | + %Check simoptions for missing fields, if there are some fill them with |
| 80 | + %the defaults |
| 81 | + if isfield(simoptions,'tolerance')==0 |
| 82 | + simoptions.tolerance=10^(-9); |
| 83 | + end |
| 84 | + if isfield(simoptions,'nsims')==0 |
| 85 | + simoptions.nsims=10^4; |
| 86 | + end |
| 87 | + if isfield(simoptions,'parallel')==0 |
| 88 | + simoptions.parallel=3-(gpuDeviceCount>0); % 3 (sparse) if cpu, 2 if gpu |
| 89 | + end |
| 90 | + if isfield(simoptions,'verbose')==0 |
| 91 | + simoptions.verbose=0; |
| 92 | + end |
| 93 | + if isfield(simoptions,'ncores')==0 |
| 94 | + try |
| 95 | + PoolDetails=gcp; |
| 96 | + simoptions.ncores=PoolDetails.NumWorkers; |
| 97 | + catch |
| 98 | + simoptions.ncores=1; |
| 99 | + end |
| 100 | + end |
| 101 | + if isfield(simoptions,'iterate')==0 |
| 102 | + simoptions.iterate=1; |
| 103 | + end |
| 104 | + if isfield(simoptions,'ExogShockFn') % If using ExogShockFn then figure out the parameter names |
| 105 | + simoptions.ExogShockFnParamNames=getAnonymousFnInputNames(simoptions.ExogShockFn); |
| 106 | + end |
| 107 | + if isfield(simoptions,'outputkron')==0 |
| 108 | + simoptions.outputkron=0; % If 1 then leave output in Kron form |
| 109 | + end |
| 110 | +end |
| 111 | + |
| 112 | +jequaloneDistKron=reshape(jequaloneDist,[N_a*N_r*N_z,1]); |
| 113 | +if simoptions.parallel~=2 && simoptions.parallel~=4 |
| 114 | + Policy=gather(Policy); |
| 115 | + jequaloneDistKron=gather(jequaloneDistKron); |
| 116 | + pi_z=gather(pi_z); |
| 117 | +end |
| 118 | + |
| 119 | +% Get policy for aprime, then get policy for rprime, then combine (all just in terms of current state) |
| 120 | +Policy=reshape(Policy,[size(Policy,1),N_a*N_r,N_z,N_j]); |
| 121 | + |
| 122 | +if l_a==1 |
| 123 | + Policy_aprime=shiftdim(Policy(l_d+1,:,:,:),1); |
| 124 | +elseif l_a==2 |
| 125 | + Policy_aprime=shiftdim(Policy(l_d+1,:,:,:)+n_a(1)*(Policy(l_d+2,:,:,:)-1),1); |
| 126 | +elseif l_a==3 |
| 127 | + Policy_aprime=shiftdim(Policy(l_d+1,:,:,:)+n_a(1)*(Policy(l_d+2,:,:,:)-1)+n_a(1)*n_a(2)*(Policy(l_d+3,:,:,:)-1),1); |
| 128 | +elseif l_a==4 |
| 129 | + Policy_aprime=shiftdim(Policy(l_d+1,:,:,:)+n_a(1)*(Policy(l_d+2,:,:,:)-1)+n_a(1)*n_a(2)*(Policy(l_d+3,:,:,:)-1)+n_a(1)*n_a(2)*n_a(3)*(Policy(l_d+4,:,:,:)-1),1); |
| 130 | +end |
| 131 | + |
| 132 | +Policy_rprime=zeros(N_a*N_r,N_z,N_j,'gpuArray'); % the lower grid point |
| 133 | +PolicyProbs=zeros(N_a*N_r,N_z,N_j,2,'gpuArray'); % The fourth dimension is lower/upper grid point |
| 134 | +for jj=1:N_j |
| 135 | + rprimeFnParamsVec=CreateVectorFromParams(Parameters, rprimeFnParamNames,jj); |
| 136 | + [rprimeIndexes, rprimeProbs]=CreaterprimePolicyResidualAsset_Case1(Policy(:,:,:,jj),rprimeFn, n_d, n_a, n_r, n_z, gpuArray(simoptions.d_grid), a_grid, r_grid, gpuArray(simoptions.z_grid), rprimeFnParamsVec); |
| 137 | + % rprimeIndexes is [N_a*N_r,N_z], rprimeProbs is [N_a*N_r,N_z] |
| 138 | + Policy_rprime(:,:,jj)=rprimeIndexes; |
| 139 | + PolicyProbs(:,:,jj,1)=rprimeProbs; |
| 140 | + PolicyProbs(:,:,jj,2)=1-rprimeProbs; |
| 141 | + |
| 142 | + if jj==30 % DEBUG |
| 143 | + max(rprimeIndexes) |
| 144 | + rprimeIndexes(end-5:end) |
| 145 | + rprimeProbs(end-5:end) |
| 146 | + end |
| 147 | +end |
| 148 | + |
| 149 | +max(max(max(Policy_aprime(:,:,30)))) |
| 150 | + |
| 151 | +% size(Policy_aprime) |
| 152 | +% size(Policy_rprime) |
| 153 | +% max(max(max(abs((Policy_aprime-Policy_rprime).*(PolicyProbs(:,:,:,1)>0))))) |
| 154 | +% max(max(max(abs(Policy_aprime-shiftdim(Policy(1,:,:,:),1))))) |
| 155 | + |
| 156 | +Policy_arprime=zeros(N_a*N_r,N_z,N_j,2,'gpuArray'); |
| 157 | +Policy_arprime(:,:,:,1)=Policy_aprime+N_a*(Policy_rprime-1); |
| 158 | +Policy_arprime(:,:,:,2)=Policy_aprime+N_a*(Policy_rprime+1-1); |
| 159 | + |
| 160 | +if simoptions.iterate==0 |
| 161 | + PolicyProbs=gather(PolicyProbs); % simulation is always with cpu |
| 162 | + Policy_arprime=gather(Policy_arprime); |
| 163 | + if simoptions.parallel>=3 |
| 164 | + % Sparse matrix is not relevant for the simulation methods, only for iteration method |
| 165 | + simoptions.parallel=2; % will simulate on parallel cpu, then transfer solution to gpu |
| 166 | + end |
| 167 | + StationaryDistKron=StationaryDist_FHorz_Case1_Simulation_TwoProbs_raw(jequaloneDistKron,AgeWeightParamNames,Policy_arprime,PolicyProbs,N_a*N_r,N_z,N_j,pi_z, Parameters, simoptions); |
| 168 | +elseif simoptions.iterate==1 |
| 169 | + StationaryDistKron=StationaryDist_FHorz_Case1_Iteration_TwoProbs_raw(jequaloneDistKron,AgeWeightParamNames,Policy_arprime,PolicyProbs,N_a*N_r,N_z,N_j,pi_z,Parameters,simoptions); % zero is n_d, because we already converted Policy to only contain aprime |
| 170 | +end |
| 171 | + |
| 172 | +if simoptions.outputkron==0 |
| 173 | + StationaryDist=reshape(StationaryDistKron,[n_a,n_r,n_z,N_j]); |
| 174 | +else |
| 175 | + % If 1 then leave output in Kron form |
| 176 | + StationaryDist=reshape(StationaryDistKron,[N_a,N_r,N_z,N_j]); |
| 177 | +end |
| 178 | + |
| 179 | +end |
0 commit comments