[Returnanalytics-commits] r2843 - in pkg/PortfolioAnalytics: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 21 07:01:14 CEST 2013


Author: rossbennett34
Date: 2013-08-21 07:01:14 +0200 (Wed, 21 Aug 2013)
New Revision: 2843

Modified:
   pkg/PortfolioAnalytics/R/extract.efficient.frontier.R
   pkg/PortfolioAnalytics/man/extract.efficient.frontier.Rd
Log:
Modifying extract.efficient.frontier to accept an objected created by optimize.portfolio or pass in a portfolio object. Also modified function to get the efficient frontier at the maximum return instead of minimum out

Modified: pkg/PortfolioAnalytics/R/extract.efficient.frontier.R
===================================================================
--- pkg/PortfolioAnalytics/R/extract.efficient.frontier.R	2013-08-21 00:46:37 UTC (rev 2842)
+++ pkg/PortfolioAnalytics/R/extract.efficient.frontier.R	2013-08-21 05:01:14 UTC (rev 2843)
@@ -10,46 +10,48 @@
 #
 ###############################################################################
 
-#' extract the efficient frontier of portfolios that meet your objectives over a range of risks
+#' Extract the efficient frontier of portfolios that meet your objectives over a range of risks
 #' 
-#' note that this function will be extremely sensitive to the objectives in your
-#' \code{\link{constraint}} object.  It will be especially obvious if you 
+#' The efficient frontier is extracted from the set of portfolios created by 
+#' \code{optimize.portfolio} with \code{trace=TRUE}.
+#' 
+#' If you do not have an optimal portfolio object created by 
+#' \code{\link{optimize.portfolio}}, you can pass in a portfolio object and an
+#' optimization will be run via \code{\link{optimize.portfolio}}
+#' 
+#' @note
+#' Note that this function will be extremely sensitive to the objectives in your
+#' \code{\link{portfolio}} object.  It will be especially obvious if you 
 #' are looking at a risk budget objective and your return preference is not set high enough.
 #' 
-#' If you do not have a set of portfolios to extract from, portfolios may be generated automatically, which would take a very long time.
 #'  
-#' @param portfolios set of portfolios as generated by \code{\link{extractStats}}
+#' @param object optimial portfolio object as created by \code{\link{optimize.portfolio}}
 #' @param from minimum value of the sequence
 #' @param to maximum value of the sequence
 #' @param by number to increment the sequence by
 #' @param match.col string name of column to use for risk (horizontal axis)
-#' @param \dots any other passthru parameters
+#' @param \dots any other passthru parameters to \code{optimize.portfolio}
 #' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns
-#' @param constraints an object of type "constraints" specifying the constraints for the optimization, see \code{\link{constraint}}
-#' @param optimize_method one of "DEoptim" or "random"
+#' @param portfolio an object of type "portfolio" specifying the constraints and objectives for the optimization, see \code{\link{portfolio.spec}}
+#' @param optimize_method one of "DEoptim", "random", "ROI", "pso", or "GenSA"
 #' @export
-extract.efficient.frontier <- 
-function (portfolios=NULL, 
-          match.col='ES', from=0, to=1, by=.005, 
-          ..., 
-          R = NULL, 
-          constraints = NULL, 
-          optimize_method='random')
+extract.efficient.frontier <- function (object=NULL, match.col='ES', from=0, to=1, by=0.005, ..., R=NULL, portfolio=NULL, optimize_method='random')
 {
     #TODO add a threshold argument for how close it has to be to count
     # do we need to recalc the constrained_objective too?  I don't think so.
+    if(!inherits(object, "optimize.portfolio")) stop("object passed in must of of class 'portfolio'")
     
     set<-seq(from=from,to=to,by=by)
     set<-cbind(quantmod::Lag(set,1),as.matrix(set))[-1,]
-    if(is.null(portfolios)){
-        if(!is.null(R)){
-            portfolios<-optimize.portfolio(constraints=constraints, R=R, optimize_method=optimize_method, ...)
+    if(is.null(object)){
+        if(!is.null(R) & !is.null(portfolio)){
+            portfolios<-optimize.portfolio(portfolio=portfolio, R=R, optimize_method=optimize_method[1], trace=TRUE, ...)
         } else {
-            stop('you must specify either a portfolio set or a return series')
+            stop('you must specify a portfolio object and a return series or an objective of class optimize.portfolio')
         }
     }
     
-    xtract<-extractStats(portfolios)
+    xtract<-extractStats(object)
     columnnames=colnames(xtract)
     #if("package:multicore" %in% search() || require("multicore",quietly = TRUE)){
     #    mclapply
@@ -67,7 +69,7 @@
     result <- foreach(i=1:nrow(set),.inorder=TRUE, .combine=rbind, .errorhandling='remove') %do% {
         tmp<-xtract[which(xtract[,mtc]>=set[i,1] & xtract[,mtc]<set[i,2]),]
         tmp<-tmp[which.min(tmp[,'out']),]
-        tmp
+        #tmp
     }
     return(result)
-}
\ No newline at end of file
+}

Modified: pkg/PortfolioAnalytics/man/extract.efficient.frontier.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/extract.efficient.frontier.Rd	2013-08-21 00:46:37 UTC (rev 2842)
+++ pkg/PortfolioAnalytics/man/extract.efficient.frontier.Rd	2013-08-21 05:01:14 UTC (rev 2843)
@@ -1,15 +1,14 @@
 \name{extract.efficient.frontier}
 \alias{extract.efficient.frontier}
-\title{extract the efficient frontier of portfolios that meet your objectives over a range of risks}
+\title{Extract the efficient frontier of portfolios that meet your objectives over a range of risks}
 \usage{
-  extract.efficient.frontier(portfolios = NULL,
+  extract.efficient.frontier(object = NULL,
     match.col = "ES", from = 0, to = 1, by = 0.005, ...,
-    R = NULL, constraints = NULL,
-    optimize_method = "random")
+    R = NULL, portfolio = NULL, optimize_method = "random")
 }
 \arguments{
-  \item{portfolios}{set of portfolios as generated by
-  \code{\link{extractStats}}}
+  \item{object}{optimial portfolio object as created by
+  \code{\link{optimize.portfolio}}}
 
   \item{from}{minimum value of the sequence}
 
@@ -20,27 +19,35 @@
   \item{match.col}{string name of column to use for risk
   (horizontal axis)}
 
-  \item{\dots}{any other passthru parameters}
+  \item{\dots}{any other passthru parameters to
+  \code{optimize.portfolio}}
 
   \item{R}{an xts, vector, matrix, data frame, timeSeries
   or zoo object of asset returns}
 
-  \item{constraints}{an object of type "constraints"
-  specifying the constraints for the optimization, see
-  \code{\link{constraint}}}
+  \item{portfolio}{an object of type "portfolio" specifying
+  the constraints and objectives for the optimization, see
+  \code{\link{portfolio.spec}}}
 
-  \item{optimize_method}{one of "DEoptim" or "random"}
+  \item{optimize_method}{one of "DEoptim", "random", "ROI",
+  "pso", or "GenSA"}
 }
 \description{
-  note that this function will be extremely sensitive to
-  the objectives in your \code{\link{constraint}} object.
+  The efficient frontier is extracted from the set of
+  portfolios created by \code{optimize.portfolio} with
+  \code{trace=TRUE}.
+}
+\details{
+  If you do not have an optimal portfolio object created by
+  \code{\link{optimize.portfolio}}, you can pass in a
+  portfolio object and an optimization will be run via
+  \code{\link{optimize.portfolio}}
+}
+\note{
+  Note that this function will be extremely sensitive to
+  the objectives in your \code{\link{portfolio}} object.
   It will be especially obvious if you are looking at a
   risk budget objective and your return preference is not
   set high enough.
 }
-\details{
-  If you do not have a set of portfolios to extract from,
-  portfolios may be generated automatically, which would
-  take a very long time.
-}
 



More information about the Returnanalytics-commits mailing list