%% Model with Menu + Bridge + Cmin--Choice Sets
% Go back to LuuRobFan Project Main Page
% In various previous files, we have developed individual model components: Menu of formal loans, using informal loans as bridge loans in an environment where formal loans can not be rolled over, and allowing for delayed repayment through minimum consumption.
% Here we combine these ingredients together.
% The model has a particular set of choice constraints. These choice constraints are impacted by the informal and formal interest rates, as well as the individual levels of cash-on-hand.
% In this file, we visualize the joint choice set
%
%% Setting Up Parameters
% Households differ in: w, ri common grid

% Formal Borrowing and Savings Interest Rates
rfb = 0.0;
rfs = -0.50;

% Borrowing Constraint
[NBC, BDcmin] = deal(2, -4);

% state grid and matrix
[w_min, w_max, w_n] = deal(-0.50, 2, 2);
w_vector = linspace(w_min, w_max, w_n);
[ri_min, ri_max, ri_n] = deal(0.15, 1.25, 4);
ri_vector = linspace(ri_min, ri_max, ri_n);

% choice grid and matrix
[kp_n, bp_n] = deal(100, 100);
[kp_min, kp_max, kp_n] = deal(0, 2 + w_max, kp_n);
[bn_min, bn_max, bn_n] = deal(-2, +2, bp_n);

breaks = 3;
bspan = (bn_min/breaks);

%
%% Conditioning Choice Sets.
% These are the linear constraints that restrict choice set for each model version and the joint model.

% Rollover Standard Borrow Save
rlov = @(kp, bp, w, rs, rb, b_bar) ...
    find((kp >= 0).*(bp >= b_bar).* ...
    (...
    (bp < 0).*(kp < (w - (1/(1+rfb).*bp))) + ...
    (bp >= 0).*(kp < (w - (1/(1+rfs).*bp))) ...
    ));

% Rollover Standard Borrow Save with Menu
inc = @(ri) (-1)*(bspan*((1/(1+rfb)) - (1/(1+ri))));
rlov_menu = @(kp, bp, w, ri, rs, rb, b_bar) ...
    find((kp >= 0).*(bp >= b_bar).* ...
    (...
    ...
    ((3*bspan <= bp).*(bp < 2*bspan).*((kp < (w + inc(ri)*2 - (1/(1+ri).*bp)))) + ...
    (2*bspan <= bp).*(bp < 1*bspan).*((kp < (w + inc(ri)*1 - (1/(1+ri).*bp)))) + ...
    (1*bspan <= bp).*(bp < 0*bspan).*((kp < (w + inc(ri)*0 - (1/(1+ri).*bp))))) ...
    + ...
    (bp >= 0).*(kp < (w - (1/(1+rfs).*bp))) ...
    ));

% Rollover Standard Borrow Save with Bridge Loans
rlov_bridge = @(kp, bp, w, ri, rs, rb, b_bar) ...
    find((kp >= 0).*(bp >= b_bar).* ...
    (...
    (w >= 0).*(bp < 0).*(kp < (w - (1/(1+rfb).*bp))) + ...
    (w < 0).*( ...
    ((w*(1+ri)) <= bp).*(bp < 0).*(kp < (w - (1/(1+ri).*bp))) + ...
    ((w*(1+ri)) > bp).*(kp < (w*((1+ri)/(1+rfb)) - (1/(1+rfb).*bp))) ...
    ) + ...
    (bp >= 0).*(kp < (w - (1/(1+rfs).*bp))) ...
    ));

% Rollover Standard Borrow Save with Bridge Loans with Menu
rlov_menu_bridge = @(kp, bp, w, ri, rs, rb, b_bar) ...
    find((kp >= 0).*(bp >= b_bar).* ...
    (...
    (w >= 0).*(...
    (6*bspan <= bp).*(bp < 5*bspan).*((kp < (w + inc(ri)*5 - (1/(1+ri).*bp)))) + ...
    (5*bspan <= bp).*(bp < 4*bspan).*((kp < (w + inc(ri)*4 - (1/(1+ri).*bp)))) + ...
    (4*bspan <= bp).*(bp < 3*bspan).*((kp < (w + inc(ri)*3 - (1/(1+ri).*bp)))) + ...
    (3*bspan <= bp).*(bp < 2*bspan).*((kp < (w + inc(ri)*2 - (1/(1+ri).*bp)))) + ...
    (2*bspan <= bp).*(bp < 1*bspan).*((kp < (w + inc(ri)*1 - (1/(1+ri).*bp)))) + ...
    (1*bspan <= bp).*(bp < 0*bspan).*((kp < (w + inc(ri)*0 - (1/(1+ri).*bp)))) ...
    )+ ...
    (w < 0).*( ...
    ((w*(1+ri)) <= bp).*(bp < 0).*(kp < (w - (1/(1+ri).*bp))) + ...
    ((6*bspan <= bp).*(bp < 5*bspan).*((kp < (w*((1+ri)/(1+rfb)) + ...
    inc(ri)*4 - (1/(1+ri).*bp)))) + ...
    (5*bspan <= bp).*(bp < 4*bspan).*((kp < (w*((1+ri)/(1+rfb)) + ...
    inc(ri)*3 - (1/(1+ri).*bp)))) + ...
    (4*bspan <= bp).*(bp < 3*bspan).*((kp < (w*((1+ri)/(1+rfb)) + ...
    inc(ri)*3 - (1/(1+ri).*bp)))) + ...
    (3*bspan <= bp).*(bp < 2*bspan).*((kp < (w*((1+ri)/(1+rfb)) + ...
    inc(ri)*2 - (1/(1+ri).*bp)))) + ...
    (2*bspan <= bp).*(bp < 1*bspan).*((kp < (w*((1+ri)/(1+rfb)) + ...
    inc(ri)*1 - (1/(1+ri).*bp)))) + ...
    (1*bspan <= bp).*(bp < 0*bspan).*((kp < (w*((1+ri)/(1+rfb)) + ...
    inc(ri)*0 - (1/(1+ri).*bp))))) ...
    ) + ...
    (bp >= 0).*(kp < (w - (1/(1+rfs).*bp))) ...
    ));

%
%% Consumption Equations
% We have to also specifiy consumption equations.

% Cmin Consumption Equation
fchat = @(c, cmin) (c).*(c >= cmin) + (cmin).*(c < cmin);

% C
fc = @(w, kn, bn) (w - kn - bn);

% consumption for basic roll-over with differential borrow save rate
fc_rlov = @(w, kn, bn) (w - kn - ...
    bn.*( ...
    (bn < 0).*(1/(1+rfb)) + ...
    (bn >= 0).*(1/(1+rfs)) ...
    ) ...
    );

% consumption for bridge + menu with differential borrow save rate
fc_rlov_grid = @(w, kn, bn, ri) (w - kn - ...
    ( ...
    (7*bspan <= bn).*(bn < 6*bspan).*(6*bspan*(1/(1+rfb)) + (bn-6*bspan).*(1/(1+ri))) + ...
    (6*bspan <= bn).*(bn < 5*bspan).*(5*bspan*(1/(1+rfb)) + (bn-5*bspan).*(1/(1+ri))) + ...
    (5*bspan <= bn).*(bn < 4*bspan).*(4*bspan*(1/(1+rfb)) + (bn-4*bspan).*(1/(1+ri))) + ...
    (4*bspan <= bn).*(bn < 3*bspan).*(3*bspan*(1/(1+rfb)) + (bn-3*bspan).*(1/(1+ri))) + ...
    (3*bspan <= bn).*(bn < 2*bspan).*(2*bspan*(1/(1+rfb)) + (bn-2*bspan).*(1/(1+ri))) + ...
    (2*bspan <= bn).*(bn < 1*bspan).*(1*bspan*(1/(1+rfb)) + (bn-1*bspan).*(1/(1+ri))) + ...
    (1*bspan <= bn).*(bn < 0*bspan).*(0*bspan*(1/(1+rfb)) + (bn-0*bspan).*(1/(1+ri))) + ...
    (bn >= 0).*bn.*(1/(1+rfs)) ...
    ) ...
    );

%
%% Generate Choice Set Graphs
% Below we generate choice set graphs over different interest rates and wealth levels. For the different model versions.

% Model Versions
% 'rollover_bridge_grid'
model_list = {'rollover', 'rollover_grid', 'rollover_bridge', ...
              'rollover_bridge_grid_cmin'};

% Loop over the choice matrix for different individuals
for subset_type = model_list        
    for w_i=1:w_n
        sub_plot_ctr = 0;
        figure();
        for ri_i=1:ri_n                        
            sub_plot_ctr = sub_plot_ctr + 1;
            
            % Grids Meshed
            if (strcmp(subset_type, 'rollover_bridge_grid_cmin'))
                bn_vector = linspace(BDcmin, bn_max, bn_n);
                bn_min_xmin = BDcmin;
            else
                bn_vector = linspace(bn_min, bn_max, bn_n);
                bn_min_xmin = bn_min;
            end
            kn_vector = linspace(kp_min, kp_max, kp_n);
            [kp_mesh, bp_mesh] = meshgrid(kn_vector, bn_vector);
            
            % Household Specific cash-on-hand and interest
            % cash-on-hand(A,k,b,eps)
            w_cur = w_vector(w_i);
            ri_cur = ri_vector(ri_i);
            
            % s stands for subset
            kp_s = kp_mesh(:);
            bp_s = bp_mesh(:);
            
            cmin = 0;
            titleline2 = ['w=' num2str(w_cur)];
            % Different subsetting functions
            if (strcmp(subset_type, 'all'))
                % Consumption Levels
                chat = fchat(fc(w_cur, kp_s, bp_s), cmin);
            else
                if (strcmp(subset_type, 'rollover'))
                    idx = rlov(kp_s, bp_s, w_cur, rfs, rfb, -NBC);
                    kp_s = kp_s(idx);
                    bp_s = bp_s(idx);
                    chat = fchat(fc_rlov(w_cur, kp_s, bp_s), cmin);
                    %                 idx_m = rlov(kp_s, bp_s, w_vector, rfs, rfb, -NBC);
                    %                 idx_m = idx_m-floor(idx_m/25)*25;
                    titleline = 'Standard BrSv';
                end
                if (strcmp(subset_type, 'rollover_grid'))
                    idx = rlov_menu(kp_s, bp_s, w_cur, ri_cur, rfs, rfb, -NBC);
                    kp_s = kp_s(idx);
                    bp_s = bp_s(idx);
                    chat = fchat(fc_rlov_grid(w_cur, kp_s, bp_s, ri_cur), cmin);
                    titleline = 'Standard BrSv + Menu';
                    titleline2 = ['w=' num2str(w_cur) ', ri=' num2str(ri_cur)];
                end
                if (strcmp(subset_type, 'rollover_bridge'))
                    idx = rlov_bridge(kp_s, bp_s, w_cur, ri_cur, rfs, rfb, -NBC);
                    kp_s = kp_s(idx);
                    bp_s = bp_s(idx);
                    chat = fchat(fc_rlov(w_cur, kp_s, bp_s), cmin);
                    titleline = 'Standard BrSv + Bridge';
                    titleline2 = ['w=' num2str(w_cur) ', ri=' num2str(ri_cur)];
                end
                if (strcmp(subset_type, 'rollover_bridge_grid'))
                    idx = rlov_menu_bridge(kp_s, bp_s, w_cur, ri_cur, rfs, rfb, -NBC);
                    kp_s = kp_s(idx);
                    bp_s = bp_s(idx);
                    chat = fchat(fc_rlov_grid(w_cur, kp_s, bp_s, ri_cur), cmin);
                    titleline = 'BrSv + Menu + Bridge';
                    titleline2 = ['w=' num2str(w_cur) ', ri=' num2str(ri_cur)];
                end
                if (strcmp(subset_type, 'rollover_bridge_grid_cmin'))
                    idx = rlov_menu_bridge(kp_s, bp_s, w_cur, ri_cur, rfs, rfb, BDcmin);
                    kp_s = kp_s(idx);
                    bp_s = bp_s(idx);
                    cmin = 0.01;
                    chat = fchat(fc_rlov_grid(w_cur, kp_s, bp_s, ri_cur), cmin);
                    titleline = 'BrSv + Menu + Bridge + Cmin';
                    titleline2 = ['w=' num2str(w_cur) ', ri=' num2str(ri_cur)];
                end
            end
            
            % Scatter Draws
            subplot(2, 2, sub_plot_ctr);
            hold on;
            scatter(bp_s, kp_s, 10, chat(:));
            
            % Graph bounds
            x_max = bn_max+0.2;
            x_min = bn_min_xmin-0.2;
            y_max = kp_max;
            y_min = w_min;
            
            % Natural Borrowing Constraint
            plot(ones(10, 1) * -NBC, linspace(y_min, y_max, 10), ...
                'b-', 'HandleVisibility', 'off');
            
            % 0 lines
            plot(linspace(x_min, x_max, 10), ones(10, 1) * 0, ...
                'k--', 'HandleVisibility', 'off');
            plot(ones(10, 1) * 0, linspace(y_min, y_max, 10), ...
                'k--', 'HandleVisibility', 'off');
            
            % Titling and Axis
            xlim([x_min, x_max]);
            ylim([y_min, y_max]);
            title({[titleline], titleline2});
            ylabel('Risky Capital Choice');
            xlabel('Borr Save (principle + r)');
            
            %
        end
    end
end