%% sipc.m
% implements equations (A28) and (A29) in appendix of Mankiw and Reis, QJE
% 2002 to obtain inflation solution when money growth follows an AR(1)
% process
% 
clear all
% following values are from Mankiw and Reis
% degree of information stickiness
lambda = 0.25;
% elasticity of inflation with respect to output
alpha = 0.1; 
% money growth process
% Dm(t) = rho*Dm(t-1) + e(t)
rho = 0.5;
rhor = rho;

for r = 1:2
    lambda = lambda + (r-1)*.25;
    lambda

% moving average process for inflation
% pi(t) = SUM g(i+1)*e(t-i)
% note: my g(1) corresponds to their g(0).
g(1) = alpha*lambda/(1 - lambda*(1-alpha));

K = 30;
SUMG(1) = g(1);
SUMRHO(1) = rhor;
SUML(1) = 1-lambda;
den(1) = 1 - lambda*(1-alpha)*(1+(1-lambda));
for i = 2:K 
    den = 1 - lambda*(1-alpha)*(1+SUML(i-1));
    g(i) = alpha*lambda*( 1 - SUMG(i-1) + SUMRHO(i-1) + (rhor^(i-1)*SUML(i-1)))/den;
    SUMG(i) = SUMG(i-1) + g(i);
    SUMRHO(i) = SUMRHO(i-1) + rhor^(i);
    SUML(i) = SUML(i-1) + (1-lambda)^(i);
end

% impulse for output
% y = SUM b(i+1)*e(t-i)
b(1) = 1 - g(1);
cb(1) = b(1);
for i = 2:K
    cb(i) = cb(i-1) + (rhor^(i-1)) - g(i); % for level of output
    b(i) =  (rhor^(i-1)) - g(i);  % for growth rate
end

% impulse for money
m(1) = 1;
for i = 2:K
    m(i) = rhor^i;
end

if r == 1;
    coeff1 = [m' g' b' cb'];
else
    if r == 2
    coeff2 = [m' g' b' cb'];
    else 
        coeff3 = [m' g' b' cb'];
    end
end
end
%% renaming
inflation1  = coeff1(:,2);
inflation2  = coeff2(:,2);
output1     = coeff1(:,4);
output2     = coeff2(:,4);
%% figures (see 
periods = 1:size(g,2);
periods=periods;
figure (3)
plot(periods,coeff1(:,2)','k',periods,coeff2(:,2),':')
legend('Inflation, \lambda = 0.25','Inflation, \lambda = 0.5')

figure(4)
plot(periods,coeff1(:,4)','k',periods,coeff2(:,4)',':'); %,tid,cb',':')
legend('Output,, \lambda = 0.25','Output, \lambda = 0.5')

%% Generating the figures
%% Figure 5.1
figure(1)
plot(periods,inflation1,periods,inflation2,':','LineWidth',1.4)
text(6,0.3,'\lambda = 0.5')
text(16,0.07,'\lambda = 0.25')
xlabel('periods')
%print -depsc2 'C:\Users\cewjl\Dropbox\4thedition\Chpt5\graphics_ch5\ch5_fig1';

%% Figure 5.2
figure(2)
plot(periods,output1,periods,output2,':','LineWidth',1.4)
text(6.5,0.3,'\lambda = 0.5')
text(9,1.0,'\lambda = 0.25')
xlabel('periods')
%print -depsc2 'C:\Users\cewjl\Dropbox\4thedition\Chpt5\graphics_ch5\ch5_fig2';



