This is a static copy of a profile report

Home

Function details for optiklThis is a static copy of a profile report

Home

optikl (Calls: 1, Time: 0.343 s)
Generated 25-Apr-2020 01:45:54 using performance time.
function in file /Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/optikl.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
optikl_unc_testerscript1
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
34
addpath(genpath('/Users/sidhan...
10.334 s97.6%
276
profile off;
10.005 s1.4%
86
[a_m,z_m] = meshgrid(ar_a,ar_z...
10.002 s0.7%
42
] = varargin{:};
10.000 s0.1%
40
fl_alpha,fl_theta,fl_delta,fl_...
10.000 s0.0%
All other lines  0.001 s0.3%
Totals  0.343 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
genpathfunction10.235 s68.5%
addpathfunction10.099 s28.9%
meshgridfunction10.002 s0.5%
Self time (built-ins, overhead, etc.)  0.007 s2.1%
Totals  0.343 s100% 
Code Analyzer results
Line numberMessage
199Terminate statement with semicolon to suppress output (in functions).
217Terminate statement with semicolon to suppress output (in functions).
Coverage results
Show coverage for parent directory
Total lines in function259
Non-code lines (comments, blank lines)87
Code lines (lines that can run)172
Code lines that did run32
Code lines that did not run140
Coverage (did run/can run)18.60 %
Function listing
time 
Calls 
 line
  24 
function [mt_k_star, mt_l_star]=optikl(varargin)
  25 

  26 
close all;
  27 
bl_profile = true;      % Switch off profile if running a tester/calling from another function
  28 
bl_saveimg = true;
  29 
if(bl_profile)
  30 
profile off;
  31 
profile on;
< 0.001 
      1 
  32
end 
  33 

  0.334 
      1 
  34
addpath(genpath('/Users/sidhantkhanna/Documents/GitHub/BKS modified/')); 
  35 

  36 

< 0.001 
      1 
  37
if ~isempty(varargin) 
  38 
    
< 0.001 
      1 
  39
    [ar_a,ar_z, ... 
      1 
  40
        fl_alpha,fl_theta,fl_delta,fl_kappa,fl_r,fl_w,fl_phi,fl_ahi,fl_zhi, ... 
      1 
  41
         it_zgridno, it_agridno, ... 
      1 
  42
        ] = varargin{:}; 
  43 
    
< 0.001 
      1 
  44
    bl_print  = false; 
< 0.001 
      1 
  45
    bl_plot   = false; 
  46 
    
  47 
else
  48 
    close all;
  49 
    
  50 
    fl_ahi         = 5;
  51 
    fl_zhi         = 2.2;
  52 
    it_agridno     = 5;
  53 
    it_zgridno     = 6;
  54 
    ar_a = linspace(0,fl_ahi,it_agridno);
  55 
    ar_z = linspace(1,fl_zhi,it_zgridno);
  56 
    fl_phi         = 0.5;
  57 
    
  58 
    fl_alpha = 0.4;
  59 
    fl_theta = 0.79-fl_alpha;
  60 
    fl_delta = 0.08;
  61 
    fl_kappa = 0;
  62 
        
  63 
    [fl_r,fl_w,fl_ahi] = ...
  64 
        deal(0.05,3,100);
  65 
    bl_print       = true;
  66 
    bl_plot        = true;
  67 
    
< 0.001 
      1 
  68
end 
  69 

  70 
%% Set Parameters
  71 

< 0.001 
      1 
  72
fl_R = fl_r + fl_delta; 
  73 

  74 

  75 
%% Set Controls
  76 

< 0.001 
      1 
  77
bl_constrained = true; 
< 0.001 
      1 
  78
if( fl_phi == 1) 
< 0.001 
      1 
  79
    bl_constrained = false; 
< 0.001 
      1 
  80
end 
< 0.001 
      1 
  81
bl_feas = true; % 
  82 

  83 

  84 
%% Mesh Grids
  85 

  0.002 
      1 
  86
[a_m,z_m] = meshgrid(ar_a,ar_z);    % creates 2 matrices of size it_zgridno x it_agridno  
  87 
                                    % a_m has it_zgridno no. of identical rows, each row is ar_a. So each column
  88 
                                    % has identical a element repeated it_zgridno times
  89 
                                    % z_m has it_agridno no of identical columns, each column is ar_z. So each
  90 
                                    % row has identical z element repeated it_agridno times
  91 

< 0.001 
      1 
  92
ar_a_v = a_m(:);                    % matrix unlayered one column under another 
< 0.001 
      1 
  93
ar_z_v = z_m(:);                    % matrix unlayered one column under another 
  94 

  95 

  96 
%% Optimal Unconstrained 
  97 

< 0.001 
      1 
  98
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 
  99
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)); 
 100 

< 0.001 
      1 
 101
mt_k_unc = reshape(ar_k_unc, [it_zgridno,it_agridno]); 
< 0.001 
      1 
 102
mt_l_unc = reshape(ar_l_unc, [it_zgridno,it_agridno]); 
 103 

< 0.001 
      1 
 104
mt_k_unc = mt_k_unc'; 
< 0.001 
      1 
 105
mt_l_unc = mt_l_unc'; 
 106 

< 0.001 
      1 
 107
if (~bl_constrained) 
 108 
    
 109 
    % in the constrained case, a can not matter, but z does matter
 110 
    
< 0.001 
      1 
 111
    mt_k_star = mt_k_unc; 
< 0.001 
      1 
 112
    mt_l_star = mt_l_unc; 
 113 
    
< 0.001 
      1 
 114
if( bl_print) 
 115 
    disp('Below is mt_k_star')
 116 
    disp(size(mt_k_star));
 117 
    disp(mt_k_star);
 118 
    disp('Below is mt_l_star')
 119 
    disp(size(mt_l_star));
 120 
    disp(mt_l_star);
 121 
end
 122 

 123 
else
 124 
    
 125 
    %% Optimal Constrained
 126 
    
 127 
    % calls function con_kl to evaluate the constraint on capital
 128 
    [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);
 129 
    
 130 
    mt_k_bar=reshape(ar_k_bar, [it_zgridno,it_agridno]);
 131 
    mt_l_bar=reshape(ar_l_bar, [it_zgridno,it_agridno]);
 132 
    
 133 
    mt_k_bar=mt_k_bar';
 134 
    mt_l_bar=mt_l_bar';
 135 
    
 136 
    
 137 
    k3        = [ar_k_unc,ar_k_bar];      
 138 
    [k4,id]   = min(abs(k3'));            % K star is min of k_unc and k_con. k3 transposed for the correct order for id
 139 
    ar_k_star = k4';
 140 
    l3        =[ar_l_unc,ar_l_bar];
 141 
    ar_l_star = ones(numel(ar_a_v),1);    % assigning l star in constrained case
 142 
    
 143 
    for i = 1:numel(ar_a_v)
 144 
        ar_l_star(i,1)=l3(i,id(i));
 145 
    end
 146 
 
 147 
    if(bl_feas)
 148 
    feas  = (1+fl_r)*fl_kappa;
 149 
    feas_v = ar_z_v.*ar_k_star.^fl_alpha.*ar_l_star.^fl_theta;
 150 
    for i = 1:numel(ar_a_v)
 151 
       if (feas_v(i)<feas)
 152 
       ar_k_star(i) = 0;
 153 
       ar_l_star(i) = 0;
 154 
       end
 155 
    end
 156 
    end
 157 

 158 

 159 
mt_k_star=reshape(ar_k_star, [it_zgridno,it_agridno]);
 160 
mt_l_star=reshape(ar_l_star, [it_zgridno,it_agridno]);
 161 

 162 
 mt_k_star=mt_k_star';
 163 
 mt_l_star=mt_l_star';
 164 
    
 165 
if( bl_print)
 166 
    disp('Below is mt_k_unc')
 167 
    disp(size(mt_k_unc));
 168 
    disp(mt_k_unc);
 169 
    disp('Below is mt_k_con')
 170 
    disp(mt_k_bar);
 171 
    disp('Below is mt_k_star')
 172 
    disp(size(mt_k_star));
 173 
    disp(mt_k_star);
 174 
    disp('Below is mt_l_unc')
 175 
    disp(size(mt_l_unc));
 176 
    disp(mt_l_unc);
 177 
    disp('Below is mt_l_con')
 178 
    disp(mt_l_bar);
 179 
    disp('Below is mt_l_star')
 180 
    disp(mt_l_star);
 181 
    
 182 
    
 183 
end
 184 

 185 
if(bl_plot)
 186 

 187 
%% 3D Plots for k* and l* 
 188 

 189 
figure (1)
 190 
a1 = surf(ar_z, ar_a, mt_k_bar , 'FaceColor','r', 'FaceAlpha',0.5, 'EdgeColor','r'); M1 = 'k bar';
 191 
hold on
 192 
a2 = surf(ar_z, ar_a, mt_k_unc , 'FaceColor','b', 'FaceAlpha',0.5, 'EdgeColor','b'); M2 = 'k unc';
 193 
%hold on
 194 
%a3 = surf(ar_z, ar_a, mt_k_star,'FaceColor','g', 'FaceAlpha',0.5, 'EdgeColor','g'); M3 = 'k star' ;
 195 
hold off
 196 
xlabel('z');
 197 
ylabel('a');
 198 
zlabel('k(unconstrained/constrained/optimal)');
 199 
LEG = legend([a1;a2], M1, M2, 'Location','north')
 200 
LEG.FontSize = 20;
 201 
view(150,15);
 202 
set(gca,'FontSize',17)
 203 
if(bl_saveimg)
 204 
saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/figures/optikl/Optimum_k.png')
 205 
end
 206 

 207 
figure (2)
 208 
b1 = surf(ar_z, ar_a, mt_l_bar, 'FaceColor','r', 'FaceAlpha',0.5, 'EdgeColor','r'); N1 = 'l bar';
 209 
hold on
 210 
b2 = surf(ar_z, ar_a, mt_l_unc, 'FaceColor','b', 'FaceAlpha',0.5, 'EdgeColor','b'); N2 = 'l unc';
 211 
%hold on
 212 
%b3 = surf(ar_z, ar_a, mt_l_star,'FaceColor','g', 'FaceAlpha',0.5, 'EdgeColor','g'); N3 = 'l star' ;
 213 
hold off
 214 
xlabel('z');
 215 
ylabel('a');
 216 
zlabel('l(unconstrained/constrained/optimal)');
 217 
LEG = legend([b1;b2], N1, N2, 'Location','north')
 218 
LEG.FontSize = 20;
 219 
view(150,15);
 220 
set(gca,'FontSize',17)
 221 
if(bl_saveimg)
 222 
saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/figures/optikl/Optimum_l.png')
 223 
end
 224 

 225 
%% 2D plot for k*
 226 

 227 
% Jet color Graph All
 228 
figure(3);
 229 
hold on;
 230 

 231 
% one line, unconstrained choice
 232 
pl_unc = plot(ar_z, mt_k_unc(1,:));
 233 
pl_unc.HandleVisibility = 'on';
 234 
pl_unc.LineStyle = '-';
 235 
pl_unc.Color = 'black';
 236 
pl_unc.LineWidth = 4;
 237 

 238 
% one constrained choice line, low a
 239 
pl_unc = plot(ar_z, mt_k_bar(2,:));
 240 
pl_unc.HandleVisibility = 'on';
 241 
pl_unc.LineStyle = '--';
 242 
pl_unc.Color = 'red';
 243 
pl_unc.LineWidth = 3;
 244 

 245 
% one constrained choice line, high a
 246 
pl_unc = plot(ar_z, mt_k_bar(4,:));
 247 
pl_unc.HandleVisibility = 'on';
 248 
pl_unc.LineStyle = '-.';
 249 
pl_unc.Color = 'blue';
 250 
pl_unc.LineWidth = 3;
 251 

 252 
% additional options
 253 
grid on;
 254 
grid minor;
 255 
xlim([1, 2.2]);
 256 
ylim([0, 10]);
 257 

 258 
title('Unconstrained Choices and Endogenous Borrowing Constraints');
 259 
ylabel('Capital Levels');
 260 
xlabel({'Productivity Levels'});
 261 
legend({'Unconstrained Optimal Choice',...
 262 
        'Borrowing Bound low A', ...
 263 
        'Borrowing Bound high A'}, 'Location', 'best', 'Interpreter','latex');
 264 

 265 
if(bl_saveimg)
 266 
saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/figures/optikl/Optimum_2D.png')
 267 
end
 268 

 269 
hold off;
 270 

 271 
end
 272 

< 0.001 
      1 
 273
end 
 274 

< 0.001 
      1 
 275
if(bl_profile) 
  0.005 
      1 
 276
profile off; 
 277 
profile viewer;
 278 
st_file_name = '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Profile/Firms/optikl';
 279 
profsave(profile('info'), st_file_name);
 280 

 281 

 282 
end