Você está na página 1de 22

Practice 2 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.

Example 1: MA(1)
setwd("~/Desktop/Chicago")
suppressPackageStartupMessages(require(fBasics))
data=read.table("d-ibmvwew6202.txt",header=TRUE)
vw=log(1+data[,3])*100 # Compute percentage log returns of the vw index.
acf(vw,lag.max=10)

0.6
0.4
0.0

0.2

ACF

0.8

1.0

Series vw

6
Lag

m1=arima(vw,order=c(0,0,1)) # fits an MA(1) model


m1 #The model is vw(t) = 0.0396+a(t)+0.1465*a(t-1).
##
1

10

##
##
##
##
##
##
##
##
##

Call:
arima(x = vw, order = c(0, 0, 1))
Coefficients:
ma1 intercept
0.1465
0.0396
s.e. 0.0099
0.0100
sigma^2 estimated as 0.7785:

log likelihood = -13188.48,

aic = 26382.96

tsdiag(m1)

20

Standardized Residuals

2000

4000

6000

8000

10000

Time

0.4
0.0

ACF

0.8

ACF of Residuals

10

20

30

40

Lag

0.8
0.4
0.0

p value

p values for LjungBox statistic

6
lag

predict(m1,5)
## $pred
## Time Series:
2

10

##
##
##
##
##
##
##
##
##
##
##

Start = 10195
End = 10199
Frequency = 1
[1] 0.05036298 0.03960887 0.03960887 0.03960887 0.03960887
$se
Time Series:
Start = 10195
End = 10199
Frequency = 1
[1] 0.8823290 0.8917523 0.8917523 0.8917523 0.8917523

Example 2: Test for Unit Root


suppressPackageStartupMessages(require(fUnitRoots))
da=read.table('q-gdpc96.txt',header=T)
gdp=log(da[,4])
adfTest(gdp,lag=4,type=c("c"))
##
## Title:
## Augmented Dickey-Fuller Test
##
## Test Results:
##
PARAMETER:
##
Lag Order: 4
##
STATISTIC:
##
Dickey-Fuller: -1.8568
##
P VALUE:
##
0.3654
##
## Description:
## Sun Aug 30 22:16:48 2015 by user:
# cannot reject the null hypothesis of a unit root.
x=diff(gdp)
ord=ar(x) # identify an AR model for the differenced series.
ord
##
##
##
##
##
##
##
##
##

Call:
ar(x = x)
Coefficients:
1
2
0.3457
0.1283

3
-0.1207

Order selected 3

sigma^2 estimated as

8.401e-05

# An AR(3) for the differenced data is confirmed.


# Our previous analysis is justified.
www<-"http://faculty.chicagobooth.edu/ruey.tsay/teaching/fts3/q-gdp4708.txt"
library(fUnitRoots)
da=read.table(www,header=T)
gdp=log(da[,4])
m1=ar(diff(gdp),method='mle')
m1$order
## [1] 10
adfTest(gdp,lags=10,type=c("c"))
##
## Title:
## Augmented Dickey-Fuller Test
##
## Test Results:
##
PARAMETER:
##
Lag Order: 10
##
STATISTIC:
##
Dickey-Fuller: -1.6109
##
P VALUE:
##
0.4569
##
## Description:
## Sun Aug 30 22:16:48 2015 by user:
x=ts(scan("q-earn-jnj.txt"),frequency=4,start=c(1960,1))
plot(x)

15
10
0

1960

1965

1970
Time

y=log(x)
plot(y)
c1=paste(c(1:4))
points(y,pch=c1) # put circles on data points.

1975

1980

2
1
0

13
123 2
123
4
2
23 1 34 4 4
3
3 12 1 4
3
2
1
2
4 4
23 1 4
1
234 4
2341
31
3412 4
2
3 34
412 1
3
2
3 12 41
3 24
3 3 23 12 41
12 2 1
1 4 4
4
1960

1965

1970
Time

acf(y,lag.max=16)

1975

1980

0.4
0.2

0.2

ACF

0.6

0.8

1.0

Series y

2
Lag

y1=as.vector(y) # Creates a sequence of data in R


dy1=diff(y1) # regular difference
acf(dy1,lag.max=16)

0.5

0.0

ACF

0.5

1.0

Series dy1

10
Lag

sdy1=diff(dy1,4) # seasonal difference


acf(sdy1,lag.max=12)

15

0.4
0.4

0.0

ACF

0.8

Series sdy1

10

12

Lag

m1=arima(y1,order=c(0,1,1),seasonal=list(order=c(0,1,1),period=4))
m1
##
##
##
##
##
##
##
##
##
##

Call:
arima(x = y1, order = c(0, 1, 1), seasonal = list(order = c(0, 1, 1), period = 4))
Coefficients:
ma1
-0.6809
s.e.
0.0982

sma1
-0.3146
0.1070

sigma^2 estimated as 0.007931:

log likelihood = 78.38,

tsdiag(m1) # Model checking

aic = -150.75

2 1

Standardized Residuals

20

40

60

80

Time

0.2

ACF

ACF of Residuals

10

15

Lag

0.0 1.0

p value

p values for LjungBox statistic

lag

f1=predict(m1,8) # prediction
f1
##
##
##
##
##
##
##
##
##
##
##
##
##
##

$pred
Time Series:
Start = 85
End = 92
Frequency = 1
[1] 2.905343 2.823891 2.912148 2.581085 3.036450 2.954999 3.043255 2.712193
$se
Time Series:
Start = 85
End = 92
Frequency = 1
[1] 0.08905414 0.09347899 0.09770366 0.10175307 0.13548771 0.14370561
[7] 0.15147833 0.15887123

da=read.table("w-gs1n36299.txt",header=TRUE)
r1=da[,1] # 1-year rate
r3=da[,2] # 3-year rate
plot(r1,type='l')
lines(1:1967,r3,lty=2)

10

10

10 12 14 16
4

r1

500

1000
Index

plot(r1,r3) # scatter plot of the two series

11

1500

2000

16
14
12
10
4

r3

10

12

14

r1

m1=lm(r3~r1) # Fit a regression model with likelihood method.


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)

12

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")

13

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)

14

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)

15

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')

16

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)

17

aic = -5055.69

2000

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series m3$residuals

10

15
Lag

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

18

20

25

30

0.4
0.2
0.0
0.4

0.2

m3$residuals

500

1000
Time

tsdiag(m3)

19

1500

2000

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)

20

aic = -5047.72

10

0.4
0.0

0.2

ACF

0.6

0.8

1.0

Series m4$residuals

10

15
Lag

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

21

20

25

30

0.4
0.2
0.0
0.4

0.2

m4$residuals

500

1000

1500

2000

Time

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

22

Você também pode gostar