[Returnanalytics-commits] r2078 - pkg/PortfolioAnalytics/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jun 26 09:52:43 CEST 2012
Author: hezkyvaron
Date: 2012-06-26 09:52:43 +0200 (Tue, 26 Jun 2012)
New Revision: 2078
Added:
pkg/PortfolioAnalytics/R/constraints_ROI.R
Modified:
pkg/PortfolioAnalytics/R/constraints.R
pkg/PortfolioAnalytics/R/optimize.portfolio.R
Log:
- moved constraint_ROI constructor to its own file.
- added namespace fix in optimize.portfolio()
- commented out constraint_ROI in constraints.R file
Modified: pkg/PortfolioAnalytics/R/constraints.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraints.R 2012-06-25 21:08:33 UTC (rev 2077)
+++ pkg/PortfolioAnalytics/R/constraints.R 2012-06-26 07:52:43 UTC (rev 2078)
@@ -210,27 +210,29 @@
}
-#' constructor for class constraint_ROI
-#'
-#' @param assets number of assets, or optionally a named vector of assets specifying seed weights
-#' @param op.problem an object of type "OP" (optimization problem, of \code{ROI}) specifying the complete optimization problem, see ROI help pages for proper construction of OP object.
-#' @param solver string argument for what solver package to use, must have ROI plugin installed for that solver. Currently support is for \code{\link{glpk}} and \code{\link{quadprog}}.
-#' @param weight_seq seed sequence of weights, see \code{\link{generatesequence}}
-#' @author Hezky Varon
-#' @export
-constraint_ROI <- function(assets, op.problem, solver=c("glpk", "quadprog"), weight_seq=NULL)
-{
- if(problem == NULL) stop("Need to pass in optimiztion problem.")
- return(structure(
- list(
- assets = assets,
- constrainted_objective = op.problem,
- solver = solver[1],
- weight_seq = weight_seq,
- objectives = list(),
- call = match.call()
- ),
- class=c("constraint_ROI","constraint")
- ))
-}
+# #' constructor for class constraint_ROI
+# #'
+# #' @param assets number of assets, or optionally a named vector of assets specifying seed weights
+# #' @param op.problem an object of type "OP" (optimization problem, of \code{ROI}) specifying the complete optimization problem, see ROI help pages for proper construction of OP object.
+# #' @param solver string argument for what solver package to use, must have ROI plugin installed for that solver. Currently support is for \code{glpk} and \code{quadprog}.
+# #' @param weight_seq seed sequence of weights, see \code{\link{generatesequence}}
+# #' @author Hezky Varon
+# #' @export
+# constraint_ROI <- function(assets, op.problem, solver=c("glpk", "quadprog"), weight_seq=NULL)
+# {
+# if(op.problem == NULL || inherits(op.problem, "OP")) {
+# stop("Need to pass in optimiztion problem of ROI:::OP type.")
+# if() stop("Need to be ROI:::OP")
+# return(structure(
+# list(
+# assets = assets,
+# constrainted_objective = op.problem,
+# solver = solver[1],
+# weight_seq = weight_seq,
+# objectives = list(),
+# call = match.call()
+# ),
+# class=c("constraint_ROI","constraint")
+# ))
+# }
Added: pkg/PortfolioAnalytics/R/constraints_ROI.R
===================================================================
--- pkg/PortfolioAnalytics/R/constraints_ROI.R (rev 0)
+++ pkg/PortfolioAnalytics/R/constraints_ROI.R 2012-06-26 07:52:43 UTC (rev 2078)
@@ -0,0 +1,60 @@
+#' constructor for class constraint_ROI
+#'
+#' @param assets number of assets, or optionally a named vector of assets specifying seed weights
+#' @param op.problem an object of type "OP" (optimization problem, of \code{ROI}) specifying the complete optimization problem, see ROI help pages for proper construction of OP object.
+#' @param solver string argument for what solver package to use, must have ROI plugin installed for that solver. Currently support is for \code{glpk} and \code{quadprog}.
+#' @param weight_seq seed sequence of weights, see \code{\link{generatesequence}}
+#' @author Hezky Varon
+#' @export
+constraint_ROI <- function(assets, op.problem, solver=c("glpk", "quadprog"), weight_seq=NULL)
+{
+ #
+ # Structure for this constructor function borrowed from "constraints.R"
+ #
+ if(op.problem == NULL || class(op.problem) != "OP")
+ stop("Need to pass in optimization problem of ROI:::OP object type.")
+
+ if (is.null(assets)) {
+ stop("You must specify the assets")
+ }
+
+ if(is.character(assets)){
+ nassets=length(assets)
+ assetnames=assets
+ message("assuming equal weighted seed portfolio")
+ assets<-rep(1/nassets,nassets)
+ names(assets)<-assetnames # set names, so that other code can access it,
+ # and doesn't have to know about the character vector
+ # print(assets)
+ }
+ if(!is.null(assets)){
+ # TODO FIXME this doesn't work quite right on matrix of assets
+ if(is.numeric(assets)){
+ if (length(assets) == 1) {
+ nassets=assets
+ #we passed in a number of assets, so we need to create the vector
+ message("assuming equal weighted seed portfolio")
+ assets<-rep(1/nassets,nassets)
+ } else {
+ nassets = length(assets)
+ }
+ # and now we may need to name them
+ if (is.null(names(assets))) {
+ for(i in 1:length(assets)){
+ names(assets)[i]<-paste("Asset",i,sep=".")
+ }
+ }
+ }
+
+ return(structure(
+ list(
+ assets = assets,
+ constrainted_objective = op.problem,
+ solver = solver[1],
+ weight_seq = weight_seq,
+ objectives = list(),
+ call = match.call()
+ ),
+ class=c("constraint_ROI","constraint")
+ ))
+}
\ No newline at end of file
Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R 2012-06-25 21:08:33 UTC (rev 2077)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R 2012-06-26 07:52:43 UTC (rev 2078)
@@ -244,7 +244,7 @@
# This will take a new constraint object that is of the same structure of a
# ROI constraint object, but with an additional solver arg.
# then we can do something like this
- roi.result <- ROI_solve(x=constraints$constrainted_objective, constraints$solver)
+ roi.result <- ROI:::ROI_solve(x=constraints$constrainted_objective, constraints$solver)
weights <- roi.result$solution
names(weights) <- colnames(R)
out$weights <- weights
More information about the Returnanalytics-commits
mailing list