[Returnanalytics-commits] r2550 - in pkg/PortfolioAnalytics: R sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jul 12 03:06:46 CEST 2013
Author: rossbennett34
Date: 2013-07-12 03:06:45 +0200 (Fri, 12 Jul 2013)
New Revision: 2550
Modified:
pkg/PortfolioAnalytics/R/optimize.portfolio.R
pkg/PortfolioAnalytics/sandbox/testing_optimize.portfolio_v2.R
Log:
adding tests to the testing_optimize.portfolio_v2.R. Fixed minor bug in optimize.portfolio
Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R 2013-07-11 23:02:27 UTC (rev 2549)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R 2013-07-12 01:06:45 UTC (rev 2550)
@@ -348,7 +348,7 @@
Amat <- cbind(rbind(1, 1, moments$mean, coredata(R)), rbind(0, 0, 0, cbind(diag(T), 1)))
dir.vec <- c(">=","<=",">=",rep(">=",T))
rhs.vec <- c(constraints$min_sum, constraints$max_sum, Rmin ,rep(0, T))
- if(try(!is.null(groups), silent=TRUE)){
+ if(try(!is.null(constraints$groups), silent=TRUE)){
zeros <- matrix(0, nrow=n.groups, ncol=(T+1))
Amat <- rbind(Amat, cbind(Amat.group, zeros), cbind(-Amat.group, zeros))
dir.vec <- c(dir.vec, rep(">=", (n.groups + n.groups)))
Modified: pkg/PortfolioAnalytics/sandbox/testing_optimize.portfolio_v2.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_optimize.portfolio_v2.R 2013-07-11 23:02:27 UTC (rev 2549)
+++ pkg/PortfolioAnalytics/sandbox/testing_optimize.portfolio_v2.R 2013-07-12 01:06:45 UTC (rev 2550)
@@ -6,35 +6,93 @@
ret <- edhec[, 1:4]
funds <- colnames(ret)
+# Set up constraints and objectives using old interface
gen.constr <- constraint(assets=funds, min=0, max=1, min_sum=0.99, max_sum=1.01,
weight_seq = generatesequence(min=0, max=1, by=0.002))
gen.constr <- add.objective(constraints=gen.constr, type="return", name="mean", enabled=TRUE, multiplier=-1)
-# gen.constr <- add.objective(constraints=gen.constr, type="risk", name="CVaR", enabled=FALSE, multiplier=0)
+# Set up constraints and objectives using new interface
pspec <- portfolio.spec(assets=funds, weight_seq = generatesequence(min=0, max=1, by=0.002))
pspec <- add.constraint(portfolio=pspec, type="leverage", min_sum=0.99, max_sum=1.01, enabled=TRUE)
pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
pspec <- add.objective_v2(portfolio=pspec, type="return", name="mean", multiplier=-1, enabled=TRUE)
-# pspec <- add.objective_v2(portfolio=pspec, type="risk", name="CVaR", multiplier=0, enabled=TRUE)
# tmp1 <- set.portfolio.moments(R=ret, constraints=gen.constr)
# tmp2 <- set.portfolio.moments_v2(R=ret, portfolio=pspec)
# all.equal(tmp1, tmp2)
-##### Simple test for DEoptim with optimize.portfolio_v2 #####
+##### Simple test for DEoptim method with optimize.portfolio_v2 #####
# generate an initial population with random_portfolios
rp <- random_portfolios_v2(portfolio=pspec, permutations=40)
set.seed(123)
-opt_out <- optimize.portfolio(R=ret, gen.constr, optimize_method="DEoptim", search_size=1000, trace=FALSE, rpseed=rp)
+opt_out_de <- optimize.portfolio(R=ret, gen.constr, optimize_method="DEoptim", search_size=1000, trace=FALSE, rpseed=rp)
set.seed(123)
-opt <- optimize.portfolio_v2(R=ret, portfolio=pspec, optimize_method="DEoptim", search_size=1000, trace=FALSE, rpseed=rp)
+opt_de <- optimize.portfolio_v2(R=ret, portfolio=pspec, optimize_method="DEoptim", search_size=1000, trace=FALSE, rpseed=rp)
-all.equal(opt_out$weights, opt$weights)
-all.equal(opt_out$objective_measures, opt$objective_measures)
+# The results should be the same using the same initial population and set.seed
+all.equal(opt_out_de$weights, opt_de$weights)
+all.equal(opt_out_de$objective_measures, opt_de$objective_measures)
+##### Simple test for random method with optimize.portfolio_v2 #####
+set.seed(123)
+opt_out_rp <- optimize.portfolio(R=ret, gen.constr, optimize_method="random", search_size=2000, trace=FALSE)
+
+set.seed(123)
+opt_rp <- optimize.portfolio_v2(R=ret, portfolio=pspec, optimize_method="random", search_size=2000, trace=FALSE)
+
+# The results should be the same
+all.equal(opt_out_rp$weights, opt_rp$weights)
+all.equal(opt_out_rp$objective_measures, opt_rp$objective_measures)
+
+##### Simple test for pso method with optimize.portfolio_v2 #####
+
+set.seed(123)
+opt_out_pso <- optimize.portfolio(R=ret, gen.constr, optimize_method="pso", search_size=2000, trace=FALSE)
+
+set.seed(123)
+opt_pso <- optimize.portfolio_v2(R=ret, portfolio=pspec, optimize_method="pso", search_size=2000, trace=FALSE)
+
+# The results should be the same
+all.equal(opt_out_pso$weights, opt_pso$weights)
+all.equal(opt_out_pso$objective_measures, opt_pso$objective_measures)
+
+##### Simple test for GenSA method with optimize.portfolio_v2 #####
+
+set.seed(123)
+opt_out_gensa <- optimize.portfolio(R=ret, gen.constr, optimize_method="GenSA", search_size=2000, trace=FALSE)
+
+set.seed(123)
+opt_gensa <- optimize.portfolio_v2(R=ret, portfolio=pspec, optimize_method="GenSA", search_size=2000, trace=FALSE)
+
+# The results should be the same
+all.equal(opt_out_gensa$weights, opt_gensa$weights)
+all.equal(opt_out_gensa$objective_measures, opt_gensa$objective_measures)
+
+##### Simple test for ROI method with optimize.portfolio_v2 #####
+# specify CVaR with old interface and ETL with new interface
+
+# Set up constraints and objectives using old interface
+gen.constr <- constraint(assets=funds, min=0, max=1, min_sum=0.99, max_sum=1.01,
+ weight_seq = generatesequence(min=0, max=1, by=0.002))
+gen.constr <- add.objective(constraints=gen.constr, type="risk", name="CVaR", enabled=TRUE, multiplier=-1)
+
+# Set up constraints and objectives using new interface
+pspec <- portfolio.spec(assets=funds, weight_seq = generatesequence(min=0, max=1, by=0.002))
+pspec <- add.constraint(portfolio=pspec, type="leverage", min_sum=0.99, max_sum=1.01, enabled=TRUE)
+pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
+pspec <- add.objective_v2(portfolio=pspec, type="risk", name="ETL", multiplier=-1, enabled=TRUE)
+
+opt_out_roi <- optimize.portfolio(R=ret, gen.constr, optimize_method="ROI", search_size=2000, trace=FALSE)
+
+opt_roi <- optimize.portfolio_v2(R=ret, portfolio=pspec, optimize_method="ROI", search_size=2000, trace=FALSE)
+
+# The results should be the same
+all.equal(opt_out_roi$weights, opt_roi$weights)
+all.equal(opt_out_roi$objective_measures, opt_roi$objective_measures)
+
##### Test version of random_portfolios #####
tmp <- random_portfolios(gen.constr)
tmp1 <- random_portfolios_v2(pspec)
More information about the Returnanalytics-commits
mailing list