time | Calls | line |
---|
| | 17 | function [mt_vf, mt_pf, mt_oploc,mt_coh, mt_o, mt_k, mt_l]=hhsave_VFI_3OC(varargin)
|
| | 18 |
|
0.014 | 1 | 19 | close all;
|
< 0.001 | 1 | 20 | bl_profile = false; % Switch off profile if running a tester/calling from another function
|
< 0.001 | 1 | 21 | bl_saveimg = true;
|
< 0.001 | 1 | 22 | if(bl_profile)
|
| | 23 | profile off;
|
| | 24 | profile on;
|
< 0.001 | 1 | 25 | end
|
| | 26 |
|
0.357 | 1 | 27 | addpath(genpath('/Users/sidhantkhanna/Documents/GitHub/BKS modified/'));
|
| | 28 |
|
< 0.001 | 1 | 29 | if ~isempty(varargin)
|
| | 30 |
|
0.002 | 1 | 31 | [ar_a,ar_z,ar_n, ...
|
| 1 | 32 | fl_alpha,fl_theta,fl_delta,fl_kappa,fl_r,fl_w,fl_phi,fl_ahi,fl_zhi, ...
|
| 1 | 33 | fl_risk,it_zgridno, it_agridno,it_ngridno, mt_trans_z,mt_trans_n,fl_beta, fl_mu_z, fl_sig_z, ...
|
| 1 | 34 | fl_rho_z,fl_lambda_z, fl_tolvfi ...
|
| 1 | 35 | ] = varargin{:};
|
| | 36 |
|
< 0.001 | 1 | 37 | bl_print = false;
|
< 0.001 | 1 | 38 | bl_plot = false;
|
| | 39 |
|
| | 40 |
|
| | 41 | else
|
| | 42 |
|
| | 43 | fl_ahi = 50;
|
| | 44 | fl_alo = 0;
|
| | 45 | fl_zhi = 2.2;
|
| | 46 | it_agridno = 100;
|
| | 47 | it_zgridno = 7;
|
| | 48 | it_ngridno = 2;
|
| | 49 | % ar_a = linspace(fl_alo ,fl_ahi,it_agridno);
|
| | 50 | mp_grid_control = containers.Map('KeyType','char', 'ValueType','any');
|
| | 51 | mp_grid_control('grid_powerspace_power') = 1;
|
| | 52 | % ar_a = ff_saveborr_grid(fl_alo, fl_ahi, it_agridno, mp_grid_control)
|
| | 53 | ar_a = ff_saveborr_grid(fl_alo, fl_ahi, it_agridno, 'grid_powerspace', mp_grid_control);
|
| | 54 | ar_a = ar_a';
|
| | 55 | fl_phi = 0;
|
| | 56 | fl_risk = 1.5;
|
| | 57 | fl_alpha = 0.4;
|
| | 58 | fl_theta = 0.79-fl_alpha;
|
| | 59 | fl_delta = 0.05;
|
| | 60 | fl_kappa = 0;
|
| | 61 | fl_mu_z = 0; % mean of AR(1) entrepeneurial productivity process
|
| | 62 | fl_rho_z = 0.9; % persistence parameter of the AR(1) entrepreneurial productivity process
|
| | 63 | fl_sig_z = 0.2;
|
| | 64 | fl_lambda_z = 3;
|
| | 65 | fl_beta = 0.92;
|
| | 66 | % [ar_z, mt_trans_z] = mytauchen_z(fl_mu_z,fl_rho_z,fl_sig_z,it_zgridno,fl_lambda_z);
|
| | 67 | [ar_z, mt_trans_z] = ffy_rouwenhorst(fl_rho_z,fl_sig_z,it_zgridno);
|
| | 68 | ar_z = exp(ar_z);
|
| | 69 | P1=mt_trans_z^1000;
|
| | 70 | sd=P1(1,:);
|
| | 71 | el=sd*ar_z;
|
| | 72 | ar_z =ar_z/el;
|
| | 73 | ar_z = ar_z';
|
| | 74 | ar_n = [0,1];
|
| | 75 | mt_trans_n = [0.1,0.9;0.9,0.1];
|
| | 76 |
|
| | 77 |
|
| | 78 | [fl_r,fl_w] = ...
|
| | 79 | deal(0.02,1);
|
| | 80 | fl_tolvfi = 10^-12;
|
| | 81 |
|
| | 82 | bl_print = true;
|
| | 83 | bl_plot = true;
|
| | 84 |
|
< 0.001 | 1 | 85 | end
|
| | 86 |
|
< 0.001 | 1 | 87 | fl_R = fl_r + fl_delta;
|
| | 88 |
|
| | 89 | %% Value function Iteration
|
| | 90 |
|
< 0.001 | 1 | 91 | mt_vf = zeros(it_agridno,it_zgridno,it_ngridno); % Value function
|
< 0.001 | 1 | 92 | mt_Tv = zeros(it_agridno,it_zgridno,it_ngridno); % Next guess of value function
|
< 0.001 | 1 | 93 | mt_oploc = zeros(it_agridno,it_zgridno,it_ngridno); %
|
| | 94 |
|
2.281 | 1 | 95 | [mt_util,mt_coh, mt_k, mt_l, mt_o] = hhsave_util_3OC(ar_a,ar_z,ar_n, ...
|
| 1 | 96 | fl_alpha,fl_theta,fl_delta,fl_kappa,fl_r,fl_w,fl_phi,fl_ahi,fl_zhi, ...
|
| 1 | 97 | fl_risk,it_zgridno, it_agridno,it_ngridno);
|
| | 98 |
|
< 0.001 | 1 | 99 | fl_crit = 1; % difference between current and next guess of value function
|
< 0.001 | 1 | 100 | mt_evf = zeros(it_agridno,it_zgridno,it_ngridno);
|
< 0.001 | 1 | 101 | while fl_crit > fl_tolvfi
|
| | 102 |
|
< 0.001 | 377 | 103 | for i = 1:it_ngridno
|
0.013 | 754 | 104 | mt_evf(:,:,i) = mt_vf(:,:,i)*mt_trans_z';
|
< 0.001 | 754 | 105 | end
|
< 0.001 | 377 | 106 | for j = 1:it_zgridno
|
0.094 | 2639 | 107 | mt_evf1(:,j,:) = squeeze(mt_evf(:,j,:))*mt_trans_n';
|
0.001 | 2639 | 108 | end
|
| | 109 |
|
0.040 | 377 | 110 | mt_ev1 = repmat(mt_evf1, [1 1 1 it_agridno]);
|
0.074 | 377 | 111 | mt_ev1 = permute(mt_ev1,[4 2 3 1]);
|
| | 112 |
|
0.148 | 377 | 113 | [mt_Tvf,mt_oploc] = max((mt_util+fl_beta.*(mt_ev1)),[], 4);
|
| | 114 |
|
0.002 | 377 | 115 | mt_diff = mt_Tvf-mt_vf;
|
0.002 | 377 | 116 | mt_diff = mt_diff(:);
|
0.002 | 377 | 117 | fl_crit = norm(mt_diff);
|
< 0.001 | 377 | 118 | mt_vf = mt_Tvf;
|
< 0.001 | 377 | 119 | end
|
| | 120 |
|
| | 121 |
|
0.002 | 1 | 122 | mt_pf = ar_a(mt_oploc);
|
< 0.001 | 1 | 123 | mt_con = mt_coh - mt_pf;
|
< 0.001 | 1 | 124 | mt_savefraccoh = mt_pf./mt_coh;
|
| | 125 |
|
< 0.001 | 1 | 126 | if(bl_print)
|
| | 127 |
|
| | 128 | st_ar_a = strsplit(num2str(ar_a));
|
| | 129 | for i=1:it_agridno
|
| | 130 | st_a(i) =strcat('a=', st_ar_a(i));
|
| | 131 | end
|
| | 132 | st_ar_z = strsplit(num2str(ar_z));
|
| | 133 | disp('Table for value function at l=0');
|
| | 134 | tb_VF = array2table(mt_vf(:,:,1),'VariableNames',{'z1','z2','z3','z4','z5','z6','z7'});
|
| | 135 | tb_VF.Properties.RowNames = st_a;
|
| | 136 | disp(tb_VF);
|
| | 137 | disp('Table for value function at l=1');
|
| | 138 | tb_VF = array2table(mt_vf(:,:,2),'VariableNames',{'z1','z2','z3','z4','z5','z6','z7'});
|
| | 139 | tb_VF.Properties.RowNames = st_a;
|
| | 140 | disp(tb_VF);
|
| | 141 | disp('Table for Policy function at l=0');
|
| | 142 | tb_pf = array2table(mt_pf(:,:,1),'VariableNames',{'z1','z2','z3','z4','z5','z6','z7'});
|
| | 143 | tb_pf.Properties.RowNames = st_a;
|
| | 144 | disp(tb_pf);
|
| | 145 | disp('Table for Policy function at l=1');
|
| | 146 | tb_pf = array2table(mt_pf(:,:,2),'VariableNames',{'z1','z2','z3','z4','z5','z6','z7'});
|
| | 147 | tb_pf.Properties.RowNames = st_a;
|
| | 148 | disp(tb_pf);
|
| | 149 | disp('Table for Consumption at l=0 ');
|
| | 150 | tb_con = array2table(mt_con(:,:,1),'VariableNames',{'z1','z2','z3','z4','z5','z6','z7'});
|
| | 151 | tb_con.Properties.RowNames = st_a;
|
| | 152 | disp(tb_con);
|
| | 153 | disp('Table for Consumption at l=1 ');
|
| | 154 | tb_con = array2table(mt_con(:,:,2),'VariableNames',{'z1','z2','z3','z4','z5','z6','z7'});
|
| | 155 | tb_con.Properties.RowNames = st_a;
|
| | 156 | disp(tb_con);
|
| | 157 | disp('Table for Savings/COH at l=0');
|
| | 158 | tb_savefraccoh = array2table(mt_savefraccoh(:,:,1),'VariableNames',{'z1','z2','z3','z4','z5','z6','z7'});
|
| | 159 | tb_savefraccoh.Properties.RowNames = st_a;
|
| | 160 | disp(tb_savefraccoh);
|
| | 161 | disp('Table for Savings/COH at l=1');
|
| | 162 | tb_savefraccoh = array2table(mt_savefraccoh(:,:,2),'VariableNames',{'z1','z2','z3','z4','z5','z6','z7'});
|
| | 163 | tb_savefraccoh.Properties.RowNames = st_a;
|
| | 164 | disp(tb_savefraccoh);
|
| | 165 |
|
< 0.001 | 1 | 166 | end
|
| | 167 |
|
| | 168 |
|
| | 169 |
|
| | 170 | %% Plot Value function and Policy Function
|
| | 171 |
|
| | 172 | % 3D Value function - l=0
|
< 0.001 | 1 | 173 | if(bl_plot)
|
| | 174 |
|
| | 175 | figure(1)
|
| | 176 | surf(ar_z,ar_a,mt_vf(:,:,1));
|
| | 177 | title('Value Function, l=0');
|
| | 178 | xlabel('Productivity') ;
|
| | 179 | ylabel('Assets');
|
| | 180 | zlabel('Value');
|
| | 181 | xt = get(gca, 'XTick');
|
| | 182 | set(gca, 'XTick', xt, 'XTickLabel', xt*fl_ahi/it_agridno);
|
| | 183 | if(bl_saveimg)
|
| | 184 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Households/figures/hhsave_VFI_3OC/Valuefunction_3D_0.png')
|
| | 185 | end
|
| | 186 |
|
| | 187 | figure(2)
|
| | 188 | surf(ar_z,ar_a,mt_vf(:,:,2));
|
| | 189 | title('Value Function, l=1');
|
| | 190 | xlabel('Productivity') ;
|
| | 191 | ylabel('Assets');
|
| | 192 | zlabel('Value');
|
| | 193 | xt = get(gca, 'XTick');
|
| | 194 | set(gca, 'XTick', xt, 'XTickLabel', xt*fl_ahi/it_agridno);
|
| | 195 | if(bl_saveimg)
|
| | 196 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Households/figures/hhsave_VFI_3OC/Valuefunction_3D_1.png')
|
| | 197 | end
|
| | 198 |
|
| | 199 | % 2D Value function
|
| | 200 | figure(3)
|
| | 201 | %chart = plot(mt_vf);
|
| | 202 | for i = 1: it_zgridno
|
| | 203 | a(i) = plot((mt_vf(:,i,1)), '.');
|
| | 204 | b{i} = sprintf(['Z shock =', num2str(ar_z(i))]);
|
| | 205 | hold on
|
| | 206 | end
|
| | 207 | clr = jet(numel(a));
|
| | 208 | for m = 1:numel(a)
|
| | 209 | set(a(m),'Color',clr(m,:))
|
| | 210 | end
|
| | 211 | legend(b,'Location','South')
|
| | 212 | %clr = jet(numel(chart));
|
| | 213 | %for m = 1:numel(chart)
|
| | 214 | % set(chart(m),'Color',clr(m,:))
|
| | 215 | %end
|
| | 216 | title('Value Function, l=0');
|
| | 217 | xlabel('Asset') ;
|
| | 218 | ylabel('Value');
|
| | 219 | xt = get(gca, 'XTick');
|
| | 220 | set(gca, 'XTick', xt, 'XTickLabel', xt*fl_ahi/it_agridno);
|
| | 221 | if(bl_saveimg)
|
| | 222 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Households/figures/hhsave_VFI_3OC/Valuefunction_2D_0.png')
|
| | 223 | end
|
| | 224 |
|
| | 225 | figure(4)
|
| | 226 | %chart = plot(mt_vf);
|
| | 227 | for i = 1: it_zgridno
|
| | 228 | a(i) = plot((mt_vf(:,i,2)),'.');
|
| | 229 | b{i} = sprintf(['Z shock =', num2str(ar_z(i))]);
|
| | 230 | hold on
|
| | 231 | end
|
| | 232 | clr = jet(numel(a));
|
| | 233 | for m = 1:numel(a)
|
| | 234 | set(a(m),'Color',clr(m,:))
|
| | 235 | end
|
| | 236 | legend(b,'Location','South')
|
| | 237 | %clr = jet(numel(chart));
|
| | 238 | %for m = 1:numel(chart)
|
| | 239 | % set(chart(m),'Color',clr(m,:))
|
| | 240 | %end
|
| | 241 | title('Value Function, l=1');
|
| | 242 | xlabel('Asset') ;
|
| | 243 | ylabel('Value');
|
| | 244 | xt = get(gca, 'XTick');
|
| | 245 | set(gca, 'XTick', xt, 'XTickLabel', xt*fl_ahi/it_agridno);
|
| | 246 | if(bl_saveimg)
|
| | 247 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Households/figures/hhsave_VFI_3OC/Valuefunction_2D_1.png')
|
| | 248 | end
|
| | 249 |
|
| | 250 |
|
| | 251 | figure(5)
|
| | 252 | for i = 1: it_zgridno
|
| | 253 | a(i) = plot((mt_pf(:,i,1)),'.');
|
| | 254 | b{i} = sprintf(['Z shock =', num2str(ar_z(i))]);
|
| | 255 | hold on
|
| | 256 | end
|
| | 257 | clr = jet(numel(a));
|
| | 258 | for m = 1:numel(a)
|
| | 259 | set(a(m),'Color',clr(m,:))
|
| | 260 | end
|
| | 261 | legend(b,'Location','north')
|
| | 262 | title('Policy Function,l=0');
|
| | 263 | xlabel('Asset') ;
|
| | 264 | ylabel('Asset Choice');
|
| | 265 | xt = get(gca, 'XTick');
|
| | 266 | set(gca, 'XTick', xt, 'XTickLabel', xt*fl_ahi/it_agridno);
|
| | 267 | if(bl_saveimg)
|
| | 268 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Households/figures/hhsave_VFI_3OC/Policyfunction_0.png')
|
| | 269 | end
|
| | 270 |
|
| | 271 | figure(6)
|
| | 272 | for i = 1: it_zgridno
|
| | 273 | a(i) = plot((mt_pf(:,i,2)),'.');
|
| | 274 | b{i} = sprintf(['Z shock =', num2str(ar_z(i))]);
|
| | 275 | hold on
|
| | 276 | end
|
| | 277 | clr = jet(numel(a));
|
| | 278 | for m = 1:numel(a)
|
| | 279 | set(a(m),'Color',clr(m,:))
|
| | 280 | end
|
| | 281 | legend(b,'Location','north')
|
| | 282 | title('Policy Function, l=1');
|
| | 283 | xlabel('Asset') ;
|
| | 284 | ylabel('Asset choice');
|
| | 285 | xt = get(gca, 'XTick');
|
| | 286 | set(gca, 'XTick', xt, 'XTickLabel', xt*fl_ahi/it_agridno);
|
| | 287 | if(bl_saveimg)
|
| | 288 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Households/figures/hhsave_VFI_3OC/Policyfunction_1.png')
|
| | 289 | end
|
| | 290 |
|
| | 291 | figure(7)
|
| | 292 | for i = 1: it_zgridno
|
| | 293 | a(i) = plot((mt_con(:,i,1)),'-o');
|
| | 294 | b{i} = sprintf(['Z shock =', num2str(ar_z(i))]);
|
| | 295 | hold on
|
| | 296 | end
|
| | 297 | clr = jet(numel(a));
|
| | 298 | for m = 1:numel(a)
|
| | 299 | set(a(m),'Color',clr(m,:))
|
| | 300 | end
|
| | 301 | legend(b,'Location','north')
|
| | 302 | title('Consumption,l=0');
|
| | 303 | xlabel('Asset') ;
|
| | 304 | ylabel('Consumption');
|
| | 305 | xt = get(gca, 'XTick');
|
| | 306 | set(gca, 'XTick', xt, 'XTickLabel', xt*fl_ahi/it_agridno);
|
| | 307 | if(bl_saveimg)
|
| | 308 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Households/figures/hhsave_VFI_3OC/Consumption_0.png')
|
| | 309 | end
|
| | 310 |
|
| | 311 | figure(8)
|
| | 312 | for i = 1: it_zgridno
|
| | 313 | a(i) = plot((mt_con(:,i,2)),'-o');
|
| | 314 | b{i} = sprintf(['Z shock =', num2str(ar_z(i))]);
|
| | 315 | hold on
|
| | 316 | end
|
| | 317 | clr = jet(numel(a));
|
| | 318 | for m = 1:numel(a)
|
| | 319 | set(a(m),'Color',clr(m,:))
|
| | 320 | end
|
| | 321 | legend(b,'Location','north')
|
| | 322 | title('Consumption, l=1');
|
| | 323 | xlabel('Asset') ;
|
| | 324 | ylabel('Consumption');
|
| | 325 | xt = get(gca, 'XTick');
|
| | 326 | set(gca, 'XTick', xt, 'XTickLabel', xt*fl_ahi/it_agridno);
|
| | 327 | if(bl_saveimg)
|
| | 328 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Households/figures/hhsave_VFI_3OC/Consumption_1.png')
|
| | 329 | end
|
| | 330 |
|
| | 331 | figure(9)
|
| | 332 | for i = 1: it_zgridno
|
| | 333 | a(i) = plot(mt_savefraccoh(:,i,1),'-o');
|
| | 334 | b{i} = sprintf(['Z shock =', num2str(ar_z(i))]);
|
| | 335 | hold on
|
| | 336 | end
|
| | 337 | clr = jet(numel(a));
|
| | 338 | for m = 1:numel(a)
|
| | 339 | set(a(m),'Color',clr(m,:))
|
| | 340 | end
|
| | 341 | legend(b,'Location','South')
|
| | 342 | title('SavingsfracCOH,l=0');
|
| | 343 | xlabel('Asset') ;
|
| | 344 | ylabel('SavingsfracCOH');
|
| | 345 | ylim([0 1])
|
| | 346 | xt = get(gca, 'XTick');
|
| | 347 | set(gca, 'XTick', xt, 'XTickLabel', xt*fl_ahi/it_agridno);
|
| | 348 | if(bl_saveimg)
|
| | 349 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Households/figures/hhsave_VFI_3OC/Savings_COH_0.png')
|
| | 350 | end
|
| | 351 |
|
| | 352 | figure(10)
|
| | 353 | for i = 1: it_zgridno
|
| | 354 | a(i) = plot(mt_savefraccoh(:,i,2),'-o');
|
| | 355 | b{i} = sprintf(['Z shock =', num2str(ar_z(i))]);
|
| | 356 | hold on
|
| | 357 | end
|
| | 358 | clr = jet(numel(a));
|
| | 359 | for m = 1:numel(a)
|
| | 360 | set(a(m),'Color',clr(m,:))
|
| | 361 | end
|
| | 362 | legend(b,'Location','South')
|
| | 363 | title('SavingsfracCOH, l=1');
|
| | 364 | xlabel('Asset') ;
|
| | 365 | ylabel('SavingsfracCOH');
|
| | 366 | ylim([0 1])
|
| | 367 | xt = get(gca, 'XTick');
|
| | 368 | set(gca, 'XTick', xt, 'XTickLabel', xt*fl_ahi/it_agridno);
|
| | 369 | if(bl_saveimg)
|
| | 370 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Households/figures/hhsave_VFI_3OC/Savings_COH_1.png')
|
| | 371 | end
|
| | 372 |
|
| | 373 |
|
< 0.001 | 1 | 374 | end
|
| | 375 |
|
| | 376 |
|
| | 377 |
|
| | 378 | %% Plot Value function and Policy Function
|
| | 379 |
|
< 0.001 | 1 | 380 | if(bl_profile)
|
| | 381 | profile off;
|
| | 382 | profile viewer;
|
| | 383 | st_file_name = '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Profile/Households/hhsave_VFI_3OC';
|
| | 384 | profsave(profile('info'), st_file_name);
|
< 0.001 | 1 | 385 | end
|
Other subfunctions in this file are not included in this listing.