[Returnanalytics-commits] r2931 - in pkg/PortfolioAnalytics: R sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 29 18:49:14 CEST 2013


Author: rossbennett34
Date: 2013-08-29 18:49:14 +0200 (Thu, 29 Aug 2013)
New Revision: 2931

Added:
   pkg/PortfolioAnalytics/sandbox/testing_risk_budget.R
Modified:
   pkg/PortfolioAnalytics/R/constrained_objective.R
Log:
Adding penalty for risk_budget_objective when min_concentration=TRUE. Adding testing script for equal CVaR pct_contrib to sandbox - includes examples with and without cleaned returns.

Modified: pkg/PortfolioAnalytics/R/constrained_objective.R
===================================================================
--- pkg/PortfolioAnalytics/R/constrained_objective.R	2013-08-29 10:47:04 UTC (rev 2930)
+++ pkg/PortfolioAnalytics/R/constrained_objective.R	2013-08-29 16:49:14 UTC (rev 2931)
@@ -662,12 +662,20 @@
           # Combined min_con and min_dif to take advantage of a better concentration obj measure
           if(!is.null(objective$min_difference) || !is.null(objective$min_concentration)){
             if(isTRUE(objective$min_difference)){
-              #                     max_diff<-max(tmp_measure[[2]]-(sum(tmp_measure[[2]])/length(tmp_measure[[2]]))) #second element is the contribution in absolute terms
+              # max_diff<-max(tmp_measure[[2]]-(sum(tmp_measure[[2]])/length(tmp_measure[[2]]))) #second element is the contribution in absolute terms
               # Uses Herfindahl index to calculate concentration; added scaling perc diffs back to univariate numbers
               max_diff <- sqrt(sum(tmp_measure[[3]]^2))/100  #third element is the contribution in percentage terms
               # out = out + penalty * objective$multiplier * max_diff
               out = out + penalty*objective$multiplier * max_diff
             }
+            if(isTRUE(objective$min_concentration)){
+              # use HHI to calculate concentration
+              # actual HHI
+              act_hhi <- sum(tmp_measure[[3]]^2)
+              # minimum possible HHI
+              min_hhi <- sum(rep(1/length(tmp_measure[[3]]), length(tmp_measure[[3]]))^2)/100
+              out <- out + penalty * objective$multiplier * abs(act_hhi - min_hhi)
+            }
           }
         } # end handling of risk_budget objective
         

Added: pkg/PortfolioAnalytics/sandbox/testing_risk_budget.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_risk_budget.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/sandbox/testing_risk_budget.R	2013-08-29 16:49:14 UTC (rev 2931)
@@ -0,0 +1,79 @@
+library(PortfolioAnalytics)
+library(DEoptim)
+data(indexes)
+indexes <- indexes[,1:4]
+# Clean the returns first rather than passing in as an argument
+R.clean <- Return.clean(R=indexes[, 1:4], method="boudt")
+
+# Create the portfolio specification object
+init.portf <- portfolio.spec(assets=colnames(indexes[,1:4]))
+# Add box constraints
+init.portf <- add.constraint(portfolio=init.portf, type='box', min = 0, max=1)
+# Add the full investment constraint that specifies the weights must sum to 1.
+init.portf <- add.constraint(portfolio=init.portf, type="full_investment")
+
+# Add objective for min CVaR concentration
+min_conc <- add.objective(portfolio=init.portf, type="risk_budget_objective",
+                         name="CVaR", arguments=list(p=0.95),
+                         min_concentration=TRUE)
+
+# Add objective for min CVaR difference
+min_diff <- add.objective(portfolio=init.portf, type="risk_budget_objective",
+                          name="CVaR", arguments=list(p=0.95),
+                          min_difference=TRUE)
+
+# min concentration
+set.seed(1234)
+opt_min_conc <- optimize.portfolio(R=R.clean, portfolio=min_conc, 
+                                   optimize_method="DEoptim", search_size=5000)
+# near equal risk pct_contrib portfolio
+print(opt_min_conc)
+
+# min difference
+set.seed(1234)
+opt_min_diff <- optimize.portfolio(R=indexes, portfolio=min_diff, 
+                                   optimize_method="DEoptim", search_size=5000)
+# Not getting an equal risk contrib using min_difference in risk_budget_objective
+# US Bonds have a approx a 2.5% contrib and the rest are 30%-35%
+print(opt_min_diff)
+
+# min difference with cleaned returns
+set.seed(1234)
+opt_min_diff_clean <- optimize.portfolio(R=R.clean, portfolio=min_diff, 
+                                   optimize_method="DEoptim", search_size=5000)
+# near equal risk pct_contrib with cleaned returns
+print(opt_min_diff_clean)
+
+# demonstrate error with clean="boudt" in arguments in objective
+# Add objective for min CVaR concentration
+min_conc_clean <- add.objective(portfolio=init.portf, type="risk_budget_objective",
+                                name="CVaR", arguments=list(p=0.95, clean="boudt"),
+                                min_concentration=TRUE)
+
+# If I pass in arguments for dots such as itermax=50, then I get an error with
+# clean.boudt.
+# Error in clean.boudt(na.omit(R[, column, drop = FALSE]), alpha = alpha,  : 
+#                       unused argument(s) (itermax = 50)
+set.seed(1234)
+opt <- optimize.portfolio(R=R.clean, portfolio=min_conc_clean, 
+                          optimize_method="DEoptim", search_size=5000,
+                          itermax=50)
+traceback()
+
+# Upon insepecting traceback(), it looks like the error is due to
+# Return.clean(R, method = objective$arguments.clean, ...) where the dots
+# are picking up the dots arguments from optimize.portfolio. Is there a way
+# to correct this? I suppose one way is to clean the returns first and not 
+# specify clean in the arguments list in the objective. The dots argument in
+# optimize.portfolio can then be used to control the parameters for the solvers.
+
+# library(iterators)
+# set.seed(1234)
+# out <- optimize.portfolio.rebalancing(R=indexes, portfolio=ObjSpec, 
+#                                       optimize_method="DEoptim", search_size=5000,
+#                                       rebalance_on="months", 
+#                                       training_period=nrow(indexes)-1)
+# 
+# ES(indexes[,1:4], weights=out$weights, p=0.95, clean="boudt", 
+#    portfolio_method="component")
+# constrained_objective(w=rep(1/4, 4), R=indexes[,1:4], portfolio=ObjSpec)



More information about the Returnanalytics-commits mailing list