Major Financial Index

Asset Management Consulting Corporation http://am-consulting.co.jp

0.1 Execution

Sys.time()

[1] “2014-10-02 14:18:51 JST”

0.2 Package

library(knitr)
library(quantmod)
library(xts)
library(gridExtra)
library(ggplot2)
library(scales)
library(tseries)
library(PerformanceAnalytics)

0.3 Import Raw Data

diff.date<-365
first.date<-Sys.Date()-diff.date
gap<-100
cnt<-1
all.data<-list()
ts.data<-list()
list.s<-list()
list.s[[1]]<-c("FRED",1,"DCOILWTICO","DGS10")
list.s[[2]]<-c("yahoo",4,"^GSPC","^FTSE","^N225")
list.s[[3]]<-c("oanda",1,"JPY","EUR","AUD","NZD","ZAR","CAD","GBP","CHF","XAU","XAG","XPT","BTC")
for(lll in 1:length(list.s)){
for(iii in 3:length(list.s[[lll]])){
if(list.s[[lll]][1]=="oanda"){
item<-paste("USD/",list.s[[lll]][iii],sep="") 
}
else{
item<- list.s[[lll]][iii]
} 
ts.data[[cnt]]<-getSymbols(item,src=list.s[[lll]][1],auto.assign=FALSE)
ts.data[[cnt]]<-ts.data[[cnt]][,as.double(list.s[[lll]][2])]
cnt<-cnt+1
}
}
#Japanese Government Bonds
url<-c(
10, 
"http://www.mof.go.jp/english/jgbs/reference/interest_rate/historical/jgbcme_all.csv",
"http://www.mof.go.jp/english/jgbs/reference/interest_rate/jgbcme.csv"
)
buf<-cnt
cnt<-cnt+1
for(ddd in 2:length(url)){
ts.data[[cnt]]<-read.table(url[ddd],header=T,sep=",",stringsAsFactor=F)
ts.data[[cnt]][,1]<-as.Date(ts.data[[cnt]][,1])
colnames(ts.data[[cnt]])[1]<-"date"
ts.data[[cnt]]<-as.xts(ts.data[[cnt]][,-1],order.by=as.Date(ts.data[[cnt]]$date),stringsAsFactor=F)
ts.data[[cnt]]<-apply.daily(ts.data[[cnt]],function(x) as.numeric(x)) #to convert character to NA
ts.data[[cnt]]<-ts.data[[cnt]][,as.double(url[1])]
#ts.data[[cnt]]<-apply(ts.data[[cnt]],c(1,2),function(x) as.numeric(x)) #change class to matrix
if(ddd==2){ts.data[[buf]]<-ts.data[[cnt]]}else{ts.data[[buf]]<-rbind.xts(ts.data[[buf]],ts.data[[cnt]])}
cnt<-cnt+1
}
cnt<-buf

0.4 Calculate Logarithmic Return

for(iii in 1:length(ts.data)){
ts.data[[iii+gap]]<-diff(log(na.omit(ts.data[[iii]])))[-1]
}

0.5 Plot Time Series Data of Level and Logarithmic Return

for(iii in 1:4){
if(iii==1 || iii==3){gap<-0}else if(iii==2 || iii==4){gap<-100}
par(mfrow=c(5,4),mar=c(3,4,3,4))
for(ppp in (1+gap):(cnt+gap)){
buf<-ts.data[[ppp]]
if(iii==3 || iii==4){ 
buf<-buf[paste(first.date,"::",sep="")]
} 
buf<-data.frame(date=as.Date(index(buf)),buf,row.names=NULL)
plot(x=buf[,1],y=buf[,2],type="l",main=colnames(buf)[2],xlab="",ylab=colnames(buf)[2],xaxt="n")
axis.Date(side=1,at=seq(first(buf[,1]),last(buf[,1]),"days"),format="%Y/%m/%d")
}
if(iii==1 || iii==3){title(paste("\n","Level",sep=""),outer=T)}else{title(paste("\n","logarithmic return",sep=""),outer=T)}
}

plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5

0.6 Data Check and Merge All Time Series Data

for(iii in 1:cnt){
print(paste(iii,"-",class(ts.data[[iii]])))
if(iii==1){all.data[[1]]<-ts.data[[iii]]}else{all.data[[1]]<-merge.xts(all.data[[1]],ts.data[[iii]])}
}
## [1] "1 - xts" "1 - zoo"
## [1] "2 - xts" "2 - zoo"
## [1] "3 - xts" "3 - zoo"
## [1] "4 - xts" "4 - zoo"
## [1] "5 - xts" "5 - zoo"
## [1] "6 - xts" "6 - zoo"
## [1] "7 - xts" "7 - zoo"
## [1] "8 - xts" "8 - zoo"
## [1] "9 - xts" "9 - zoo"
## [1] "10 - xts" "10 - zoo"
## [1] "11 - xts" "11 - zoo"
## [1] "12 - xts" "12 - zoo"
## [1] "13 - xts" "13 - zoo"
## [1] "14 - xts" "14 - zoo"
## [1] "15 - xts" "15 - zoo"
## [1] "16 - xts" "16 - zoo"
## [1] "17 - xts" "17 - zoo"
## [1] "18 - xts" "18 - zoo"
head(all.data[[1]],10)
##            DCOILWTICO DGS10 GSPC.Close FTSE.Close N225.Close USD.JPY
## 1962-01-02         NA  4.06         NA         NA         NA      NA
## 1962-01-03         NA  4.03         NA         NA         NA      NA
## 1962-01-04         NA  3.99         NA         NA         NA      NA
## 1962-01-05         NA  4.02         NA         NA         NA      NA
## 1962-01-08         NA  4.03         NA         NA         NA      NA
## 1962-01-09         NA  4.05         NA         NA         NA      NA
## 1962-01-10         NA  4.07         NA         NA         NA      NA
## 1962-01-11         NA  4.08         NA         NA         NA      NA
## 1962-01-12         NA  4.08         NA         NA         NA      NA
## 1962-01-15         NA  4.10         NA         NA         NA      NA
##            USD.EUR USD.AUD USD.NZD USD.ZAR USD.CAD USD.GBP USD.CHF USD.XAU
## 1962-01-02      NA      NA      NA      NA      NA      NA      NA      NA
## 1962-01-03      NA      NA      NA      NA      NA      NA      NA      NA
## 1962-01-04      NA      NA      NA      NA      NA      NA      NA      NA
## 1962-01-05      NA      NA      NA      NA      NA      NA      NA      NA
## 1962-01-08      NA      NA      NA      NA      NA      NA      NA      NA
## 1962-01-09      NA      NA      NA      NA      NA      NA      NA      NA
## 1962-01-10      NA      NA      NA      NA      NA      NA      NA      NA
## 1962-01-11      NA      NA      NA      NA      NA      NA      NA      NA
## 1962-01-12      NA      NA      NA      NA      NA      NA      NA      NA
## 1962-01-15      NA      NA      NA      NA      NA      NA      NA      NA
##            USD.XAG USD.XPT USD.BTC X10
## 1962-01-02      NA      NA      NA  NA
## 1962-01-03      NA      NA      NA  NA
## 1962-01-04      NA      NA      NA  NA
## 1962-01-05      NA      NA      NA  NA
## 1962-01-08      NA      NA      NA  NA
## 1962-01-09      NA      NA      NA  NA
## 1962-01-10      NA      NA      NA  NA
## 1962-01-11      NA      NA      NA  NA
## 1962-01-12      NA      NA      NA  NA
## 1962-01-15      NA      NA      NA  NA
tail(all.data[[1]],10)
##            DCOILWTICO DGS10 GSPC.Close FTSE.Close N225.Close USD.JPY
## 2014-09-23      91.55  2.54       1983       6676         NA   108.9
## 2014-09-24      93.60  2.57       1998       6706      16167   108.7
## 2014-09-25      93.59  2.52       1966       6640      16374   108.8
## 2014-09-26      95.55  2.54       1983       6649      16230   109.1
## 2014-09-27         NA    NA         NA         NA         NA   109.0
## 2014-09-28         NA    NA         NA         NA         NA   109.3
## 2014-09-29      94.53  2.50       1978       6647      16311   109.3
## 2014-09-30         NA  2.52       1972       6623      16174   109.4
## 2014-10-01         NA    NA       1946       6558      16082   109.5
## 2014-10-02         NA    NA         NA         NA         NA   109.6
##            USD.EUR USD.AUD USD.NZD USD.ZAR USD.CAD USD.GBP USD.CHF
## 2014-09-23  0.7785   1.124   1.230   11.12   1.098  0.6121  0.9399
## 2014-09-24  0.7776   1.126   1.234   11.15   1.104  0.6107  0.9388
## 2014-09-25  0.7796   1.128   1.239   11.15   1.108  0.6106  0.9415
## 2014-09-26  0.7843   1.134   1.254   11.18   1.109  0.6129  0.9473
## 2014-09-27  0.7857   1.139   1.265   11.21   1.113  0.6138  0.9485
## 2014-09-28  0.7886   1.142   1.272   11.24   1.116  0.6155  0.9516
## 2014-09-29  0.7886   1.142   1.272   11.24   1.116  0.6155  0.9516
## 2014-09-30  0.7883   1.146   1.285   11.27   1.116  0.6158  0.9515
## 2014-10-01  0.7905   1.145   1.285   11.28   1.117  0.6162  0.9537
## 2014-10-02  0.7929   1.148   1.283   11.29   1.120  0.6174  0.9566
##              USD.XAU USD.XAG   USD.XPT  USD.BTC   X10
## 2014-09-23 0.0008232 0.05652 0.0007553 0.002521    NA
## 2014-09-24 0.0008191 0.05625 0.0007507 0.002384 0.526
## 2014-09-25 0.0008190 0.05641 0.0007622 0.002312 0.521
## 2014-09-26 0.0008231 0.05698 0.0007622 0.002422 0.517
## 2014-09-27 0.0008193 0.05690 0.0007710 0.002478    NA
## 2014-09-28 0.0008212 0.05676 0.0007710 0.002501    NA
## 2014-09-29 0.0008212 0.05676 0.0007696 0.002574 0.522
## 2014-09-30 0.0008209 0.05700 0.0007686 0.002664 0.522
## 2014-10-01 0.0008243 0.05771 0.0007743 0.002628 0.527
## 2014-10-02 0.0008263 0.05859 0.0007798 0.002604    NA

0.7 Interest Spread between JGB and U.S.Treasury

par(mfrow=c(1,1),mar=c(3,4,3,4))
buf<-na.omit(all.data[[1]])
buf<-buf$DGS10-buf$X10
colnames(buf)[1]<-"spread"
buf<-buf[paste(first.date,"::",sep="")]
buf<-data.frame(date=as.Date(index(buf)),buf,row.names=NULL)
g<-ggplot(buf,aes(x=date,y=spread))+geom_line()+scale_x_date(labels=date_format("%Y/%b/%d"))
g<-g+ylab("Interest spread(%)")+ggtitle("Interest Spread between 10year JGB and 10year U.S.Treasury")
g+geom_smooth(method=lm)+geom_smooth(method=loess,color="red")+geom_point()

plot of chunk unnamed-chunk-7

summary(buf)
##       date                spread    
##  Min.   :2013-10-02   Min.   :1.82  
##  1st Qu.:2014-01-04   1st Qu.:1.98  
##  Median :2014-04-02   Median :2.04  
##  Mean   :2014-04-02   Mean   :2.05  
##  3rd Qu.:2014-07-01   3rd Qu.:2.12  
##  Max.   :2014-09-29   Max.   :2.32
adf.spread<-adf.test(buf$spread)
adf.spread.diff<-adf.test(diff(buf$spread))

0.8 Exchange Rate between JPY and USD

par(mfrow=c(1,1),mar=c(3,4,3,4))
buf<-na.omit(all.data[[1]]$USD.JPY)
buf<-buf[paste(first.date,"::",sep="")]
buf<-data.frame(date=as.Date(index(buf)),buf,row.names=NULL)
g<-ggplot(buf,aes(x=date,y=USD.JPY))+geom_line()+scale_x_date(labels=date_format("%Y/%b/%d"))
g<-g+ylab("USD.JPY")+ggtitle("Exchange Rate between JPY and USD")+geom_smooth(method=lm)
g+geom_smooth(method=loess,color="red")+geom_point()

plot of chunk unnamed-chunk-8

summary(buf)
##       date               USD.JPY   
##  Min.   :2013-10-02   Min.   : 97  
##  1st Qu.:2014-01-01   1st Qu.:102  
##  Median :2014-04-02   Median :102  
##  Mean   :2014-04-02   Mean   :102  
##  3rd Qu.:2014-07-02   3rd Qu.:103  
##  Max.   :2014-10-02   Max.   :110
adf.rate<-adf.test(buf$USD.JPY)
adf.rate.diff<-adf.test(diff(buf$USD.JPY))

0.9 Unit Root Test and Cointegration Test

#po.test H0:No cointegrated
print(adf.spread)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  buf$spread
## Dickey-Fuller = -2.611, Lag order = 6, p-value = 0.3192
## alternative hypothesis: stationary
print(adf.spread.diff)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(buf$spread)
## Dickey-Fuller = -7.1, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
print(adf.rate)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  buf$USD.JPY
## Dickey-Fuller = -0.89, Lag order = 7, p-value = 0.9531
## alternative hypothesis: stationary
print(adf.rate.diff)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(buf$USD.JPY)
## Dickey-Fuller = -7.779, Lag order = 7, p-value = 0.01
## alternative hypothesis: stationary
buf<-na.omit(all.data[[1]])
buf$spread<-buf$DGS10-buf$X10
buf<-cbind(buf$spread,buf$USD.JPY)
po.test(buf,demean=T,lshort=T)
## 
##  Phillips-Ouliaris Cointegration Test
## 
## data:  buf
## Phillips-Ouliaris demeaned = -15.55, Truncation lag parameter = 3,
## p-value = 0.1344

0.10 Rolling Correlation between Interest Spread and USD/JPY Exchange Rate

for(lll in 1:2){
buf<-na.omit(all.data[[1]])
buf$spread<-buf$DGS10-buf$X10
if(lll==2){
buf<-diff(log(buf))[-1]
}
buf<-buf[paste(first.date,"::",sep="")]
width<-30
corr<-rollapply(buf,width,function(z)cor(z$USD.JPY ,z$spread),by.column=FALSE)
corr<-data.frame(date=as.Date(index(corr)),corr,row.names=NULL)
colnames(corr)[2]<-"correlation.coefficient"
g1<-ggplot(corr,aes(x=date,y=correlation.coefficient))
g1<-g1+geom_bar(stat="identity",position="identity",fill="grey",alpha=0.2,color="black")+scale_x_date(labels=date_format("%Y/%b/%d"))
g1<-g1+ggtitle("Rolling Correlation between Interest Spread and USD/JPY Exchange Rate")
g2<-ggplot(corr,aes(x=correlation.coefficient))+geom_histogram(binwidth=0.1,fill="grey",alpha=0.5,color="black")
grid.arrange(g1,g2,nrow=1,ncol=2)
print(summary(corr))
}

plot of chunk unnamed-chunk-10

##       date            correlation.coefficient
##  Min.   :2013-10-02   Min.   :-0.578         
##  1st Qu.:2014-01-04   1st Qu.: 0.226         
##  Median :2014-04-02   Median : 0.428         
##  Mean   :2014-04-02   Mean   : 0.376         
##  3rd Qu.:2014-07-01   3rd Qu.: 0.653         
##  Max.   :2014-09-29   Max.   : 0.849         
##                       NA's   :29

plot of chunk unnamed-chunk-10

##       date            correlation.coefficient
##  Min.   :2013-10-02   Min.   :-0.459         
##  1st Qu.:2014-01-04   1st Qu.:-0.245         
##  Median :2014-04-02   Median :-0.089         
##  Mean   :2014-04-02   Mean   :-0.134         
##  3rd Qu.:2014-07-01   3rd Qu.:-0.042         
##  Max.   :2014-09-29   Max.   : 0.126         
##                       NA's   :29

0.11 Plot both Interest Spread and USD/JPY Exchange Rate

par(mfrow=c(1,1),mar=c(3,4,3,4))
buf<-na.omit(all.data[[1]])
buf$spread<-buf$DGS10-buf$X10
buf<-buf[paste(first.date,"::",sep="")]
chart.TimeSeries(buf$USD.JPY,main="",yaxis.right=F,type="o",lwd="1",legend.loc="bottomleft",color="blue")
par(new=T)
chart.TimeSeries(buf$spread,main="",type="o",lwd="1",xaxis=F,yaxis.right=T,legend.loc="topleft")

plot of chunk unnamed-chunk-11

0.12 Reference