% TABLE 2
%
% This file accompanies `Can Sticky Price Models Generate Volatile 
% and Persistent Real Exchange Rates?'
%

format compact

%
% Read in data.
% (See README file for details on data sources.)
%

load tradeus/nexpus.dat
load tradeus/nimpus.dat
load nexchr/ner.dat
load pgood/cpiaglf.dat
load pgood/cpifood.dat
load pgood/cpirent.dat
load pgood/cpislr.dat

%
% Sample and ordering of countries:
%

sample     = ptime(1960.1,161,4);
countries  = ['YEAR.QTR   ';
              '  Austria  ';
              '  Belgium  ';
              '  Canada   ';
              '  Denmark  ';
              '  Finland  ';
              '  France   ';
              '  Germany  ';
              '   Italy   ';
              '   Japan   ';
              '  Mexico   ';
              'Netherlands';
              '  Norway   ';
              '   Spain   ';
              'Switzerland';
              '    UK     ';
              '    US     ';
              ' W.Germany '];


t          = 53:156;           % 1973:1-1998:4
T          = length(t);
i          = [7,9,12,13];      % France,Italy,Netherlands,Norway
j          = [i,17];           % add US
I          = length(i);
ot         = ones(T,1);
oi         = ones(I,1);
oi1        = ones(I+1,1);
oti        = ones(T,I);

%
% Trade weights:
%

twgt       = (nexpus(t,i)+nimpus(t,i))./(sum(nexpus(t,i)'+nimpus(t,i)')' * oi');
cwgt       = mean(twgt)'/sum(mean(twgt)');

%
% Variables we will work with:
%

exch       = oti./ner(t,i);
E          = exch./(ot*exch(1,:));           % unit: $/Franc
Eeur       = E*cwgt;                         % trade-weighted exchange rate

p_aglf     = cpiaglf(t,j);
p_f        = cpifood(t,j);
p_slr      = cpislr(t,j);
p_r        = cpirent(t,j);
p_aglf     = p_aglf./(ot * p_aglf(1,:));
p_f        = p_f   ./(ot * p_f(1,:));
p_slr      = p_slr ./(ot * p_slr(1,:));
p_r        = p_r   ./(ot * p_r(1,:));
qwgt_aglf  = .25584*oi1';
qwgt_f     = .17332*oi1';
qwgt_slr   = .29413*oi1';
qwgt_r     = .27671*oi1';
qtwgt_aglf = .5961 *oi1';
qtwgt_f    = .4039 *oi1';
qnwgt_slr  = .5153 *oi1';
qnwgt_r    = .4847 *oi1';
P          = p_aglf .* (ot* qwgt_aglf)  + p_f .* (ot* qwgt_f)  ...
            +p_slr  .* (ot* qwgt_slr)   + p_r .* (ot* qwgt_r);

PT         = p_aglf .* (ot* qtwgt_aglf) + p_f .* (ot* qtwgt_f);
PN         = p_slr  .* (ot* qnwgt_slr)  + p_r .* (ot* qnwgt_r);
Peur       = P(:,1:I) *cwgt;
PTeur      = PT(:,1:I)*cwgt;
PNeur      = PN(:,1:I)*cwgt;
Pus        = P(:,I+1);
PTus       = PT(:,I+1);
PNus       = PN(:,I+1);

PR         = P(:,1:I) ./(Pus * oi');
PTR        = PT(:,1:I)./(PTus * oi');
PNR        = PN(:,1:I)./(PNus * oi');
PReur      = Peur./Pus;
PTReur     = PTeur./PTus;
PNReur     = PNeur./PNus;

Q          = E    .*P(:,1:I)  ./(Pus *oi');
QT         = E    .*PT(:,1:I) ./(PTus*oi');
QN         = E    .*PN(:,1:I) ./(PNus*oi');
Qeur       = Eeur .*Peur      ./ Pus;
QTeur      = Eeur .*PTeur     ./ PTus;
QNeur      = Eeur .*PNeur     ./ PNus;

%
% Detrending:
%

t          = sample(t);

dPR        = log(PR)         - hptrend(log(PR),1600);
dPTR       = log(PTR)        - hptrend(log(PTR),1600);
dPNR       = log(PNR)        - hptrend(log(PNR),1600);
dQ         = log(Q)          - hptrend(log(Q),1600);
dQT        = log(QT)         - hptrend(log(QT),1600);
dQN        = log(QN)         - hptrend(log(QN),1600);
dE         = log(E)          - hptrend(log(E),1600);
dPReur     = log(PReur)      - hptrend(log(PReur),1600);
dPTReur    = log(PTReur)     - hptrend(log(PTReur),1600);
dPNReur    = log(PNReur)     - hptrend(log(PNReur),1600);
dQeur      = log(Qeur)       - hptrend(log(Qeur),1600);
dQTeur     = log(QTeur)      - hptrend(log(QTeur),1600);
dQNeur     = log(QNeur)      - hptrend(log(QNeur),1600);
dEeur      = log(Eeur)       - hptrend(log(Eeur),1600);

%
% Correlations and Autocorrelations 
%

for k=1:I;
  sPR      = std(dPR(:,k))*100;
  sPTR     = std(dPTR(:,k))*100;
  sPNR     = std(dPNR(:,k))*100;
  sE       = std(dE(:,k))*100;
  sQ       = std(dQ(:,k))*100;
  sQT      = std(dQT(:,k))*100;
  sQN      = std(dQN(:,k))*100;
  aPR      = corrcoef([dPR(1:T-1,k), dPR(2:T,k)]);
  aPTR     = corrcoef([dPTR(1:T-1,k),dPTR(2:T,k)]);
  aPNR     = corrcoef([dPNR(1:T-1,k),dPNR(2:T,k)]);
  aE       = corrcoef([dE(1:T-1,k),  dE(2:T,k)]);
  aQ       = corrcoef([dQ(1:T-1,k),  dQ(2:T,k)]);
  aQT      = corrcoef([dQT(1:T-1,k), dQT(2:T,k)]);
  aQN      = corrcoef([dQN(1:T-1,k), dQN(2:T,k)]);
  cQE      = corrcoef([dQ(:,k),dE(:,k)]);
  cQTE     = corrcoef([dQT(:,k),dE(:,k)]);
  cQNE     = corrcoef([dQN(:,k),dE(:,k)]);
  cQQT     = corrcoef([dQ(:,k),dQT(:,k)]);
  tab2a(:,k)= [sPR;sE;sQ;aPR(1,2);aE(1,2);aQ(1,2);cQE(1,2)];
  tab2b(:,k)= [sPTR;sE;sQT;aPTR(1,2);aE(1,2);aQT(1,2);cQTE(1,2)];
  tab2c(:,k)= [sPNR;sE;sQN;aPNR(1,2);aE(1,2);aQN(1,2);cQNE(1,2)];
  tab2d(1,k)= cQQT(1,2);
end;
sPR        = std(dPReur)*100;
sPTR       = std(dPTReur)*100;
sPNR       = std(dPNReur)*100;
sE         = std(dEeur)*100;
sQ         = std(dQeur)*100;
sQT        = std(dQTeur)*100;
sQN        = std(dQNeur)*100;
aPR        = corrcoef([dPReur(1:T-1), dPReur(2:T)]);
aPTR       = corrcoef([dPTReur(1:T-1),dPTReur(2:T)]);
aPNR       = corrcoef([dPNReur(1:T-1),dPNReur(2:T)]);
aE         = corrcoef([dEeur(1:T-1),  dEeur(2:T)]);
aQ         = corrcoef([dQeur(1:T-1),  dQeur(2:T)]);
aQT        = corrcoef([dQTeur(1:T-1), dQTeur(2:T)]);
aQN        = corrcoef([dQNeur(1:T-1), dQNeur(2:T)]);
cQE        = corrcoef([dQeur, dEeur]);
cQQT       = corrcoef([dQeur,dQTeur]);
tab2a(:,I+1)= [sPR;sE;sQ;aPR(1,2);aE(1,2);aQ(1,2);cQE(1,2)];
tab2b(:,I+1)= [sPTR;sE;sQT;aPTR(1,2);aE(1,2);aQT(1,2);cQTE(1,2)];
tab2c(:,I+1)= [sPNR;sE;sQN;aPNR(1,2);aE(1,2);aQN(1,2);cQNE(1,2)];
tab2d(1,I+1)= cQQT(1,2);

%
% Convert Years:Quarters to string variables and print table
%

yr0        = int2str(floor(t(1)));
yr1        = int2str(floor(t(length(t))));
q0         = int2str((t(1)-floor(t(1)))*1000);
q1         = int2str((t(length(t))-floor(t(length(t))))*1000);
switch q0
  case '125',  qr0='1';
  case '375',  qr0='2';
  case '625',  qr0='3';
  case '875',  qr0='4';
end
switch q1
  case '125',  qr1='1';
  case '375',  qr1='2';
  case '625',  qr1='3';
  case '875',  qr1='4';
end

disp(['TABLE 2a. Properties of Exchange Rates and CPI''s, ', ...
       yr0,':',qr0,'-',yr1,':',qr1])
disppr(tab2a',[countries(i,:);'  Europe   '])
disp(' ')
disp(' ')
disp('TABLE 2b. Traded component')
disppr(tab2b',[countries(i,:);'  Europe   '])
disp(' ')
disp(' ')


