%% Bridge Loan, Uncertainty and Risky Investment
% Go back to Project Main Page
% In Multi-period Loans, One Period Loans, Roll-over Loans, Bridge Loans, we defined what multi-period, one-period, roll-over and bridge loans are in our models. We discussed the role of bridge loan in improving consumption smoothing when only one period no-roll-over loans are allowed in a 3 period deterministic model with fixed but unevenly distributed endowments.
% In the setting below, we discuss the role of bridge-loan in a dynamic investment context with uncertainty.
% Table of Contents
% A Three Period Model With Uncertainty and Investment
% Is Bridge Loan Needed If ? No
%  and Optimal Capital Choice with Roll-Over Loans
%  and Optimal Capital Choice with No-Roll-Over Loan
% Budget and Constraints with No-Roll-Over Loans + Bridge Loans
% Optimal Investments and Borrowing given Bridge Loan + No-Roll-Over Loan
% Optimal Investments and Consumptions with Shifting Bridge Loan Interest Rates
% Graphing the Effects of Bridge Loans with Varying Interest Rates
% Tabulate All Results
%
%% A Three Period Model With Uncertainty and Investment
% There are three model periods, and two possible states.
% Households can borrow/save in the first period to finance either consumption, c_1, or capital investments, K.
% Households can also borrow/save in the second period given shock realization.
% initial period: e_{1} = 0
% low state: Y_{t,l} = 1, p_l is the low state probability
% high state: Y_{t,h} = A \cdot K + 1, p_h = 1 - p_l is the high state probability
% The two shocks are i.i.d.
% In the first period, households choose K and b_2. In the second period, conditional on realized shocks and b_2, households choose b_3. The first period's problem is:
% \max_{K\ge 0, b_2} \left\{ \log(c_1) + \sum_{s \in \left\{l,h\right\}} p_s\cdot\log(c_{2,s}) + \sum_{s \in \left\{l,h\right\}} \sum_{\hat{s} \in \left\{l,h\right\}} p_s \cdot p_{\hat{s}} \cdot \log(c_{3,s}) \right\}
% Not allowing bridge loans for now, the budget constraints are:
% c_1 + b_2 + K = e_1
% c_2 + b_3\left(b_2, s\right) = Y_{2,s}\left(K\right) + b_2
% and, c_3 = Y_{3,s}\left(K\right) + b_3\left(b_2, s\right)
% where b_3\left(b_2, s\right) is the second period optimal choice function
% If roll-over loans are allowed, then the implicit (but never-binding) borrowing constraint is the natural borrowing constraint determined by endowment in the lower state of shock--this is to gurantee repayments even if the household gets hit with two low shocks in a row:
%  b_2 > - \left( Y_{2,l} + Y_{3,l}\right)
% and, b_3\left(b_2, s\right) > - \left( Y_{3,l}\right)
% On the other hand, following the previous discussions, if households only have one-period no-roll-over loans, then the borrowing constraint (possibly binding) is based only on endowment in the next period, this is tighter than the natual borrowing constraint:
% b_2 \ge - Y_{2,l}
% and b_3\left(b_2, s\right) \ge - Y_{3,l}
%
%% Is Bridge Loan Needed If A=0? No
% If capital investment does not lead to additional outputs in the high state, households face a fixed endowment of 1 in all future states, uncertainty becomes irrelevant, and we are back to the model in Multi-period Loans, One Period Loans, Roll-over Loans, Bridge Loans.
% \max_{b_2, b_3} \log(c_1) + \log(c_2) + \log(c_3)
% such that: c_1 + b_2 = e_1, c_2 + b_3 = e_2 + b_2, and c_3 = e_3 + b_3
% Previously, the endowment stream was significantly increasing: \textbf{e} = \left\{ e_1=0, e_2=1, e_3=8 \right\}, and that gave bridge loan a role to complement short-term no-roll-over loans. Now the endowment stream is: \textbf{e} = \left\{ e_1=0, e_2=1, e_3=1 \right\}, optimal choices are:
% b_2^{*}=-\frac{2}{3}, b_3^{*}=-\frac{1}{3}
% c_1^{*}=c_2^{*}=c_3^{*}=\frac{0+1+1}{3}=\frac{2}{3}
% Bridge Loan is not needed in this context even with no-roll-over loans, because the no-roll-over borrowing constraint does not bind: b^*_2 > -e_2, optimal borrowing choice is less than 2nd period endowment.
%
%% A\ne 0 and Optimal Capital Choice with Roll-Over Loans
% Now we solve for the optimal borrowing choices when roll-overs are allowed and A\neq 0, which makes shock matter, and increases households desire for borrowing to not only smooth consumption but also increase income through capital investments.
% Investment and Borrowing:
% With A=3, and p_l = 0.5, optimal choices are:
% K^{*} = 0.68 and b^{*}_{2} = -1.35
% Second period b_3 choice, Borrow given low shock, save given high shock:
% b_{3}\left(b_2^{*}, s=l\right) = -0.77, b_{3}\left(b_2^{*}, s=h\right) = 0.09
% Consumption Paths:
% Consumption in the first period:
% c_1^{*} = 0.67
% Consumption path with low shock in the second period, and another bad shock or good shock in the third period:
% c_{2,l}^{*} = 0.42, c_{3,l}^{*}\left(b_{3}\left(b_2^{*}, s=l\right)\right) = 0.23, c_{3,h}^{*}\left(b_{3}\left(b_2^{*}, s=l\right)\right) = 2.28
% Consumption path with high shock in the second period, and a bad shock or another good shock in the third period:
%  c_{2,h}^{*} = 1.61, c_{3,l}^{*}\left(b_{3}\left(b_2^{*}, s=h\right)\right) = 1.09, c_{3,h}^{*}\left(b_{3}\left(b_2^{*}, s=h\right)\right) = 3.13
%
% Compare to A=0:
% Borrowing to smooth consumption as well as increasing investments
% Consumptions are higher along the path where second period shock is h. And when the third period shock is h.
% Consumption is lower for c^{*}_{2,l} and c_{3,l}^{*}\left(b_{3}\left(b_2^{*}, s=l\right)\right), where households have to pay back debts but have bad shock.
% We obtain the above solutions following the codes below:

% Some Functions
% Normally, second period problem is solved first than first
% Given we have only 2 states, we can solve them jointly, with b3Hs b3Ls
syms As Ks ps e1s e2s e3s b2s b3LsT2 b3HsT2 b3Ls b3Hs
expected_t2_income = ps*e2s + (1-ps)*(e2s + As*Ks);
expected_t3_income = ps*e3s + (1-ps)*(e3s + As*Ks);
expected_utility = log(e1s-b2s-Ks) + ...
                   ps*log(e2s + b2s - b3LsT2) + ...
                   (1-ps)*log(As*Ks + e2s + b2s - b3HsT2) + ...
                   (ps)*(ps)*log(e3s + b3Ls) +...
                   (ps)*(1-ps)*log(As*Ks + e3s + b3Ls) +...
                   (1-ps)*(ps)*log(e3s + b3Hs) +...
                   (1-ps)*(1-ps)*log(As*Ks + e3s + b3Hs);

% Variables and Parmaeters
e1=0;
e2=1;
e3=1;
p=0.50;
A=3;

% Function Handle for Constrained Maximization Problem
% x(1) is K
% x(2) is b2
% x(3) is b3L
% x(4) is b3H
U_neg = @(x) (-1)*(log(e1-x(2)-x(1)) + ...
        p*log(e2 + x(2) - x(3)) + ...
        (1-p)*log(A*x(1) + e2 + x(2) - x(4)) + ...
        (p)*(p)*log(e3 + x(3)) +...
        (p)*(1-p)*log(A*x(1) + e3 + x(3)) +...
        (1-p)*(p)*log(e3 + x(4)) +...
        (1-p)*(1-p)*log(A*x(1) + e3 + x(4)));

% Linear Inequality Constraints K>=0, and b2, b3L and b3H can be positive or negative
LIN_CON = [-1,0,0,0];
q = [0];

% Starting Search Points
b0 = [0.1,-0.15,-0.15,-0.15]; 

% Optimization
[x_argmin, V_at_argmin] = fmincon(U_neg, b0, LIN_CON, q);
% Optimal Risky and Safe Asset Choices
K_o = x_argmin(1);
b2_o = x_argmin(2);
b3L_o = x_argmin(3);
b3H_o = x_argmin(4);

% Value at optimal Choices and Expected Income
EY = double(subs(expected_t2_income, {e2s, ps, As, Ks}, {e2, p, A, K_o}));
EV = double(subs(expected_utility,...
              {As, Ks, ps, e1s, e2s, e3s, b2s, b3LsT2, b3HsT2, b3Ls, b3Hs},...
              {A, K_o, p, e1, e2, e3, b2_o, b3L_o, b3H_o, b3L_o, b3H_o}));

% Consumption
c1_o = e1-b2_o-K_o;
c2_l_o = e2 + b2_o - b3L_o;
c2_h_o = e2 + A*K_o + b2_o - b3H_o;
c3_ll_o = e3 + b3L_o;
c3_lh_o = e3 + A*K_o + b3L_o;
c3_hl_o = e3 + b3H_o;
c3_hh_o = e3 + A*K_o + b3H_o;

% Display Results
b3L_B_o = false;
b3H_B_o = false;
opti_K_b2_b3 = table(K_o, b2_o, b3L_o, b3H_o, b3L_B_o, b3H_B_o, EY, EV);
opti_consumption = table(K_o, c1_o,...
                         c2_l_o, c3_ll_o, c3_lh_o,...
                         c2_h_o, c3_hl_o, c3_hh_o);
opti_K_b2_b3.Variables =  round(opti_K_b2_b3.Variables, 2);
disp(opti_K_b2_b3);
opti_consumption.Variables =  round(opti_consumption.Variables, 2);
disp(opti_consumption);

%
%% A\ne 0 and Optimal Capital Choice with No-Roll-Over Loan
% Above, we solved the maximization problem with loans that allowed for roll-over. What are the optimal choices and consumption paths when roll-over loans are not allowed?
% To answer that, we resolve the problem above, but now imposeg an additional borrowing constraint for first period borrowing choice, our one-period no-roll-over constraint:
% b_2 \ge -Y_{2,l}
% Compared to the world where roll-over loans are allowed, the optimal choices show that:
% There is less capital investments, and less expected income, and lower welfare overall.
% Consumption in the high shock periods as well as the initial period are much lower.
% Consumption in the second period with l shock, and in the third period after two l shocks are higher because debt burdens are lower.

% Linear Inequality Constraints -K<=0, b2>-e2, and b3L and b3H can be positive or negative
LIN_CON_no_roll_over = [-1, 0,0,0;
                         0,-1,0,0]; 
q_no_roll_over = [0;e2];

% Optimization
[x_argmin, V_at_argmin] = fmincon(U_neg, b0, LIN_CON_no_roll_over, q_no_roll_over);
% Optimal Risky and Safe Asset Choices
K_o = x_argmin(1);
b2_o = x_argmin(2);
b3L_o = x_argmin(3);
b3H_o = x_argmin(4);

% Value at optimal Choices and Expected Income
EY = double(subs(expected_t2_income, {e2s, ps, As, Ks}, {e2, p, A, K_o}));
EV = double(subs(expected_utility,...
                          {As, Ks, ps, e1s, e2s, e3s, b2s, b3LsT2, b3HsT2, b3Ls, b3Hs},...
                          {A, K_o, p, e1, e2, e3, b2_o, b3L_o, b3H_o, b3L_o, b3H_o}));

% Consumption
c1_o = e1-b2_o-K_o;
c2_l_o = e2 + b2_o - b3L_o;
c2_h_o = e2 + A*K_o + b2_o - b3H_o;
c3_ll_o = e3 + b3L_o;
c3_lh_o = e3 + A*K_o + b3L_o;
c3_hl_o = e3 + b3H_o;
c3_hh_o = e3 + A*K_o + b3H_o;

% Combine Results
b3L_B_o = false;
b3H_B_o = false;
opti_K_b2_b3_nro = table(K_o, b2_o, b3L_o, b3H_o, b3L_B_o, b3H_B_o, EY, EV);
opti_consumption_nro = table(K_o, c1_o,...
                         c2_l_o, c3_ll_o, c3_lh_o,...
                         c2_h_o, c3_hl_o, c3_hh_o);

% No-Roll-Over and Roll-Over Resutls Together
opti_K_b2_b3_all = [opti_K_b2_b3; opti_K_b2_b3_nro];
opti_consumption_all = [opti_consumption; opti_consumption_nro];
opti_K_b2_b3_all.Properties.RowNames = {'roll-over allowed', 'no roll-over'};
opti_consumption_all.Properties.RowNames = {'roll-over allowed', 'no roll-over'};

% Display
opti_K_b2_b3_all.Variables =  round(opti_K_b2_b3_all.Variables, 2);
disp(opti_K_b2_b3_all);
opti_consumption_all.Variables =  round(opti_consumption_all.Variables, 2);
disp(opti_consumption_all);

%
%% Budget and Constraints with No-Roll-Over Loans + Bridge Loans
% To improve the sitution given no-roll-over one-period loans, we can introduce bridge loans.
% Following the discussions in Multi-period Loans, One Period Loans, Roll-over Loans, Bridge Loans, with bridge loans, the budget and constraints become:
%  c_1 + b^A_2 + K = e_1
% c_2 + b^A_3\left(b^A_2, s\right) + b^B_3\left(b^A_2, s\right) = Y_{2,s}\left(K\right) + b^A_2
% c_3 = Y_{3,s}\left(K\right) + b^A_3\left(b^A_2, s\right) + b^B_3\left(b^A_2, s\right)  \cdot (1+r_{\text{B}})
% And the borrowing constraints are:
% debt is less than endowment and new B borrowing: b^A_2 \ge -Y_{2,s}\left(K\right) + b^B_3\left(b^A_2, s\right)
% and b^A_3\left(b^A_2, s\right) + b^B_3\left(b^A_2, s\right) \cdot (1+r_{\text{B}}) \ge - Y_{3,s} \left(K\right)
% We have to solve for the optimal bridge loan choice in both h and l states in the second period.
%
%% Optimal Investments and Borrowing given Bridge Loan + No-Roll-Over Loan
% Given r_B = 0.30, we can solve for optimal (informal) bridge loan borrowing quantity in addition to (formal) no-roll-over one period loans. We just have to change our linear constraint slightly:
% To solve the updated problem:
% The linear constraint matrix needs two more column for the bridge loan choice for h and l: b^B_3\left(b^A_2, s\right)
% Two additional negativity constraint rows for the bridge loan choices for h and l: b^B_3\left(b^A_2, s\right)\le 0
% Two updated borrowing constraint for b^A_2: b^A_2 - b^B_3\left(b^A_2, l\right) \ge -Y_{2,l}, and b^A_2 - b^B_3\left(b^A_2, l\right) + A\cdot K \ge -Y_{2,l}
% The optimal choices, shown in table form below indicates that investment given bridge + no-roll-over is between no-roll-over and roll-over worlds.

% Bridge Loan Interest Rate
rB = 0.30;

% Linear Inequality Constraints
% Columns 1 to 4 are for: 
% 1:K>0 --> -K < 0
% 2:b2 - bB(3,l) >= -e(2,l) --> e(2,l) >= -b2 + bB(3,l)
% 3:b2 - bB(3,l) >= -e(2,l) - A*K --> e(2,l) >= -b2 + bB(3,l) - AK
% 4:bridge low < 0
% 5:bridge high < 0
LIN_CON_no_rlov_brdge = [-1, 0, 0, 0, 0, 0;
                          0,-1, 0, 0, 1, 0;
                         -A,-1, 0, 0, 0, 1;
                          0, 0, 0, 0, 1, 0;
                          0, 0, 0, 0, 0, 1;];
q_no_rlov_brdge = [0;e2;e2;0;0];

% Starting Search Points
% K, b2, b3L, b3H, b3Lbridge, b3Hbridge
b0_no_rlov_brdge = [0.1,-0.15,-0.15,-0.15,-0.01,-0.01]; 

% Add the Bridge Loan Choice to the Utility
U_neg = @(x) (-1)*(log(e1-x(2)-x(1)) + ...
        p*log(e2 + x(2) - x(3) - x(5)) + ...
        (1-p)*log(A*x(1) + e2 + x(2) - x(4) - x(6)) + ...
        (p)*(p)*log(e3 + x(3) + x(5)*(1+rB)) +...
        (p)*(1-p)*log(A*x(1) + e3 + x(3) + x(5)*(1+rB)) +...
        (1-p)*(p)*log(e3 + x(4) + x(6)*(1+rB)) +...
        (1-p)*(1-p)*log(A*x(1) + e3 + x(4) + x(6)*(1+rB)));
    
% Optimization
[x_argmin, V_at_argmin] = fmincon(U_neg,...
    b0_no_rlov_brdge, LIN_CON_no_rlov_brdge, q_no_rlov_brdge);
% Optimal Risky and Safe Asset Choices
K_o = x_argmin(1);
b2_o = x_argmin(2);
b3L_o = x_argmin(3);
b3H_o = x_argmin(4);
b3L_B_o = x_argmin(5); % Bridge bad shock t = 2
b3H_B_o = x_argmin(6); % Bridge good shock t = 2

% Value at optimal Choices and Expected Income
EY = double(subs(expected_t2_income, {e2s, ps, As, Ks}, {e2, p, A, K_o}));
EV = double(subs(expected_utility,...
                          {As, Ks, ps, e1s, e2s, e3s, b2s,...
                           b3LsT2, b3HsT2,...
                           b3Ls, b3Hs},...
                          {A, K_o, p, e1, e2, e3, b2_o,...
                           (b3L_o+b3L_B_o), (b3H_o+b3H_B_o),...
                           (b3L_o+b3L_B_o*(1+rB)), (b3H_o+b3H_B_o*(1+rB))}));
                     
% Consumption
c1_o = e1-b2_o-K_o;
c2_l_o = e2 + b2_o - b3L_o - b3L_B_o;
c2_h_o = e2 + A*K_o + b2_o - b3H_o  - b3H_B_o;
c3_ll_o = e3 + b3L_o + b3L_B_o*(1+rB);
c3_lh_o = e3 + A*K_o + b3L_o + b3L_B_o*(1+rB);
c3_hl_o = e3 + b3H_o + b3H_B_o*(1+rB);
c3_hh_o = e3 + A*K_o + b3H_o + b3H_B_o*(1+rB);

% Combine Results
opti_K_b2_b3_nro_brdg = table(K_o, b2_o, b3L_o, b3H_o, b3L_B_o, b3H_B_o, EY, EV);
opti_K_b2_b3_nro_brdg.Variables =  round(opti_K_b2_b3_nro_brdg.Variables, 2);
disp(opti_K_b2_b3_nro_brdg);
opti_consumption_nro_brdg = table(K_o, c1_o,...
                                  c2_l_o, c3_ll_o, c3_lh_o,...
                                  c2_h_o, c3_hl_o, c3_hh_o);

% No-Roll-Over and Roll-Over Resutls Together
opti_K_b2_b3_all = [opti_K_b2_b3; opti_K_b2_b3_nro; opti_K_b2_b3_nro_brdg];
opti_K_b2_b3_all.Properties.RowNames = {'roll-over allowed',...
                                            'no roll-over',...
                                            ['no roll-over + bridge r=' num2str(rB)]};

opti_consumption_all = [opti_consumption; opti_consumption_nro; opti_consumption_nro_brdg];
opti_consumption_all.Properties.RowNames = {'roll-over allowed',...
                                            'no roll-over',...
                                            ['no roll-over + bridge r=' num2str(rB)]};

% Display
opti_K_b2_b3_all.Variables =  round(opti_K_b2_b3_all.Variables, 2);
disp(opti_K_b2_b3_all);
opti_consumption_all.Variables =  round(opti_consumption_all.Variables, 2);
disp(opti_consumption_all);

%
%% Optimal Investments and Consumptions with Shifting Bridge Loan Interest Rates
% Keep the same parameters as before, we will now solve for optimal choices given different bridge loan interest rates.

% Loop over variations in interest rates of the bridge loan
bridge_r_min = 0.01; % no better than main/formal loan r
bridge_r_max = 1.0; % if higher than this, want to save
bridge_r_n = 100;
bridge_loan_r_vector = linspace(bridge_r_min, bridge_r_max, bridge_r_n);

lows_p_min = 0.40;
lows_p_max = 0.60;
lows_p_n = 3;
lows_p_vector = linspace(lows_p_min, lows_p_max, lows_p_n);

% Matrixes to Store Relevant Values
value_mat = zeros(lows_p_n, bridge_r_n);
K_o_mat = zeros(lows_p_n, bridge_r_n);
c2ll_o_mat = zeros(lows_p_n, bridge_r_n);
b3LB_frac_o_mat = zeros(lows_p_n, bridge_r_n);

% Table Storage
table_results_all = [];
table_rows_all = {};
graph_counter_all = 0;

% Loop over bridge loan
for lows_p_ctr=1:length(lows_p_vector)
    p = lows_p_vector(lows_p_ctr);
    for bridge_r_ctr=1:length(bridge_loan_r_vector)
        rB = bridge_loan_r_vector(bridge_r_ctr);
        
        % Bridge Loan utility function with varying interest rates
        U_neg = @(x) (-1)*(log(e1-x(2)-x(1)) + ...
                p*log(e2 + x(2) - x(3) - x(5)) + ...
                (1-p)*log(A*x(1) + e2 + x(2) - x(4) - x(6)) + ...
                (p)*(p)*log(e3 + x(3) + x(5)*(1+rB)) +...
                (p)*(1-p)*log(A*x(1) + e3 + x(3) + x(5)*(1+rB)) +...
                (1-p)*(p)*log(e3 + x(4) + x(6)*(1+rB)) +...
                (1-p)*(1-p)*log(A*x(1) + e3 + x(4) + x(6)*(1+rB)));
            
        % Optimization
        options = optimoptions('FMINCON','Display','off');
        [x_argmin, V_at_argmin] = fmincon(U_neg,...
            b0_no_rlov_brdge, LIN_CON_no_rlov_brdge, q_no_rlov_brdge,...
            [], [], [], [], [], options);
        
        % Optimal Risky and Safe Asset Choices
        K_o = x_argmin(1);
        b2_o = x_argmin(2);
        b3L_o = x_argmin(3);
        b3H_o = x_argmin(4);
        b3L_B_o = x_argmin(5); % Bridge bad shock t = 2
        b3H_B_o = x_argmin(6); % Bridge good shock t = 2
        
        % Value at optimal Choices and Expected Income
        EY = double(subs(expected_t2_income, {e2s, ps, As, Ks}, {e2, p, A, K_o}));
        EV = double(subs(expected_utility,...
                                  {As, Ks, ps, e1s, e2s, e3s, b2s,...
                                   b3LsT2, b3HsT2,...
                                   b3Ls, b3Hs},...
                                  {A, K_o, p, e1, e2, e3, b2_o,...
                                   (b3L_o+b3L_B_o), (b3H_o+b3H_B_o),...
                                   (b3L_o+b3L_B_o*(1+rB)), (b3H_o+b3H_B_o*(1+rB))}));
                              
        % Consumption
        c1_o = e1-b2_o-K_o;
        c2_l_o = e2     + b2_o - b3L_o - b3L_B_o;
        c2_h_o = e2 + A*K_o + b2_o - b3H_o  - b3H_B_o;
        c3_ll_o = e3 + b3L_o + b3L_B_o*(1+rB);
        c3_lh_o = e3 + A*K_o + b3L_o + b3L_B_o*(1+rB);
        c3_hl_o = e3 + b3H_o + b3H_B_o*(1+rB);
        c3_hh_o = e3 + A*K_o + b3H_o + b3H_B_o*(1+rB);
        
        % Store Results
        value_mat(lows_p_ctr, bridge_r_ctr) = EV;
        c2ll_o_mat(lows_p_ctr, bridge_r_ctr) = c3_ll_o;
        K_o_mat(lows_p_ctr, bridge_r_ctr) = K_o;
        b3LB_frac_o_mat(lows_p_ctr, bridge_r_ctr) = b3L_B_o/b2_o;
        
        % Combine Results
        opti_K_b2_b3_nro_brdg = table(K_o, b2_o, b3L_o, b3H_o, b3L_B_o, b3H_B_o, EY, EV);
        
        % Store to Table
        graph_counter_all = graph_counter_all + 1;
        table_results_all = [table_results_all; opti_K_b2_b3_nro_brdg];
        table_row_names_all{graph_counter_all} =...
            ['brdg-r:' sprintf('%3.2f', rB) ' low-p:' sprintf('%3.2f', p)];
        
    end
end

%
%% Graphing the Effects of Bridge Loans with Varying Interest Rates
% Similar to what we saw before, bridge loan has significant effects on all model outcomes. Bridge loans are useful even when bridge loan interest rates are close to 100 percent:
% If the chance of the high productivity state happening is higher, bridge loan demand is higher.
% Bridge loan usage is higher when its rates are lower
% Bridge loans help to signfiicantly increase optimal capital investments, especially when high productivity probability is higher.
% When investments increase (with lower bridge loan costs), due to higher borrowing initially, consumption along low shock path is worse.

for graph_vars=1:1:4
    
    figure();
    hold on;
    p_legend = {};
    for lows_p_ctr=1:length(lows_p_vector)
        
        p = lows_p_vector(lows_p_ctr);
        if (graph_vars == 1)
            data_vector = b3LB_frac_o_mat(lows_p_ctr,:);            
        elseif (graph_vars == 2)
            data_vector = K_o_mat(lows_p_ctr,:);
        elseif (graph_vars == 3)
            data_vector = value_mat(lows_p_ctr,:);
        elseif (graph_vars == 4)
            data_vector = c2ll_o_mat(lows_p_ctr,:);            
        end
        
        p_legend{lows_p_ctr} = ['High Productivity Prob = ' num2str(1-p)];
        
        if (lows_p_ctr == 1)            
            plot(bridge_loan_r_vector, data_vector, ':', 'LineWidth', 3);            
        elseif (lows_p_ctr == 2)
            plot(bridge_loan_r_vector, data_vector, ':', 'LineWidth', 3);            
        elseif (lows_p_ctr == 3)
            plot(bridge_loan_r_vector, data_vector, '--', 'LineWidth', 2);            
        elseif (lows_p_ctr == 4)
            plot(bridge_loan_r_vector, data_vector, '-',  'LineWidth', 1);
        end    
        
    end

    if (graph_vars == 1)    
        title({['(Bridge Loan T=2 Low Shock)/(T=2 Debt)'],...
               ['(Bridge Loan T=2 high Shock is always zero)'],...
               ['e1 = ' num2str(e1)...
                ', Y(low) = ' num2str(e2)...
                ', Y(high) = ' num2str(A) '*K+' num2str(e2)],...
               ['Formal/No-Rollover R = ' num2str(0), ', Informal/Bridge R varies']});
        ylabel({['Bridge Loan Choice T=2 Low Shock Fraction'],['(Bridge + No-Roll-Over)']});
        legend(p_legend, 'Location','northeast');        
    elseif (graph_vars == 2)
        title({['Optimal K'],...
               ['e1 = ' num2str(e1)...
                ', Y(low) = ' num2str(e2)...
                ', Y(high) = ' num2str(A) '*K+' num2str(e2)],...
               ['Formal/No-Rollover R = ' num2str(0), ', Informal/Bridge R varies']});
        ylabel({['Risky Capital Investment'],['(Bridge + No-Roll-Over)']});
        legend(p_legend, 'Location','northeast');
    elseif (graph_vars == 3)
        title({['E(V) 3 periods'],...
               ['e1 = ' num2str(e1)...
                ', Y(low) = ' num2str(e2)...
                ', Y(high) = ' num2str(A) '*K+' num2str(e2)],...
               ['Formal/No-Rollover R = ' num2str(0), ', Informal/Bridge R varies']});
        ylabel({['E(V) 3 Periods'],['(Bridge + No-Roll-Over)']});
        legend(p_legend, 'Location','northeast');
    elseif (graph_vars == 4)
        title({['C after 2 Bad Shocks'],...
               ['e1 = ' num2str(e1)...
                ', Y(low) = ' num2str(e2)...
                ', Y(high) = ' num2str(A) '*K+' num2str(e2)],...
               ['Formal/No-Rollover R = ' num2str(0), ', Informal/Bridge R varies']});
        ylabel('Consumption After Two Bad Shocks');
        ylabel({['Consumption After Two Bad Shocks'],['(Bridge + No-Roll-Over)']});
        legend(p_legend, 'Location','southeast');
    end
    
    x_title_line = ['(Informal) Bridge Loan Interest Rate, 0.1 = 10%'];
    xlabel(x_title_line);
        
    % grids
    grid on;
end

%
%% Tabulate All Results
% The Tables below tabulate results, showing 100 rows from the graph above:

table_results_all.Properties.RowNames = table_row_names_all;
[rows, cols] = size(table_results_all);
% Only show 100 rows
rows_display = floor(linspace(1, rows, 100));

% 8 Columns of Choices and Outcomes in the Table Below:
% K_o: K^{*}, Optimal capital choice (made in t=1)
% b2_o: b^{A,*}_2, Optimal 1st period No-Roll-Over (formal) borrowing/savings choice (debt to be paid back in t=2). Households will borrow because e_1=0
% b3L_o: b^{A,*}_3\left(b^A_2, l\right), Optimal 2nd period, when shock is l, No-Roll-Over (formal) borrowing/savings choice (to be paid back in t=3). Households will borrow here because shock is low.
% b3H_o: b^{A,*}_3\left(b^A_2, h\right), Optimal 2nd period, when shock is h, No-Roll-Over (formal) borrowing/savings choice (to be paid back in t=3).
% b3L_B_o: b^{B,*}_3\left(b^A_2, l\right), Optimal 2nd period, when shock is l, Bridge Loan (informal) borrowing choice. Used to pay b^{A,*}_2 debts when Y_{2,l}--in bad shock state--is insufficient to cover b^{A,*}_2 fully. Borrowing bridge loan allows households to clear current debt, b^{A,*}_2, with formal lender, and borrow new b^{A,*}_3\left(b^A_2, l\right)from formal lender again, which is to be paid back in t=3.
% b3H_B_o: b^B_3\left(b^A_2, h\right), Optimal 2nd period, when shock is h, Bridge Loan (informal) borrowing choice. This will be 0, because in high shock state, there is enough output to pay back debt b^{A,*}_2, bridge loan is unnecessary.
% EY: expected income, same in the 2nd and 3rd period because there is a single investment decision in the first period.
% EV: expected value given optimal choices.

table_results_all.Variables =  round(table_results_all.Variables, 2);
disp(table_results_all(rows_display,:))