[Returnanalytics-commits] r2772 - in pkg/PortfolioAnalytics: demo sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 13 01:42:56 CEST 2013


Author: rossbennett34
Date: 2013-08-13 01:42:56 +0200 (Tue, 13 Aug 2013)
New Revision: 2772

Added:
   pkg/PortfolioAnalytics/sandbox/testing_return_target.R
Modified:
   pkg/PortfolioAnalytics/demo/constrained_optim.R
   pkg/PortfolioAnalytics/demo/sortino.R
   pkg/PortfolioAnalytics/demo/testing_GenSA.R
   pkg/PortfolioAnalytics/demo/testing_ROI.R
   pkg/PortfolioAnalytics/demo/testing_pso.R
Log:
fixing the files in the demo folder to run using v1 specification

Modified: pkg/PortfolioAnalytics/demo/constrained_optim.R
===================================================================
--- pkg/PortfolioAnalytics/demo/constrained_optim.R	2013-08-12 22:39:54 UTC (rev 2771)
+++ pkg/PortfolioAnalytics/demo/constrained_optim.R	2013-08-12 23:42:56 UTC (rev 2772)
@@ -2,10 +2,11 @@
 require("PortfolioAnalytics")
 require("DEoptim")
 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())
 # 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(constraints, type="return", name="mean", arguments=list(), enabled=TRUE)
-constraints<-add.objective(constraints, type="risk_budget", name="ES", arguments=list(), enabled=TRUE, p=.95, min_prisk=.05, max_prisk=.15)
+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
 #now set some additional bits
 # I should have set the multiplier for returns to negative
@@ -17,14 +18,14 @@
 
 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(R=edhec[,1:10], constraints, optimize_method="DEoptim", search_size=1000, trace=TRUE)
+opt_out<-optimize.portfolio_v1(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(R=edhec[,1:10], constraints, optimize_method="random", search_size=1000, trace=TRUE)
+opt_out_random<-optimize.portfolio_v1(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(R=edhec[,1:10], constraints, optimize_method="DEoptim", search_size=1000, trace=FALSE,rebalance_on='quarters')
+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)
@@ -33,4 +34,4 @@
 charts.PerformanceSummary(rebalancing_returns)
 
 # and now lets rebalance quarterly with 48 mo trailing
-opt_out_trailing<-optimize.portfolio.rebalancing(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_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

Modified: pkg/PortfolioAnalytics/demo/sortino.R
===================================================================
--- pkg/PortfolioAnalytics/demo/sortino.R	2013-08-12 22:39:54 UTC (rev 2771)
+++ pkg/PortfolioAnalytics/demo/sortino.R	2013-08-12 23:42:56 UTC (rev 2772)
@@ -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(SortinoConstr, type="return", name="SortinoRatio",  enabled=TRUE, arguments = list(MAR=MAR))
-SortinoConstr <- add.objective(SortinoConstr, type="return", name="mean",  enabled=TRUE, multiplier=0) # multiplier 0 makes it availble for plotting, but not affect optimization
+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
 
 ### Use random portfolio engine
-SortinoResult<-optimize.portfolio(R=indexes[,1:4], constraints=SortinoConstr, optimize_method='random', search_size=2000, trace=TRUE, verbose=TRUE)
+SortinoResult<-optimize.portfolio_v1(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(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_v1(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-12 22:39:54 UTC (rev 2771)
+++ pkg/PortfolioAnalytics/demo/testing_GenSA.R	2013-08-12 23:42:56 UTC (rev 2772)
@@ -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(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)
+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)
 
 
 # =====================
@@ -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(R, max.port, "GenSA", trace=TRUE)
+max.solution <- optimize.portfolio_v1(R, max.port, "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(R, gmv.port, "GenSA", trace=TRUE)
+gmv.solution <- optimize.portfolio_v1(R, gmv.port, "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(R, cvar.port, "pso")
+cvar.solution <- optimize.portfolio_v1(R, cvar.port, "GenSA")
 
 
 

Modified: pkg/PortfolioAnalytics/demo/testing_ROI.R
===================================================================
--- pkg/PortfolioAnalytics/demo/testing_ROI.R	2013-08-12 22:39:54 UTC (rev 2771)
+++ pkg/PortfolioAnalytics/demo/testing_ROI.R	2013-08-12 23:42:56 UTC (rev 2772)
@@ -20,9 +20,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(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)
+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)
 
 
 # =====================
@@ -33,7 +33,7 @@
 max.port$max <- rep(0.30,N)
 max.port$objectives[[1]]$enabled <- TRUE
 max.port$objectives[[1]]$target <- NULL
-max.solution <- optimize.portfolio(edhec, max.port, "ROI")
+max.solution <- optimize.portfolio_v1(edhec, max.port, "ROI")
 
 
 # =====================
@@ -42,7 +42,7 @@
 gmv.port <- gen.constr
 gmv.port$objectives[[2]]$enabled <- TRUE
 gmv.port$objectives[[2]]$risk_aversion <- 1
-gmv.solution <- optimize.portfolio(edhec, gmv.port, "ROI")
+gmv.solution <- optimize.portfolio_v1(edhec, gmv.port, "ROI")
 
 
 # ========================
@@ -51,7 +51,7 @@
 target.port <- gen.constr
 target.port$objectives[[1]]$enabled <- TRUE
 target.port$objectives[[2]]$enabled <- TRUE
-target.solution <- optimize.portfolio(edhec, target.port, "ROI")
+target.solution <- optimize.portfolio_v1(edhec, target.port, "ROI")
 
 
 # ========================
@@ -62,7 +62,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(edhec, dollar.neu.port, "ROI")
+dollar.neu.solution <- optimize.portfolio_v1(edhec, dollar.neu.port, "ROI")
 
 
 # ========================
@@ -71,7 +71,7 @@
 cvar.port <- gen.constr
 cvar.port$objectives[[1]]$enabled <- TRUE
 cvar.port$objectives[[3]]$enabled <- TRUE
-cvar.solution <- optimize.portfolio(edhec, cvar.port, "ROI")
+cvar.solution <- optimize.portfolio_v1(edhec, cvar.port, "ROI")
 
 
 # =====================
@@ -84,7 +84,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(edhec, groups.port, "ROI")
+groups.solution <- optimize.portfolio_v1(edhec, groups.port, "ROI")
 
 
 # ========================
@@ -97,5 +97,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(edhec, group.cvar.port, "ROI")
+group.cvar.solution <- optimize.portfolio_v1(edhec, group.cvar.port, "ROI")
 

Modified: pkg/PortfolioAnalytics/demo/testing_pso.R
===================================================================
--- pkg/PortfolioAnalytics/demo/testing_pso.R	2013-08-12 22:39:54 UTC (rev 2771)
+++ pkg/PortfolioAnalytics/demo/testing_pso.R	2013-08-12 23:42:56 UTC (rev 2772)
@@ -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(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)
+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)
 
 
 # =====================
@@ -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(R, max.port, "pso", trace=TRUE)
+max.solution <- optimize.portfolio_v1(R, max.port, "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(R, gmv.port, "pso", trace=TRUE)
+gmv.solution <- optimize.portfolio_v1(R, gmv.port, "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(R, cvar.port, "pso", trace=TRUE)
+cvar.solution <- optimize.portfolio_v1(R, cvar.port, "pso", trace=TRUE)
 
 
 

Added: pkg/PortfolioAnalytics/sandbox/testing_return_target.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_return_target.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/testing_return_target.R	2013-08-12 23:42:56 UTC (rev 2772)
@@ -0,0 +1,53 @@
+library(PortfolioAnalytics)
+library(ROI)
+require(ROI.plugin.glpk)
+require(ROI.plugin.quadprog)
+
+data(edhec)
+ret <- edhec[, 1:4]
+
+pspec1 <- portfolio.spec(assets=colnames(ret))
+pspec1 <- add.constraint(portfolio=pspec1, type="leverage", min_sum=1, max_sum=1)
+pspec1 <- add.constraint(portfolio=pspec1, type="box")
+pspec1 <- add.objective(portfolio=pspec1, type="return", name="mean", target=0.007)
+
+opt1 <- optimize.portfolio(R=ret, portfolio=pspec1, optimize_method="ROI")
+opt1
+summary(opt1)
+wts1 <- extractWeights(opt1)
+
+mean(ret %*% wts1)
+colMeans(ret) %*% wts1
+
+pspec2 <- portfolio.spec(assets=colnames(ret))
+pspec2 <- add.constraint(portfolio=pspec2, type="leverage", min_sum=1, max_sum=1)
+pspec2 <- add.constraint(portfolio=pspec2, type="box")
+pspec2 <- add.constraint(portfolio=pspec2, type="return", return_target=0.007)
+pspec2 <- add.objective(portfolio=pspec2, type="return", name="mean")
+
+opt2 <- optimize.portfolio(R=ret, portfolio=pspec2, optimize_method="ROI")
+opt2
+summary(opt2)
+wts2 <- extractWeights(opt2)
+
+mean(ret %*% wts2)
+colMeans(ret) %*% wts2
+all.equal(wts1, wts2)
+
+set.seed(123)
+opt_de1 <- optimize.portfolio(R=ret, portfolio=pspec1, optimize_method="DEoptim", search_size=4000, traceDE=5)
+opt_de1
+mean(ret %*% opt_de1$weights)
+
+set.seed(123)
+opt_de2 <- optimize.portfolio(R=ret, portfolio=pspec2, optimize_method="DEoptim", search_size=4000, traceDE=5)
+opt_de2
+mean(ret %*% opt_de2$weights)
+
+opt_rp1 <- optimize.portfolio(R=ret, portfolio=pspec1, optimize_method="random", search_size=4000)
+opt_rp1
+mean(ret %*% opt_rp1$weights)
+
+opt_rp2 <- optimize.portfolio(R=ret, portfolio=pspec2, optimize_method="random", search_size=4000)
+opt_rp2
+mean(ret %*% opt_rp2$weights)



More information about the Returnanalytics-commits mailing list