Tutorial 1 code - Department of Economics Sciences Po

advertisement
% In the following code, you will learn to isolate the trend and cyclical
% components of an economic time-series. We will discuss
% four empirical methods to do so. They are: 1) Linear de-trending, 2) Firstdifferencing, 3)
% HP Filter and 4) Moving averages.
% Then you will analyze the comovement of GDP with other important macro
% variables.
% -----------------------------------------------------------------------------------% Part 0: Load data and save
% In general, get rid of the first row and column.
% We should do so because the command csvread('FILENAME') can only contain
numeric values.
% As you will notice when you open the file in Excel, the first row and column
% contains non-numeric values (example: DATE, VALUE, /).
clear all
data=xlsread('t1_data.xls'); % data is in the following order:
y,c,i,g,n,h,w,tfp,population
data=data(:,1:8);
data_gdp=data(:,1);
data_cons=data(:,2);
data_inv=data(:,3);
data_gov=data(:,4);
data_emp=data(:,5);
data_hrs=data(:,6);
data_wage=data(:,7);
data_tfp=data(:,8);
time=1948.25:0.25:2013.75; % Creates a row vector of dates corresponding to the
data
time=time.'; % .' transposes the time vector in to a column vector
T=size(time,1);
% Part 1: Decomposing Trend and Cyclical Component
figure % Creates a new figure window.
plot(time, data_gdp);
title('Time series of the Real US GDP per capita between 1948 and 2013');
xlabel('Time (years)');
ylabel ('US Real GDP (in thousands of chained 2009 dollars)');
% Transforming the US GDP in logs. Remember: log transformation when used in
% conjuction with first differencing converts absolute
% differences into relative (i.e. percentage) differences.
gdp=log(data_gdp); % The command: log() applies log transformation
figure
plot(time, gdp);
title('Time series of the US GDP (in logs)');
xlabel('Time (years)');
ylabel ('US GDP (in log-scale)');
% Method 1: Isolating the cyclical component by linear filtering (Linear
% de-trending)
detrendedgdp=detrend(gdp); % Matlab has a very simple command to detrend a timeseries.
% Alternatively, you can also do OLS.
% Y = detrend(X) removes the best straight-line fit linear trend from the
% data in vector X and returns the residual (the cyclical component) in vector
Y. Hence, the vector detrendedgdppc contains the residuals.
figure
plot (time, detrendedgdp);
hold on % holds the current plot and all axis properties so that subsequent
graphing commands add to the existing graph.
plot(time,zeros(T,1),':k') % Allows you to draw a horizontal dotted line at 0
hold off
title('Percentage Deviation from Trend of the US Real GDP (Method: Linear
Detrending)');
xlabel('Time (years)');
ylabel ('Percentage Deviation');
% Recovering trend: We can recover the trend using the following decomposition:
% x(t) = log(X(t))- log (X*(t)), where x(t) is the residual/percentage deviation
% from the trend X*. Hence, log (X*(t))= log(X(t))- x(t). Hence,
trendld=gdp-detrendedgdp;
figure
plot (time, gdp);
hold on
plot (time, trendld, ':r')
hold off
title('Time Series of the US Real GDP with a linear trend');
xlabel('Time (years)');
ylabel ('Real GDP of the US (in log scale)');
% Method 2: First differencing
diffgdp=zeros(T-1,1);
for t=1:T-1,
diffgdp(t)=gdp(t+1)-gdp(t);
end
% Alternatively, you can do the following: diffgdp=gdp(2:end)-gdp(1:end-1);
figure
plot(time(1:T-1), diffgdp);
hold on
plot(time(1:T-1),zeros(T-1,1),':k')
hold off
title('Percentage Deviation from Trend of the US Real GDP (Method 2: First
Differencing)');
xlabel('Time (years)');
ylabel ('Percentage Deviation');
axis([1946 2014 -0.03 0.05]);
% Method 3: HP Filter
hpgdp=hpfilter(gdp,1600); % Returns the trend component of the time-series.
hpgdpcycle=gdp-hpgdp; % Allows you to collect the residuals, using the same
decomposition as described in method 1.
figure % This will plot a graph of the GDP time series with the HP trend
plot(time,gdp)
hold on;
plot(time,hpgdp,'k')
hold off
title('US Real GDP with HP Trend');
xlabel('Time (years)');
ylabel ('US Real GDP (in log scale)');
figure
plot(time, hpgdpcycle);
hold on
plot(time,zeros(T,1),':k')
hold off
title('Percentage Deviation from Trend of the US Real GDP (Method 3: HP
Filter)');
xlabel('Time (years)');
ylabel ('Percentage Deviation');
% Method 4: Moving averages of order 7
magdp=zeros(T-6,1);
for t=1:T-6,
magdp(t)=sum(gdp(t:t+6),1)/7;
end
magdpcycle=gdp(7:T)-magdp;
figure
plot(time(7:T), magdpcycle);
hold on
plot(time(7:T),zeros(T-6,1),':k')
hold off
title('Percentage Deviation from Trend of the US Real GDP (Method 4: Moving
Averages of order 7)');
xlabel('Time (years)');
ylabel ('Percentage Deviation');
% We create a figure with all the cyclical fluctuations created with the
% different methods
figure
plot (time(7:T-1), detrendedgdp(7:T-1),'k');
hold on
plot(time(7:T-1), diffgdp(7:end),'r');
hold on
plot(time(7:T-1), hpgdpcycle(7:T-1),'c');
hold on
plot(time(7:T-1), magdpcycle(1:end-1));
hold on
plot(time(7:T-1),zeros(T-7,1),':k')
hold off
legend('linear', 'first diff', 'hp filter', 'moving av');
% Part 2: Establishing Business Cycle Facts
%
%
%
%
%
%
%
While real GDP fluctuates in irregular patterns, macroeconomic variables
fluctuate together in patterns that exhibit strong regularities. Such
patterns in fluctuations are called comovement. Now, we will look at how
different macroeconomic variables (example: consumption, investment,
government spending, employment, real wages etc) move over time and more
importantly what regularities/patterns do they exhibit in terms of
comovement with real GDP.
% For this section, all the economic time-series will be detrended using an
% HP filter, as shown above.
% Variable 1: Real Personal Consumption Expenditure: Non durable goods
% Applying log transformation
cons=log(data_cons);
% Filtering the consumption time-series by applying the HP Filter
hpcons=hpfilter(cons,1600);
hpconscycle=cons-hpcons;
% Plotting cyclical component of Real GDP with Consumption
figure
plot(time, hpconscycle, 'r');
hold on
plot(time, hpgdpcycle, '-b');
hold on
plot(time,zeros(T,1),':k')
hold off
title('Percentage deviation from Trend in Real Consumption (red line) and Real
GDP (blue line)');
xlabel('Year)');
ylabel ('Percentage Deviation from Trend');
% Variable 2: Real Domestic Investment
inv=log(data_inv);
hpinv=hpfilter(inv,1600);
hpinvcycle=inv-hpinv;
figure
plot(time, hpinvcycle, 'r');
hold on
plot(time, hpgdpcycle, '-b');
hold on
plot(time,zeros(T,1),':k')
hold off
title('Percentage deviation from Trend in Real Domestic Investment per capita
(red line) and Real GDP (blue line)');
xlabel('Year)');
ylabel ('Percentage Deviation from Trend');
% Variable 3: Government Spending
gov=log(data_gov);
hpgov=hpfilter(gov,1600);
hpgovcycle=gov-hpgov;
figure
plot(time, hpgovcycle, 'r');
hold on
plot(time, hpgdpcycle, '-b');
hold on
plot(time,zeros(T,1),':k')
hold off
title('Percentage deviation from Trend in Real Government Expenditure per capita
(red line) and Real GDP (blue line)');
xlabel('Year)');
ylabel ('Percentage Deviation from Trend');
% Variable 4: Employment
emp=log(data_emp);
hpemp=hpfilter(emp,1600);
hpempcycle=emp-hpemp;
figure
plot(time, hpempcycle, 'r');
hold on
plot(time, hpgdpcycle, '-b');
hold on
plot(time,zeros(T,1),':k')
hold off
title('Percentage deviation from Trend in Employment (red line) and Real GDP
(blue line)');
xlabel('Year)');
ylabel ('Percentage Deviation from Trend');
% Variable 5: Total Hours
hour=log(data_hrs);
hphour=hpfilter(hour,1600);
hphourcycle=hour-hphour;
figure
plot(time, hphourcycle, 'r');
hold on
plot(time, hpgdpcycle, '-b');
hold on
plot(time,zeros(T,1),':k')
hold off
title('Percentage deviation from Trend in Total hours worked per capita in the
nor farm business sector (red line) and Real GDP (blue line)');
xlabel('Year)');
ylabel ('Percentage Deviation from Trend');
% Variable 6: Real Wages
rw=log(data_wage);
hprw=hpfilter(rw,1600);
hprwcycle=rw-hprw;
figure
plot(time, hprwcycle, 'r');
hold on
plot(time, hpgdpcycle, '-b');
hold on
plot(time,zeros(T,1),':k')
hold off
title('Percentage deviation from Trend in Real Wages per hour (red line) and
Real GDP (blue line)');
xlabel('Year)');
ylabel ('Percentage Deviation from Trend');
% Variable 7: Productivity per capita
tfp=log(data_tfp);
hptfp=hpfilter(tfp,1600);
hptfpcycle=tfp-hptfp;
figure
plot(time, hptfpcycle, 'r');
hold on
plot(time, hpgdpcycle, '-b');
hold on
plot(time,zeros(T,1),':k')
hold off
title('Percentage deviation from Trend in Productivity per capita (red line) and
Real GDP (blue line)');
xlabel('Year)');
ylabel ('Percentage Deviation from Trend');
%Calculating Standard Deviations
sd_gdp = std(hpgdpcycle);
sd_cons = std(hpconscycle);
sd_inv = std(hpinvcycle);
sd_gov = std(hpgovcycle);
sd_emp = std(hpempcycle);
sd_hrs = std(hphourcycle);
sd_rw = std(hprwcycle);
sd_tfp = std(hptfpcycle);
%Relative SD
rsd_gdp = sd_gdp/sd_gdp;
rsd_cons = sd_cons/sd_gdp;
rsd_inv = sd_inv/sd_gdp;
rsd_gov = sd_gov/sd_gdp;
rsd_emp = sd_emp/sd_gdp;
rsd_hrs = sd_hrs/sd_gdp;
rsd_rw = sd_rw/sd_gdp;
rsd_tfp = sd_tfp/sd_gdp;
%Calculate correlation with GDP
corr_gdp = corrcoef(hpgdpcycle,hpgdpcycle);
corr_gdp=corr_gdp(1,2);
corr_cons = corrcoef(hpgdpcycle,hpconscycle);
corr_cons=corr_cons(1,2);
corr_inv = corrcoef(hpgdpcycle,hpinvcycle);
corr_inv=corr_inv(1,2);
corr_gov = corrcoef(hpgdpcycle,hpgovcycle);
corr_gov=corr_gov(1,2);
corr_emp = corrcoef(hpgdpcycle,hpempcycle);
corr_emp=corr_emp(1,2);
corr_hrs = corrcoef(hpgdpcycle,hphourcycle);
corr_hrs=corr_hrs(1,2);
corr_rw = corrcoef(hpgdpcycle,hprwcycle);
corr_rw=corr_rw(1,2);
corr_tfp = corrcoef(hpgdpcycle,hptfpcycle);
corr_tfp=corr_tfp(1,2);
%calculate autocorrelation
acf_gdp = corrcoef(hpgdpcycle(1:T-1), hpgdpcycle(2:T));
acf_gdp = acf_gdp(1,2);
Download