You are on page 1of 11

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

0.1
0.0
0.2

ibm

0.2

0.3

data=read.table("m-ibm6708.txt",header=T)
ibm=data$ibm
sp=data$sprtn
plot(sp,ibm)

0.2

0.1

0.0
sp

cor(ibm,sp,method="pearson")
## [1] 0.5949041
cor(sp,ibm,method="kendall")
## [1] 0.4223351
1

0.1

cor(sp,ibm,method="spearman") # rank correlation


## [1] 0.5897587
x=rank(ibm)
y=rank(sp)
cor(x,y)
## [1] 0.5897587
x=exp(rnorm(1000))
y=exp(20*rnorm(1000))
cor(x,y,method='pearson')
## [1] 0.002533287
cor(x,y,method='kendall')
## [1] 0.04594194
cor(x,y,method='spearman')
## [1] 0.06884192
data<-read.table("d-aapl0413.txt",header=T)
data.ts<-ts(data[,3],frequency=252,start=c(2004,1)) # Start from Jan and 252 weekdays
plot(data.ts,type='l',xlab='year',ylab='rtn')
title(main='Daily returns of Apple stock from 2004 to 2013')

0.05
0.15

rtn

0.05

0.15

Daily returns of Apple stock from 2004 to 2013

2004

2006

2008

2010
year

percentage=data.ts*100 #percentage returns


hist(percentage,nclass=50,main='Percentage returns')

2012

2014

100 150 200 250


0

50

Frequency

Percentage returns

15

10

percentage

d1<-density(percentage)
plot(d1$x,d1$y,xlab='returns',ylab='den',type='l',main="Density")

10

15

0.10
0.00

0.05

den

0.15

0.20

Density

20

15

10

returns

data1<-read.table("m-tb3ms.txt",header=T)
data2<-read.table("m-tb6ms.txt",header=T)
dim(data1);dim(data2)# unequal time frame
## [1] 914

## [1] 615

914-615 #difference
## [1] 299
window=cbind(data1[300:914,4],data2[,4]) # Window from 299 + 1
index=(c(1:dim(data2)[1])+11)/12+1959 # start from 1958 Dec
plot(index,window[,1],xlab='year',ylab='rate',type='l')
lines(index,window[,2],lty=2) # Plot the 6m-TB rate on the same frame

10

15

15
10
0

rate

1960

1970

1980

1990

2000

year

plot(index,window[,2]-window[,1],xlab='year',ylab='spread',type='l')
abline(h=c(0),col=2)

2010

1.5
1.0
0.5
0.0
1.0

0.5

spread

1960

1970

1980

1990
year

data=read.table("q-ko-earns8309.txt",header=T)
index=c(1:dim(data)[1])/12+1983
plot(index,data[,3],xlab='year',ylab='earnings',type='l')
title(main='EPS of Coca Cola: 1983-2009')
points(index,data[,3])

2000

2010

0.6
0.4
0.0

0.2

earnings

0.8

1.0

EPS of Coca Cola: 19832009

1984

1986

1988

1990

year

data=read.table("d-exuseu.txt",header=T) # Load USEU exchange rates


index=c(1:dim(data)[1])/252+1999
plot(index,data[,4],xlab='year',ylab='eu',type='l')
title(main='Dollars per Euro')

1992

1.2
0.8

1.0

eu

1.4

1.6

Dollars per Euro

2000

2002

2004

2006

2008

year

logrt=diff(log(data[,4])) # Compute log returns


plot(index[2:3567],logrt,xlab='year',ylab='rtn',type='l')
title(main='ln-rtn: US-EU')

2010

2012

0.00
0.02

rtn

0.02

0.04

lnrtn: USEU

2000

2002

2004

2006
year

hist(logrt,nclass=50,main='hist of ln-rtn: US-EU')

10

2008

2010

2012

100 200 300 400 500


0

Frequency

hist of lnrtn: USEU

0.02

0.00

0.02

0.04

logrt

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

11

You might also like