capital=xlsread('CapitalStock.xlsx',1,'B2:B282');
population=xlsread('Dataset_cycles.xlsx','Quarterly','T8:T288');

alppha=0.338;

capacity_utilization=xlsread('Dataset_cycles.xlsx','Quarterly','AD8:AD288'); %only starts in 48

capital_services = capital.*capacity_utilization;

GDP=xlsread('Dataset_cycles.xlsx','Quarterly','B12:B292');
%% hours
hours=xlsread('Dataset_cycles.xlsx','Quarterly','P12:P292');


%% King Rebelo
log_SR=log(GDP)-alppha*log(capital)-(1-alppha)*log(hours);
A=detrend(log_SR(~isnan(log_SR)));

log_SR_util=log(GDP)-alppha*log(capital_services)-(1-alppha)*log(hours);
A_util=detrend(log_SR_util(~isnan(log_SR_util)));

log_SR(~isnan(log_SR))-A;
diff(ans);
x=[A(1:end-1,:)];
betta_TFP_obs=x\A(2:end,:)
resids=A(2:end,:)-x*betta_TFP_obs;
var_eps=sqrt(1/(length(resids-1))*sum(resids.^2))

[~, tfp_obs_cycle]=myhpfilter(log_SR(~isnan(log_SR)),1600);
[~, tfp_obs_cycle_util]=myhpfilter(log_SR_util(~isnan(log_SR_util)),1600);


x=[tfp_obs_cycle(1:end-1,:)];
betta_TFP_obs_HP=x\tfp_obs_cycle(2:end,:)
resids_HP=tfp_obs_cycle(2:end,:)-x*betta_TFP_obs_HP;
var_eps_HP=sqrt(1/(length(resids_HP-1))*sum(resids_HP.^2))

load('recessiondates')

figure
subplot(2,1,1)
plot(timeline_quarterly(~isnan(log_SR)),A,'LineWidth',2);
aaa=ylim;
bottom=aaa(1,1);
top=aaa(1,2);
for ii=1:length(recessiondates)
hold on
ha = area([recessiondates(ii,1) recessiondates(ii,2)], [bottom top-bottom; bottom top-bottom],'FaceColor',[0.9 0.9 0.9],'EdgeColor','black');
set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha, 'LineStyle', '-')
end

h=plot(timeline_quarterly(~isnan(log_SR)),A,'b-',timeline_quarterly(~isnan(log_SR)),A_util,'k--','LineWidth',2);
set(gca,'Layer','top')
axis tight
% hline(0)
title('Linearly detrended Solow residual')
set(gca,'YTick',-0.14:0.04:0.10)
legend(h,{'Standard';'Utilization adjusted'},'Location','South')



subplot(2,1,2)
plot(timeline_quarterly(~isnan(log_SR)),tfp_obs_cycle,'LineWidth',2);
aaa=ylim;
bottom=aaa(1,1);
top=aaa(1,2);
for ii=1:length(recessiondates)
hold on
ha = area([recessiondates(ii,1) recessiondates(ii,2)], [bottom top-bottom; bottom top-bottom],'FaceColor',[0.9 0.9 0.9],'EdgeColor','black');
set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha, 'LineStyle', '-')
end

h=plot(timeline_quarterly(~isnan(log_SR)),tfp_obs_cycle,'b-',timeline_quarterly(~isnan(log_SR)),tfp_obs_cycle_util,'k--','LineWidth',2);
set(gca,'Layer','top')
axis tight
% hline(0)
title('HP-filtered Solow residual')
legend(h,{'Standard';'Utilization adjusted'},'Location','South')

