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