Reputation: 13
I want to solve an optimization problem regarding a minimum variance portfolio using R as shortly described on this website: http://enricoschumann.net/R/minvar.htm
The problem is: the matrix I want to use has more columns (=assets) than rows (=observations), which is why it is not positive definite and non-invertible.
You can recreate this problem by taking the opposite values for the variables as on the website, which results in the following:
nO <- 10L ## number of observations
nA <- 100L ## number of assets
mData <- array(rnorm(nO * nA, sd = 0.05),
dim = c(nO, nA)) #Creating sample stock observations
library("quadprog")
aMat <- array(1, dim = c(1,nA))
bVec <- 1
zeros <- array(0, dim = c(nA,1))
solQP <- solve.QP(cov(mData), zeros, t(aMat), bVec, meq = 1) #Minimize optimization
solQP$solution
which results in the following error:
matrix D in quadratic function is not positive definite!
Does anybody know other functions to solve the optimization with mData or ways to make mData invertible without losing information?
The desired result are the weights for each asset for the minimum variance portfolio.
Upvotes: 1
Views: 471
Reputation: 16724
You can try:
library(Matrix)
Q = nearPD(cov(mData))$mat
and then use Q
instead of cov(mData)
.
There is also an alternative Mean-Variance model based on adjusted returns that handles your case directly. See link. Unfortunately, this is not so easy to implement using QuadProg (link).
Upvotes: 2