You are on page 1of 21

Practice 3 from Analysis of Financial Time Series

YIK LUN, KEI


allen29@ucla.edu
This paper is a practice from the book called Analysis of Financial Time Series
by Ruey S. Tsay. All R codes and comments below are belonged to the book
and author.
da=read.table("m-deciles08.txt",header=T)
d1=da[,2]
jan=rep(c(1,rep(0,11)),39) # Create January dummy.
m1=lm(d1~jan)
summary(m1)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Call:
lm(formula = d1 ~ jan)
Residuals:
Min
1Q
Median
-0.30861 -0.03475 -0.00176

3Q
0.03254

Max
0.40671

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.002864
0.003333
0.859
0.391
jan
0.125251
0.011546 10.848
<2e-16 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.06904 on 466 degrees of freedom
Multiple R-squared: 0.2016, Adjusted R-squared: 0.1999
F-statistic: 117.7 on 1 and 466 DF, p-value: < 2.2e-16

m2=arima(d1,order=c(1,0,0),seasonal=list(order=c(1,0,1),period=12))
m2
##
##
##
##
##
##
##
##
##
##

Call:
arima(x = d1, order = c(1, 0, 0), seasonal = list(order = c(1, 0, 1), period = 12))
Coefficients:
ar1
sar1
0.1769 0.9882
s.e. 0.0456 0.0093

sma1
-0.9144
0.0335

sigma^2 estimated as 0.004717:

intercept
0.0118
0.0129
log likelihood = 584.07,

aic = -1158.14

tsdiag(m2,gof=36)

4 4

Standardized Residuals

100

200

300

400

Time

0.0

ACF

ACF of Residuals

10

15

20

25

Lag

0.0 1.0

p value

p values for LjungBox statistic

10

15

20

25

30

35

lag

m2=arima(d1,order=c(1,0,0),seasonal=list(order=c(1,0,1),period=12),include.mean=F)
m2
##
##
##
##
##
##
##
##
##
##
##

Call:
arima(x = d1, order = c(1, 0, 0), seasonal = list(order = c(1, 0, 1), period = 12),
include.mean = F)
Coefficients:
ar1
sar1
0.1787 0.9886
s.e. 0.0456 0.0089

sma1
-0.9127
0.0335

sigma^2 estimated as 0.00472:

log likelihood = 583.68,

aic = -1159.36

r1=read.table("http://faculty.chicagobooth.edu/ruey.tsay/teaching/fts3/w-gs1yr.txt",header=T)[,4]
r3=read.table("http://faculty.chicagobooth.edu/ruey.tsay/teaching/fts3/w-gs3yr.txt",header=T)[,4]
plot(r1,r3)

15
10
5

r3

10
r1

m1=lm(r3~r1)
summary(m1)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Call:
lm(formula = r3 ~ r1)
Residuals:
Min
1Q
Median
-1.82319 -0.37691 -0.01462

3Q
0.38661

Max
1.35679

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.83214
0.02417
34.43
<2e-16 ***
r1
0.92955
0.00357 260.40
<2e-16 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.5228 on 2465 degrees of freedom
Multiple R-squared: 0.9649, Adjusted R-squared: 0.9649
F-statistic: 6.781e+04 on 1 and 2465 DF, p-value: < 2.2e-16

plot(m1$residuals,type='l')

15

0.5 1.0
0.5
1.5

m1$residuals

500

1000

1500
Index

acf(m1$residuals,lag=36)

2000

2500

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series m1$residuals

10

15

20

25

Lag

c1=diff(r1)
c3=diff(r3)
m2=lm(c3~-1+c1)
summary(m2)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Call:
lm(formula = c3 ~ -1 + c1)
Residuals:
Min
1Q
Median
-0.42469 -0.03589 -0.00127

3Q
0.03456

Max
0.48911

Coefficients:
Estimate Std. Error t value Pr(>|t|)
c1 0.791935
0.007337
107.9
<2e-16 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.06896 on 2465 degrees of freedom
Multiple R-squared: 0.8253, Adjusted R-squared: 0.8253
F-statistic: 1.165e+04 on 1 and 2465 DF, p-value: < 2.2e-16

acf(m2$residuals,lag=36)

30

35

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series m2$residuals

10

15

20

25

30

Lag

m3=arima(c3,order=c(0,0,1),xreg=c1,include.mean=F)
m3
##
##
##
##
##
##
##
##
##
##

Call:
arima(x = c3, order = c(0, 0, 1), xreg = c1, include.mean = F)
Coefficients:
ma1
c1
0.1823 0.7936
s.e. 0.0196 0.0075
sigma^2 estimated as 0.0046:

log likelihood = 3136.62,

rsq=(sum(c3^2)-sum(m3$residuals^2))/sum(c3^2)
rsq
## [1] 0.8310077
da=read.table("w-gs1n36299.txt",header=TRUE)
r1=da[,1]
r3=da[,2]
plot(r1,type='l')
lines(1:1967,r3,lty=2)

aic = -6267.23

35

10 12 14 16
4

r1

500

1000
Index

plot(r1,r3)

1500

2000

16
14
12
10
4

r3

10

12

14

r1

m1=lm(r3~r1)
summary(m1)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Call:
lm(formula = r3 ~ r1)
Residuals:
Min
1Q
-1.8121 -0.4023

Median
0.0031

3Q
0.4026

Max
1.3388

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.910687
0.032250
28.24
<2e-16 ***
r1
0.923854
0.004389 210.51
<2e-16 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.538 on 1965 degrees of freedom
Multiple R-squared: 0.9575, Adjusted R-squared: 0.9575
F-statistic: 4.431e+04 on 1 and 1965 DF, p-value: < 2.2e-16

acf(m1$residuals) # strong correlation

16

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series m1$residuals

10

15
Lag

plot(m1$residuals,type='l')

20

25

30

0.5 1.0
0.5
1.5

m1$residuals

500

1000
Index

c3=diff(r3)
c1=diff(r1)
plot(c1,c3)

10

1500

2000

1.5
1.0
0.5
0.0
1.0

c3

1.5

1.0

0.5

0.0

0.5

c1

m2=lm(c3~c1) # Fit a regression with likelihood method.


summary(m2)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

Call:
lm(formula = c3 ~ c1)
Residuals:
Min
1Q
Median
-0.38060 -0.03338 -0.00054

3Q
0.03437

Max
0.47418

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.0002475 0.0015380
0.161
0.872
c1
0.7810590 0.0074651 104.628
<2e-16 ***
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.06819 on 1964 degrees of freedom
Multiple R-squared: 0.8479, Adjusted R-squared: 0.8478
F-statistic: 1.095e+04 on 1 and 1964 DF, p-value: < 2.2e-16

acf(m2$residuals)

11

1.0

1.5

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series m2$residuals

10

15
Lag

plot(m2$residuals,type='l')

12

20

25

30

0.4
0.2
0.0
0.4

0.2

m2$residuals

500

1000

1500

Index

m3=arima(c3,xreg=c1,order=c(0,0,1)) # Residuals follow an MA(1) model


m3
##
##
##
##
##
##
##
##
##
##

Call:
arima(x = c3, order = c(0, 0, 1), xreg = c1)
Coefficients:
ma1 intercept
0.2115
0.0002
s.e. 0.0224
0.0018

c1
0.7824
0.0077

sigma^2 estimated as 0.004456:

log likelihood = 2531.84,

acf(m3$residuals)

13

aic = -5055.69

2000

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series m3$residuals

10

15
Lag

tsdiag(m3)

14

20

25

30

Standardized Residuals

500

1000

1500

2000

Time

0.0

ACF

ACF of Residuals

10

15

20

25

30

Lag

0.0 1.0

p value

p values for LjungBox statistic

lag

m4=arima(c3,xreg=c1,order=c(1,0,0)) # Residuals follow an AR(1) model.


m4
##
##
##
##
##
##
##
##
##
##

Call:
arima(x = c3, order = c(1, 0, 0), xreg = c1)
Coefficients:
ar1 intercept
0.1922
0.0003
s.e. 0.0221
0.0019

c1
0.7829
0.0077

sigma^2 estimated as 0.004474:

log likelihood = 2527.86,

acf(m4$residuals)

15

aic = -5047.72

10

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series m4$residuals

10

15
Lag

tsdiag(m4)

16

20

25

30

Standardized Residuals

500

1000

1500

2000

Time

0.0

ACF

ACF of Residuals

10

15

20

25

30

Lag

0.0 1.0

p value

p values for LjungBox statistic

6
lag

suppressPackageStartupMessages(require(quantmod))
options("getSymbols.warning4.0"=FALSE)
getSymbols("^GSPC",from="2007-01-03",to="2015-04-13")
## [1] "GSPC"
chartSeries(GSPC,theme="white")

17

10

GSPC

[20070103/20150413]

Last 2092.429932

2000

1500

1000

10000
8000
6000
4000
2000

Volume (millions):
2,908,420,000

Jan 03
2007

Jul 01 Jul 01 Jul 01 Jul 01 Jul 02


2008 2009 2010 2011 2012

spc=log(as.numeric(GSPC[,6]))
rtn=diff(spc)
acf(rtn)

18

Jan 02
2014

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series rtn

10

15

20

25

30

Lag

m1=arima(rtn,order=c(0,0,2),include.mean=F)
m1
##
##
##
##
##
##
##
##
##
##

Call:
arima(x = rtn, order = c(0, 0, 2), include.mean = F)
Coefficients:
ma1
ma2
-0.119 -0.0502
s.e.
0.022
0.0228
sigma^2 estimated as 0.0001899:

log likelihood = 5966.01,

resi=m1$residuals
acf(resi)

19

aic = -11926.01

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series resi

10

15
Lag

acf(resi^2)

20

20

25

30

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series resi^2

10

15

20

25

30

Lag

Reference:
Tsay, Ruey S. Analysis of financial time series. Vol. 543. John
Wiley & Sons, 2005.

21

You might also like