%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  This code draws figures 1 and 2 from the working paper
%  Fiscal Policy Can reduce Unemployment But There is a Better Alternative
%  (c) Roger E. A. Farmer, department of Economics UCLA
%  October 18 2009
%  It also Prints the data used to construct Table 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  This is the main prgram
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

opt=findopt;                
Zstar=opt(1);
Rstar=opt(2);
pkstar=opt(3);
fac=0.995;      % fac sets bounds on the axes in Figure 1
                % It may need to be adjusted for parameter values that
                % are diffferent from the baseline
RL = Rstar*fac;
RU=Rstar/fac;
R=RL:0.0001:RU;   
thet = setparams;
alph=thet(4);
                            
plot(([NA(R,pkstar);ME(R,pkstar,0,0)])',R);
figure(2);

pa=pkstar*0.8;
Za=pa*(Rstar-1)/alph;
Ba=0;
[Gb, Rb, taub]=solveforG(pa,Ba);   
                                
fac=0.985;       % fac sets bounds on the axes in Figure 2
                % It may need to be adjusted for parameter values that
                % are diffferent from the baseline
RL=Rb*fac;                  
RU=Rb/fac;                  
R=RL:0.0001:RU;

G=[0;0;Gb];     % G,B and pk must all have the same number of value: 
B=[0;0;0];      % One for each plot
pk=[pkstar;pa;pa];
plot(([NA(R,pk);ME(R,pk,B,G)])', kron(ones(6,1),R)' );

%  Print data to construct Table 1
disp(['Za (percent of Z*)= ' num2str(Za/Zstar)...
    '  Zstar = ' num2str(Zstar) '  G (Percent of Z*) =' ...
    num2str(Gb/Zstar) '  tau = ' num2str(taub) ...
    'Rstar = ' num2str(Rstar) '  R = ' num2str(Rb)]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



function thet = setparams                           
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  This functions sets the parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
bet=0.97;                   % Discount factor
alph=0.34;                  % Capital's share of income
pye=0.98;                   % Survival probability
betaT=(1-pye.*(1-bet.*pye))./(pye);  
alphaT=(1-bet.*pye).*(1-pye)./(1-pye*(1-bet.*pye));
thet=[alphaT;betaT;bet;alph;pye];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  END OF FUNCTION 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function opt=findopt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  This function computs the optimum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
thet = setparams;
Rstar=fsolve(@solveforR,1.05);

alph=thet(4);
Zstar = 0.5./(1-alph);
pkstar=alph*Zstar/(Rstar-1);
opt=[Zstar,Rstar,pkstar];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  END OF FUNCTION 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




function x=solveforR(R)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  This function computs the interest rate that supports the optimum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                          
    thet=setparams;
    alph=thet(4);
    alphaT=thet(1);
    betaT=thet(2);
    x= (R-1)/alph - alphaT*R*betaT/((1-alphaT)*R*betaT-1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  END OF FUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function [G, R, tau]=solveforG(p,B)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  This function computes the fiscal intervention that restores full
%  employment and the and the tax rate that finances it
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                          
    thet=setparams;
    alph=thet(4);
    opt=findopt;
    Zstar=opt(1);
    G= Zstar - H((alph*Zstar/p)+1)*(p + B/((alph*Zstar/p)+1));
    R=(alph*Zstar/p)+1;
    tau= (1/((1-alph)*Zstar))*((R-1)*B/R + G);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  END OF FUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function Z=NA(R,pk)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  This function computes the No Arbitrage Equation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin < 2
    pk=1;
end
thet=setparams;
alph=thet(4);
Z=(kron(pk,ones(1,size(R,2))).*(kron(R,ones(size(pk,1),1))-1))./alph;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  END OF FUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    
function Z = ME(R,pk,B,G)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  This function computes the Market Equilibrium Equation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                         
if nargin < 4
    G=0;
end
if nargin < 3
    B=0;
end
if nargin <2
    pk=1;
end
Z=kron(ones(size(B,1),1),H(R)).*(kron(ones(1,size(R,2)),(pk)) + ...
    kron(1./R,(B)))+ kron(ones(1,size(R,2)),G);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  END OF FUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  This  computes the function H(R) 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
function y=H(R)
thet = setparams;
betaT=thet(2);
alphaT=thet(1);
y=alphaT.*R.*betaT./((1-alphaT).*R.*betaT-1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  END OF FUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  END OF SCRIPT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%