You are on page 1of 8

Chapter 2 - Solution

Giriraj Ranawat

9/20/2019

Question 1
Consider the monthly US unemployment rate from January 1948 to November 2011 in the
file m-unrate-4811.txt. The data are seasonally adjusted and obtained from the Federal
Reserve Bank at St Louis.
(a) Does the monthly unemployment rate have a unit root? Why?

(b) Build a time series model for the monthly unemployment rates. Check the fitted model
for adequacy. Then, use the model to forecast the unemployment rate for the
December 2011 and the first three months of 2012. (Note that there are more than one
model that fits the data well. You only need an adequate model.)

(c) Does the fitted model imply the existence of business cycles? Why?

Solution
part (a)
library(fUnitRoots)
library(tseries)
da<-read.table("C:/Users/GIRIRAJ SINGH RANA/Desktop/t - 4/fincial analys/data
used in chapter-2/m-unrate-4811.txt",header=T)
head(da)
summary(da)
rate=da$rate
boxplot.stats(rate,coef = 1.5,do.conf = TRUE,do.out = TRUE)
jarque.bera.test(rate)

trate=ts(rate,frequency = 12,start = c(1948,1))


plot(trate)
lrate=log(rate)
tlrate=ts(lrate,frequency = 12,start = c(1948,1))
plot(tlrate)
drate=diff(lrate)
tdrate=ts(drate,frequency = 12,start = c(1948,1))
plot(tdrate)
acf(lrate,lag=100)
pacf(drate)
a=pacf(drate)
a
m3=ar(diff(lrate),method = 'mle') # based on AIC
m3$order
plot(m3$aic,type='l')

adfTest(lrate,lags=12,type=("ct")) # p-value is more than .05 sonull accept.


adfTest(lrate,lags=2,type=("ct")) # based on PACF p-value is more than .05
so null accept.

part (b)
m1=arima(drate,order = c(12,0,0))
m1
# (1-0.1056B-0.2374B^2)(Xt-0.0013)=At, sigma^2(A)=0.001415
#estimateed std error 0.0352,0.0353 and 0.0021
# third order polynomial equation 1-0.1056Z-0.2374Z^2=0
#model checking
Box.test(m1$residuals,lag = 12,type = 'Ljung')

part(c)
tsdiag(m1,gof=12)
p1=c(1,-m1$coef[1:3])
r1=polyroot(p1)
r1
Mod(r1)
k=(2*pi/acos(1.573350/ 1.573350))
k

————————————————————————————————
—————————————————————————-
Question 2
Consider the monthly simple returns of CRSP Decile 1, 2, 5, 9, and 10 portfolios based on
the market capitalization of NYSE/AMEX/NASDAQ. The data span is from January 1961 to
September 2011.
(a) For the return series of Decile 2 and Decile 10, test the null hypothesis that the first 12
lags of autocorrelations are 0 at the 5% level. Draw your conclusion.

(b) Build an ARMA model for the return series of Decile 2. Perform model checking and
write down the fitted model.

(c) Use the fitted ARMA model to produce 1- to 12-step ahead forecasts of the series and
the associated standard errors of forecasts. ## Solution

part(a)
library(fUnitRoots)
library(tseries)
da<-read.table("C:/Users/GIRIRAJ SINGH RANA/Desktop/t - 4/fincial analys/data
used in chapter-2/m-dec125910-6111.txt",header=T)
head(da)
summary(da)
d2=da$dec2
d10=da$dec10
td2=ts(d2,frequency = 12,start = c(1961,1),end = c(2011,9))
plot(td2)

td10=ts(d10,frequency = 12,start = c(1961,1),end = c(2011,9))


plot(td10)
f2=acf(d2)
f10=acf(d10)
f2$acf
tt2=f2$acf[13]*sqrt(609)
tt2
f10$acf
tt10=f10$acf[13]*sqrt(609)
tt10

part (b)
ld2=log(da$dec2+1)
tld2=ts(ld2,frequency = 12,start = c(1961,1),end = c(2011,9))
plot(tld2)

library(TSA)
m1=eacf(ld2,6,12)
print(m1$eacf,digits=2)#(5,1) p=83 min
s=ar(td2,method = 'mle')
s$order# for finding lag
m2=arima(tld2,order = c(5,0,1))
m2
Box.test(m2$residuals,lag=2,type = 'Ljung')
pp=1-pchisq(0.006924,4)
pp

m3=arima(tld2,order = c(7,0,1))# pacf and acf


m3
Box.test(m3$residuals,lag=2,type = 'Ljung')

pp=1-pchisq(0.0012385,6)
pp

part (c)
predict(m2,12)
predict(m3,12)
————————————————————————————————
—————————————————————————-
Quetion 3
Consider the daily range (daily high–daily low) of Apple stock from January 2, 2007 to
December 23, 2011. One can obtain the data by the package quantmod from Yahoo.
Compute the first 100 lags of ACF of the series. Is there evidence of long-range
dependence? Why? If the range series has long memory, build an AFRIMA model for the
data.

Solution
library(quantmod)
library(tseries)
library(fracdiff)
getSymbols("AAPL",from="2007-01-02",to="2011-12-24")
head(AAPL)
summary(AAPL)
acf(AAPL$AAPL.High,lag=100)
acf(AAPL$AAPL.Low,lag=100)
#PAGE=120
m1=fdGPH(AAPL$AAPL.High)
m1
library(TSA)
m2=eacf(AAPL$AAPL.High,6,12)
print(m2$eacf,digits=2)#(1,7) p=83 min
m3=fracdiff(AAPL$AAPL.High,nar = 1,nma = 7)
summary(m3)

————————————————————————————————
—————————————————————————-
Question 4
Consider the monthly yields of Moody’s Aaa & Baa seasoned bonds from January 1919 to
November, 2011. The data are obtained from FRED of Federal Reserve Bank of St. Louis.
Consider the log series of monthly Aaa bond yields. Build a time series model for the series,
including model checking.

Solution
library(fUnitRoots)
library(tseries)
da1<-read.table("C:/Users/GIRIRAJ SINGH RANA/Desktop/t - 4/fincial
analys/data used in chapter-2/m-aaa-1911.txt",header=T)
head(da1)
summary(da1)
x=ts(log(da1$yield),start = c(1911,1),frequency = 12,end = c(2011,11))
plot(x)
d=diff(x)
plot(d)
jarque.bera.test(d)
adf.test(x)
acf(x,lag=100)
pacf(d)#ar(1,2,8)
m1=ar(d,method = 'mle')
m1$order
m2=arima(d,order = c(2,0,0))
m2
Box.test(m2$residuals,lag = 2,type = 'Ljung')

————————————————————————————————
—————————————————————————-
Question 5
Consider again the monthly log series of Moody’s Aaa bound yield. Use the exponential
smoothing method to produce 1- to 12-step ahead out-of-sample forecasts at the forecast
origin November 2010.

Solution
library(fUnitRoots)
library(tseries)
da1<-read.table("C:/Users/GIRIRAJ SINGH RANA/Desktop/t - 4/fincial
analys/data used in chapter-2/m-aaa-1911.txt",header=T)
head(da1)
da=da1[1103:1115,4]
da=na.omit(da)
summary(da)
x=ts(da,start = c(2010,11),frequency = 12,end = c(2011,11))
l=log(x)
plot(l,type='l')
d=diff(l,2)
plot(d,type='l')
dd=diff(d)
plot(dd,type='l')
acf(l,lag=20)
pacf(dd,lag=15)#MR(1)
m1=ar(dd,method = 'mle')
m1$order
m2=arima(l,order = c(0,2,1))
m2
Box.test(m2$residuals,lag = 12,type = 'Ljung')
pp=1-pchisq(4.0545,6)
pp
predict(m2,12)
————————————————————————————————
—————————————————————————-
Question 6
Consider the two bond yield series of the previous exercise. What is the relationship
between the two series? To answer this question, take the log transformation of the data to
build a time series model for the Aaa yields using Baa yields as an explanatory variable.
Write down the fitted model, including model checking.

Solution
library(fUnitRoots)
library(tseries)
library(forecast)
da<-read.table("C:/Users/GIRIRAJ SINGH RANA/Desktop/t - 4/fincial analys/data
used in chapter-2/m-aaa-1911.txt",header=T)
da1<-read.table("C:/Users/GIRIRAJ SINGH RANA/Desktop/t - 4/fincial
analys/data used in chapter-2/m-baa-1911.txt",header=T)
head(da)
summary(da)
head(da1)
summary(da1)
aa=ts(log(da[,4]),frequency = 12,start = c(1919,1),end = c(2011,11))
bb=ts(log(da1[,4]),frequency = 12,start = c(1919,1),end = c(2011,11))
m1=tslm(bb~aa)
summary(m1)
plot(m1$residuals,type='l')
adf.test(m1$residuals)
acf(m1$residuals,lag=12)
pacf(diff(m1$residuals),lag=12)#AR 1 , 2,7,8,9
a=ar(m1$residuals,method = 'mle')
a$order
m2=arima(m1$residuals,order = c(9,1,0))
m2
Box.test(m2$residuals,lag = 12,type='Ljung')

————————————————————————————————
—————————————————————————-
Question 7
Consider the quarterly earnings per share of the Johnson & Johnson from the first quarter
of 1992 to the second quarter of 2011. The data are in the file q-jnj-earns-9211.txt and are
obtained from the First Call Historical Database of Thomson Reuters. Take log
transformation of the data if necessary. Build a time series model for the data. Perform
model checking to assess the adequacy of the fitted model. Write down the model. Refit the
model using data from 1992 to 2008. Perform 1-step to 10-step ahead forecasts of the
quarterly earnings and obtain a forecast plot.

Solution
library(fUnitRoots)
library(tseries)
library(forecast)
da<-read.table("C:/Users/GIRIRAJ SINGH RANA/Desktop/t - 4/fincial analys/data
used in chapter-2/q-jnj-earns-9211.txt",header=T)
head(da)
summary(da)
earn=log(da$earns)
tearn=ts(earn,frequency = 4,start = c(1992,1))
plot(tearn,type='l')
dearn=diff(earn)
tdearn=ts(dearn,frequency = 4,start = c(1992,1))
plot(tdearn,type='l')
acf(dearn)
dearn=diff(earn,4)
acf(dearn)
ddearn=diff(dearn)
acf(ddearn)
auto.arima(tearn,seasonal = T)
m1=arima(tearn,order=c(0,1,1),seasonal=list(order=c(0,1,1),period=4))
m1
tsdiag(m1,gof=20)
Box.test(m1$residuals,lag = 12,type = 'Ljung')
y=tearn[1:68]
m1=arima(y,order = c(0,1,1),seasonal = list(order=c(0,1,1),period=4))
m1
pm1=predict(m1,12)
pm1

pt=ts(pm1$pred,frequency = 4,start = c(2009,1),end = c(2011,2))


plot(pt,type='l')

————————————————————————————————
—————————————————————————-
Question 8
Consider the US quarterly real gross national product from the first quarter of 1947 to the
third quarter of 2011. The data are in the file q-GNPC96.txt, seasonally adjusted, and in
billions of chained 2005 dollars. Let xt be the growth rate series of the real GDP.
(a) The ar command identifies an AR(4) model for xt via the AIC criterion. Fit the model. Is
the model adequate? Why?
(b) The sample PACF of xt specifies an AR(3) model. Fit the model. Is it adequate? Why?

(c) What is the model for xt if one uses in-sample model comparison? Why?

(d) Divide the data into estimation and forecasting subsamples using the fourth quarter of
2000 as the initial forecast origin and apply the backtesting procedure with MSFE as
the criterion. Select a model for xt . Justify the choice. ## Solution
library(fUnitRoots)
library(tseries)
library(forecast)
da<-read.table("C:/Users/GIRIRAJ SINGH RANA/Desktop/t - 4/fincial analys/data
used in chapter-2/q-GNPC96.txt",header=T)
head(da)
summary(da)
########A
gnp=log(da$gnp)
tgnp=ts(gnp,frequency = 4,start = c(1947,1),end = c(2011,3))
plot(tgnp,type='l')
dgnp=diff(gnp)
tdgnp=ts(dgnp,frequency = 4,start = c(1947,1),end = c(2011,3))
plot(tdgnp,type='l')
m=ar(diff(gnp),method = 'mle')
m$aic #4
m1=arima(dgnp,order = c(4,0,0))
m1

###########B

acf(dgnp)
pacf(dgnp)# 3
m2=arima(dgnp,order=c(3,0,0))
m2
length(da$gnp)

##############C

source("C:/Users/GIRIRAJ SINGH RANA/Desktop/t - 4/fincial analys/data used in


chapter-2/backtest.txt")
mm1=backtest(m1,dgnp,254,1)# 259-4
mm2=backtest(m2,dgnp,254,1)# 259-4

================================completed=================
==========================================================

You might also like