[Returnanalytics-commits] r2631 - pkg/PortfolioAnalytics/sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jul 23 02:49:04 CEST 2013


Author: rossbennett34
Date: 2013-07-23 02:49:03 +0200 (Tue, 23 Jul 2013)
New Revision: 2631

Modified:
   pkg/PortfolioAnalytics/sandbox/testing_ROI_Martin.R
   pkg/PortfolioAnalytics/sandbox/testing_optimize.portfolio_v2.R
   pkg/PortfolioAnalytics/sandbox/testing_portfolio_specification.R
Log:
updatind testing scripts to omit the old interface and omit the _v2 in optimize.portfolio and add.objective

Modified: pkg/PortfolioAnalytics/sandbox/testing_ROI_Martin.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_ROI_Martin.R	2013-07-23 00:26:04 UTC (rev 2630)
+++ pkg/PortfolioAnalytics/sandbox/testing_ROI_Martin.R	2013-07-23 00:49:03 UTC (rev 2631)
@@ -25,36 +25,16 @@
 returns <- midcap.ts[, 1:10]
 funds <- colnames(returns)
 
-# Set up initial constraint object
-# Here we specify the minimum weight of any asset is -Inf and the maximum
-# weight of any asset is Inf. This is essentially an unconstrained GMV portfolio
-# We specify the full investment constraint (w' 1 = 1) by setting min_sum=1 
-# and max_sum=1.
-gen.constr <- constraint(assets=funds, min=-Inf, max=Inf, min_sum=1, max_sum=1)
 
-# Add objective to minimize variance
-gen.constr <- add.objective(constraints=gen.constr, type="risk", name="var", enabled=TRUE)
-
 ##### Example 1.1: Global Minimum Variance (GMV) Portfolio #####
-# Global Minimum variance portfolio
-gmv.constr <- gen.constr
 
-# Call the optimizer to minimize portfolio variance
-gmv.opt <- optimize.portfolio(R=returns, constraints=gmv.constr, optimize_method="ROI")
-
-# Optimal weights
-round(gmv.opt$weights, 3)
-
-# Portfolio standard deviation
-sqrt(gmv.opt$out)
-
 # GMV portfolio using new interface
 pspec <- portfolio.spec(assets=funds)
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
 pspec <- add.constraint(portfolio=pspec, type="box", min=-Inf, max=Inf, enabled=TRUE)
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="var", enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -63,32 +43,14 @@
 sqrt(opt$out)
 
 ##### Example 1.2: Long Only GMV Portfolio #####
-gmv.longonly.constr <- gen.constr
 
-# Set the min and max vectors for long only constraints
-min <- rep(0, length(funds))
-max <- rep(1, length(funds))
-
-# Modify the min and max vectors in gmv.longonly.constr
-gmv.longonly.constr$min <- min
-gmv.longonly.constr$max <- max
-
-# Call the optimizer
-gmv.longonly.opt <- optimize.portfolio(R=returns, constraints=gmv.longonly.constr, optimize_method="ROI")
-
-# Optimal weights
-round(gmv.longonly.opt$weights, 3)
-
-# Portfolio standard deviation
-sqrt(gmv.longonly.opt$out)
-
 # GMV long only portfolio using new interface
 pspec <- portfolio.spec(assets=funds)
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
 pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="var", enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -97,34 +59,14 @@
 sqrt(opt$out)
 
 ##### Example 1.3: GMV Box Constraints #####
-gmv.box.constr <- gen.constr
 
-# Set the min and max vectors for box constraints
-# The box constraints are such that the minimum weight of any asset is 0.03
-# and the maximum weight of any asset is 0.25
-min <- rep(0.03, length(funds))
-max <- rep(0.25, length(funds))
-
-# Modify the min and max vectors in gmv.longonly.constr
-gmv.box.constr$min <- min
-gmv.box.constr$max <- max
-
-# Call the optimizer
-gmv.box.opt <- optimize.portfolio(R=returns, constraints=gmv.box.constr, optimize_method="ROI")
-
-# Optimal weights
-round(gmv.box.opt$weights, 3)
-
-# Portfolio standard deviation
-sqrt(gmv.box.opt$out)
-
 # GMV box constraints portfolio using new interface
 pspec <- portfolio.spec(assets=funds)
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
 pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="var", enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -132,31 +74,6 @@
 # Portfolio standard deviation
 sqrt(opt$out)
 
-##### Example 1.3a: GMV Box Constraints #####
-gmv.box.constr <- gen.constr
-
-# As an alternative to box constriants, we can also linear inequality
-# constraints for the minimum and maximum asset weights
-
-# Set the min and max vectors for box constraints
-# The box constraints are such that the minimum weight of any asset is 0.03
-# and the maximum weight of any asset is 0.25
-min <- c(0.02, 0.02, 0.02, 0.04, 0.05, 0.05, 0.02, 0, 0, 0.1)
-max <- c(0.2, 0.4, 0.4, 0.45, 0.3, 0.5, 0.4, 0.4, 0.4, 0.4)
-
-# Modify the min and max vectors in gmv.longonly.constr
-gmv.box.constr$min <- min
-gmv.box.constr$max <- max
-
-# Mean variance optimization (MVO) seeks to minimize portfolio variance
-gmv.box.opt <- optimize.portfolio(R=returns, constraints=gmv.box.constr, optimize_method="ROI")
-
-# Optimal weights
-round(gmv.box.opt$weights, 3)
-
-# Portfolio standard deviation
-sqrt(gmv.box.opt$out)
-
 ##### Example 1.4: GMV long only with Group Constraints #####
 # Combine returns from different market cap groups
 returns.cap <- cbind(microcap.ts[, 1:2],
@@ -166,31 +83,6 @@
 
 funds.cap <- colnames(returns.cap)
 
-# Set up constraints object for the market caps
-lo.group.constr <- constraint(assets=funds.cap, min=0, max=1, min_sum=1, max_sum=1)
-
-# Add group constraints to gmv.box.group.constr
-# Market cap constraints
-# At least 10% and no more than 25% in micro-caps
-# At least 15% and no more than 35% in small-caps
-# At least 0% and no more than 35% in mid-caps
-# At least 0% and no more than 45% in large-caps
-lo.group.constr$groups <- c(2, 2, 2, 2)
-lo.group.constr$cLO <- c(0.1, 0.15, 0, 0)
-lo.group.constr$cUP <- c(0.25, .35, 0.35, 0.45)
-
-# Add objective to minimize variance
-gmv.lo.group.constr <- add.objective(constraints=lo.group.constr, type="risk", name="var", enabled=TRUE)
-
-# Call optimizer
-gmv.lo.group.opt <- optimize.portfolio(R=returns.cap, constraints=gmv.lo.group.constr, optimize_method="ROI")
-
-# Optimal weights
-round(gmv.lo.group.opt$weights, 3)
-
-# Group weights
-gmv.lo.group.opt$weights[c(1, 3, 5, 7)] + gmv.lo.group.opt$weights[c(2, 4, 6, 8)]
-
 # GMV group constraints portfolio using new interface
 pspec <- portfolio.spec(assets=funds.cap)
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
@@ -200,9 +92,9 @@
                         group_min=c(0.1, 0.15, 0, 0), 
                         group_max=c(0.25, .35, 0.35, 0.45),
                         group_labels=c("MICRO", "SMALL", "MID", "LARGE"))
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="var", enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="risk", name="var", enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns.cap, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns.cap, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -211,8 +103,8 @@
 # This is something I will work to include in the summary.optimize.portfolio.ROI
 groups <- pspec$constraints[[3]]$groups
 group_labels <- pspec$constraints[[3]]$group_labels
-group_weights <- rep(0, n.groups)
 n.groups <- length(groups)
+group_weights <- rep(0, n.groups)
 k <- 1
 l <- 0
 for(i in 1:n.groups){
@@ -231,29 +123,14 @@
 # The solve.QP plugin is selected automatically by optimize.portfolio when "var" is the objective
 
 ##### Example 1.6: Maximize mean-return with box constraints #####
-# Set up initial constraint object
-# Here we specify the minimum weight of any asset is 0.03 and the maximum weight of any asset is 0.25
-# We specify the full investment constraint (w' 1 = 1) by setting min_sum=1 and max_sum=1
-gen.constr <- constraint(assets=funds, min=0.03, max=0.25, min_sum=1, max_sum=1)
 
-# Add objective to maximize return
-gen.constr <- add.objective(constraints=gen.constr, type="return", name="mean", enabled=TRUE)
-
-maxret.constr <- gen.constr
-
-# Call optimizer to maximize return subject to given constraints
-maxret.opt <- optimize.portfolio(R=returns, constraints=maxret.constr, optimize_method="ROI")
-
-# Optimal weights
-maxret.opt$weights
-
 # Maximize mean return with box constraints portfolio using new interface
 pspec <- portfolio.spec(assets=funds)
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
 pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)
-pspec <- add.objective_v2(portfolio=pspec, type="return", name="mean", enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="return", name="mean", enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -262,19 +139,7 @@
 sqrt(opt$out)
 
 ##### Example 1.7 Maximize mean-return Long Only with Group Constraints #####
-# Re-use lo.group.constr from Example 1.5
-maxret.lo.group.constr <- lo.group.constr
 
-maxret.lo.group.constr <- add.objective(constraints=maxret.lo.group.constr, type="return", name="mean", enabled=TRUE)
-
-maxret.lo.group.opt <- optimize.portfolio(R=returns.cap, constraints=maxret.lo.group.constr, optimize_method="ROI")
-
-# Optimal weights
-maxret.lo.group.opt$weights
-
-# Group weights
-maxret.lo.group.opt$weights[c(1, 3, 5, 7)] + maxret.lo.group.opt$weights[c(2, 4, 6, 8)]
-
 # GMV group constraints portfolio using new interface
 pspec <- portfolio.spec(assets=funds.cap)
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
@@ -284,9 +149,9 @@
                         group_min=c(0.1, 0.15, 0, 0), 
                         group_max=c(0.25, .35, 0.35, 0.45),
                         group_labels=c("MICRO", "SMALL", "MID", "LARGE"))
-pspec <- add.objective_v2(portfolio=pspec, type="return", name="mean", enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="return", name="mean", enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns.cap, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns.cap, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -311,21 +176,6 @@
 # Portfolio standard deviation
 sqrt(opt$out)
 
-##### Example 1.X: Maximize Quadratic Utility #####
-# Quadratic utility maximize return penalizing variance
-qu.constr <- constraint(assets=funds, min=0, max=1, min_sum=1, max_sum=1)
-
-# Add mean return as an objective
-qu.constr <- add.objective(constraints=qu.constr, type="return", name="mean", enabled=TRUE)
-
-# Add variance as an objective
-qu.constr <- add.objective(constraints=qu.constr, type="risk", name="var", enabled=TRUE, risk_aversion=20)
-
-qu.opt <- optimize.portfolio(R=returns, constraints=qu.constr, optimize_method="ROI")
-
-wts1 <- round(qu.opt$weights, 4)
-wts1
-
 # Check results for quadratic utility with manual code
 p <- ncol(returns)
 V <- var(returns)
@@ -343,37 +193,20 @@
 names(wts2) <- colnames(returns)
 wts2
 
-all.equal(wts1, wts2)
-
 # Note that target mean return CANNOT be specified as a constraint currently
 # It is specified as a target in the return objective
 # Can do quadratic utility optimization with target return
 
-##### Example 1.X: Maximize Quadratic Utility #####
-# Quadratic utility maximize return penalizing variance
-qu.constr <- constraint(assets=funds, min=0, max=1, min_sum=1, max_sum=1)
-
-# Add mean return as an objective
-qu.constr <- add.objective(constraints=qu.constr, type="return", name="mean", target=0.025, enabled=TRUE)
-
-# Add variance as an objective
-# Set risk aversion parameter high to approximate mvo
-qu.constr <- add.objective(constraints=qu.constr, type="risk", name="var", enabled=TRUE, risk_aversion=1e6)
-
-qu.opt <- optimize.portfolio(R=returns, constraints=qu.constr, optimize_method="ROI")
-
-round(qu.opt$weights, 4)
-
 ##### Example X: Mean Variance Optimization (MVO) with target mean return constraint #####
 
 # MVO with target mean return
 pspec <- portfolio.spec(assets=funds)
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
 pspec <- add.constraint(portfolio=pspec, type="box", min=-Inf, max=Inf, enabled=TRUE)
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)
-pspec <- add.objective_v2(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -388,10 +221,10 @@
 pspec <- portfolio.spec(assets=funds)
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
 pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)
-pspec <- add.objective_v2(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -405,10 +238,10 @@
 pspec <- portfolio.spec(assets=funds)
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
 pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)
-pspec <- add.objective_v2(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="risk", name="var", risk_aversion=1e6, enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="return", name="mean", target=0.014, enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -422,9 +255,9 @@
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
 pspec <- add.constraint(portfolio=pspec, type="box", min=0, max=1, enabled=TRUE)
 # This can be specified with ETL, ES, or CVaR for name
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -435,9 +268,9 @@
 pspec <- add.constraint(portfolio=pspec, type="full_investment", enabled=TRUE)
 pspec <- add.constraint(portfolio=pspec, type="box", min=0.03, max=0.25, enabled=TRUE)
 # This can be specified with ETL, ES, or CVaR for name
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)
@@ -453,9 +286,9 @@
                         group_min=c(0.1, 0.15, 0, 0), 
                         group_max=c(0.25, .35, 0.35, 0.45),
                         group_labels=c("MICRO", "SMALL", "MID", "LARGE"))
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)
+pspec <- add.objective(portfolio=pspec, type="risk", name="ETL", alpha=0.05, enabled=TRUE)
 
-opt <- optimize.portfolio_v2(R=returns.cap, portfolio=pspec, optimize_method="ROI")
+opt <- optimize.portfolio(R=returns.cap, portfolio=pspec, optimize_method="ROI")
 
 # Optimal weights
 round(opt$weights, 3)

Modified: pkg/PortfolioAnalytics/sandbox/testing_optimize.portfolio_v2.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_optimize.portfolio_v2.R	2013-07-23 00:26:04 UTC (rev 2630)
+++ pkg/PortfolioAnalytics/sandbox/testing_optimize.portfolio_v2.R	2013-07-23 00:49:03 UTC (rev 2631)
@@ -7,9 +7,9 @@
 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 <- 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)
 
 # Set up constraints and objectives using new interface
 pspec <- portfolio.spec(assets=funds, weight_seq = generatesequence(min=0, max=1, by=0.002))
@@ -25,62 +25,62 @@
 # generate an initial population with random_portfolios
 rp <- random_portfolios_v2(portfolio=pspec, permutations=40)
 
-set.seed(123)
-opt_out_de <- optimize.portfolio(R=ret, gen.constr, optimize_method="DEoptim", search_size=1000, trace=FALSE, rpseed=rp)
+# set.seed(123)
+# opt_out_de <- optimize.portfolio(R=ret, gen.constr, optimize_method="DEoptim", search_size=1000, trace=FALSE, rpseed=rp)
 
 set.seed(123)
 opt_de <- optimize.portfolio_v2(R=ret, portfolio=pspec, optimize_method="DEoptim", search_size=1000, trace=FALSE, rpseed=rp)
 
 # 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)
+# all.equal(opt_out_de$weights, opt_de$weights)
+# all.equal(opt_out_de$objective_measures, opt_de$objective_measures)
 
 # Note that values are now different since I added fnMap=fn_map to DEoptim in optimize.portfolio_v2
 # This is likely due to how normalization/transformation is handled
 
 ##### 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_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)
+# 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_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)
+# 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_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)
+# 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)
+# 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))
@@ -88,16 +88,16 @@
 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_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)
+# 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)
-all(rowSums(tmp1) <= 1.01) & all(rowSums(tmp1) >= 0.99)
+# tmp <- random_portfolios(gen.constr)
+# tmp1 <- random_portfolios_v2(pspec)
+# all(rowSums(tmp1) <= 1.01) & all(rowSums(tmp1) >= 0.99)
 

Modified: pkg/PortfolioAnalytics/sandbox/testing_portfolio_specification.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_portfolio_specification.R	2013-07-23 00:26:04 UTC (rev 2630)
+++ pkg/PortfolioAnalytics/sandbox/testing_portfolio_specification.R	2013-07-23 00:49:03 UTC (rev 2631)
@@ -1,7 +1,6 @@
 # Testing for the new portfolio specification
 
 # Load necessary packages
-library(PerformanceAnalytics)
 library(PortfolioAnalytics)
 
 # Load the edhec data
@@ -46,15 +45,15 @@
 print(pspec)
 
 # Add objectives to the pspec object
-pspec <- add.objective_v2(portfolio=pspec, type="return", name="mean", 
+pspec <- add.objective(portfolio=pspec, type="return", name="mean", 
                        enabled=FALSE, multiplier=0)
 print(pspec)
 
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="var", 
+pspec <- add.objective(portfolio=pspec, type="risk", name="var", 
                        enabled=FALSE, multiplier=0, risk_aversion=10)
 print(pspec)
 
-pspec <- add.objective_v2(portfolio=pspec, type="risk", name="CVaR", 
+pspec <- add.objective(portfolio=pspec, type="risk", name="CVaR", 
                        enabled=FALSE, multiplier=0)
 print(pspec)
 



More information about the Returnanalytics-commits mailing list