I am using quadprog
to do an optimization with two constraints (sum of asset weights equals 1) and asset weights are between 0 and 10%. Everything works fine but there is some inverse of my result going on that I do not understand. When TAU
(my risk tolerance) is at its lowest (=0) I should have the lowest value in my results matrix. This is saying that at at my lowest risk tolerance I will have my lowest return. This follows Modern Portfolio Theory. Likewise when TAU
is at its highest my return vector should also be at its highest. I know there is some inverse or sign error going on. Can someone explain?
mu #return vector of our 30 assets
Dmatrix <- Q #covariance matrix
na <- 30 #number of assets
wmin <- 0 #minimum weight of an asset
wmax <- 0.1 #maximum weight of an asset, 10% = 0.1
A <- rbind(1,-diag(na), diag(na)) #A matrix with proper diagonals for linear algebra
b <- c(1, -rep(wmax, na), rep(wmin, na)) #b vector to store weights
print(b)
# [1] 1.0 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 -0.1
#[26] -0.1 -0.1 -0.1 -0.1 -0.1 -0.1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
#[51] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
#cbind(A, b)
TAU <- seq(0, 0.5, by = 0.001) ## choose an appropriate stepsize
results <- numeric(length(TAU))
weights <- array(NA, dim = c(na, length(TAU)))
for (i in seq_along(TAU)) {
solQP <- solve.QP(Dmat = Dmatrix,
dvec = TAU[i]*mu,
Amat = t(A),
bvec = b, meq = 1)
## an equivalent computation
## NMOF::mvPortfolio(mu, V, wmax = 0.1, lambda = c(TAU[i], 0.5))
results[i] <- solQP$value
weights[, i] <- solQP$solution
}
results[1] #TAU = 0
#[1] 0.2214824
results[501] #TAU = 0.5
#[1] 0.2046475