[Returnanalytics-commits] r3022 - pkg/PortfolioAnalytics/sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Sep 7 22:45:34 CEST 2013
Author: rossbennett34
Date: 2013-09-07 22:45:33 +0200 (Sat, 07 Sep 2013)
New Revision: 3022
Modified:
pkg/PortfolioAnalytics/sandbox/testing_weight_conc.R
Log:
Added example of weight concentration penalty using solve.QP manually.
Modified: pkg/PortfolioAnalytics/sandbox/testing_weight_conc.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_weight_conc.R 2013-09-07 19:45:19 UTC (rev 3021)
+++ pkg/PortfolioAnalytics/sandbox/testing_weight_conc.R 2013-09-07 20:45:33 UTC (rev 3022)
@@ -59,3 +59,51 @@
opt4 <- optimize.portfolio(R=R, portfolio=conc, optimize_method="ROI", trace=TRUE)
opt4
chart.Weights(opt4)
+
+#####
+# Use solve.QP manually
+library(quadprog)
+
+# number of assets
+N <- ncol(R)
+
+# concentration aversion parameter
+lambda_hhi <- 2
+
+# Quadratic objective
+Q <- 2*var(R) + lambda_hhi * diag(N)
+
+# Constraints matrix and rhs for full investment and long only
+Amat <- cbind(rep(1, N), diag(N), -diag(N))
+rhs <- c(1, rep(0, N), rep(-1, N))
+
+sol <- solve.QP(Dmat=Q, dvec=rep(0, N), Amat=Amat, bvec=rhs, meq=1)
+sol$solution
+
+conc <- add.objective(portfolio=init, type="weight_concentration", name="HHI",
+ conc_aversion=2)
+
+opt <- optimize.portfolio(R=R, portfolio=conc, optimize_method="ROI", trace=TRUE)
+all.equal(opt$weights, sol$solution, check.attributes=F)
+
+# concentration aversion parameter by group
+lambda_hhi <- c(0.1, 0.05, 0.1, 0)
+
+hhi1 <- diag(N)
+hhi1[3:8,] <- 0
+
+hhi2 <- diag(N)
+hhi2[c(1:2, 5:8),] <- 0
+
+hhi3 <- diag(N)
+hhi3[c(1:4, 7:8),] <- 0
+
+hhi4 <- diag(N)
+hhi4[1:6,] <- 0
+
+Q <- 2*var(R) + lambda_hhi[1]*hhi1 + lambda_hhi[2]*hhi2 + lambda_hhi[3]*hhi3 + lambda_hhi[4]*hhi4
+
+sol <- solve.QP(Dmat=Q, dvec=rep(0, N), Amat=Amat, bvec=rhs, meq=1)
+sol$solution
+all.equal(opt3$weights, sol$solution, check.attributes=F)
+
More information about the Returnanalytics-commits
mailing list