time | Calls | line |
---|
| | 25 | function [mt_k_star, mt_l_star, mt_id]=optikl(varargin)
|
| | 26 |
|
0.003 | 1 | 27 | close all;
|
< 0.001 | 1 | 28 | bl_profile = false; % Switch off profile if running a tester/calling from another function
|
< 0.001 | 1 | 29 | bl_saveimg = false;
|
< 0.001 | 1 | 30 | if(bl_profile)
|
| | 31 | profile off;
|
| | 32 | profile on;
|
< 0.001 | 1 | 33 | end
|
| | 34 |
|
0.562 | 1 | 35 | addpath(genpath('/Users/sidhantkhanna/Documents/GitHub/BKS modified/'));
|
| | 36 |
|
| | 37 |
|
< 0.001 | 1 | 38 | if ~isempty(varargin)
|
| | 39 |
|
0.002 | 1 | 40 | [ar_a,ar_z, ...
|
| 1 | 41 | fl_alpha,fl_theta,fl_delta,fl_kappa,fl_r,fl_w,fl_phi,fl_ahi,fl_zhi, ...
|
| 1 | 42 | it_zgridno, it_agridno, ...
|
| 1 | 43 | ] = varargin{:};
|
| | 44 |
|
< 0.001 | 1 | 45 | bl_print = false;
|
< 0.001 | 1 | 46 | bl_plot = false;
|
| | 47 |
|
| | 48 | else
|
| | 49 | close all;
|
| | 50 |
|
| | 51 | fl_ahi = 5;
|
| | 52 | fl_zhi = 2.2;
|
| | 53 | it_agridno = 5;
|
| | 54 | it_zgridno = 6;
|
| | 55 | ar_a = linspace(0,fl_ahi,it_agridno);
|
| | 56 | ar_z = linspace(1,fl_zhi,it_zgridno);
|
| | 57 | fl_phi = 0.1;
|
| | 58 |
|
| | 59 | fl_alpha = 0.4;
|
| | 60 | fl_theta = 0.79-fl_alpha;
|
| | 61 | fl_delta = 0.05;
|
| | 62 | fl_kappa = 0;
|
| | 63 |
|
| | 64 | [fl_r,fl_w,fl_ahi] = ...
|
| | 65 | deal(0.05,3,100);
|
| | 66 | bl_print = true;
|
| | 67 | bl_plot = true;
|
| | 68 |
|
< 0.001 | 1 | 69 | end
|
| | 70 |
|
| | 71 | %% Set Parameters
|
| | 72 |
|
< 0.001 | 1 | 73 | fl_R = fl_r + fl_delta;
|
| | 74 |
|
| | 75 |
|
| | 76 | %% Set Controls
|
| | 77 |
|
< 0.001 | 1 | 78 | bl_constrained = true;
|
< 0.001 | 1 | 79 | if( fl_phi == 1)
|
| | 80 | bl_constrained = false;
|
< 0.001 | 1 | 81 | end
|
< 0.001 | 1 | 82 | bl_feas = true; %
|
| | 83 |
|
| | 84 |
|
| | 85 | %% Mesh Grids
|
| | 86 |
|
< 0.001 | 1 | 87 | [a_m,z_m] = meshgrid(ar_a,ar_z); % creates 2 matrices of size it_zgridno x it_agridno
|
| | 88 | % a_m has it_zgridno no. of identical rows, each row is ar_a. So each column
|
| | 89 | % has identical a element repeated it_zgridno times
|
| | 90 | % z_m has it_agridno no of identical columns, each column is ar_z. So each
|
| | 91 | % row has identical z element repeated it_agridno times
|
| | 92 |
|
< 0.001 | 1 | 93 | ar_a_v = a_m(:); % matrix unlayered one column under another
|
< 0.001 | 1 | 94 | ar_z_v = z_m(:); % matrix unlayered one column under another
|
| | 95 |
|
| | 96 |
|
0.001 | 1 | 97 | ar_k_unc = exp((log(fl_R./(ar_z_v.*fl_alpha))- fl_theta*log(fl_R./(ar_z_v.*fl_alpha))+ fl_theta.*log(fl_w./(ar_z_v.*fl_theta)))./(fl_alpha+fl_theta-1));
|
0.002 | 1 | 98 | ar_l_unc = exp((log(fl_w./(ar_z_v.*fl_theta))+ fl_alpha*log(fl_R./(ar_z_v.*fl_alpha))- fl_alpha.*log(fl_w./(ar_z_v.*fl_theta)))./(fl_alpha+fl_theta-1));
|
| | 99 |
|
< 0.001 | 1 | 100 | mt_k_unc = reshape(ar_k_unc, [it_zgridno,it_agridno]);
|
0.001 | 1 | 101 | mt_l_unc = reshape(ar_l_unc, [it_zgridno,it_agridno]);
|
| | 102 |
|
< 0.001 | 1 | 103 | mt_k_unc = mt_k_unc';
|
< 0.001 | 1 | 104 | mt_l_unc = mt_l_unc';
|
| | 105 |
|
| | 106 |
|
| | 107 | %% Optimal Unconstrained
|
| | 108 |
|
< 0.001 | 1 | 109 | if (~bl_constrained)
|
| | 110 |
|
| | 111 | % in the constrained case, a can not matter, but z does matter
|
| | 112 | if(bl_feas)
|
| | 113 | feas = (1+fl_r)*fl_kappa;
|
| | 114 | feas_v = ar_z_v.*ar_k_unc.^fl_alpha.*ar_l_unc.^fl_theta;
|
| | 115 | for i = 1:numel(ar_a_v)
|
| | 116 | if (feas_v(i)<feas)
|
| | 117 | ar_k_unc(i) = 0;
|
| | 118 | ar_l_unc(i) = 0;
|
| | 119 | end
|
| | 120 | end
|
| | 121 | end
|
| | 122 |
|
| | 123 | mt_k_unc = reshape(ar_k_unc, [it_zgridno,it_agridno]);
|
| | 124 | mt_l_unc = reshape(ar_l_unc, [it_zgridno,it_agridno]);
|
| | 125 |
|
| | 126 | mt_k_unc = mt_k_unc';
|
| | 127 | mt_l_unc = mt_l_unc';
|
| | 128 |
|
| | 129 | mt_k_star = mt_k_unc;
|
| | 130 | mt_l_star = mt_l_unc;
|
| | 131 | mt_id = ones(it_zgridno,it_agridno); % index of min between constraint k and unconstraned k
|
| | 132 |
|
| | 133 |
|
| | 134 | if( bl_print)
|
| | 135 | disp('Below is mt_k_star')
|
| | 136 | disp(size(mt_k_star));
|
| | 137 | disp(mt_k_star);
|
| | 138 | disp('Below is mt_l_star')
|
| | 139 | disp(size(mt_l_star));
|
| | 140 | disp(mt_l_star);
|
| | 141 | end
|
| | 142 |
|
< 0.001 | 1 | 143 | else
|
| | 144 |
|
| | 145 | %% Optimal Constrained
|
| | 146 |
|
| | 147 | % calls function con_kl to evaluate the constraint on capital
|
0.363 | 1 | 148 | [ar_k_bar,ar_l_bar] = con_kl(ar_a_v, ar_z_v,ar_a,ar_z, fl_ahi,fl_zhi,fl_phi,fl_theta,fl_alpha,fl_w,fl_r,fl_delta,fl_kappa,it_agridno,it_zgridno);
|
| | 149 |
|
< 0.001 | 1 | 150 | mt_k_bar=reshape(ar_k_bar, [it_zgridno,it_agridno]);
|
< 0.001 | 1 | 151 | mt_l_bar=reshape(ar_l_bar, [it_zgridno,it_agridno]);
|
| | 152 |
|
< 0.001 | 1 | 153 | mt_k_bar = mt_k_bar';
|
< 0.001 | 1 | 154 | mt_l_bar = mt_l_bar';
|
| | 155 |
|
| | 156 |
|
< 0.001 | 1 | 157 | k3 = [ar_k_unc,ar_k_bar];
|
< 0.001 | 1 | 158 | [k4,id] = min(abs(k3')); % K star is min of k_unc and k_con. k3 transposed for the correct order for id
|
< 0.001 | 1 | 159 | ar_k_star = k4';
|
< 0.001 | 1 | 160 | l3 =[ar_l_unc,ar_l_bar];
|
< 0.001 | 1 | 161 | ar_l_star = zeros(numel(ar_a_v),1); % assigning l star in constrained case
|
| | 162 |
|
< 0.001 | 1 | 163 | for i = 1:numel(ar_a_v)
|
< 0.001 | 700 | 164 | ar_l_star(i,1)=l3(i,id(i));
|
< 0.001 | 700 | 165 | end
|
| | 166 |
|
< 0.001 | 1 | 167 | if(bl_feas)
|
< 0.001 | 1 | 168 | feas = (1+fl_r)*fl_kappa;
|
< 0.001 | 1 | 169 | feas_v = ar_z_v.*ar_k_star.^fl_alpha.*ar_l_star.^fl_theta;
|
< 0.001 | 1 | 170 | for i = 1:numel(ar_a_v)
|
< 0.001 | 700 | 171 | if (feas_v(i)<feas)
|
| | 172 | ar_k_star(i) = 0;
|
| | 173 | ar_l_star(i) = 0;
|
< 0.001 | 700 | 174 | end
|
< 0.001 | 700 | 175 | end
|
< 0.001 | 1 | 176 | end
|
| | 177 |
|
| | 178 |
|
< 0.001 | 1 | 179 | mt_k_star=reshape(ar_k_star, [it_zgridno,it_agridno]);
|
< 0.001 | 1 | 180 | mt_l_star=reshape(ar_l_star, [it_zgridno,it_agridno]);
|
< 0.001 | 1 | 181 | mt_id = reshape(id, [it_zgridno,it_agridno]);
|
| | 182 |
|
< 0.001 | 1 | 183 | mt_k_star=mt_k_star';
|
< 0.001 | 1 | 184 | mt_l_star=mt_l_star';
|
< 0.001 | 1 | 185 | mt_id = mt_id';
|
| | 186 |
|
< 0.001 | 1 | 187 | if( bl_print)
|
| | 188 | disp('Below is mt_k_unc')
|
| | 189 | disp(size(mt_k_unc));
|
| | 190 | disp(mt_k_unc);
|
| | 191 | disp('Below is mt_k_con')
|
| | 192 | disp(mt_k_bar);
|
| | 193 | disp('Below is mt_k_star')
|
| | 194 | disp(size(mt_k_star));
|
| | 195 | disp(mt_k_star);
|
| | 196 | disp('Below is mt_l_unc')
|
| | 197 | disp(size(mt_l_unc));
|
| | 198 | disp(mt_l_unc);
|
| | 199 | disp('Below is mt_l_con')
|
| | 200 | disp(mt_l_bar);
|
| | 201 | disp('Below is mt_l_star')
|
| | 202 | disp(mt_l_star);
|
| | 203 |
|
| | 204 |
|
< 0.001 | 1 | 205 | end
|
| | 206 |
|
< 0.001 | 1 | 207 | if(bl_plot)
|
| | 208 |
|
| | 209 | %% 3D Plots for k* and l*
|
| | 210 |
|
| | 211 | figure (1)
|
| | 212 | a1 = surf(ar_z, ar_a, mt_k_bar , 'FaceColor','r', 'FaceAlpha',0.5, 'EdgeColor','r'); M1 = 'k bar';
|
| | 213 | hold on
|
| | 214 | a2 = surf(ar_z, ar_a, mt_k_unc , 'FaceColor','b', 'FaceAlpha',0.5, 'EdgeColor','b'); M2 = 'k unc';
|
| | 215 | %hold on
|
| | 216 | %a3 = surf(ar_z, ar_a, mt_k_star,'FaceColor','g', 'FaceAlpha',0.5, 'EdgeColor','g'); M3 = 'k star' ;
|
| | 217 | hold off
|
| | 218 | xlabel('z');
|
| | 219 | ylabel('a');
|
| | 220 | zlabel('k(unconstrained/constrained/optimal)');
|
| | 221 | LEG = legend([a1;a2], M1, M2, 'Location','north')
|
| | 222 | LEG.FontSize = 20;
|
| | 223 | view(150,15);
|
| | 224 | set(gca,'FontSize',17)
|
| | 225 | if(bl_saveimg)
|
| | 226 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/figures/optikl/Optimum_k.png')
|
| | 227 | end
|
| | 228 |
|
| | 229 | figure (2)
|
| | 230 | b1 = surf(ar_z, ar_a, mt_l_bar, 'FaceColor','r', 'FaceAlpha',0.5, 'EdgeColor','r'); N1 = 'l bar';
|
| | 231 | hold on
|
| | 232 | b2 = surf(ar_z, ar_a, mt_l_unc, 'FaceColor','b', 'FaceAlpha',0.5, 'EdgeColor','b'); N2 = 'l unc';
|
| | 233 | %hold on
|
| | 234 | %b3 = surf(ar_z, ar_a, mt_l_star,'FaceColor','g', 'FaceAlpha',0.5, 'EdgeColor','g'); N3 = 'l star' ;
|
| | 235 | hold off
|
| | 236 | xlabel('z');
|
| | 237 | ylabel('a');
|
| | 238 | zlabel('l(unconstrained/constrained/optimal)');
|
| | 239 | LEG = legend([b1;b2], N1, N2, 'Location','north')
|
| | 240 | LEG.FontSize = 20;
|
| | 241 | view(150,15);
|
| | 242 | set(gca,'FontSize',17)
|
| | 243 | if(bl_saveimg)
|
| | 244 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/figures/optikl/Optimum_l.png')
|
| | 245 | end
|
| | 246 |
|
| | 247 | %% 2D plot for k*
|
| | 248 |
|
| | 249 | % Jet color Graph All
|
| | 250 | figure(3);
|
| | 251 | hold on;
|
| | 252 |
|
| | 253 | % one line, unconstrained choice
|
| | 254 | pl_unc = plot(ar_z, mt_k_unc(1,:));
|
| | 255 | pl_unc.HandleVisibility = 'on';
|
| | 256 | pl_unc.LineStyle = '-';
|
| | 257 | pl_unc.Color = 'black';
|
| | 258 | pl_unc.LineWidth = 4;
|
| | 259 |
|
| | 260 | % one constrained choice line, low a
|
| | 261 | pl_unc = plot(ar_z, mt_k_bar(2,:));
|
| | 262 | pl_unc.HandleVisibility = 'on';
|
| | 263 | pl_unc.LineStyle = '--';
|
| | 264 | pl_unc.Color = 'red';
|
| | 265 | pl_unc.LineWidth = 3;
|
| | 266 |
|
| | 267 | % one constrained choice line, high a
|
| | 268 | pl_unc = plot(ar_z, mt_k_bar(4,:));
|
| | 269 | pl_unc.HandleVisibility = 'on';
|
| | 270 | pl_unc.LineStyle = '-.';
|
| | 271 | pl_unc.Color = 'blue';
|
| | 272 | pl_unc.LineWidth = 3;
|
| | 273 |
|
| | 274 | % additional options
|
| | 275 | grid on;
|
| | 276 | grid minor;
|
| | 277 | xlim([1, 2.2]);
|
| | 278 | ylim([0, 10]);
|
| | 279 |
|
| | 280 | title('Unconstrained Choices and Endogenous Borrowing Constraints');
|
| | 281 | ylabel('Capital Levels');
|
| | 282 | xlabel({'Productivity Levels'});
|
| | 283 | legend({'Unconstrained Optimal Choice',...
|
| | 284 | 'Borrowing Bound low A', ...
|
| | 285 | 'Borrowing Bound high A'}, 'Location', 'best', 'Interpreter','latex');
|
| | 286 |
|
| | 287 | if(bl_saveimg)
|
| | 288 | saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/figures/optikl/Optimum_2D.png')
|
| | 289 | end
|
| | 290 |
|
| | 291 | hold off;
|
| | 292 |
|
< 0.001 | 1 | 293 | end
|
| | 294 |
|
< 0.001 | 1 | 295 | end
|
| | 296 |
|
< 0.001 | 1 | 297 | if(bl_profile)
|
| | 298 | profile off;
|
| | 299 | profile viewer;
|
| | 300 | st_file_name = '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Profile/Firms/optikl';
|
| | 301 | profsave(profile('info'), st_file_name);
|
| | 302 |
|
| | 303 |
|
< 0.001 | 1 | 304 | end
|