%% OLS exchange rate pass-through regressions (for Figures 1 and 3)

clear all
clc
addpath([pwd '/functions/'])


%% Load and set up data

[num,txt,raw] = xlsread('data_JIE.xlsx','data_base');
dates = datenum(txt(3:end,1),'dd/mm/yyyy');
first = find(dates == datenum('31/03/1991','dd/mm/yyyy'));
last = size(num,1)-find(dates == datenum('31/03/2015','dd/mm/yyyy'));

DateNumber = datenum(txt(2+first:end-last,1),'dd/mm/yyyy');
data = num(first:end-last,:);

p = 8; % lag order

Ypm = data(p+1:end,5);
Ycpi = data(p+1:end,2);
[n,k] = size(Ypm);
X = [ones(n,1), data(p+1:end,4), xlags(data(:,4),p), data(p+1:end,6), xlags(data(:,6),p)];

%% Import price regressions
[hacvar,se,beta] = hac(X(:,2:end),Ypm,'bandwidth',4);

se_beta_eri = [];
erpt_pm = [];
for i = 0:p
    R = zeros(1,size(X,2)); 
    R(2:2+i) = 1;
    var_beta_eri = R*hacvar*R';
    se_beta_eri = [se_beta_eri; sqrt(var_beta_eri)];
    erpt_pm = [erpt_pm; sum(beta(2:2+i),1)];
end
erpt_pm_lower95 = erpt_pm - 2*se_beta_eri;
erpt_pm_upper95 = erpt_pm + 2*se_beta_eri;

erpt_pm = [erpt_pm; repmat(erpt_pm(end,:),11,1)]; % extend to 20q horizon
erpt_pm_lower95 = [erpt_pm_lower95; repmat(erpt_pm_lower95(end,:),11,1)];
erpt_pm_upper95 = [erpt_pm_upper95; repmat(erpt_pm_upper95(end,:),11,1)];

%% CPI regressions
[hacvar,se,beta] = hac(X(:,2:end),Ycpi,'bandwidth',4);

se_beta_eri = [];
erpt_cpi =[];
for i = 0:p
    R = zeros(1,size(X,2)); 
    R(2:2+i) = 1;
    var_beta_eri = R*hacvar*R';
    se_beta_eri = [se_beta_eri; sqrt(var_beta_eri)];
    erpt_cpi = [erpt_cpi; sum(beta(2:2+i),1)];
end
erpt_cpi_lower95 = erpt_cpi - 2*se_beta_eri;
erpt_cpi_upper95 = erpt_cpi + 2*se_beta_eri;

erpt_cpi = [erpt_cpi; repmat(erpt_cpi(end,:),11,1)]; % extend to 20q horizon
erpt_cpi_lower95 = [erpt_cpi_lower95; repmat(erpt_cpi_lower95(end,:),11,1)];
erpt_cpi_upper95 = [erpt_cpi_upper95; repmat(erpt_cpi_upper95(end,:),11,1)];

%% Save results
xlswrite('output\OLS_erpt.xlsx', [erpt_pm, erpt_pm_lower95, erpt_pm_upper95], 'pm');
xlswrite('output\OLS_erpt.xlsx', [erpt_cpi, erpt_cpi_lower95, erpt_cpi_upper95], 'cpi');

