[Returnanalytics-commits] r2823 - pkg/PortfolioAnalytics/demo

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Aug 19 07:23:27 CEST 2013


Author: rossbennett34
Date: 2013-08-19 07:23:27 +0200 (Mon, 19 Aug 2013)
New Revision: 2823

Modified:
   pkg/PortfolioAnalytics/demo/constrained_optim.R
   pkg/PortfolioAnalytics/demo/sortino.R
   pkg/PortfolioAnalytics/demo/testing_GenSA.R
   pkg/PortfolioAnalytics/demo/testing_pso.R
Log:
modifying demo scripts that use the v1 constraints object so that they no longer use functions with a _v1 suffix

Modified: pkg/PortfolioAnalytics/demo/constrained_optim.R
===================================================================
--- pkg/PortfolioAnalytics/demo/constrained_optim.R	2013-08-19 05:04:50 UTC (rev 2822)
+++ pkg/PortfolioAnalytics/demo/constrained_optim.R	2013-08-19 05:23:27 UTC (rev 2823)
@@ -1,13 +1,16 @@
 require("PerformanceAnalytics")
 require("PortfolioAnalytics")
 require("DEoptim")
+
+# Load the data
 data(edhec)
-pspec <- portfolio.spec(assets=colnames(edhec[, 1:10]))
-constraints=constraint(assets = colnames(edhec[, 1:10]), min = 0.01, max = 0.4, min_sum=1, max_sum=1, weight_seq = generatesequence())
+
+#constraints
+constraints <- constraint(assets = colnames(edhec[, 1:10]), min = 0.01, max = 0.4, min_sum=1, max_sum=1, weight_seq = generatesequence())
 # note that if you wanted to do a random portfolio optimization, mun_sum of .99 and max_sum of 1.01 might be more appropriate
-constraints<-add.objective_v1(constraints, type="return", name="mean", arguments=list(), enabled=TRUE)
-constraints<-add.objective_v1(constraints, type="risk_budget", name="ES", arguments=list(), enabled=TRUE, p=.95, min_prisk=.05, max_prisk=.15)
-#constraints
+constraints <- add.objective(constraints=constraints, type="return", name="mean", arguments=list(), enabled=TRUE)
+constraints <- add.objective(constraints=constraints, type="risk_budget", name="ES", arguments=list(), enabled=TRUE, p=.95, min_prisk=.05, max_prisk=.15)
+
 #now set some additional bits
 # I should have set the multiplier for returns to negative
 constraints$objectives[[1]]$multiplier=-1
@@ -18,20 +21,21 @@
 
 print("We'll use a search_size parameter of 1000 for this demo, but realistic portfolios will likely require search_size parameters much larger, the default is 20000 which is almost always large enough for any realistic portfolio and constraints, but will take substantially longer to run.")
 # look for a solution using both DEoptim and random portfolios
-opt_out<-optimize.portfolio_v1(R=edhec[,1:10], constraints=constraints, optimize_method="DEoptim", search_size=1000, trace=TRUE)
+opt_out <- optimize.portfolio(R=edhec[,1:10], constraints=constraints, optimize_method="DEoptim", search_size=1000, trace=TRUE)
+
 #we need a little more wiggle in min/max sum for random portfolios or it takes too long to converge
-constraints$min_sum<-.99
-constraints$max_sum<-1.01
-opt_out_random<-optimize.portfolio_v1(R=edhec[,1:10], constraints=constraints, optimize_method="random", search_size=1000, trace=TRUE)
+constraints$min_sum <- 0.99
+constraints$max_sum <- 1.01
+opt_out_random <- optimize.portfolio(R=edhec[,1:10], constraints=constraints, optimize_method="random", search_size=1000, trace=TRUE)
 
 # now lets try a portfolio that rebalances quarterly
-opt_out_rebalancing<-optimize.portfolio.rebalancing_v1(R=edhec[,1:10], constraints, optimize_method="DEoptim", search_size=1000, trace=FALSE,rebalance_on='quarters')
-rebalancing_weights<-matrix(nrow=length(opt_out_rebalancing),ncol=length(opt_out_rebalancing[[1]]]$weights))
-rownames(rebalancing_weights)<-names(opt_out_rebalancing)
-colnames(rebalancing_weights)<-names(opt_out_rebalancing[[1]]$weights)
-for(period in 1:length(opt_out_rebalancing)) rebalancing_weights[period,]<-opt_out_rebalancing[[period]]$weights
-rebalancing_returns<-Return.rebalancing(R=edhec,weights=rebalancing_weights)
+opt_out_rebalancing <- optimize.portfolio.rebalancing_v1(R=edhec[,1:10], constraints=constraints, optimize_method="DEoptim", search_size=1000, trace=FALSE,rebalance_on='quarters')
+rebalancing_weights <- matrix(nrow=length(opt_out_rebalancing),ncol=length(opt_out_rebalancing[[1]]]$weights))
+rownames(rebalancing_weights) <- names(opt_out_rebalancing)
+colnames(rebalancing_weights) <- names(opt_out_rebalancing[[1]]$weights)
+for(period in 1:length(opt_out_rebalancing)) rebalancing_weights[period,] <- opt_out_rebalancing[[period]]$weights
+rebalancing_returns <- Return.rebalancing(R=edhec,weights=rebalancing_weights)
 charts.PerformanceSummary(rebalancing_returns)
 
 # and now lets rebalance quarterly with 48 mo trailing
-opt_out_trailing<-optimize.portfolio.rebalancing_v1(R=edhec[,1:10], constraints, optimize_method="DEoptim", search_size=1000, trace=FALSE,rebalance_on='quarters',trailing_periods=48,training_period=48)
\ No newline at end of file
+opt_out_trailing<-optimize.portfolio.rebalancing(R=edhec[,1:10], constraints=constraints, optimize_method="DEoptim", search_size=1000, trace=FALSE,rebalance_on='quarters',trailing_periods=48,training_period=48)

Modified: pkg/PortfolioAnalytics/demo/sortino.R
===================================================================
--- pkg/PortfolioAnalytics/demo/sortino.R	2013-08-19 05:04:50 UTC (rev 2822)
+++ pkg/PortfolioAnalytics/demo/sortino.R	2013-08-19 05:23:27 UTC (rev 2823)
@@ -32,11 +32,11 @@
 
 #'# Example 1 maximize Sortino Ratio
 SortinoConstr <- constraint(assets = colnames(indexes[,1:4]), min = 0.05, max = 1, min_sum=.99, max_sum=1.01, weight_seq = generatesequence(by=.001))
-SortinoConstr <- add.objective_v1(SortinoConstr, type="return", name="SortinoRatio",  enabled=TRUE, arguments = list(MAR=MAR))
-SortinoConstr <- add.objective_v1(SortinoConstr, type="return", name="mean",  enabled=TRUE, multiplier=0) # multiplier 0 makes it availble for plotting, but not affect optimization
+SortinoConstr <- add.objective(constraints=SortinoConstr, type="return", name="SortinoRatio",  enabled=TRUE, arguments = list(MAR=MAR))
+SortinoConstr <- add.objective(constraints=SortinoConstr, type="return", name="mean",  enabled=TRUE, multiplier=0) # multiplier 0 makes it availble for plotting, but not affect optimization
 
 ### Use random portfolio engine
-SortinoResult<-optimize.portfolio_v1(R=indexes[,1:4], constraints=SortinoConstr, optimize_method='random', search_size=2000, trace=TRUE, verbose=TRUE)
+SortinoResult<-optimize.portfolio(R=indexes[,1:4], constraints=SortinoConstr, optimize_method='random', search_size=2000, trace=TRUE, verbose=TRUE)
 plot(SortinoResult, risk.col='SortinoRatio')
 
 ### alternately, Use DEoptim engine
@@ -44,7 +44,7 @@
 #plot(SortinoResultDE, risk.col='SortinoRatio')
 
 ### now rebalance quarterly
-SortinoRebalance <- optimize.portfolio.rebalancing_v1(R=indexes[,1:4], constraints=SortinoConstr, optimize_method="random", trace=TRUE, rebalance_on='quarters', trailing_periods=NULL, training_period=36, search_size=2000)
+SortinoRebalance <- optimize.portfolio.rebalancing(R=indexes[,1:4], constraints=SortinoConstr, optimize_method="random", trace=TRUE, rebalance_on='quarters', trailing_periods=NULL, training_period=36, search_size=2000)
 
 ###############################################################################
 # R (http://r-project.org/) Numeric Methods for Optimization of Portfolios

Modified: pkg/PortfolioAnalytics/demo/testing_GenSA.R
===================================================================
--- pkg/PortfolioAnalytics/demo/testing_GenSA.R	2013-08-19 05:04:50 UTC (rev 2822)
+++ pkg/PortfolioAnalytics/demo/testing_GenSA.R	2013-08-19 05:23:27 UTC (rev 2823)
@@ -23,10 +23,10 @@
 mu.port <- mean(colMeans(R))
 
 gen.constr <- constraint(assets = funds, min=-2, max=2, min_sum=0.99, max_sum=1.01, risk_aversion=1)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="return", name="mean", enabled=FALSE, target=mu.port)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="risk", name="var", enabled=FALSE, risk_aversion=10)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="risk", name="CVaR", enabled=FALSE)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="risk", name="sd", enabled=FALSE)
+gen.constr <- add.objective(constraints=gen.constr, type="return", name="mean", enabled=FALSE, target=mu.port)
+gen.constr <- add.objective(constraints=gen.constr, type="risk", name="var", enabled=FALSE, risk_aversion=10)
+gen.constr <- add.objective(constraints=gen.constr, type="risk", name="CVaR", enabled=FALSE)
+gen.constr <- add.objective(constraints=gen.constr, type="risk", name="sd", enabled=FALSE)
 
 
 # =====================
@@ -37,14 +37,14 @@
 max.port$objectives[[1]]$enabled <- TRUE
 max.port$objectives[[1]]$target <- NULL
 max.port$objectives[[1]]$multiplier  <- -1
-max.solution <- optimize.portfolio_v1(R, max.port, "GenSA", trace=TRUE)
+max.solution <- optimize.portfolio(R=R, constraints=max.port, optimize_method="GenSA", trace=TRUE)
 
 
 # =====================
 # Mean-variance:  Fully invested, Global Minimum Variance Portfolio
 gmv.port <- gen.constr
 gmv.port$objectives[[4]]$enabled <- TRUE
-gmv.solution <- optimize.portfolio_v1(R, gmv.port, "GenSA", trace=TRUE)
+gmv.solution <- optimize.portfolio(R=R, constraints=gmv.port, optimize_method="GenSA", trace=TRUE)
 
 
 
@@ -56,7 +56,7 @@
 cvar.port$max <- rep(1,N)
 cvar.port$objectives[[3]]$enabled <- TRUE
 cvar.port$objectives[[3]]$arguments <- list(p=0.95, clean="boudt")
-cvar.solution <- optimize.portfolio_v1(R, cvar.port, "GenSA")
+cvar.solution <- optimize.portfolio(R=R, constraints=cvar.port, optimize_method="GenSA")
 
 
 

Modified: pkg/PortfolioAnalytics/demo/testing_pso.R
===================================================================
--- pkg/PortfolioAnalytics/demo/testing_pso.R	2013-08-19 05:04:50 UTC (rev 2822)
+++ pkg/PortfolioAnalytics/demo/testing_pso.R	2013-08-19 05:23:27 UTC (rev 2823)
@@ -22,10 +22,10 @@
 mu.port <- mean(colMeans(R))
 
 gen.constr <- constraint(assets = funds, min=-2, max=2, min_sum=0.99, max_sum=1.01, risk_aversion=1)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="return", name="mean", enabled=FALSE, target=mu.port)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="risk", name="var", enabled=FALSE, risk_aversion=10)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="risk", name="CVaR", enabled=FALSE)
-gen.constr <- add.objective_v1(constraints=gen.constr, type="risk", name="sd", enabled=FALSE)
+gen.constr <- add.objective(constraints=gen.constr, type="return", name="mean", enabled=FALSE, target=mu.port)
+gen.constr <- add.objective(constraints=gen.constr, type="risk", name="var", enabled=FALSE, risk_aversion=10)
+gen.constr <- add.objective(constraints=gen.constr, type="risk", name="CVaR", enabled=FALSE)
+gen.constr <- add.objective(constraints=gen.constr, type="risk", name="sd", enabled=FALSE)
 
 
 # =====================
@@ -36,14 +36,14 @@
 max.port$objectives[[1]]$enabled <- TRUE
 max.port$objectives[[1]]$target <- NULL
 max.port$objectives[[1]]$multiplier  <- -1
-max.solution <- optimize.portfolio_v1(R, max.port, "pso", trace=TRUE)
+max.solution <- optimize.portfolio(R=R, constraints=max.port, optimize_method="pso", trace=TRUE)
 
 
 # =====================
 # Mean-variance:  Fully invested, Global Minimum Variance Portfolio
 gmv.port <- gen.constr
 gmv.port$objectives[[4]]$enabled <- TRUE
-gmv.solution <- optimize.portfolio_v1(R, gmv.port, "pso", trace=TRUE)
+gmv.solution <- optimize.portfolio(R=R, constraints=gmv.port, optimize_method="pso", trace=TRUE)
 
 
 
@@ -55,7 +55,7 @@
 cvar.port$max <- rep(1,N)
 cvar.port$objectives[[3]]$enabled <- TRUE
 cvar.port$objectives[[3]]$arguments <- list(p=0.95, clean="boudt")
-cvar.solution <- optimize.portfolio_v1(R, cvar.port, "pso", trace=TRUE)
+cvar.solution <- optimize.portfolio(R=R, constraints=cvar.port, optimize_method="pso", trace=TRUE)
 
 
 



More information about the Returnanalytics-commits mailing list