General Equilbrium - 3OC

This function solves the 3OC model outcomes given interest rate and wages Back to full code page https://kritikhanna.github.io/BKS-modified/

Function Inputs : asset grid(1 by M) and entrepreneurial productivity grid(1 by N) relevant model parameters required Calls function hhsave_dis_2OC.m

Function Outputs :

function [fl_y, mt_dis, mt_o, mt_k, mt_l]= GE_3OC(varargin)
addpath(genpath('C:\Users\kriti\Desktop\BKS-modified'));

close all;
bl_profile = false;      % Switch off profile if running a tester/calling from another function
bl_saveimg = false;
if(bl_profile)
profile off;
profile on;
end


if ~isempty(varargin)

    [ar_a,ar_z,ar_n, ...
        fl_alpha,fl_theta,fl_delta,fl_kappa,fl_r,fl_w,fl_phi,fl_ahi,fl_zhi, ...
         fl_risk,it_zgridno, it_agridno,mt_trans_z,mt_trans_n,fl_beta, fl_mu_z, fl_sig_z,fl_rho_z,fl_lambda_z, fl_tolvfi ...
        ] = varargin{:};

    bl_print  = true;
    bl_plot   = false;


else
    fl_alo         = 0;
    fl_ahi         = 50;
    fl_zhi         = 2.2;
    it_agridno     = 100;
    it_zgridno     = 7;
    it_ngridno     = 2;
 %   ar_a           = linspace(0,fl_ahi,it_agridno);
    mp_grid_control = containers.Map('KeyType','char', 'ValueType','any');
    mp_grid_control('grid_powerspace_power') = 2;
 %  ar_a            = ff_saveborr_grid(fl_alo, fl_ahi, it_agridno,  mp_grid_control)
    ar_a           = ff_saveborr_grid(fl_alo, fl_ahi, it_agridno, 'grid_powerspace', mp_grid_control);
    ar_a           = ar_a';
    fl_risk        = 1.5;
    fl_alpha       = 0.29;
    fl_theta       = 0.79-fl_alpha;
    fl_delta       = 0.06;
    fl_kappa       = 0;
    fl_mu_z        = 0;                   % mean of AR(1) entrepeneurial productivity process
    fl_rho_z       = 0.9;                 % persistence parameter of the AR(1) entrepreneurial productivity process
    fl_sig_z       = 0.2;
    fl_lambda_z    = 3;
    fl_beta        = 0.92;
   %[ar_z, mt_trans_z] = mytauchen_z(fl_mu_z,fl_rho_z,fl_sig_z,it_zgridno,fl_lambda_z);
    [ar_z, mt_trans_z] = ffy_rouwenhorst(fl_rho_z,fl_sig_z,it_zgridno);
    ar_z           = exp(ar_z);
    P1             = mt_trans_z^1000;
    sd             = P1(1,:);
    el             = sd*ar_z;
    ar_z           = ar_z/el;
    ar_z           = ar_z';
    fl_chi         = 0.9;
    ar_n =[0,1];
    mt_trans_n =[1-fl_chi,fl_chi;1-fl_chi,fl_chi];
    [fl_r,fl_w] = ...
        deal(0.04,3);
    fl_tolvfi    = 10^-10;
    fl_toldis    = 10^-10;              % Tolerance level for convergence stationary distribution
    bl_print  = true;
    bl_plot   = false;
end

fl_R  = fl_r + fl_delta;
[a_m,z_m] = meshgrid(ar_a,ar_z);
a_m       = a_m';
z_m       = z_m';
a_m_3OC   = repmat (a_m, [1 1 it_ngridno]);
z_m_3OC   = repmat (z_m, [1 1 it_ngridno]);

%ar_phi = [0.66:0.005:0.72];
%ar_chi = [0.76:0.02:0.97];

ar_phi = 1;
ar_chi= 0.6;
%output matrices

mt_y = zeros(length(ar_phi),length(ar_chi));
mt_y_form = zeros(length(ar_phi),length(ar_chi));
mt_y_inf = zeros(length(ar_phi),length(ar_chi));
mt_Kdemand = zeros(length(ar_phi),length(ar_chi));
mt_Ldemand = zeros(length(ar_phi),length(ar_chi));
mt_Ksupply = zeros(length(ar_phi),length(ar_chi));
mt_Lsupply = zeros(length(ar_phi),length(ar_chi));
mt_percent_form  = zeros(length(ar_phi),length(ar_chi));
mt_percent_inf = zeros(length(ar_phi),length(ar_chi));
mt_percent_worker = zeros(length(ar_phi),length(ar_chi));
mt_mean_tal_form = zeros(length(ar_phi),length(ar_chi));
mt_sd_tal_form = zeros(length(ar_phi),length(ar_chi));
mt_mean_tal_inf = zeros(length(ar_phi),length(ar_chi));
mt_sd_tal_inf = zeros(length(ar_phi),length(ar_chi));
mt_mean_l = zeros(length(ar_phi),length(ar_chi));
mt_sd_l = zeros(length(ar_phi),length(ar_chi));
mt_mean_l_form = zeros(length(ar_phi),length(ar_chi));
mt_sd_l_form = zeros(length(ar_phi),length(ar_chi));
mt_mean_l_inf = zeros(length(ar_phi),length(ar_chi));
mt_sd_l_inf = zeros(length(ar_phi),length(ar_chi));
mt_extfinance = zeros(length(ar_phi),length(ar_chi));
mt_extfintogdp = zeros(length(ar_phi),length(ar_chi));
mt_kbyy = zeros(length(ar_phi),length(ar_chi));
mt_eqir = zeros(length(ar_phi),length(ar_chi));
mt_eqiw = zeros(length(ar_phi),length(ar_chi));


% Additional outcomes at individual level
mt_extfinance_azn = zeros(it_agridno,it_zgridno,it_ngridno);
mt_borr_azn       = zeros(it_agridno,it_zgridno,it_ngridno);
mt_extfintogdp_azn = zeros(it_agridno,it_zgridno,it_ngridno);

%titlecsv={'phi','chi','r','w','extfingdp','%Work','%Inf','%Form','Y','Y-inf','Y-form','K_d','K_s','L_d','L_s','Mean_tal_form','SD_tal_form','Mean_tal_inf',...
 %   'SD_tal_inf','Mean_firmsize','SD_firmsize', 'Mean_firmsize_formal', 'SD_formalfirmsize','Mean_inffirmsize', 'SD_inffirmsize','K/Y',...
 %   'KL_DIFF','KL_DIFF_int'};
%dlmwrite('C:\Users\kriti\Desktop\test1.csv', titlecsv,'-append','delimiter',',')


for g=1:length(ar_chi)
    for h=1:length(ar_phi)

    fl_phi= ar_phi(h);
    fl_chi = ar_chi(g);
    mt_trans_n =[1-fl_chi,fl_chi;1-fl_chi,fl_chi];

[fl_y,mt_dis, mt_vf,mt_pf,mt_coh, mt_o, mt_k, mt_l] = PE_3OC(ar_a,ar_z,ar_n, ...
        fl_alpha,fl_theta,fl_delta,fl_kappa,fl_r,fl_w,fl_phi,fl_ahi,fl_zhi, ...
         fl_risk,it_zgridno, it_agridno,it_ngridno, mt_trans_z,mt_trans_n,fl_beta, fl_mu_z, fl_sig_z,fl_rho_z, ...
     fl_lambda_z,fl_tolvfi, fl_toldis);

 it_r_gridpoints = 20;                       % No of points on interest rate grid
 it_w_gridpoints = 20;                       % No of points on wages grid
 it_rgridmin     = 0;
 it_rgridmax     = 0.08;
 it_wgridmin     = 0.6;
 it_wgridmax     = 1.9;

 ar_r            = linspace(it_rgridmin,it_rgridmax,it_r_gridpoints);    % array for interest rate guesses to solve GE price
 ar_w            = linspace(it_wgridmin,it_wgridmax,it_w_gridpoints);      % array for wage rate guesses to solve GE prices
 ar_R            = ar_r +fl_delta*ones(1,it_r_gridpoints); % array for rental rate guesses to solve GE prices
 mt_K_demand     = zeros(it_r_gridpoints,it_w_gridpoints);
 mt_K_supply     = zeros(it_r_gridpoints,it_w_gridpoints);
 mt_ex_K_supply    = zeros(it_r_gridpoints,it_w_gridpoints);
 mt_L_demand    = zeros(it_r_gridpoints,it_w_gridpoints);
 mt_L_supply    = zeros(it_r_gridpoints,it_w_gridpoints);
 mt_ex_L_supply = zeros(it_r_gridpoints,it_w_gridpoints);


for i=1:it_r_gridpoints
    for j=1: it_w_gridpoints
        fl_r = ar_r(i);
        fl_w = ar_w(j);
        [fl_y,mt_dis, mt_vf,mt_pf,mt_wage,mt_profit,mt_income,mt_coh, mt_o, mt_k, mt_l]=PE_3OC(ar_a,ar_z,ar_n, ...
        fl_alpha,fl_theta,fl_delta,fl_kappa,fl_r,fl_w,fl_phi,fl_ahi,fl_zhi, ...
        fl_risk,it_zgridno, it_agridno,it_ngridno, mt_trans_z,mt_trans_n,fl_beta, fl_mu_z, fl_sig_z,fl_rho_z,fl_lambda_z,fl_tolvfi, fl_toldis);
        mt_o1 = mt_o;
        mt_o1(mt_o==1|mt_o==2)=0;
        mt_o1(mt_o==0)=1;

        mt_K_demand(i,j)=sum(sum(sum(mt_k.*mt_dis)));
        mt_K_supply(i,j)=sum(sum(sum(mt_pf.*mt_dis)));
        mt_ex_K_supply(i,j)=mt_K_supply(i,j)-mt_K_demand(i,j);
        mt_L_demand(i,j)=sum(sum(sum(mt_l.*mt_dis)));
        mt_L_supply(i,j)=sum(sum(sum(mt_o1.*mt_dis)));
        mt_ex_L_supply(i,j)=mt_L_supply(i,j)-mt_L_demand(i,j);
    end
end
disp(mt_K_demand);

mt_ex_K_supply1=mt_ex_K_supply;
mt_ex_L_supply1=mt_ex_L_supply;
mt_ex_K_supply1(mt_K_supply==0)=NaN;
mt_ex_K_supply1(mt_K_demand==0)=NaN;
mt_ex_L_supply1(mt_L_supply==0)=NaN;
mt_ex_L_supply1(mt_L_demand==0)=NaN;
mt_ex_L_supply2=mt_ex_L_supply1;
mt_ex_L_supply2(abs(mt_ex_L_supply1)>=0.1)=NaN;
mt_KL_DIFF = abs(mt_ex_K_supply1) + abs(mt_ex_L_supply2);
%mt_KL_DIFF_2 = abs(mt_ex_K_supply1) + abs(mt_ex_L_supply1);
mt_KL_DIFF_2 = mt_KL_DIFF;
mt_KL_DIFF_2(isnan(mt_KL_DIFF_2))=10;

%disp('K_demand');
%disp(mt_K_demand);
%disp('K_supply');
%disp(mt_K_supply);
%disp('L_demand');
%disp(mt_L_demand);
%disp('L_supply');
%disp(mt_L_supply);
%disp ('ex_K_supply:')
%disp (mt_ex_K_supply1);
%disp ('ex_L_supply:')
%disp(mt_ex_L_supply);
%disp ('mt_KL_DIFF:');
%disp (mt_KL_DIFF);

if(bl_plot)
figure(1);
% Household b (some borrow some save added up)
subplot(2,2,1);
hold on;
    chart = plot(ar_r, mt_K_supply);
% Show smoother colors
clr = jet(numel(chart));
for m = 1:numel(chart)
   set(chart(m),'Color',clr(m,:))
end
plot(ar_r,ones(size(ar_r)) * 0, 'k-.');
xlim([min(ar_r) max(ar_r)]);
%ylim([-4, 2]);
grid on;
title('Supply for Credit');
ylabel('Supply for Credit');
xlabel('r')
legend2plot = [1 round(numel(chart)/2) numel(chart)];
legendCell = cellstr(num2str(ar_w', 'wage=%3.2f'));
legend(chart(legend2plot), legendCell(legend2plot), 'Location','southeast');
%legend(legendCell, 'Location','southeast');

% Household b (some borrow some save added up)
subplot(2,2,2);
hold on;
chart = plot(ar_r, mt_K_demand);
% Show smoother colors
clr = jet(numel(chart));
for m = 1:numel(chart)
   set(chart(m),'Color',clr(m,:))
end
plot(ar_r,ones(size(ar_r)) * 0, 'k-.');
xlim([min(ar_r) max(ar_r)]);
%ylim([-4, 2]);
grid on;
title('Demand for Credit');
ylabel('Demand for Credit');
xlabel('r')
legend2plot = [1 round(numel(chart)/2) numel(chart)];
legendCell = cellstr(num2str(ar_w', 'wage=%3.2f'));
legend(chart(legend2plot), legendCell(legend2plot), 'Location','southeast');
%legend(legendCell, 'Location','southeast');

subplot(2,2,3);
hold on;
chart = plot(ar_r, mt_L_demand);
% Show smoother colors
clr = jet(numel(chart));
for m = 1:numel(chart)
   set(chart(m),'Color',clr(m,:))
end
plot(ar_r,ones(size(ar_r)) * 0, 'k-.');
xlim([min(ar_r) max(ar_r)]);
%ylim([-4, 2]);
grid on;
title('Demand for Labor');
ylabel('Demand for Labor');
xlabel('r')
legend2plot = [1 round(numel(chart)/2) numel(chart)];
legendCell = cellstr(num2str(ar_w', 'wage=%3.2f'));
legend(chart(legend2plot), legendCell(legend2plot), 'Location','southeast');
%legend(legendCell, 'Location','southeast');

subplot(2,2,4);
hold on;
chart = plot(ar_r, mt_L_supply);
% Show smoother colors
clr = jet(numel(chart));
for m = 1:numel(chart)
   set(chart(m),'Color',clr(m,:))
end
plot(ar_r,ones(size(ar_r)) * 0, 'k-.');
xlim([min(ar_r) max(ar_r)]);
%ylim([-4, 2]);
grid on;
title('Supply for Labor');
ylabel('Supply for Labor');
xlabel('r')
legend2plot = [1 round(numel(chart)/2) numel(chart)];
legendCell = cellstr(num2str(ar_w', 'wage=%3.2f'));
legend(chart(legend2plot), legendCell(legend2plot), 'Location','southeast');
%legend(legendCell, 'Location','southeast');
snapnow;
%st_file_name = sprintf([ '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/PE_GE/figures/GE_3OC/demsupkl_', num2str(fl_phi),'.png']);
%saveas(gcf,st_file_name );
snapnow;


figure(2)
% Household b (some borrow some save added up)
subplot(1,2,1);
hold on;
chart = plot(ar_r, mt_ex_K_supply);
% Show smoother colors
clr = jet(numel(chart));
for m = 1:numel(chart)
   set(chart(m),'Color',clr(m,:))
end
plot(ar_r,ones(size(ar_r)) * 0, 'k-.');
xlim([min(ar_r) max(ar_r)]);
%ylim([-4, 2]);
grid on;
title('Excess Supply for Credit');
ylabel(' Excess Supply for Credit');
xlabel('r')
legend2plot = [1 round(numel(chart)/2) numel(chart)];
legendCell = cellstr(num2str(ar_w', 'wage=%3.2f'));
legend(chart(legend2plot), legendCell(legend2plot), 'Location','southeast');
%legend(legendCell, 'Location','southeast');

% Household b (some borrow some save added up)
subplot(1,2,2);
hold on;
chart = plot(ar_r, mt_ex_L_supply);
% Show smoother colors
clr = jet(numel(chart));
for m = 1:numel(chart)
   set(chart(m),'Color',clr(m,:))
end
plot(ar_r,ones(size(ar_r)) * 0, 'k-.');
xlim([min(ar_r) max(ar_r)]);
%ylim([-0.1, 0.1]);
grid on;
title('Excess supply of labor');
ylabel('Excess supply of labor');
xlabel('r')
legend2plot = [1 round(numel(chart)/2) numel(chart)];
legendCell = cellstr(num2str(ar_w', 'wage=%3.2f'));
legend(chart(legend2plot), legendCell(legend2plot), 'Location','southeast');
%legend(legendCell, 'Location','southeast');
snapnow;
%st_file_name = sprintf([ '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/PE_GE/figures/GE_3OC/ex_kl_', num2str(fl_phi),'.png']);
%saveas(gcf,st_file_name );
snapnow;
end


[DS_KL_diff_EQUI_val, EQUI_IDX]  = min(min(mt_KL_DIFF));

[r_idx, w_idx]=find(mt_KL_DIFF==DS_KL_diff_EQUI_val);

fl_equi_r_1 = ar_r(r_idx);
fl_equi_w_1 = ar_w(w_idx);

[mt_r,mt_w] = meshgrid(ar_r,ar_w);

it_r_gridpoints_2 = 39;
it_w_gridpoints_2 = 39;
ar_r_2           = linspace(it_rgridmin,it_rgridmax,it_r_gridpoints_2);    % array for interest rate guesses to solve GE price
ar_w_2           = linspace(it_wgridmin,it_wgridmax,it_w_gridpoints_2);

[mt_r_2,mt_w_2] = meshgrid(ar_r_2,ar_w_2);
mt_KL_DIFF_3 = interp2(mt_r,mt_w,mt_KL_DIFF_2,mt_r_2,mt_w_2);

[DS_KL_diff_EQUI_val_2, EQUI_IDX_2]  = min(min(mt_KL_DIFF_3));

[r_idx_2, w_idx_2]=find(mt_KL_DIFF_3==DS_KL_diff_EQUI_val_2);

fl_KL_DIFF = DS_KL_diff_EQUI_val;
fl_KL_DIFF_interp = DS_KL_diff_EQUI_val_2;

fl_equi_r_2 = ar_r_2(r_idx_2);
fl_equi_w_2 = ar_w_2(w_idx_2);

mt_eqir(g,h) =fl_equi_r_2;
mt_eqiw(g,h) =fl_equi_w_2;

fl_r = fl_equi_r_2;
fl_w = fl_equi_w_2;


[fl_y,mt_dis, mt_vf,mt_pf,mt_wage,mt_profit,mt_income,mt_coh, mt_o, mt_k, mt_l]=PE_3OC(ar_a,ar_z,ar_n, ...
        fl_alpha,fl_theta,fl_delta,fl_kappa,fl_r,fl_w,fl_phi,fl_ahi,fl_zhi, ...
         fl_risk,it_zgridno, it_agridno,it_ngridno, mt_trans_z,mt_trans_n,fl_beta, fl_mu_z, fl_sig_z,fl_rho_z, ...
     fl_lambda_z,fl_tolvfi, fl_toldis );

%mt_o((mt_l(:,:,1)> 0.5)) = 2;      % 1 for Informal Firm owner , 0 for wage worker

mt_y(g,h) = fl_y;

mt_y_azn             = z_m_3OC.*mt_dis.*mt_k.^fl_alpha.*mt_l.^fl_theta;
mt_y_inf_azn         = zeros(it_agridno,it_zgridno,it_ngridno);
mt_y_form_azn        = zeros(it_agridno,it_zgridno,it_ngridno);
mt_l_inf             = zeros(it_agridno,it_zgridno,it_ngridno);
mt_l_form            = zeros(it_agridno,it_zgridno,it_ngridno);
mt_y_inf_azn(mt_o==1)= mt_y_azn(mt_o==1);
mt_y_form_azn(mt_o==2)   = mt_y_azn(mt_o==2);
mt_l_inf(mt_o==1)    = mt_l(mt_o==1);
mt_l_form(mt_o==2)   = mt_l(mt_o==2);
fl_y_inf             = sum(sum(sum(mt_y_inf_azn)));
fl_y_form            = sum(sum(sum(mt_y_form_azn)));
mt_y_inf(g,h)        = fl_y_inf;
mt_y_form(g,h)       = fl_y_form;

mt_form              = zeros(it_agridno,it_zgridno,it_ngridno);
mt_inf               = zeros(it_agridno,it_zgridno,it_ngridno);
mt_worker            = zeros(it_agridno,it_zgridno,it_ngridno);

mt_form(mt_o==2)      = 1;
mt_inf (mt_o==1)      = 1;
mt_worker(mt_o==0)    = 1;

mt_Kdemand(g,h)   = sum(sum(sum(mt_dis.*mt_k)));
mt_Ldemand(g,h)   = sum(sum(sum(mt_dis.*mt_l)));
fl_Kdemand            = mt_Kdemand(g,h);
fl_Ldemand            = mt_Ldemand(g,h);


% External Finance
mt_borr_azn         = mt_k-a_m_3OC;
mt_extfinance_azn(mt_k>a_m_3OC) = mt_borr_azn(mt_k>a_m_3OC);
mt_extfinance(g,h)   = sum(sum(sum(mt_dis.*mt_extfinance_azn)));
mt_extfintogdp(g,h)  =  mt_extfinance(g,h)/mt_y(g,h);
fl_extfintogdp       =  mt_extfintogdp(g,h);
mt_kbyy(g,h)         =  mt_Kdemand(g,h)/mt_y(g,h);
fl_kbyy              =  mt_kbyy(g,h);

% Capital and labor supplied

mt_Ksupply(g,h)       = sum(sum(sum(mt_dis.*mt_pf)));
mt_Lsupply(g,h)       = sum(sum(sum(mt_dis.*mt_worker)));
fl_Ksupply            = mt_Ksupply(g,h);
fl_Lsupply            = mt_Lsupply(g,h);


% Percent employed in each occupations

mt_percent_form(g,h)       = sum(sum(sum(mt_dis.*mt_form)));
mt_percent_inf(g,h)        = sum(sum(sum(mt_dis.*mt_inf)));
mt_percent_worker(g,h)    = sum(sum(sum(mt_dis.*mt_worker)));

fl_percent_form           = mt_percent_form(g,h);
fl_percent_inf            = mt_percent_inf(g,h);
fl_percent_worker         = mt_percent_worker(g,h);


% Talent of entrepreneurs

mt_dis_form          = mt_dis;
mt_dis_form(mt_o~=2) = 0;
mt_z_m_3OC_form      = z_m_3OC;
mt_z_m_3OC_form(mt_o~=2)= 0;
ar_z_m_3OC_form      = mt_z_m_3OC_form(:);
ar_dis_form          = mt_dis_form(:);
ar_dis_form(ar_z_m_3OC_form==0)   = [];
ar_z_m_3OC_form(ar_z_m_3OC_form==0) = [];
ar_dis_form          = ar_dis_form/sum(ar_dis_form);
fl_length_form = length(ar_dis_form);

%fl_mean_tal_ent = (sum(sum(sum(z_m_3OC.*mt_dis_ent))))/(sum(sum(sum(mt_dis_ent))));
fl_mean_tal_form = sum(ar_z_m_3OC_form.*ar_dis_form);
mt_mean_tal_form(g,h) = fl_mean_tal_form;
W = ar_dis_form;
X = ar_z_m_3OC_form;
Wmean = fl_mean_tal_form;
N = fl_length_form;
mt_sd_tal_form(g,h) =  sqrt((sum(W.*(X - Wmean.*ones(N,1)).^2)));
fl_sd_tal_form      =  mt_sd_tal_form(g,h);

%Talent of Informal firm owners
mt_dis_inf          = mt_dis;
mt_dis_inf(mt_o~=1) = 0;

mt_z_m_3OC_inf      = z_m_3OC;
mt_z_m_3OC_inf(mt_o~=1)= 0;
ar_z_m_3OC_inf      = mt_z_m_3OC_inf(:);
ar_dis_inf          = mt_dis_inf(:);
ar_dis_inf(ar_z_m_3OC_inf==0)   = [];
ar_z_m_3OC_inf(ar_z_m_3OC_inf==0) = [];
fl_length_inf = length(ar_dis_inf);
ar_dis_inf = ar_dis_inf/sum(ar_dis_inf);

%fl_mean_tal_ent = (sum(sum(sum(z_m_3OC.*mt_dis_ent))))/(sum(sum(sum(mt_dis_ent))));
fl_mean_tal_inf = sum(ar_z_m_3OC_inf.*ar_dis_inf);
mt_mean_tal_inf(g,h) = fl_mean_tal_inf;
W = ar_dis_inf;
X = ar_z_m_3OC_inf;
Wmean = fl_mean_tal_inf;
N = fl_length_inf;
mt_sd_tal_inf(g,h) =  sqrt((sum(W.*(X - Wmean.*ones(N,1)).^2)));
fl_sd_tal_inf      =  mt_sd_tal_inf(g,h);


% Firm size distributions
ar_l   = mt_l(:);
ar_dis = mt_dis(:);
ar_dis(ar_l==0) = [];
ar_l(ar_l==0)   = [];
fl_length_l = length(ar_l);
ar_dis =ar_dis/sum(ar_dis);
fl_mean_l        = sum(ar_dis.*ar_l);


mt_mean_l(g,h) = fl_mean_l;
W = ar_dis;
X = ar_l;
Wmean = fl_mean_l;
N = fl_length_l;
mt_sd_l(g,h) =  sqrt((sum(W.*(X - Wmean.*ones(N,1)).^2)));
fl_sd_l      = mt_sd_l(g,h);


% Firm size distributions - informal Sector

ar_l_inf   = mt_l_inf(:);
ar_dis = mt_dis(:);
ar_dis(ar_l_inf==0) = [];
ar_l_inf(ar_l_inf==0)   = [];
fl_length_l_inf = length(ar_l_inf);
ar_dis=ar_dis/sum(ar_dis);
fl_mean_l_inf        = sum(ar_dis.*ar_l_inf);


mt_mean_l_inf(g,h) = fl_mean_l_inf;
W = ar_dis;
X = ar_l_inf;
Wmean = fl_mean_l_inf;
N = fl_length_l_inf;
mt_sd_l_inf(g,h) =  sqrt((sum(W.*(X - Wmean.*ones(N,1)).^2)));
fl_sd_l_inf      = mt_sd_l_inf(g,h);
%

% Firm size distributions - formal Sector
ar_l_form   = mt_l_form(:);
ar_dis = mt_dis(:);
ar_dis(ar_l_form==0) = [];
ar_l_form(ar_l_form==0)   = [];
ar_dis=ar_dis/sum(ar_dis);
fl_mean_l_form        = sum(ar_dis.*ar_l_form);
fl_length_l_form = length(ar_l_form);

mt_mean_l_form(g,h) = fl_mean_l_form;
W = ar_dis;
X = ar_l_form;
Wmean = fl_mean_l_form;
N = fl_length_l_form;
mt_sd_l_form(g,h) = sqrt((sum(W.*(X - Wmean.*ones(N,1)).^2)));
fl_sd_l_form      = mt_sd_l_form(g,h);

%
ar_data=[fl_phi,fl_chi,fl_equi_r_2, fl_equi_w_2, fl_extfintogdp,fl_percent_worker,fl_percent_inf,fl_percent_form,fl_y,fl_y_inf,...
    fl_y_form, fl_Kdemand, fl_Ksupply, fl_Ldemand, fl_Lsupply,fl_mean_tal_form,fl_sd_tal_form...
    fl_mean_tal_inf, fl_sd_tal_inf,fl_mean_l,fl_sd_l, fl_mean_l_form, fl_sd_l_form,...
    fl_mean_l_inf, fl_sd_l_inf,fl_kbyy, fl_KL_DIFF,fl_KL_DIFF_interp];

dlmwrite('C:\Users\kriti\Desktop\test.csv', ar_data,'-append','delimiter',',');

    end
end

    disp ('Degree of Contract enforcement friction is:')
    disp(ar_phi);
    disp('Output per capita is:')
    disp(mt_y);
    disp('Output per capita - informa')
    disp(mt_y_inf);
    disp('Output per capita is - formal')
    disp(mt_y_form);
    disp('Total capital per capita demanded is:')
    disp(mt_Kdemand);
    disp('Total labor per capita demanded is:')
    disp(mt_Ldemand);
    disp('Total capital per capita supplied is:')
    disp(mt_Ksupply);
    disp('Total labor per capita supplied is:')
    disp(mt_Lsupply);
    disp('Percentage entrepreneurs')
    disp(mt_percent_form);
    disp('Percentage self-employed other than entrepreneurs')
    disp(mt_percent_inf);
    disp('Percentage workers');
    disp(mt_percent_worker);
    disp('Mean talent of Entrepreneurs');
    disp(mt_mean_tal_form);
    disp('Mean talent of Self Employed');
    disp(mt_mean_tal_inf);
    disp('SD talent of Entrepreneurs');
    disp(mt_sd_tal_form);
    disp('SD talent of Self Employed');
    disp(mt_sd_tal_inf);
    disp('Mean of Firm size');
    disp(mt_mean_l);
    disp('SD of firm size');
    disp(mt_sd_l);
    disp('Mean of formal Firm size');
    disp(mt_mean_l_form);
    disp('SD of formal firm size');
    disp(mt_sd_l_form);
    disp('Mean of informal firm size');
    disp(mt_mean_l_inf);
    disp('SD of informal firm size');
    disp(mt_sd_l_inf);
    disp('External Finance');
    disp(mt_extfinance);
    disp('External Finance to GDP');
    disp(mt_extfintogdp);
    disp('Capital to GDP');
    disp(mt_kbyy);

  %  dlmwrite('test.csv',ar_phi,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_y,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_y_form,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_y_inf,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_kdemand,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_ldemand,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_ksupply,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_lsupply,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_percent_ent,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_percent_se,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_percent_worker,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_mean_tal_ent,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_sd_tal_se,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_meanl,'delimiter',',','-append');
  %  dlmwrite('test.csv',ar_sd_l,'delimiter',',','-append');

bl_print_fl = false;
bl_print_ar = false;
if(bl_print_fl)
    disp ('phi =');
    disp (fl_phi);
    disp ('Excess Capital Supply is');
    disp(mt_ex_K_supply);
    disp('Excess Labor Supply is');
    disp(mt_ex_L_supply);
    disp('r eq =');
    disp(fl_equi_r_1);
    disp('w eq =');
    disp(fl_equi_w_1);
    disp('diff=');
    disp(mt_KL_DIFF );

end



if(bl_print_ar)
    disp ('ar_phi');
    disp (ar_phi);
    disp ('ar_eqir')
    disp(mt_eqir);
    disp ('ar_eqiw')
    disp (mt_eqiw);
    disp ('ar_y')
    disp(ar_y)

end



end
  Columns 1 through 7

  139.8323  105.6997   83.8111   67.8397   55.8674   46.6868   39.5093
  118.9807   89.9379   71.3133   57.7236   47.5366   39.7250   33.6177
  102.2826   77.3157   61.3050   49.6224   40.8651   34.1498   28.8997
   86.7321   67.0681   53.1795   43.0454   35.4488   29.6235   22.4963
   75.8419   58.6469   46.5022   37.6405   30.9977   25.9039   19.6716
   66.7956   51.6516   40.9554   33.1508   27.3004   22.8142   17.3252
   59.2080   45.7843   36.3031   29.3851   24.1992   18.1470   15.3571
   52.7883   40.8201   32.3669   26.1989   21.5754   16.1794   13.6920
   47.3138   36.5867   29.0102   23.4819   19.3379   14.5015   12.2721
   42.6116   32.9507   26.1271   21.1482   17.4160   13.0603   11.0524
   38.5464   29.8071   23.6345   19.1306   14.1375   11.8143    9.9980
   35.0105   27.0729   21.4666   17.3758   12.8407   10.7306    9.0809
   31.9181   24.6816   19.5704   15.8410   11.7065    9.7828    8.2788
   29.1996   22.5794   17.9036   14.4918   10.7094    8.9496    7.5737
   26.7985   20.7227   16.4314   13.3001    9.8288    8.2136    6.9509
   24.6684   19.0755   15.1253   10.9864    9.0475    7.5608    6.3984
   22.7709   17.6082   13.9619   10.1413    8.3516    6.9792    5.9062
   21.0741   16.2962   12.9215    9.3857    7.7293    6.4591    5.4661
   19.5514   15.1187   11.9879    8.7075    7.1708    5.9924    5.0712
   18.1804   14.0585   11.1472    8.0969    6.6680    5.5722    4.7156

  Columns 8 through 14

   30.3336   26.2027   22.8272   20.0372   17.7077   15.7448   14.0771
   25.8103   22.2954   19.4232   17.0493   15.0672   13.3970   11.9779
   22.1880   19.1664   16.6973   14.6566   12.9526   11.5168   10.2969
   19.2471   16.6260   14.4842   12.7139   11.2358    9.9903    8.9321
   16.8304   14.5384   12.6655   11.1176    9.8250    8.7359    7.8106
   14.8229   12.8043   11.1548    9.7915    8.6531    7.6939    6.8790
   13.1391   11.3498    9.8877    8.6792    7.6702    6.8199    6.0975
   11.7145   10.1192    8.8156    7.7382    6.8385    6.0805    5.4364
   10.4996    9.0698    7.9014    6.9357    6.1293    5.4499    4.8726
    9.4561    8.1684    7.1161    6.2464    5.5202    4.9083    4.3884
    8.5540    7.3891    6.4372    5.6505    4.9935    4.4400    2.8885
    7.7693    6.7113    5.8467    5.1321    4.5355    4.0327    2.6235
    7.0831    6.1185    5.3303    4.6788    4.1349    3.6765    2.3918
    6.4798    5.5974    4.8763    4.2803    3.7827    2.4473    2.1881
    5.9470    5.1371    4.4753    3.9284    3.4716    2.2461    2.0082
    5.4743    4.7288    4.1196    3.6161    3.1957    2.0675    1.8485
    5.0532    4.3650    3.8027    3.3379    2.1464    1.9085    1.7063
    4.6767    4.0398    3.5194    3.0892    1.9865    1.7663    1.5792
    4.3387    3.7479    3.2651    2.8660    1.8430    1.6387    1.4651
    4.0345    3.4851    3.0361    2.6650    1.7137    1.5238    1.3624

  Columns 15 through 20

   12.6494   11.4190   10.3518    9.4208    6.2608    5.7373
   10.7632    9.7162    8.8081    8.0160    5.3272    4.8818
    9.2526    8.3526    7.5720    5.0141    4.5796    4.1966
    8.0263    7.2455    4.7793    4.3495    3.9726    3.6404
    7.0185    6.3358    4.1792    3.8034    3.4738    3.1833
    6.1813    4.0602    3.6807    3.3497    3.0594    2.8036
    5.4792    3.5990    3.2626    2.9692    2.7119    2.4851
    3.5545    3.2088    2.9089    2.6473    2.4179    2.2157
    3.1859    2.8760    2.6072    2.3727    2.1671    1.9859
    2.8693    2.5902    2.3481    2.1369    1.9517    1.7885
    2.5955    2.3431    2.1241    1.9331    1.7655    1.6179
    2.3575    2.1281    1.9292    1.7557    1.6036    1.4695
    2.1492    1.9402    1.7588    1.6007    1.4619    1.3397
    1.9662    1.7749    1.6090    1.4643    1.3374    1.2256
    1.8045    1.6290    1.4767    1.3439    1.2275    1.1248
    1.6611    1.4995    1.3593    1.2371    1.1299    1.0354
    1.5333    1.3841    1.2548    1.1419    1.0430    0.9558
    1.4190    1.2810    1.1613    1.0568    0.9653    0.8845
    1.3165    1.1884    1.0774    0.9805    0.8955    0.8206
    1.2242    1.1051    1.0018    0.9117    0.8327    0.7631

Degree of Contract enforcement friction is:
     1

Output per capita is:
    1.4784

Output per capita - informa
    0.1414

Output per capita is - formal
    1.3370

Total capital per capita demanded is:
    4.0327

Total labor per capita demanded is:
    0.5202

Total capital per capita supplied is:
    4.1599

Total labor per capita supplied is:
    0.5344

Percentage entrepreneurs
    0.1094

Percentage self-employed other than entrepreneurs
    0.3563

Percentage workers
    0.5344

Mean talent of Entrepreneurs
    2.0289

Mean talent of Self Employed
    0.8736

SD talent of Entrepreneurs
    0.3030

SD talent of Self Employed
    0.3073

Mean of Firm size
    1.1172

SD of firm size
    2.7591

Mean of formal Firm size
    4.3012

SD of formal firm size
    4.3660

Mean of informal firm size
    0.1396

SD of informal firm size
    0.1715

External Finance
    2.8570

External Finance to GDP
    1.9325

Capital to GDP
    2.7277


ans =

    1.4784