[Returnanalytics-commits] r2921 - in pkg/PortfolioAnalytics: R sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 29 03:30:00 CEST 2013


Author: rossbennett34
Date: 2013-08-29 03:29:57 +0200 (Thu, 29 Aug 2013)
New Revision: 2921

Modified:
   pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
   pkg/PortfolioAnalytics/R/extract.efficient.frontier.R
   pkg/PortfolioAnalytics/sandbox/testing_efficient_frontier.R
Log:
Renaming the class of the actual frontier data in efficient.frontier objects. Omitting the hardcoded pch arg in calls to plot for efficient frontier charts. Extending examples for testing_efficient_frontier file.

Modified: pkg/PortfolioAnalytics/R/charts.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/charts.efficient.frontier.R	2013-08-29 00:21:38 UTC (rev 2920)
+++ pkg/PortfolioAnalytics/R/charts.efficient.frontier.R	2013-08-29 01:29:57 UTC (rev 2921)
@@ -120,7 +120,7 @@
   }
   
   # plot a scatter of the assets
-  plot(x=asset_risk, y=asset_ret, xlab=match.col, ylab="mean", main=main, xlim=xlim, ylim=ylim, pch=5, axes=FALSE, ...)
+  plot(x=asset_risk, y=asset_ret, xlab=match.col, ylab="mean", main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...)
   text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=0.8)
   # plot the efficient line
   lines(x=x.f, y=y.f, col="darkgray", lwd=2)
@@ -205,7 +205,7 @@
   }
   
   # plot a scatter of the assets
-  plot(x=asset_risk, y=asset_ret, xlab=match.col, ylab="mean", main=main, xlim=xlim, ylim=ylim, pch=5, axes=FALSE, ...)
+  plot(x=asset_risk, y=asset_ret, xlab=match.col, ylab="mean", main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...)
   text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=0.8)
   # plot the efficient line
   lines(x=x.f, y=y.f, col="darkgray", lwd=2)
@@ -403,7 +403,7 @@
   }
   
   # plot the efficient frontier line
-  plot(x=frontier[, mtc], y=frontier[, mean.mtc], ylab="mean", xlab=match.col, main=main, xlim=xlim, ylim=ylim, pch=5, axes=FALSE, ...)
+  plot(x=frontier[, mtc], y=frontier[, mean.mtc], ylab="mean", xlab=match.col, main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...)
   if(chart.assets){
     # risk-return scatter of the assets
     points(x=asset_risk, y=asset_ret)

Modified: pkg/PortfolioAnalytics/R/extract.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/extract.efficient.frontier.R	2013-08-29 00:21:38 UTC (rev 2920)
+++ pkg/PortfolioAnalytics/R/extract.efficient.frontier.R	2013-08-29 01:29:57 UTC (rev 2921)
@@ -92,7 +92,7 @@
     }
     # combine the stats from the optimal portfolio to result matrix
     result <- rbind(opt, result)
-    return(structure(result, class="efficient.frontier"))
+    return(structure(result, class="frontier"))
 }
 
 #' Generate the efficient frontier for a mean-variance portfolio
@@ -174,7 +174,7 @@
     extractStats(optimize.portfolio(R=R, portfolio=portfolio, optimize_method="ROI"))
   }
   colnames(out) <- names(stats)
-  return(structure(out, class="efficient.frontier"))
+  return(structure(out, class="frontier"))
 }
 
 #' Generate the efficient frontier for a mean-etl portfolio
@@ -258,7 +258,7 @@
     extractStats(optimize.portfolio(R=R, portfolio=portfolio, optimize_method="ROI"))
   }
   colnames(out) <- names(stats)
-  return(structure(out, class="efficient.frontier"))
+  return(structure(out, class="frontier"))
 }
 
 #' create an efficient frontier
@@ -415,6 +415,6 @@
   return(structure(list(call=call,
                         frontier=frontier,
                         R=R,
-                        portfolio=portfolio), class="efficient.frontier"))
+                        portfolio=portf), class="efficient.frontier"))
 }
 

Modified: pkg/PortfolioAnalytics/sandbox/testing_efficient_frontier.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_efficient_frontier.R	2013-08-29 00:21:38 UTC (rev 2920)
+++ pkg/PortfolioAnalytics/sandbox/testing_efficient_frontier.R	2013-08-29 01:29:57 UTC (rev 2921)
@@ -38,27 +38,48 @@
 # mean-var efficient frontier
 meanvar.ef <- create.EfficientFrontier(R=R, portfolio=meanvar.portf, type="mean-StdDev")
 print(meanvar.ef)
-summary(meanvar.ef)
-chart.EfficientFrontier(meanvar.ef, match.col="StdDev", type="b")
-chart.EfficientFrontier(meanvar.ef, match.col="StdDev", type="l", rf=0)
+summary(meanvar.ef, digits=2)
+meanvar.ef$frontier
+# The RAR.text argument can be used for the risk-adjusted-return name on the legend,
+# by default it is 'Modified Sharpe Ratio'
+chart.EfficientFrontier(meanvar.ef, match.col="StdDev", type="b", RAR.text="Sharpe Ratio")
+# The tangency portfolio and line are plotted by default, these can be ommitted
+# by setting rf=NULL
+chart.EfficientFrontier(meanvar.ef, match.col="StdDev", type="l", rf=NULL)
 chart.Weights.EF(meanvar.ef, colorset=bluemono, match.col="StdDev")
 
 # run optimize.portfolio and chart the efficient frontier for that object
 opt_meanvar <- optimize.portfolio(R=R, portfolio=meanvar.portf, optimize_method="ROI", trace=TRUE)
-chart.EfficientFrontier(opt_meanvar, match.col="StdDev", n.portfolios=50)
+
+# The efficient frontier is created from the 'opt_meanvar' object by getting
+# The portfolio and returns objects and then passing those to create.EfficientFrontier
+chart.EfficientFrontier(opt_meanvar, match.col="StdDev", n.portfolios=25)
+
+# Rerun the optimization with a new risk aversion parameter to change where the
+# portfolio is along the efficient frontier. The 'optimal' portfolio plotted on
+# the efficient frontier is the optimal portfolio returned by optimize.portfolio.
+meanvar.portf$objectives[[2]]$risk_aversion=0.25
+opt_meanvar <- optimize.portfolio(R=R, portfolio=meanvar.portf, optimize_method="ROI", trace=TRUE)
+chart.EfficientFrontier(opt_meanvar, match.col="StdDev", n.portfolios=25)
+
 # The weights along the efficient frontier can be plotted by passing in the
 # optimize.portfolio output object
 chart.Weights.EF(opt_meanvar, match.col="StdDev")
-# or we can extract the efficient frontier and then plot it
+
+# Extract the efficient frontier and then plot it
+# Note that if you want to do multiple charts of the efficient frontier from
+# the optimize.portfolio object, it is best to extractEfficientFrontier as shown
+# below
 ef <- extractEfficientFrontier(object=opt_meanvar, match.col="StdDev", n.portfolios=15)
 print(ef)
+summary(ef, digits=5)
 chart.Weights.EF(ef, match.col="StdDev", colorset=bluemono)
 
 # mean-etl efficient frontier
 meanetl.ef <- create.EfficientFrontier(R=R, portfolio=meanetl.portf, type="mean-ES")
 print(meanetl.ef)
 summary(meanetl.ef)
-chart.EfficientFrontier(meanetl.ef, match.col="ES", main="mean-ETL Efficient Frontier", type="l", col="blue")
+chart.EfficientFrontier(meanetl.ef, match.col="ES", main="mean-ETL Efficient Frontier", type="l", col="blue", RAR.text="STARR")
 chart.Weights.EF(meanetl.ef, colorset=bluemono, match.col="ES")
 
 # mean-etl efficient frontier using random portfolios
@@ -86,7 +107,8 @@
 
 # group constraints (also add long only constraints to the group portfolio)
 group.portf <- add.constraint(portfolio=init.portf, type="group", 
-                              groups=c(2, 3), 
+                              groups=list(groupA=c(1, 3),
+                                          groupB=c(2, 4, 5)),
                               group_min=c(0.25, 0.15), 
                               group_max=c(0.75, 0.55))
 group.portf <- add.constraint(portfolio=group.portf, type="long_only")



More information about the Returnanalytics-commits mailing list