Tuesday, March 27, 2012

R Fell on Alabama–Presentation to Birmingham Open Source

Birmingham Open Source Software (BOSS) was kind enough to invite me to do a debut presentation on R.  I had a lot of fun.  Below is the embedded Youtube.  Please let me know how I can improve.  I really appreciate helpful feedback.

Thursday, March 15, 2012

Opinions Not Backed by Money Updated Again

Strange that I am updating this post for a third time and nothing really has changed, but the fact that nothing has changed is incredibly interesting to me.  Since it is an update, I will not duplicate the explanation, so please read the last version Opinions Not Backed by Money Are Not That Believable--Updated and with R.  The basic conclusion is that we are in a bull market and AAII surveys are bullish, but nobody is willing to bet money on a good stock market.

From TimelyPortfolio

Data in a Google Doc (https://docs.google.com/spreadsheet/ccc?key=0Amqp2r96khJPdENIRnE1SG5nanJ5OXFyYVUxOXRBVmc) sourced from AAII- The American Association of Individual Investors and ICI.

R code (not even worth putting in GIST):

require(quantmod)
require(ggplot2)

aaii_ici=read.csv("aaii-ici-noblank.csv",row.names=1)

#using ggplot example from http://learnr.wordpress.com/2009/03/16/ggplot2-split-data-range-into-multiple-chart-series/
p <- ggplot(data=aaii_ici, aes(x = AAIIBULL-AAIIBEAR, y = runSum(aaii_ici[,4],3), colour = Range, shape = Range, label = Range))
p1 <- p + geom_point() + scale_y_continuous(limits = c(-200000, 200000),formatter=comma) + geom_hline(yintercept=0) + geom_vline(xintercept=0) + stat_smooth(method="lm", se=FALSE) + ylab("ICI Equity Rolling 3-mo Sum") + opts(title = "ICI Equity Flows by AAII Survey")
print(p1)

Tuesday, March 13, 2012

Japan Trade More Specifically with Korea

Macro analysis of Japanese trade in posts Japanese Trade and the Yen and Japan Trade by Geographic Region revealed some very interesting changes.  Since the Korean Won is so undervalued versus the Japanese Yen on a Purchasing Power Parity (PPP) basis, I was very interested in how this undervaluation might have affected Japanese and Korean trade between themselves and with the United States.  The 2008-2009 financial crisis ended the growing Japanese surplus with Korea, and currently the Japanese surplus with Korea is at its lowest level since January 2003.

From TimelyPortfolio

When we look at the Japanese and Korean deficits with the United States, we can see Korea has maintained the level of surplus achieved prior to the 2008-2009 financial crisis, while the Japanese surplus has eroded rapidly over that same time frame.

From TimelyPortfolio

If the Korean Won appreciates versus the Yen, will Korean exporters maintain their recent market share gains?  If the Korean Won appreciates versus the Yen, will there be any impact on the US $?  If there is unexpected inflation, will this exacerbate the Korean Won appreciation?

R Code from GIST:

#further explore Japanese trade data by region
#website is http://www.customs.go.jp/toukei/suii/html/time_e.htm
#format is http://www.customs.go.jp/toukei/suii/html/data/d42ma001.csv
#and the filename increments from 001 to 008 for the geographic areas
require(quantmod)
numgeog <- 8 #there are really 9 but the last is "special area"
urls <- paste("http://www.customs.go.jp/toukei/suii/html/data/d42ma00",
1:numgeog,".csv",sep="")
geogdata <- vector("list",numgeog)
for (i in 1:numgeog) {
#read the csv file with geographic area trade data
geogdata[[i]] <- read.csv(urls[i],stringsAsFactors=FALSE,skip=3)
}
#focus on Korea, so i will be 1 for Asia
i=1
#get date in usable form yyyy-mm-dd
origdate <- geogdata[[i]][,1]
xtsdate <- as.Date(paste(substr(origdate,0,4),substr(origdate,6,8),"01",sep="-"))
#get one xts with summay geographic area data
geogdata.xts<-merge(as.xts(geogdata[[i]][,2:NCOL(geogdata[[i]])],order.by=xtsdate))
geogdata.xts.Korea<-merge(geogdata.xts[,3:4],geogdata.xts[,3]-geogdata.xts[,4])
#eliminate trailing zeros
geogdata.xts.Korea <- geogdata.xts.Korea[which(geogdata.xts.Korea[,2]!=0),]
plot.zoo(geogdata.xts.Korea,screens=c(1,1,2))
getSymbols("EXKOUS",src="FRED")
getSymbols("EXJPUS",src="FRED")
KRWJPY <- EXJPUS/EXKOUS #since these are expressed in denominator, reverse
trade.Korea <- na.omit(merge(geogdata.xts.Korea,KRWJPY))
colnames(trade.Korea)<-c("Exports","Imports","SurplusDeficit","KRWJPY")
#panel.special function to better control the plot
panel.special <- function(col="grey30",...){
#to debug print(as.list(parent.frame()))
#set up titles for each of the panels in the graph
titles<-c("Imports and Exports",NA,"Surplus or Deficit","KRW/JPY")
#get the data so we can use for axis labelling
plotdata <- parent.frame()$x
#this will tell us the panel number or screens so we can customize
panel.number <- parent.frame()$panel.number
#this will tell us the number of the column
n.data <- parent.frame()$i
if (panel.number==1){ #for the import and export data
lines(col=c("steelblue2","steelblue4")[n.data],...)
#draw line for the x axis
abline(h=par()$usr[3],col="black")
} else { #for the surplus/deficit and korean won/yen
lines(col="grey70",...)
}
if (panel.number==2) { #mark the 0 on surplus/deficit
#draw horizontal line at zero
abline(h=0,col="grey30")
axis(side=2,at=0,lwd=0,lwd.ticks=1,las=1,cex.axis=0.8)
}
if (panel.number==3){ #add the date x axis on the last panel
#draw line for the x axis
abline(h=par()$usr[3],col="black")
#do x axis
axis(side=1,
at=c(index(plotdata)[1],pretty(index(plotdata))),
labels=format(c(index(plotdata)[1],pretty(index(plotdata))),"%Y"),
lwd=0,lwd.ticks=1)
}
#label each of the panels
title(main=titles[n.data],adj=0, line=-1,col.main="grey30",cex.main=0.85)
}
plot.zoo(trade.Korea,
screens=c(1,1,2,3),
ylab=NA,xlab=NA,
yaxt="n",xaxt="n",
bty="n",
main="Japan Trade with Korea and the Won/Yen",col.main="grey30",
panel=panel.special,
)
#get US trade data with Korea and Japan
library(XML)
url.japan<-"http://www.mac.doc.gov/japan-korea/statistics/us-japan-trade.htm"
url.korea<-"http://www.mac.doc.gov/japan-korea/statistics/us-Korea-trade.htm"
us.japan<-readHTMLTable(url.japan,skip.rows=1,header=TRUE)[[2]]
us.korea<-readHTMLTable(url.korea,skip.rows=1,header=TRUE)[[2]]
#combine into one xts object
us.trade <- cbind(us.japan[,2:4],us.korea[,2:4])
#erase commas
col2cvt <- 1:NCOL(us.trade)
us.trade[,col2cvt] <- lapply(us.trade[,col2cvt],
function(x){as.numeric(gsub(",", "", x))})
us.trade.xts <- as.xts(us.trade,order.by=as.Date(paste(us.japan[,1],"-12-31",sep="")))
colnames(us.trade.xts)<-c("Japan.Exports","Japan.Imports","Japan.Trade","Korea.Exports","Korea.Imports","Korea.Trade")
plot.zoo(us.trade.xts[,c(3,6)],
screens=1,
col=c("steelblue2","indianred3"),
lwd=3,
bty="n",
yaxt="n",
xaxt="n",
ylab="US$ Billion",
xlab=NA)
#draw horizontal lines at each breakpoint
abline(h=pretty(c(0,min(us.trade.xts[,3]))),col="grey70")
#draw line for the x axis
abline(h=par()$usr[3],col="black")
#do x axis
axis(side=1,
at=c(index(us.trade.xts)[1],pretty(index(us.trade.xts))),
labels=format(c(index(us.trade.xts)[1],pretty(index(us.trade.xts))),"%Y"),
lwd=0,lwd.ticks=1)
#do y axis
axis(side=2,
at=pretty(c(0,min(us.trade.xts[,3]))),
labels=pretty(c(0,min(us.trade.xts[,3])))/1000,
lwd=0,las=1)
title(main="US Trade Deficit with Japan and Korea",adj=0, col.main="grey30")
#add a legend
legend("right",c("Korea","Japan"),lwd=3,lty=1,col=c("indianred3","steelblue2"),bty="n")

Monday, March 12, 2012

Japan Trade by Geographic Region

To further the analysis presented in Japanese Trade and the Yen, I thought I would take the more granular data provided by the Japanese Ministry of Finance on trade by geographic region.  Of course, I will use R to read, analyze, and plot the .csv data.  Below you will see there are only two remaining regions where Japan has a trade surplus (North and South America), and those surpluses are declining very rapidly.

From TimelyPortfolio

R code from GIST:

#further explore Japanese trade data by region
#website is http://www.customs.go.jp/toukei/suii/html/time_e.htm
#format is http://www.customs.go.jp/toukei/suii/html/data/d42ma001.csv
#and the filename increments from 001 to 008 for the geographic areas
require(quantmod)
require(reshape)
require(lattice)
require(latticeExtra)
numgeog <- 8
urls <- paste("http://www.customs.go.jp/toukei/suii/html/data/d42ma00",
1:numgeog,".csv",sep="")
geogdata <- vector("list",numgeog)
for (i in 1:numgeog) {
#read the csv file with geographic area trade data
geogdata[[i]] <- read.csv(urls[i],stringsAsFactors=FALSE,skip=3)
#get date in usable form yyyy-mm-dd
origdate <- geogdata[[i]][,1]
xtsdate <- as.Date(paste(substr(origdate,0,4),substr(origdate,6,8),"01",sep="-"))
#get one xts with summay geographic area data
ifelse(i==1,geogdata.xts<-merge(as.xts(geogdata[[i]][,2:3],order.by=xtsdate),as.xts(geogdata[[i]][,2]-geogdata[[i]][,3],order.by=xtsdate)),
geogdata.xts<-merge(geogdata.xts,merge(as.xts(geogdata[[i]][,2:3],order.by=xtsdate),as.xts(geogdata[[i]][,2]-geogdata[[i]][,3],order.by=xtsdate))))
}
#set up labels for geographic areas
geogarea <- c("Asia","Oceania","NorthAmerica","SouthAmerica","WestEurope","EastEurope","MiddleEast","Africa")
#name columns by appending geographic area with "Exports" and "Imports"
colnames(geogdata.xts)[seq(from=1,to=numgeog*3,by=3)]<-paste(geogarea,"Exports",sep="")
colnames(geogdata.xts)[seq(from=2,to=numgeog*3,by=3)]<-paste(geogarea,"Imports",sep="")
colnames(geogdata.xts)[seq(from=3,to=numgeog*3,by=3)]<-paste(geogarea,"SurpDef",sep="")
#melt data for lattice plotting
geogdata.melt <- melt(as.data.frame(cbind(index(geogdata.xts),coredata(geogdata.xts)),
stringsAsFactors=FALSE),
id.vars=1)
#get melted data into form -- date, geographic area, direction of trade, and amount
geogdata.melt <- as.data.frame(cbind(format(index(geogdata.xts),"%Y-%m-%d"),
substr(geogdata.melt[,2],1,nchar(as.vector(geogdata.melt[,2]))-7),
substr(geogdata.melt[,2],nchar(as.vector(geogdata.melt[,2]))-6,nchar(as.vector(geogdata.melt[,2]))),
geogdata.melt[,3]),stringsAsFactors=FALSE)
#convert to Date object
geogdata.melt[,1] <- as.Date(geogdata.melt[,1])
#get strings as numeric since I do not know any better way to preserve numeric
geogdata.melt[,4] <- as.numeric(geogdata.melt[,4])
#data comes with 0s filling remainder of months left in the year
geogdata.melt <- geogdata.melt[which(geogdata.melt[,4]!=0),]
colnames(geogdata.melt)<-c("Date","Area","Direction","Amount")
#add alpha to colors
addalpha <- function(cols,alpha=180) {
rgbcomp <- col2rgb(cols)
rgbcomp[4] <- alpha
return(rgb(rgbcomp[1],rgbcomp[2],rgbcomp[3],rgbcomp[4],maxColorValue=255))
}
xyplot(Amount~Date|factor(Area),groups=Direction,data=geogdata.melt,
type="l",lwd=0.5,
layout=c(8,1),
col=apply(as.matrix(c("steelblue4","steelblue2","gray70")),MARGIN=1,addalpha),
par.settings = theEconomist.theme(box = "transparent"),
scales=list(x=list(alternating=c(1,0),draw=TRUE,
at=as.Date(index(geogdata.xts)[c(1,NROW(geogdata.xts))]),
labels=format(as.Date(index(geogdata.xts)[c(1,NROW(geogdata.xts))]),"%Y"))),
auto.key = list(space = "right",col=apply(as.matrix(c("steelblue4","steelblue2","gray70")),MARGIN=1,addalpha,alpha=210),
points=FALSE))+
layer(panel.abline(h=0, col="black"))

Wednesday, March 7, 2012

Japanese Trade and the Yen

I have had the pleasure over the last couple of weeks to help plan the CFA Society of Alabama 2012 Dinner featuring Jim Rogers and Barron’s Senior Editor Jack Willoughby.  The event was fantastic, and I would like to publicly thank Jim Rogers and Jack Willoughby for investing so much time and effort into the State of Alabama.  Both are incredible men, and I was delighted to meet them.

Jack Willoughby challenged Jim Rogers’ statement “you must understand currencies” with the point that  leveraged forex trading that has grown popular is not suitable for investors.  Jim responded by saying currencies seem unsafe just like all investments seem unsafe before a bull run, and that with proper education and management, currencies represent outstanding opportunities.  I think both are right.

Over a year ago in Japan Intentional or Accidental Pursuit of Deflation, I contended that the persistently appreciating Yen was starting to pose an extreme competitive disadvantage to the Japanese exporters.  Korean electronic, auto, and appliance manufacturers (LG, Samsung, Hyundai, Kia, etc.) have exploded their market share in the US due to their huge pricing advantage over their Japanese neighbors discussed recently in Hyundai Motor Europe confident on 2012 goal

“…Japanese rival Toyota, which has been overshadowed by the success of South Koreans in Europe in recent years, said good products backed by a currency which is 'extremely competitive' make Hyundai and Kia outperform the market.

'It is probably the mirror of the yen,' Mr Alain Uyttenhoven, vice-president at Toyota Europe, told reporters, referring to the strong yen that hurt Japanese carmakers. “

Japan seems very slow to change, but the Yen seems to have finally found a limit as the Japanese now suffer a significant trade deficit with the world.  The Japanese no longer can pursue the harmful Yen policy of the past.  Instead of Yen to US$ and Euro, they should focus on the Yen versus their competing Asian currencies.

From TimelyPortfolio

The question then becomes does this represents an opportunity for investors, and if it does, how do you go from US stocks and bonds to a short Yen long emerging currency position?

R code from GIST:

require(quantmod)
#get data from Japan Ministry of Finance website in csv form
url = "http://www.customs.go.jp/toukei/suii/html/data/d41ma.csv"
japantrade <- read.csv(url,skip=2,stringsAsFactors=FALSE)
#start cleaning data and getting in xts form
japantrade.xts <- japantrade[2:NROW(japantrade),]
#remove trailing 0 for future data
japantrade.xts <- japantrade.xts[which(japantrade.xts[,2]!=0),]
#get dates in yyyy-mm-dd form for xts
origdate <- japantrade.xts[,1]
xtsdate <- paste(substr(origdate,0,4),substr(origdate,6,8),"01",sep="-")
japantrade.xts <- as.xts(japantrade.xts[,2:3], order.by=as.Date(xtsdate))
plot.zoo(merge(japantrade.xts,japantrade.xts[,1]-japantrade.xts[,2]))
#get Japanese Yen data from St. Louis Fed Fred
getSymbols("EXJPUS",src="FRED")
#combine Yen data with Japanese trade data
tradeyen <- na.omit(merge(japantrade.xts,japantrade.xts[,1]-japantrade.xts[,2],1/EXJPUS))
colnames(tradeyen) <- c("Exports","Imports","SurplusDeficit","JPYUSD")
panel.special <- function(...) {
titles = c("Japan Imports and Exports","Surplus or Deficit","JPY/USD")
lines(...)
plotdata <- parent.frame()$x
panel.number <- parent.frame()$panel.number
if(panel.number==1) {
axis(side=4)
} else if(panel.number==2) {
abline(h=0,col="grey30",lwd=2)
axis(side=2)
} else {
axis(side=4)
}
title(main=titles[parent.frame()$panel.number],adj=0, line=-1,col="grey30")
}
#add another to color deficit points
deficit <- ifelse(tradeyen[,3]>0,0,tradeyen[,3])
plot.zoo(merge(tradeyen,deficit),screens=c(1,1,2,3,2),lwd=2,
ylab=NA, xlab=NA, yaxt="n",
panel=panel.special,
col=c("steelblue2","steelblue4","darkolivegreen3","gray40","indianred4"),
main="Japan Trade and the Yen",cex.main=2)