[Returnanalytics-commits] r2821 - in pkg/PortfolioAnalytics: R demo
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Aug 19 06:50:33 CEST 2013
Author: rossbennett34
Date: 2013-08-19 06:50:32 +0200 (Mon, 19 Aug 2013)
New Revision: 2821
Modified:
pkg/PortfolioAnalytics/R/objective.R
pkg/PortfolioAnalytics/R/optimize.portfolio.R
pkg/PortfolioAnalytics/demo/testing_ROI.R
Log:
modifying objectives and optimize.portfolio for backwards compatibility with v1_constraint object. No longer need to use _v1 with functions
Modified: pkg/PortfolioAnalytics/R/objective.R
===================================================================
--- pkg/PortfolioAnalytics/R/objective.R 2013-08-18 19:54:43 UTC (rev 2820)
+++ pkg/PortfolioAnalytics/R/objective.R 2013-08-19 04:50:32 UTC (rev 2821)
@@ -121,7 +121,11 @@
#' @rdname add.objective
#' @name add.objective
#' @export
-add.objective_v2 <- function(portfolio, type, name, arguments=NULL, enabled=TRUE, ..., indexnum=NULL){
+add.objective_v2 <- function(portfolio, constraints=NULL, type, name, arguments=NULL, enabled=TRUE, ..., indexnum=NULL){
+ if(!is.null(constraints) & inherits(constraints, "v1_constraint")){
+ return(add.objective_v1(constraints=constraints, type=type, name=name, arguments=arguments, enabled=enabled, ...=..., indexnum=indexnum))
+ }
+
# This function is based on the original add.objective function, but modified
# to add objectives to a portfolio object instead of a constraint object.
if (!is.portfolio(portfolio)) {stop("portfolio passed in is not of class portfolio")}
Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R 2013-08-18 19:54:43 UTC (rev 2820)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R 2013-08-19 04:50:32 UTC (rev 2821)
@@ -430,7 +430,7 @@
#' @export
optimize.portfolio_v2 <- function(
R,
- portfolio,
+ portfolio=NULL,
constraints=NULL,
objectives=NULL,
optimize_method=c("DEoptim","random","ROI","ROI_old","pso","GenSA"),
@@ -448,22 +448,20 @@
#store the call for later
call <- match.call()
- if (is.null(portfolio) | !is.portfolio(portfolio)){
- stop("you must pass in an object of class portfolio to control the optimization")
+ if (!is.null(portfolio) & !is.portfolio(portfolio)){
+ stop("you must pass in an object of class 'portfolio' to control the optimization")
}
- R <- checkData(R)
- N <- length(portfolio$assets)
- if (ncol(R) > N) {
- R <- R[,names(portfolio$assets)]
- }
- T <- nrow(R)
-
# Check for constraints and objectives passed in separately outside of the portfolio object
if(!is.null(constraints)){
if(inherits(constraints, "v1_constraint")){
- warning("constraint object passed in is a 'v1_constraint' object, updating to v2 specification")
- portfolio <- update_constraint_v1tov2(portfolio=portfolio, v1_constraint=constraints)
+ if(is.null(portfolio)){
+ # If the user has not passed in a portfolio, we will create one for them
+ tmp_portf <- portfolio.spec(assets=constraints$assets)
+ }
+ message("constraint object passed in is a 'v1_constraint' object, updating to v2 specification")
+ portfolio <- update_constraint_v1tov2(portfolio=tmp_portf, v1_constraint=constraints)
+ # print.default(portfolio)
}
if(!inherits(constraints, "v1_constraint")){
# Insert the constraints into the portfolio object
@@ -475,6 +473,13 @@
portfolio <- insert_objectives(portfolio=portfolio, objectives=objectives)
}
+ R <- checkData(R)
+ N <- length(portfolio$assets)
+ if (ncol(R) > N) {
+ R <- R[,names(portfolio$assets)]
+ }
+ T <- nrow(R)
+
out <- list()
weights <- NULL
Modified: pkg/PortfolioAnalytics/demo/testing_ROI.R
===================================================================
--- pkg/PortfolioAnalytics/demo/testing_ROI.R 2013-08-18 19:54:43 UTC (rev 2820)
+++ pkg/PortfolioAnalytics/demo/testing_ROI.R 2013-08-19 04:50:32 UTC (rev 2821)
@@ -12,6 +12,10 @@
library(Ecdat)
library(PortfolioAnalytics)
+var.portfolio <- function(R, weights){
+ weights <- matrix(weights, ncol=1)
+ return(as.numeric(t(weights) %*% var(R) %*% weights))
+}
# General Parameters for sample code
data(edhec)
@@ -20,9 +24,9 @@
N <- length(funds)
gen.constr <- constraint(assets = colnames(edhec), min=-Inf, max =Inf, min_sum=1, max_sum=1, risk_aversion=1)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="return", name="mean", enabled=FALSE, multiplier=0, target=mu.port)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="risk", name="var", enabled=FALSE, multiplier=0, risk_aversion=10)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="risk", name="CVaR", enabled=FALSE, multiplier=0)
+gen.constr <- add.objective(constraints=gen.constr, type="return", name="mean", enabled=FALSE, multiplier=0, target=mu.port)
+gen.constr <- add.objective(constraints=gen.constr, type="risk", name="var", enabled=FALSE, multiplier=0, risk_aversion=10)
+gen.constr <- add.objective(constraints=gen.constr, type="risk", name="CVaR", enabled=FALSE, multiplier=0)
# =====================
@@ -33,7 +37,7 @@
max.port$max <- rep(0.30,N)
max.port$objectives[[1]]$enabled <- TRUE
max.port$objectives[[1]]$target <- NULL
-max.solution <- optimize.portfolio_v1(edhec, max.port, "ROI")
+max.solution <- optimize.portfolio(R=edhec, constraints=max.port, optimize_method="ROI")
# =====================
@@ -42,7 +46,7 @@
gmv.port <- gen.constr
gmv.port$objectives[[2]]$enabled <- TRUE
gmv.port$objectives[[2]]$risk_aversion <- 1
-gmv.solution <- optimize.portfolio_v1(edhec, gmv.port, "ROI")
+gmv.solution <- optimize.portfolio(R=edhec, constraints=gmv.port, optimize_method="ROI")
# ========================
@@ -51,9 +55,9 @@
target.port <- gen.constr
target.port$objectives[[1]]$enabled <- TRUE
target.port$objectives[[2]]$enabled <- TRUE
-target.solution <- optimize.portfolio_v1(edhec, target.port, "ROI")
+target.solution <- optimize.portfolio(R=edhec, constraints=target.port, optimize_method="ROI")
-
+target.solution$weights %*% var(edhec) %*% target.solution$weights
# ========================
# Mean-variance: Maximize quadratic utility, dollar-neutral, target portfolio return
#
@@ -62,7 +66,7 @@
dollar.neu.port$max_sum <- 0
dollar.neu.port$objectives[[1]]$enabled <- TRUE
dollar.neu.port$objectives[[2]]$enabled <- TRUE
-dollar.neu.solution <- optimize.portfolio_v1(edhec, dollar.neu.port, "ROI")
+dollar.neu.solution <- optimize.portfolio(R=edhec, constraints=dollar.neu.port, optimize_method="ROI")
# ========================
@@ -71,7 +75,7 @@
cvar.port <- gen.constr
cvar.port$objectives[[1]]$enabled <- TRUE
cvar.port$objectives[[3]]$enabled <- TRUE
-cvar.solution <- optimize.portfolio_v1(edhec, cvar.port, "ROI")
+cvar.solution <- optimize.portfolio(R=edhec, constraints=cvar.port, optimize_method="ROI")
# =====================
@@ -84,7 +88,7 @@
groups.port$cUP <- rep(0.30,length(groups))
groups.port$objectives[[2]]$enabled <- TRUE
groups.port$objectives[[2]]$risk_aversion <- 1
-groups.solution <- optimize.portfolio_v1(edhec, groups.port, "ROI")
+groups.solution <- optimize.portfolio(R=edhec, constraints=groups.port, optimize_method="ROI")
# ========================
@@ -97,5 +101,5 @@
group.cvar.port$cUP <- rep(0.30,length(groups))
group.cvar.port$objectives[[1]]$enabled <- TRUE
group.cvar.port$objectives[[3]]$enabled <- TRUE
-group.cvar.solution <- optimize.portfolio_v1(edhec, group.cvar.port, "ROI")
+group.cvar.solution <- optimize.portfolio(R=edhec, constraints=group.cvar.port, optimize_method="ROI")
More information about the Returnanalytics-commits
mailing list