## Wednesday, May 25, 2011

### Eigen-who? How Can I Write About Eigen-anything and Expect You to Read?

After the very nice Convore reply

@timelyportfolio some of your posts include "eigenvalue ratio plots" -- kindly tell us what they show and how they might be useful in constructing a portfolio.

I felt like I should at least attempt to offer a little more detail on eigenvectors, which allow us to visually see similarity between variables (in my mind, time series of asset classes, indexes, stocks, or other financial prices).  In other posts, I have used the fAssets package function assetsCorEigenPlot for Long XLU Short SPY and  Russell Napier, ASIP in FT Says Emerging Market CurrenciesMichael Friendly’s wonderful paper does a very fine job of explaining eigenvalues and their use in sorting for helpful visualizations of correlation.  Wikipedia also gives a decent introduction in these two articles http://en.wikipedia.org/wiki/Principal_component_analysis and http://en.wikipedia.org/wiki/Eigenvector.  Also, I’m anxious to read the following book whose authors run http://factominer.free.fr/classical-methods/index.html

Really, the closer the variables in distance and angle, the more closely they are related.  I thought some currency data from the St. Louis Fed would provide a nice example.  Similar to milktrader’s Chop, Slice and Dice Your Returns in R, I also wanted to show multiple ways in R of achieving a plot of eigenvalues with fAssets, SciViews, and corrgram.  This analysis does not yield any real surprises—Mexican Peso and Brazilian Real are closely related, but both are least related to the Japanese Yen.

 From TimelyPortfolio
 From TimelyPortfolio
 From TimelyPortfolio

Since I used Michael Friendly’s paper so much in writing this article, I wanted to show a corrgram of the currency data.  The corrgram package offers lots of potentially useful variations of this visualization.

 From TimelyPortfolio

The second part of the Convore question is how can we use eigenvalues to construct a portfolio.  Maybe I can answer that in one of my next posts…

R code:

`#explain basics of principal component analysis#by showing the various methods of charting eigenvalues#of currency data   #give specific credit to Michael Friendly#and his paper http://www.math.yorku.ca/SCS/Papers/corrgram.pdf#another example of similar techniques used for both#baseball and finance   #for additional information on principal component analysis (PCA)#see http://en.wikipedia.org/wiki/Principal_component_analysis   require(quantmod)   #get currency data from the FED FRED data seriesKorea <- getSymbols("DEXKOUS",src="FRED",auto.assign=FALSE) #load KoreaMalaysia <- getSymbols("DEXMAUS",src="FRED",auto.assign=FALSE) #load MalaysiaSingapore <- getSymbols("DEXSIUS",src="FRED",auto.assign=FALSE) #load SingaporeTaiwan <- getSymbols("DEXTAUS",src="FRED",auto.assign=FALSE) #load TaiwanChina <- getSymbols("DEXCHUS",src="FRED",auto.assign=FALSE) #load ChinaJapan <- getSymbols("DEXJPUS",src="FRED",auto.assign=FALSE) #load JapanThailand <- getSymbols("DEXTHUS",src="FRED",auto.assign=FALSE) #load ThailandBrazil <- getSymbols("DEXBZUS",src="FRED",auto.assign=FALSE) #load BrazilMexico <- getSymbols("DEXMXUS",src="FRED",auto.assign=FALSE) #load MexicoIndia <- getSymbols("DEXINUS",src="FRED",auto.assign=FALSE) #load IndiaUSDOther <- getSymbols("DTWEXO",src="FRED",auto.assign=FALSE) #load US Dollar Other Trading PartnersUSDBroad <- getSymbols("DTWEXB",src="FRED",auto.assign=FALSE) #load US Dollar Broad   #combine all the currencies into one big currency xtscurrencies<-merge(Korea, Malaysia, Singapore, Taiwan,	China, Japan, Thailand, Brazil, Mexico, India,	USDOther, USDBroad)currencies<-na.omit(currencies)colnames(currencies)<-c("Korea", "Malaysia", "Singapore", "Taiwan",	"China", "Japan", "Thailand", "Brazil", "Mexico", "India",	"USDOther", "USDBroad")#get daily percent changescurrencies<-currencies/lag(currencies)-1       #using fAssetsrequire(fAssets)assetsCorEigenPlot(as.timeSeries(currencies))       #using techniques from corrgram package documentation#get correlation matrix(currencies.cor <- cor(currencies,use="pair"))#get two largest eigenvectors(currencies.eig<-eigen(currencies.cor)\$vectors[,1:2])e1 <- currencies.eig[,1]e2 <- currencies.eig[,2]#make the chartplot(e1,e2,col='white', xlim=range(e1,e2), ylim=range(e1,e2),	main="Plot of 2 Largest Eigenvectors for Various Asian 	and American Currencies (corrgram)")arrows(0, 0, e1, e2, cex=0.5, col="red", length=0.1)text(e1,e2, rownames(currencies.cor), cex=0.75)#run an interesting corrgram chartrequire(corrgram) #do not need for previous eigenvector plotdf1 <- data.frame(cbind(index(currencies),coredata(currencies)))corrgram(df1, order=TRUE,           main="Currency data PC2/PC1 order",          lower.panel=panel.shade, upper.panel=panel.pie,          text.panel=panel.txt)       #using techniques from SciViews package#do principal component analysisrequire(SciViews)(currencies.pca <- pcomp(~Korea + Malaysia + Singapore + Taiwan +	China + Japan + Thailand + Brazil + Mexico + India +	USDOther + USDBroad,	data = currencies))#make the chartplot(currencies.pca, which = "correlations",	main="Plot of 2 Largest Eigenvectors for Various Asian 	and American Currencies (SciViews)")#more SciViews fun#summary(currencies.pca)#screeplot(currencies.pca)#currencies.ldg<-loadings(currencies.pca)#(currencies.cor <- correlation(currencies.pca))#plot(currencies.pca, which = "scores", cex = 0.8)#pairs(currencies.pca)         #compare 2 largest eigenvectors from the sciview and corrgramcbind(loadings(currencies.pca)[,1],e1,loadings(currencies.pca)[,2],e2)`

Created by Pretty R at inside-R.org