[Returnanalytics-commits] r3207 - in pkg/PortfolioAnalytics/sandbox/symposium2013: . R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Oct 5 15:36:19 CEST 2013


Author: peter_carl
Date: 2013-10-05 15:36:19 +0200 (Sat, 05 Oct 2013)
New Revision: 3207

Modified:
   pkg/PortfolioAnalytics/sandbox/symposium2013/R/chart.UnStackedBar.R
   pkg/PortfolioAnalytics/sandbox/symposium2013/R/page.Distributions.R
   pkg/PortfolioAnalytics/sandbox/symposium2013/analyze.HFindexes.R
   pkg/PortfolioAnalytics/sandbox/symposium2013/optimize.HFindexes.R
   pkg/PortfolioAnalytics/sandbox/symposium2013/results.HFindexes.R
Log:
- checkpoint commits across the board

Modified: pkg/PortfolioAnalytics/sandbox/symposium2013/R/chart.UnStackedBar.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/symposium2013/R/chart.UnStackedBar.R	2013-10-05 04:15:16 UTC (rev 3206)
+++ pkg/PortfolioAnalytics/sandbox/symposium2013/R/chart.UnStackedBar.R	2013-10-05 13:36:19 UTC (rev 3207)
@@ -1,4 +1,4 @@
-chart.UnStackedBar <- function(w, colorset=1:NROW(w), rotate=c("vertical", "horizontal"), yaxis=TRUE, equal.line=FALSE)
+chart.UnStackedBar <- function(w, colorset=1:NROW(w), rotate=c("vertical", "horizontal"), yaxis=TRUE, equal.line=FALSE, las=par(las), ...)
   {
   # Weights should come in as:
   #              Convertible Arbitrage CTA Global Distressed Securities 
@@ -16,20 +16,20 @@
     layout(matrix(c(1:NCOL(w)), nr = 1, byrow = TRUE))
     for(i in 1:NCOL(w)){
       if(i==1){
-        barplot(w[,i], col=colorset[i], horiz=TRUE, xlim=c(0,max(w)), axes=FALSE, names.arg=row.names, las=2, cex.names=1)
+        barplot(w[,i], col=colorset[i], horiz=TRUE, xlim=c(0,max(w)), axes=FALSE, names.arg=row.names, las=las,...)
         abline(v=0, col="darkgray")
         if(equal.line)
           abline(v=1/NROW(w), col="darkgray", lty=2)
-        axis(1, cex.axis = 1, col = "darkgray", las=1)
+        axis(1, cex.axis = 1, col = "darkgray", las=las)
         mtext(colnames(w)[i], side= 3, cex=0.8, adj=0.5)
       } 
       else{
-        barplot(w[,i], col=colorset[i], horiz=TRUE, xlim=c(0,max(w)), axes=FALSE, names.arg="", ylab=colnames(w)[i])
+        barplot(w[,i], col=colorset[i], horiz=TRUE, xlim=c(0,max(w)), axes=FALSE, names.arg="", ylab=colnames(w)[i], ...)
         abline(v=0, col="darkgray")
         if(equal.line)
           abline(v=1/NROW(w), col="darkgray", lty=2)
         if(yaxis)
-          axis(1, cex.axis = 1, col = "darkgray", las=1)
+          axis(1, cex.axis = 1, col = "darkgray", las=las)
         mtext(colnames(w)[i], side= 3, cex=0.8, adj=0.5)
       }
     }
@@ -39,20 +39,20 @@
     layout(matrix(c(1:NCOL(w)), nr = NCOL(w), byrow = FALSE))
     for(i in 1:NCOL(w)){
       if(i==NCOL(w)){
-        barplot(w[,i], col=colorset[i], horiz=FALSE, ylim=c(0,max(w)), axes=FALSE, names.arg=row.names, las=2, cex.names=1.5)
+        barplot(w[,i], col=colorset[i], horiz=FALSE, ylim=c(0,max(w)), axes=FALSE, names.arg=row.names, las=las, cex.names=1.5, ...)
         abline(h=0, col="darkgray")
         if(equal.line)
           abline(h=1/NROW(w), col="darkgray", lty=2)
-        axis(2, cex.axis = 1, col = "darkgray", las=1)
+        axis(2, cex.axis = 1, col = "darkgray", las=las)
         mtext(colnames(w)[i], side= 3, cex=1, adj=0)
       } 
       else{
-        barplot(w[,i], col=colorset[i], horiz=FALSE, ylim=c(0,max(w)), axes=FALSE, names.arg="", ylab=colnames(w)[i])
+        barplot(w[,i], col=colorset[i], horiz=FALSE, ylim=c(0,max(w)), axes=FALSE, names.arg="", ylab=colnames(w)[i], ...)
         abline(h=0, col="darkgray")
         if(equal.line)
           abline(h=1/NROW(w), col="darkgray", lty=2)
         if(yaxis)
-          axis(2, cex.axis = 1, col = "darkgray", las=1)
+          axis(2, cex.axis = 1, col = "darkgray", las=las)
         mtext(colnames(w)[i], side= 3, cex=1, adj=0)
       }
     }

Modified: pkg/PortfolioAnalytics/sandbox/symposium2013/R/page.Distributions.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/symposium2013/R/page.Distributions.R	2013-10-05 04:15:16 UTC (rev 3206)
+++ pkg/PortfolioAnalytics/sandbox/symposium2013/R/page.Distributions.R	2013-10-05 13:36:19 UTC (rev 3207)
@@ -1,6 +1,6 @@
 
 # Histogram, QQPlot and ECDF plots aligned by scale for comparison
-page.Distributions <- function (R, ...) {
+page.Distributions <- function (R, colorset = c("black", "#00008F", "#005AFF", "#23FFDC", "#ECFF13", "#FF4A00", "#800000"), ...) {
   require(PerformanceAnalytics)
   op <- par(no.readonly = TRUE)
   # c(bottom, left, top, right)
@@ -16,26 +16,25 @@
       plot.new()
       text(x=1, y=0.5, adj=c(1,0.5), labels=row.names[i], cex=1.1)
       chart.Histogram(R[,i], main="", xlim=c(chart.mins, chart.maxs), 
-                      breaks=seq(round(chart.mins, digits=2)-0.01, round(chart.maxs, digits=2)+0.01, by=0.01), note.lines=boxplot.stats(as.vector(R[,i]))$stats, note.color="#005AFF",
-                      show.outliers=TRUE, methods=c("add.normal"), colorset = 
-                        c("black", "#00008F", "#005AFF", "#23FFDC", "#ECFF13", "#FF4A00", "#800000"))
+                      breaks=seq(round(chart.mins, digits=3)-0.005, round(chart.maxs, digits=3)+0.005, by=0.005), note.lines=boxplot.stats(as.vector(R[,i]))$stats, note.color=colorset[2],
+                      show.outliers=TRUE, methods=c("add.normal"), colorset = colorset)
       abline(v=0, col="darkgray", lty=2)
-      chart.QQPlot(R[,i], main="", pch=20, envelope=0.95, col=c(1,"#005AFF"), ylim=c(chart.mins, chart.maxs))
+      chart.QQPlot(R[,i], main="", pch=20, envelope=0.95, col=colorset, ylim=c(chart.mins, chart.maxs), cex=0.5)
       abline(v=0, col="darkgray", lty=2)
-      chart.ECDF(R[,i], main="", xlim=c(chart.mins, chart.maxs), lwd=2)
+      chart.ECDF(R[,i], main="", xlim=c(chart.mins, chart.maxs), lwd=2, colorset=colorset)
       abline(v=0, col="darkgray", lty=2)
     }
     else{
       plot.new()
       text(x=1, y=0.5, adj=c(1,0.5), labels=row.names[i], cex=1.1)
       chart.Histogram(R[,i], main="", xlim=c(chart.mins, chart.maxs), 
-                      breaks=seq(round(chart.mins, digits=2)-0.01, round(chart.maxs, digits=2)+0.01, by=0.01),  note.lines=boxplot.stats(as.vector(R[,i]))$stats, note.color="#005AFF",
-                      xaxis=FALSE, yaxis=FALSE, show.outliers=TRUE, methods=c("add.normal"), colorset = 
-                        c("black", "#00008F", "#005AFF", "#23FFDC", "#ECFF13", "#FF4A00", "#800000"))
+                      breaks=seq(round(chart.mins, digits=3)-0.005, round(chart.maxs, digits=3)+0.005, by=0.005),  note.lines=boxplot.stats(as.vector(R[,i]))$stats, note.color=colorset[2],
+                      xaxis=FALSE, yaxis=FALSE, show.outliers=TRUE, methods=c("add.normal"), colorset = colorset
+                        )
       abline(v=0, col="darkgray", lty=2)
-      chart.QQPlot(R[,i], main="", xaxis=FALSE, yaxis=FALSE, pch=20, envelope=0.95, col=c(1,"#005AFF"), ylim=c(chart.mins, chart.maxs))
+      chart.QQPlot(R[,i], main="", xaxis=FALSE, yaxis=FALSE, pch=20, envelope=0.95, col=c(colorset), ylim=c(chart.mins, chart.maxs), cex=0.5)
       abline(v=0, col="darkgray", lty=2)
-      chart.ECDF(R[,i], main="", xlim=c(chart.mins, chart.maxs), xaxis=FALSE, yaxis=FALSE, lwd=2)
+      chart.ECDF(R[,i], main="", xlim=c(chart.mins, chart.maxs), xaxis=FALSE, yaxis=FALSE, lwd=2, colorset=colorset)
       abline(v=0, col="darkgray", lty=2)
     }
   }

Modified: pkg/PortfolioAnalytics/sandbox/symposium2013/analyze.HFindexes.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/symposium2013/analyze.HFindexes.R	2013-10-05 04:15:16 UTC (rev 3206)
+++ pkg/PortfolioAnalytics/sandbox/symposium2013/analyze.HFindexes.R	2013-10-05 13:36:19 UTC (rev 3207)
@@ -13,12 +13,53 @@
 functionsdir = "./R/"
 
 ### Load the necessary packages
+require(PerformanceAnalytics)
 require(vcd) # for color palates
 require(corrplot) # for correlation charts
+require(gplots)
+require(extrafont)
+require(Cairo)
+require(Hmisc)
 
-# This may be useful for PCA analysis of index data
-# require(FactorAnalytics) # development version > build 
+### Graphics
+# To insert in Powerpoint 2007 (gah!), graphics should be rendered as pdf objects using the Cario package for better font handling:
+# > CairoPDF(file="ConcPercESContrib-mETL-wHull.pdf", height=5.5, width=9, family="Cambria")
+# then, do post-processing with ImageMagick on Linux:
+# $ convert -density 300 ConcPercESContrib-mETL-wHull.pdf -quality 100 -sharpen 0x1.0 ConcPercESContrib-mETL-wHull.png
+# That appears to give a sharp, dense picture that shows well on slides.  Use the pdf directly if possible, of course.
 
+# Corporate colors and fonts
+wb13color = c(
+  rgb(0,65,101, max=255), # Blair Blue
+  rgb(129,147,219, max=255), # Light Purple
+  rgb(0,133,102, max=255), # Dark Green
+  rgb(0,0,0, max=255), # Black
+  rgb(154,155,156, max=255), # Light Grey
+  rgb(0,122,201, max=255), # Dark Cyan
+  rgb(240,171,0, max=255), # Bright Orange
+  rgb(72,72,74, max=255), # Dark Grey
+  rgb(122,184,0, max=255), # Bright Green
+  rgb(87,6,140, max=255), # Dark Purple
+  rgb(220,80,52, max=255), # Dark Orange
+  rgb(243,211,17, max=255), # Yellow
+  rgb(61,183,228, max=255)  # Bright Cyan
+  )
+
+# Skewed-scale GrYlRd in WmB colors for correlation charts
+skewedWB20 = c(colorpanel(16, "#008566","#E1E56D"), colorpanel(5, "#E1E56D", "#742414")[-1])
+
+CairoFonts(
+  regular="Cambria:style=Regular",
+  bold="Cambria:style=Bold",
+  italic="Cambria:style=Italic",
+  bolditalic="Cambria:style=Bold Italic,BoldItalic",
+  symbol="Symbol"
+)
+
+par(las=1) # axis labels all horizontal
+par(cex.lab=.8) # shrink axis labels
+op <- par(no.readonly = TRUE)
+
 ### Set up color palates
 pal <- function(col, border = "light gray", ...){
   n <- length(col)
@@ -42,7 +83,7 @@
 # Constants
 p=1-(1/12) # 
 Rf=.03/12 # Monthly risk free rate
-colorset = rich8equal
+colorset = wb13color
 dataname="EDHEC"
 
 ########################################################################
@@ -66,14 +107,12 @@
 # --------------------------------------------------------------------
 # Returns through time
 # --------------------------------------------------------------------
-png(filename=paste(resultsdir, dataname, "-Cumulative-Returns.png", sep=""), units="in", height=5.5, width=9, res=96) 
-par(cex.lab=.8) # should set these parameters once at the top
-op <- par(no.readonly = TRUE)
+CairoPDF(file=paste(resultsdir, dataname, "-Cumulative-Returns.pdf", sep=""), height=5.5, width=9) 
 layout(matrix(c(1, 2)), height = c(2, 1.3), width = 1)
 par(mar = c(1, 4, 1, 2)) #c(bottom, left, top, right)
-chart.CumReturns(R, main = "", xaxis = FALSE, legend.loc = "topleft", ylab = "Cumulative Return", colorset= rainbow8equal, ylog=TRUE, wealth.index=TRUE, cex.legend=.7, cex.axis=.6, cex.lab=.7)
+chart.CumReturns(R, main = "", xaxis = FALSE, legend.loc = "topleft", ylab = "Cumulative Return", colorset= wb13color, ylog=TRUE, wealth.index=TRUE, cex.legend=.7, cex.axis=.6, cex.lab=.7, las=1, pch="")
 par(mar = c(4, 4, 0, 2))
-chart.Drawdown(edhec.R, main = "", ylab = "Drawdown", colorset = rainbow8equal, cex.axis=.6, cex.lab=.7)
+chart.Drawdown(edhec.R, main = "", ylab = "Drawdown", colorset = wb13color, cex.axis=.6, cex.lab=.7, las=1)
 par(op)
 dev.off()
 
@@ -81,52 +120,51 @@
 # Monthly Returns and Risk
 # --------------------------------------------------------------------
 # Done
-png(filename=paste(resultsdir, dataname, "-BarVaR.png", sep=""), units="in", height=5.5, width=9, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-BarVaR.pdf", sep=""), height=5.5, width=9) 
 # Generate charts of returns with ETL and VaR through time
 par(mar=c(3, 4, 0, 2) + 0.1) #c(bottom, left, top, right)
 charts.BarVaR(R[,1:4], p=p, gap=36, main="", show.greenredbars=TRUE, 
               methods=c("ModifiedES", "ModifiedVaR"), show.endvalue=TRUE, 
-              colorset=rep("Black",7), ylim=c(-.1,.15))
+              colorset=rep("Black",7), ylim=c(-.1,.15), las=1, clean="boudt")
 par(op)
 dev.off()
-png(filename=paste(resultsdir, dataname, "-BarVaR2.png", sep=""), units="in", height=5.5, width=9, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-BarVaR2.pdf", sep=""), height=5.5, width=9) 
 # Generate charts of returns with ETL and VaR through time
 par(mar=c(3, 4, 0, 2) + 0.1) #c(bottom, left, top, right)
 charts.BarVaR(R[,5:7], p=p, gap=36, main="", show.greenredbars=TRUE, 
               methods=c("ModifiedES", "ModifiedVaR"), show.endvalue=TRUE, 
-              colorset=rep("Black",7), ylim=c(-.1,.15))
+              colorset=rep("Black",7), ylim=c(-.1,.15), las=1, clean="boudt")
 par(op)
 dev.off()
 
 # --------------------------------------------------------------------
 # Rolling Performance
 # --------------------------------------------------------------------
-png(filename=paste(resultsdir, dataname, "-RollPerf.png", sep=""), units="in", height=5.5, width=9, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-RollPerf.pdf", sep=""), height=5.5, width=9) 
 # Generate charts of EDHEC index returns with ETL and VaR through time
 par(mar=c(5, 4, 0, 2) + 0.1) #c(bottom, left, top, right)
-charts.RollingPerformance(R, width=36, main="", colorset=rainbow8equal, legend.loc="topleft")
+charts.RollingPerformance(R, width=36, main="", pch="", colorset=wb13color, legend.loc="topleft", las=1)
 par(op)
 dev.off()
 
 # --------------------------------------------------------------------
 # Returns and Risk Scatter
 # --------------------------------------------------------------------
-png(filename=paste(resultsdir, dataname, "-Scatter36m.png", sep=""), units="in", height=5.5, width=4.5, res=96) 
-chart.RiskReturnScatter(last(edhec.R,36), main="EDHEC Index Trailing 36-Month Performance", colorset=rainbow8equal, ylim=c(0,.2), xlim=c(0,.12))
+CairoPDF(file=paste(resultsdir, dataname, "-Scatter36m.pdf", sep=""), height=5.5, width=4.5) 
+chart.RiskReturnScatter(last(edhec.R,36), main="EDHEC Index Trailing 36-Month Performance", colorset=wb13color, ylim=c(0,.2), xlim=c(0,.12), las=1)
 dev.off()
-png(filename=paste(resultsdir, dataname, "-ScatterSinceIncept.png", sep=""), units="in", height=5.5, width=4.5, res=96) 
-chart.RiskReturnScatter(edhec.R, main="EDHEC Index Since Inception Performance", colorset=rainbow8equal, ylim=c(0,.2), xlim=c(0,.12))
+CairoPDF(file=paste(resultsdir, dataname, "-ScatterSinceIncept.pdf", sep=""), height=5.5, width=4.5) 
+chart.RiskReturnScatter(edhec.R, main="EDHEC Index Since Inception Performance", colorset=wb13color, ylim=c(0,.2), xlim=c(0,.12), las=1)
 dev.off()
 
 # --------------------------------------------------------------------
 # Table of Return and Risk Statistics
 # --------------------------------------------------------------------
 # @TODO: Too small, break into two panels?
-require(Hmisc)
 source(paste(functionsdir,'table.RiskStats.R', sep=""))
 incept.stats = t(table.RiskStats(R=R, p=p, Rf=Rf))
 write.csv(incept.stats, file=paste(resultsdir, dataname, "-inception-stats.csv", sep=""))
-png(filename=paste(resultsdir, dataname, "-InceptionStats.png", sep=""), units="in", height=5.5, width=9, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-InceptionStats.pdf", sep=""), height=5.5, width=4.5) 
 textplot(format.df(incept.stats, na.blank=TRUE, numeric.dollar=FALSE, cdec=c(3,3,1,3,1,3,3,1,3,3,1,1,3,3,1,0), rmar = 0.8, cmar = 1,  max.cex=.9, halign = "center", valign = "top", row.valign="center", wrap.rownames=20, wrap.colnames=10, mar = c(0,0,4,0)+0.1))
 dev.off()
 
@@ -134,38 +172,34 @@
 # Compare Distributions
 # --------------------------------------------------------------------
 # 
-png(filename=paste(resultsdir, dataname, "-Distributions.png", sep=""), units="in", height=5.5, width=9, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-Distributions.pdf", sep=""), height=5.5, width=9) 
 #source(paste(functionsdir, "/page.Distributions", sep=""))
-page.Distributions(R[,1:4])
+page.Distributions(R[,1:4], colorset=wb13color, las=1)
 dev.off()
 
-png(filename=paste(resultsdir, dataname, "-Distributions2.png", sep=""), units="in", height=5.5, width=9, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-Distributions2.pdf", sep=""), height=5.5, width=9) 
 #source(paste(functionsdir, "/page.Distributions", sep=""))
-page.Distributions(R[,5:7])
+page.Distributions(R[,5:7], colorset=wb13color, las=1)
 dev.off()
 
 # --------------------------------------------------------------------
 # Correlation Panels
 # --------------------------------------------------------------------
-# col3 <- colorRampPalette(c("darkgreen", "white", "darkred"))
-library(gplots)
-# Generate some color choices for the scale
-skewedWB20 = c(colorpanel(16, "#008566","#E1E56D"), colorpanel(5, "#E1E56D", "#742414")[-1])
-       
+# col3 <- colorRampPalette(c("darkgreen", "white", "darkred"))       
 M <- cor(R)
 colnames(M) = rownames(M) 
 order.hc2 <- corrMatOrder(M, order="hclust", hclust.method="complete")
 M.hc2 <- M[order.hc2,order.hc2]
-png(filename=paste(resultsdir, dataname, "-cor-inception.png", sep=""), units="in", height=5.5, width=4.5, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-cor-inception.pdf", sep=""), height=5.5, width=4.5) 
 corrplot(M.hc2, tl.col="black", tl.cex=0.8, method="shade", col=skewedWB20, cl.offset=.75, cl.cex=.7, cl.align.text="l", cl.ratio=.25, shade.lwd=0, cl.length=11)
 corrRect.hclust(M.hc2, k=3, method="complete", col="blue")
 dev.off()
 
 M36 <- cor(last(R,36))
-colnames(M36) = rownames(M36) = row.names
+colnames(M36) = rownames(M36) = rownames(M)
 order36.hc2 <- corrMatOrder(M36, order="hclust", hclust.method="complete")
 M36.hc2 <- M36[order36.hc2,order36.hc2]
-png(filename=paste(resultsdir, dataname, "-cor-tr36m.png", sep=""), units="in", height=5.5, width=4.5, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-cor-tr36m.pdf", sep=""), height=5.5, width=4.5) 
 corrplot(M36.hc2, tl.col="black", tl.cex=0.8, method="shade", col=skewedWB20, cl.offset=.75, cl.cex=.7, cl.align.text="l", cl.ratio=.25, shade.lwd=0, cl.length=11)
 corrRect.hclust(M36.hc2, k=3, method="complete", col="blue")
 dev.off()
@@ -178,14 +212,15 @@
   colnames(M)=R.foldednames
 # write(print(xtable(M, digits=1, align=rep("c",8)), type="html", html.table.attributes = "border = '0', align = 'center'"), file=paste(resultsdir, dataname, "-inception-cor.html", sep=""))
 # write(print(xtable(M, digits=1, align=rep("c",8))), file=paste(resultsdir, dataname, "-inception-cor.latex", sep=""))
-write(pandoc.table.return(M, digits=1, split.tables=Inf), file=paste(resultsdir, dataname, "-inception-cor.md", sep=""))
+# write(pandoc.table.return(M, digits=1, split.tables=Inf), file=paste(resultsdir, dataname, "-inception-cor.md", sep=""))
   
 # --------------------------------------------------------------------
 # Rolling Correlation to S&P500 TR
 # --------------------------------------------------------------------
 
-png(filename=paste(resultsdir, dataname, "-RollCorr.png", sep=""), units="in", height=5.5, width=9, res=96) 
-chart.RollingCorrelation(R,SP500.TR, width=24, legend.loc="bottomleft", colorset=rainbow8equal, main="Rolling 24-Month Correlations")
+CairoPDF(file=paste(resultsdir, dataname, "-RollCorr.pdf", sep=""), height=5.5, width=9) 
+chart.RollingCorrelation(R,SP500.TR, width=24, legend.loc=NULL, colorset=wb13color, main="Rolling 24-Month Correlation to S&P500 TR", las=1)
+legend("bottomleft", legend=colnames(R), inset = 0.02, border.col="darkgrey", lwd=3, col=wb13color, cex=0.7, y.intersp=1.1)
 dev.off()
        
 # --------------------------------------------------------------------
@@ -195,15 +230,15 @@
 # require(Hmisc)
 AC.stats = t(table.Autocorrelation(R=R))
 write.csv(AC.stats, file=paste(resultsdir, dataname, "-AC-stats.csv", sep=""))
-png(filename=paste(resultsdir, dataname, "-ACStats.png", sep=""), units="in", height=5.5, width=9, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-ACStats.pdf", sep=""), height=5.5, width=4.5) 
 # sort by p-value
 AC.order = order(AC.stats[,7], decreasing=FALSE)
 textplot(format.df(AC.stats[AC.order,], na.blank=TRUE, numeric.dollar=FALSE, rdec=c(rep(4,dim(AC.stats)[1])), col.just=rep("nc",dim(AC.stats)[2])), rmar = 0.7, cmar = 0.9, max.cex=1, halign = "center", valign = "center", row.valign="center", wrap.rownames=50, wrap.colnames=10)
 dev.off()
 
-png(filename=paste(resultsdir, dataname, "-ACStackedBars.png", sep=""), units="in", height=5.5, width=9, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-ACStackedBars.pdf", sep=""), height=5.5, width=9) 
 rownames(AC.stats)= sapply(colnames(R), function(x) paste(strwrap(x,10), collapse = "\n"), USE.NAMES=FALSE)
-chart.StackedBar(as.matrix(AC.stats[,1:6]), colorset=bluemono, main="Observed Autocorrelation")
+chart.StackedBar(as.matrix(AC.stats[,1:6]), colorset=bluemono, main="Observed Autocorrelation", las=1)
 dev.off()
        
 # --------------------------------------------------------------------
@@ -211,16 +246,15 @@
 # --------------------------------------------------------------------
 # Requires a recent modification to the chart in PerformanceAnalytics to make the y-axes match; in  revision 3191
 source('./R/chart.VaRSensitivity.R')
-png(filename=paste(resultsdir, dataname, "-ETL-sensitivity.png", sep=""), units="in", height=5.5, width=9, res=96)
-op <- par(no.readonly = TRUE)
+CairoPDF(file=paste(resultsdir, dataname, "-ETL-sensitivity.pdf", sep=""), height=5.5, width=9)
 layout(matrix(c(1:8), nrow=2))
 par(mar = c(4, 4, 5, 2)+0.1) #c(bottom, left, top, right)
 for(i in 1:NCOL(R)){
-  chart.VaRSensitivity(R[,i], methods=c("ModifiedES","HistoricalES", "GaussianES"), legend.loc=NULL, clean=clean, colorset=c("orange", "black", "darkgray"), lty=c(2,1,2), lwd=3, main=R.names[i], ylim=c(-0.09,0), ylab="Expected Tail Loss")
-  abline(v = 1-1/12, col = "red", lty = 2, lwd=1)
+  chart.VaRSensitivity(R[,i], methods=c("ModifiedES","HistoricalES", "GaussianES"), legend.loc=NULL, clean="boudt", colorset=wb13color, lty=c(2,1,2), lwd=3, main=R.names[i], ylim=c(-0.09,0), ylab="Expected Tail Loss", las=1) #c("orange", "black", "darkgray")
+  abline(v = 1-1/12, col = wb13color[11], lty = 2, lwd=1)
 }
   plot.new()
-  legend("center", legend=c("Modified \nETL","Historical \nETL", "Gaussian \nETL"), lty=c(2,1,2), lwd=3, col=c("orange", "black", "darkgray"), cex=1.2, y.intersp=2)
+  legend("center", legend=c("Modified \nETL","Historical \nETL", "Gaussian \nETL"), lty=c(2,1,2), lwd=3, col=wb13color, cex=1.2, y.intersp=2, box.col="darkgrey")
 par(op)
 dev.off()
   

Modified: pkg/PortfolioAnalytics/sandbox/symposium2013/optimize.HFindexes.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/symposium2013/optimize.HFindexes.R	2013-10-05 04:15:16 UTC (rev 3206)
+++ pkg/PortfolioAnalytics/sandbox/symposium2013/optimize.HFindexes.R	2013-10-05 13:36:19 UTC (rev 3207)
@@ -104,6 +104,7 @@
 MeanmETL.portf <- add.objective(portfolio=init.portf,
                                 type="return", # the kind of objective this is
                                 name="mean" # name of the function
+  , multiplier=-12
                                 )
 MeanmETL.portf <- add.objective(portfolio=MeanmETL.portf,
                                 type="risk", # the kind of objective this is
@@ -239,9 +240,25 @@
 
 # Modify the init.portf specification to get RP running 
 rp.portf <- init.portf
-# rp.portf$constraints[[1]]$min_sum = 0.99 # set to speed up RP
-# rp.portf$constraints[[1]]$max_sum = 1.01
+rp.portf$constraints[[1]]$min_sum = 0.99 # set to speed up RP
+rp.portf$constraints[[1]]$max_sum = 1.01
 # rp = random_portfolios(portfolio=rp.portf, permutations=10000, max_permutations=400)
+rp1 = random_portfolios(portfolio=rp.portf, permutations=10000, max_permutations=400, rp_method="sample")
+rp1.mean = apply(rp1, 1, function(w) mean(R %*% w))
+rp1.sd = apply(rp1, 1, function(x) StdDev(R=R, weights=x, p=p))
+rp1.etl=NULL; for(i in 1:NROW(rp1)) {rp1.etl[i]=ETL(R=R, weights=as.vector(rp1[i,]), p=p, portfolio_method="component")[[1]]}
+plot(rp1.sd, rp1.mean, col="gray", cex=0.5)
+
+rp2 = random_portfolios(portfolio=rp.portf, permutations=10000, max_permutations=400, rp_method="simplex", fev=2)
+rp2.mean = apply(rp2, 1, function(w) mean(R %*% w))
+rp2.sd = apply(rp2, 1, function(x) StdDev(R=R, weights=x, p=p))
+points(rp2.sd,rp2.mean, col="blue", cex=0.5)
+
+rp3 = random_portfolios(portfolio=rp.portf, permutations=10000, max_permutations=400, rp_method="grid")
+rp3.mean = apply(rp3, 1, function(w) mean(R %*% w))
+rp3.sd = apply(rp3, 1, function(x) StdDev(R=R, weights=x, p=p))
+points(rp3.sd,rp3.mean, col="green", cex=0.5)
+
 # print(paste('done constructing random portfolios at',Sys.time()))
 # save(rp,file=paste(resultsdir, 'random-portfolios-', Sys.Date(), '-', runname, '.rda',sep=''))
 load(file=paste(resultsdir,'random-portfolios-2013-09-28.historical.moments.rda'))
@@ -277,6 +294,13 @@
 #                                  rp=rp,
 #                                  trace=TRUE
 # ) 
+# OR with random portfolios
+MeanmETL.DE<-optimize.portfolio(R=R,
+                                 portfolio=MeanmETL.portf,
+                                 optimize_method='DEoptim',
+                                 search_size=20000
+) 
+
 # plot(MeanmETL.RND, risk.col="StdDev", return.col="mean", chart.assets=TRUE, main="Mean-mETL Portfolio")
 # plot(MeanmETL.RND, risk.col="ES", return.col="mean", chart.assets=TRUE, main="Mean-mETL Portfolio")
 
@@ -316,18 +340,25 @@
   rp=rp,
   trace=TRUE
   ) 
+
+EqSD.DE<-optimize.portfolio(R=R,
+  portfolio=EqSD.portf,
+  optimize_method='DEoptim',
+  search_size=20000,
+  trace=FALSE
+  ) 
 plot(EqSD.RND, risk.col="StdDev", return.col="mean", chart.assets=TRUE, main="Equal Volatility Contribution Portfolio")
 chart.RiskBudget(EqSD.RND, risk.type="percentage", neighbors=25)
 save(EqSD.RND,file=paste(resultsdir, 'EqSD.RND-', Sys.Date(), '-', runname, '.rda',sep=''))
 
 
 # or with DE
-# EqSD.DE<-optimize.portfolio(R=R,
-#   portfolio=EqSD.portf,
-#   optimize_method='DEoptim',
-#   search_size=1000, 
-#   trace=TRUE, verbose=TRUE
-# ) 
+EqSD.DE<-optimize.portfolio(R=R,
+  portfolio=EqSD.portf,
+  optimize_method='DEoptim',
+  search_size=1000#, 
+  #trace=TRUE, verbose=TRUE
+) 
 # plot(EqSD.DE, risk.col="StdDev", return.col="mean", chart.assets=TRUE, main="Equal Volatility Contribution Portfolio")
 # chart.RiskBudget(EqSD.DE, risk.type="percentage")
 # save(EqSD.DE,file=paste(resultsdir, 'EqSD.DE-', Sys.Date(), '-', runname, '.rda',sep=''))

Modified: pkg/PortfolioAnalytics/sandbox/symposium2013/results.HFindexes.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/symposium2013/results.HFindexes.R	2013-10-05 04:15:16 UTC (rev 3206)
+++ pkg/PortfolioAnalytics/sandbox/symposium2013/results.HFindexes.R	2013-10-05 13:36:19 UTC (rev 3207)
@@ -6,19 +6,19 @@
 # Plot Ex Ante scatter of RP and ONLY Equal Weight portfolio in StdDev space
 # --------------------------------------------------------------------
 # Done
-png(filename=paste(resultsdir, "RP-EqWgt-MeanSD-ExAnte.png", sep=""), units="in", height=5.5, width=9, res=96) 
-par(mar=c(5, 4, 1, 2) + 0.1) #c(bottom, left, top, right)
+CairoPDF(file=paste(resultsdir, dataname, "-RP-EqWgt-MeanSD-ExAnte.pdf", sep=""), height=6, width=9)  
+par(mar=c(5, 5, 1, 2) + 0.1) #c(bottom, left, top, right)
 # Calculate chart bounds to unify with the charts below
 xlim.StdDev=c(min(c(xtract[,"StdDev"], buoys.portfmeas[,"StdDev"])), max(c(xtract[,"StdDev"], buoys.portfmeas[,"StdDev"])))
 ylim.mean=c(min(c(xtract[,"mean"], buoys.portfmeas[,"Mean"])), max(c(xtract[,"mean"], buoys.portfmeas[,"Mean"])))
 
-plot(xtract[,"StdDev"],xtract[,"mean"], xlab="Ex Ante Std Dev", ylab="Ex Ante Mean", col="darkgray", axes=FALSE, main="", cex=.7, xlim=xlim.StdDev, ylim=ylim.mean)
+plot(xtract[,"StdDev"],xtract[,"mean"], xlab="Ex Ante Std Dev", ylab="Ex Ante Mean", col="darkgray", axes=FALSE, main="", cex=.6, xlim=xlim.StdDev, ylim=ylim.mean)
 grid(col = "darkgray")
 abline(h = 0, col = "darkgray")
 # Overplot the equal weight portfolio
 points(buoys.portfmeas[8,"StdDev"],buoys.portfmeas[8,"Mean"], col=tol8qualitative[8], pch=16, cex=1.5) # watch the order in portfmeas
-axis(1, cex.axis = 0.8, col = "darkgray")
-axis(2, cex.axis = 0.8, col = "darkgray")
+axis(1, cex.axis = 0.8, col = "darkgray", las=1)
+axis(2, cex.axis = 0.8, col = "darkgray", las=1)
 box(col = "darkgray")
 legend("bottomright",legend=results.names[8], col=tol8qualitative[8], pch=16, ncol=1,  border.col="darkgray", y.intersp=1.2, cex=0.8, inset=.02)
 par(op)
@@ -28,12 +28,12 @@
 # Plot Ex Ante scatter of RP and ASSET portfolios in StdDev space
 # --------------------------------------------------------------------
 # @TODO: add the assets to this chart
-png(filename=paste(resultsdir, "RP-Assets-MeanSD-ExAnte.png", sep=""), units="in", height=5.5, width=9, res=96) 
+CairoPDF(file=paste(resultsdir, dataname, "-RP-Assets-MeanSD-ExAnte.pdf", sep=""), height=6, width=9)  
 xlim.StdDev.assets =c(min(c(xtract[,"StdDev"], assets.portfmeas[,"StdDev"], 0)), max(c(xtract[,"StdDev"], assets.portfmeas[,"StdDev"],0.03)))
 ylim.mean.assets =c(min(c(xtract[,"mean"], assets.portfmeas[,"Mean"], 0)), max(c(xtract[,"mean"], assets.portfmeas[,"Mean"])))
-par(mar=c(5, 4, 1, 2) + 0.1) #c(bottom, left, top, right)
+par(mar=c(5, 5, 1, 2) + 0.1) #c(bottom, left, top, right)
 # Revise the chart bounds to include the asssets
-plot(xtract[,"StdDev"],xtract[,"mean"], xlab="Ex Ante mETL", ylab="Ex Ante Mean", col="darkgray", axes=FALSE, main="", cex=.7, xlim=xlim.StdDev.assets, ylim=ylim.mean.assets)
+plot(xtract[,"StdDev"],xtract[,"mean"], xlab="Ex Ante mETL", ylab="Ex Ante Mean", col="darkgray", axes=FALSE, main="", cex=.6, xlim=xlim.StdDev.assets, ylim=ylim.mean.assets)
 grid(col = "darkgray")
 abline(h = 0, col = "darkgray")
 abline(v = 0, col = "darkgray")
@@ -42,8 +42,8 @@
 text(x=buoys.portfmeas[8,"StdDev"], y=buoys.portfmeas[8,"Mean"], labels=rownames(buoys.portfmeas)[8], pos=4, cex=1)
 points(assets.portfmeas[,"StdDev"],assets.portfmeas[,"Mean"], col=rich8equal, pch=18, cex=1.5) # watch the order in portfmeas
 text(x=assets.portfmeas[,"StdDev"], y=assets.portfmeas[,"Mean"], labels=rownames(assets.portfmeas), pos=4, cex=1)
-axis(1, cex.axis = 0.8, col = "darkgray")
-axis(2, cex.axis = 0.8, col = "darkgray")
+axis(1, cex.axis = 0.7, col = "darkgray")
+axis(2, cex.axis = 0.7, col = "darkgray")
 box(col = "darkgray")
 #legend("right",legend=rownames(assets.portfmeas), col=rich8equal, pch=16, ncol=1,  border.col="darkgray", y.intersp=1.2, cex=0.8, inset=.02)
 par(op)
@@ -53,15 +53,15 @@
 # Plot Ex Ante scatter of RP and BUOY portfolios in StdDev space
 # --------------------------------------------------------------------
 # Done
-png(filename=paste(resultsdir, "RP-BUOY-MeanSD-ExAnte.png", sep=""), units="in", height=5.5, width=9, res=96) 
-par(mar=c(5, 4, 1, 2) + 0.1) #c(bottom, left, top, right)
-plot(xtract[,"StdDev"],xtract[,"mean"], xlab="Ex Ante Std Dev", ylab="Ex Ante Mean", col="darkgray", axes=FALSE, main="", cex=.7, xlim=xlim.StdDev, ylim=ylim.mean)
+CairoPDF(file=paste(resultsdir, dataname, "-RP-BUOY-MeanSD-ExAnte.pdf", sep=""), height=6, width=9)  
+par(mar=c(5, 5, 1, 2) + 0.1) #c(bottom, left, top, right)
+plot(xtract[,"StdDev"],xtract[,"mean"], xlab="Ex Ante Std Dev", ylab="Ex Ante Mean", col="darkgray", axes=FALSE, main="", cex=.6, xlim=xlim.StdDev, ylim=ylim.mean)
 grid(col = "darkgray")
 abline(h = 0, col = "darkgray")
 # Overplot the buoy portfolios
 points(buoys.portfmeas[,"StdDev"],buoys.portfmeas[,"Mean"], col=tol8qualitative, pch=16, cex=1.5) # watch the order in portfmeas
-axis(1, cex.axis = 0.8, col = "darkgray")
-axis(2, cex.axis = 0.8, col = "darkgray")
+axis(1, cex.axis = 0.7, col = "darkgray")
+axis(2, cex.axis = 0.7, col = "darkgray")
 box(col = "darkgray")
 legend("bottomright",legend=results.names, col=tol8qualitative, pch=16, ncol=1,  border.col="darkgray", y.intersp=1.2, cex=0.8, inset=.02)
 par(op)
@@ -71,15 +71,15 @@
 # Plot Ex Ante scatter of RP and BUOY portfolios in mETL space
 # --------------------------------------------------------------------
 # Done
-png(filename=paste(resultsdir, "RP-BUOYS-mETL-ExAnte.png", sep=""), units="in", height=5.5, width=9, res=96) 
-par(mar=c(5, 4, 1, 2) + 0.1) #c(bottom, left, top, right)
+CairoPDF(file=paste(resultsdir, dataname, "-RP-BUOYS-mETL-ExAnte.pdf", sep=""), height=6, width=9)  
+par(mar=c(5, 5, 1, 2) + 0.1) #c(bottom, left, top, right)
 xlim.ES=c(min(c(xtract[,"ES"], buoys.portfmeas[,"mETL"])), max(c(xtract[,"ES"], buoys.portfmeas[,"mETL"])))
-plot(xtract[,"ES"],xtract[,"mean"], xlab="Ex Ante mETL", ylab="Ex Ante Mean", col="darkgray", axes=FALSE, main="", cex=.7, xlim=xlim.ES, ylim=ylim.mean)
+plot(xtract[,"ES"],xtract[,"mean"], xlab="Ex Ante mETL", ylab="Ex Ante Mean", col="darkgray", axes=FALSE, main="", cex=.6, xlim=xlim.ES, ylim=ylim.mean)
 grid(col = "darkgray")
 # Overplot the buoy portfolios
 points(buoys.portfmeas[,"mETL"],buoys.portfmeas[,"Mean"], col=tol8qualitative, pch=16, cex=1.5) # watch the order in portfmeas
-axis(1, cex.axis = 0.8, col = "darkgray")
-axis(2, cex.axis = 0.8, col = "darkgray")
+axis(1, cex.axis = 0.7, col = "darkgray")
+axis(2, cex.axis = 0.7, col = "darkgray")
 box(col = "darkgray")
 legend("bottomright",legend=results.names, col=tol8qualitative, pch=16, ncol=1,  border.col="darkgray", y.intersp=1.2, cex=0.8, inset=.02)
 par(op)
@@ -91,7 +91,7 @@
 # Done
 source('./R/chart.UnStackedBar.R')
 # Wgts = extractWeights(buoys)
-png(filename=paste(resultsdir, "Weights-Buoys.png", sep=""), units="in", height=5.5, width=9, res=96)
+CairoPDF(file=paste(resultsdir, dataname, "-Weights-Buoys.png", sep=""), units="in", height=6, width=9, res=96)
 chart.UnStackedBar(t(Wgts), colorset=tol8qualitative, equal.line=TRUE)
 dev.off()
 
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/returnanalytics -r 3207


More information about the Returnanalytics-commits mailing list