clear all;
close all;
clc;

% This code is an implementation of a 2x2 limited commitment problem
% there are two states (1 and 2) or (lo and hi)
% 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    (2 equations) - participation constraints
% [1 1]*c=1          (1 equation)  - consumptions sum up to total endowment
% w(2)=Uaut(2)       (1 equation)  - 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.65;    %discount factor
y=[0.35; 0.65];  %endowment vector (lo; hi)
pi     = 0.75;   %transition probability (persistence)

P=[pi,1-pi;1-pi,pi]; %transition matrix

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

    %solve the system
    x0=[Upr;Upr;0.5;0.5];
    x=fsolve(@myfunc,x0);  
    c1=x(3:4,1);
    end

%vary beta
for i=1:100
    betai(i)=(i-1)/100;
    beta=betai(i);
    P=[pi,1-pi;1-pi,pi]; %transition matrix

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

    %solve the system
    x0=[Upr;Upr;0.5;0.5];
    x=fsolve(@myfunc,x0);    
    c(:,i)=x(3:4,1);
    end

end
figure(1)
plot(betai,c(1,:),betai,c(2,:))
legend('risk sharing depending on beta');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%parameters
gamma = 2;       %risk aversion
beta  = 0.7;    %discount factor
y=[0.35; 0.65];  %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=[pi,1-pi;1-pi,pi]; %transition matrix

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

    %solve the system
    x0=[Upr;Upr;0.5;0.5];
    x=fsolve(@myfunc,x0);    
    c(:,i)=x(3:4,1);
    end

end
figure(2)
plot(betai,c(1,:),betai,c(2,:))
legend('risk sharing depending on gamma');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%parameters
gamma = 2;       %risk aversion
beta  = 0.7;    %discount factor
y=[0.35; 0.65];  %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=[pi,1-pi;1-pi,pi]; %transition matrix

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

    %solve the system
    x0=[Upr;Upr;0.5;0.5];
    x=fsolve(@myfunc,x0);  
    c(:,i)=x(3:4,1);
    end

end
figure(3)
plot(betai,c(1,:),betai,c(2,:))
legend('risk sharing depending on pi');
