clear all;
close all;
clc;

% This code is an implementation of a 7x2 limited commitment problem
% there are seven states and two agents
% there are also promised utilities w and consumption values c,
% corresponding to each state
%
% The equations we need to solve in general look the following way:
% w=u(c)+beta*P*w      (7 equations)  - participation constraints
% M*c=1                (4 equations)  - consumptions sum up to total endowment
% w(hi)=max(Uaut(hi),U(1/2))       (3 equations)  - in high endowment states agents are guaranteed autarky values at least
global beta gamma P y Uau Upr

%parameters
gamma = 2;       %risk aversion
beta  = 0.75;    %discount factor
%y=[0.35; 0.4; 0.45; 0.5; 0.55; 0.6; 0.65];  %endowment vector (lo; hi)
y=[0.2; 0.3; 0.4; 0.5; 0.6; 0.7; 0.8];  %endowment vector (lo; hi)
pi     = 0.75;   %transition probability (persistence)

P=(ones(7,7)-eye(7,7))*(1-pi)/6+eye(7,7)*pi;

    %find autarky levels of utility
    Uau=inv(eye(7,7)-beta*P)*(y.^(1-gamma))/(1-gamma);
    
    %check if risk sharing is possible
    Upr=(((ones(1,7)*y/7)^(1-gamma))/(1-gamma))/(1-beta);
    if max(Uau)<Upr
        %display('perfect risk sharing is possible');
        c(:,i)=(ones(1,7)*y/7)*ones(7,1);
    else
        %display('perfect risk sharing is NOT possible');

    %solve the system
    x0=[Upr;Upr;Upr;Upr;Upr;Upr;Upr;0.5;0.5;0.5;0.5;0.5;0.5;0.5];
    x=fsolve(@myfunc7,x0);
    
    c1=x(8:14,1);
    end


%vary beta
for i=1:100
    betai(i)=(i-1)/100;
    beta=betai(i);
    P=(ones(7,7)-eye(7,7))*(1-pi)/6+eye(7,7)*pi;

    %find autarky levels of utility
    Uau=inv(eye(7,7)-beta*P)*(y.^(1-gamma))/(1-gamma);
    
    %check if risk sharing is possible
    Upr=(((ones(1,7)*y/7)^(1-gamma))/(1-gamma))/(1-beta);
    if max(Uau)<Upr
        %display('perfect risk sharing is possible');
        c(:,i)=(ones(1,7)*y/7)*ones(7,1);
    else
        %display('perfect risk sharing is NOT possible');

    %solve the system
    x0=[Upr;Upr;Upr;Upr;Upr;Upr;Upr;0.5;0.5;0.5;0.5;0.5;0.5;0.5];
    x=fsolve(@myfunc7,x0);
    
    c(:,i)=x(8:14,1);
    end

end
 figure(1)
 plot(betai,c(1,:),betai,c(2,:),betai,c(3,:),betai,c(4,:),betai,c(5,:),betai,c(6,:),betai,c(7,:))
 legend('risk sharing depending on beta');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%parameters
gamma = 2;       %risk aversion
beta  = 0.7;    %discount factor
%y=[0.35; 0.4; 0.45; 0.5; 0.55; 0.6; 0.65];  %endowment vector (lo; hi)
y=[0.2; 0.3; 0.4; 0.5; 0.6; 0.7; 0.8];  %endowment vector (lo; hi)
pi     = 0.75;   %transition probability (persistence)

%%now vary gamma
for i=1:100
    betai(i)=i/33 + 1;
    gamma=betai(i);
    P=(ones(7,7)-eye(7,7))*(1-pi)/6+eye(7,7)*pi;

    %find autarky levels of utility
    Uau=inv(eye(7,7)-beta*P)*(y.^(1-gamma))/(1-gamma);
    
    %check if risk sharing is possible
    Upr=(((ones(1,7)*y/7)^(1-gamma))/(1-gamma))/(1-beta);
    if max(Uau)<Upr
        %display('perfect risk sharing is possible');
        c(:,i)=(ones(1,7)*y/7)*ones(7,1);
    else
        %display('perfect risk sharing is NOT possible');

    %solve the system
    x0=[Upr;Upr;Upr;Upr;Upr;Upr;Upr;0.5;0.5;0.5;0.5;0.5;0.5;0.5];
    x=fsolve(@myfunc7,x0);
    
    c(:,i)=x(8:14,1);
    end

end
figure(2)
plot(betai,c(1,:),betai,c(2,:),betai,c(3,:),betai,c(4,:),betai,c(5,:),betai,c(6,:),betai,c(7,:))
 legend('risk sharing depending on gamma');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%parameters
gamma = 2;       %risk aversion
beta  = 0.75;    %discount factor
%y=[0.35; 0.4; 0.45; 0.5; 0.55; 0.6; 0.65];  %endowment vector (lo; hi)
y=[0.2; 0.3; 0.4; 0.5; 0.6; 0.7; 0.8];  %endowment vector (lo; hi)
pi     = 0.75;   %transition probability (persistence)

%%now vary pi
for i=1:100
    betai(i)=i/100;
    pi=betai(i);
    P=(ones(7,7)-eye(7,7))*(1-pi)/6+eye(7,7)*pi;

    %find autarky levels of utility
    Uau=inv(eye(7,7)-beta*P)*(y.^(1-gamma))/(1-gamma);
    
    %check if risk sharing is possible
    Upr=(((ones(1,7)*y/7)^(1-gamma))/(1-gamma))/(1-beta);
    if max(Uau)<Upr
        %display('perfect risk sharing is possible');
        c(:,i)=(ones(1,7)*y/7)*ones(7,1);
    else
        %display('perfect risk sharing is NOT possible');

    %solve the system
    x0=[Upr;Upr;Upr;Upr;Upr;Upr;Upr;0.5;0.5;0.5;0.5;0.5;0.5;0.5];
    x=fsolve(@myfunc7,x0);
    
    c(:,i)=x(8:14,1);
    end
    
end
figure(3)
plot(betai,c(1,:),betai,c(2,:),betai,c(3,:),betai,c(4,:),betai,c(5,:),betai,c(6,:),betai,c(7,:))
legend('risk sharing depending on pi');
