2014年9月30日 主要金融指標 10年物日本国債金利、米国財務省証券金利、ドル円為替レート


Data Source

Federal Reserve Bank of St. Louis http://research.stlouisfed.org/

OANDA Corporation http://www.oanda.com/

Ministry of Finance Japan http://www.mof.go.jp/english/index.htm

Sys.time()

[1] “2014-09-30 20:15:56 JST”

Package

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

Read 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

logarithmic return

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

Plot

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

Data check

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]])}
}
#head(all.data[[1]],10)
#tail(all.data[[1]],10)

Interest Spread between Japan and USA

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-09-30   Min.   :1.82  
##  1st Qu.:2013-12-26   1st Qu.:1.98  
##  Median :2014-03-29   Median :2.04  
##  Mean   :2014-03-28   Mean   :2.05  
##  3rd Qu.:2014-06-25   3rd Qu.:2.12  
##  Max.   :2014-09-22   Max.   :2.32
adf.test(buf$spread)$p.value
## [1] 0.3078
adf.test(diff(buf$spread))$p.value
## [1] 0.01

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-09-30   Min.   : 97  
##  1st Qu.:2013-12-30   1st Qu.:102  
##  Median :2014-03-31   Median :102  
##  Mean   :2014-03-31   Mean   :102  
##  3rd Qu.:2014-06-30   3rd Qu.:103  
##  Max.   :2014-09-30   Max.   :109
adf.test(buf$USD.JPY)$p.value
## [1] 0.9595
adf.test(diff(buf$USD.JPY))$p.value
## [1] 0.01

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-9

##       date            correlation.coefficient
##  Min.   :2013-09-30   Min.   :-0.578         
##  1st Qu.:2013-12-26   1st Qu.: 0.221         
##  Median :2014-03-29   Median : 0.428         
##  Mean   :2014-03-28   Mean   : 0.368         
##  3rd Qu.:2014-06-25   3rd Qu.: 0.645         
##  Max.   :2014-09-22   Max.   : 0.812         
##                       NA's   :29

plot of chunk unnamed-chunk-9

##       date            correlation.coefficient
##  Min.   :2013-09-30   Min.   :-0.459         
##  1st Qu.:2013-12-26   1st Qu.:-0.246         
##  Median :2014-03-29   Median :-0.090         
##  Mean   :2014-03-28   Mean   :-0.134         
##  3rd Qu.:2014-06-25   3rd Qu.:-0.042         
##  Max.   :2014-09-22   Max.   : 0.126         
##                       NA's   :29

Plot

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-10