# [R-SIG-Finance] Failure of solve.QP in portfolio modeling

Enrico Schumann es at enricoschumann.net
Tue Sep 22 16:56:44 CEST 2015

```On Tue, 22 Sep 2015, aschmid1 <aschmid1 at stevens.edu> writes:

> Hi everyone,
> I'm trying to estimate optimal Markowitz portfolio weights for a list
> of stocks chosen upon some criterion using solve.QP from quadprog
> library. When the number of stocks N reaches some limit, I get a
> message "matrix D in quadratic function is not positive definite." For
> example, if I rebalance every 6 weeks (which implies that variance is
> calculated for 6-week interval prior to the period for which I
> calculate portfolio weights), I can get solution for 25>=N<50. For
> 12-week interval, solution exists for 50>=N<100, and for 24-week
> interval, I can get solution for N=100. My attempt to remedy this
> problem with Higham's method doesn't help. I'll greatly appreciate you
> input: first, why this may happen (can there be lack of local
> minimum?), and second, whether there are R solvers that may need only
> semi positive definite matrix.
>
> Thanks! Alec
>

The thing you may want to look up is the "rank" of a matrix.

For instance, I create a small data set R -- suppose these were
daily-returns data of 10 equities.

na   <- 10 ## number of assets
nobs <- 10 ## number of observations

R <- array(rnorm(nobs * na, sd = 0.01), dim = c(nobs, na))
qr(cov(R))\$rank

The rank of the covariance matrix is only 9; you need na+1 observations
to get full rank. You can still compute the standard deviation of a
portfolio:

ew <- rep(1/na, na) ## equal-weight portfolio
sqrt(ew %*% cov(R) %*% ew)

But with a non-full rank matrix and no constraints, it is guaranteed
that you have portfolios like this one:

zerovol <- svd(cov(R))\$v[,10]
sqrt(abs(zerovol %*% cov(R) %*% zerovol))

You get a zero-volatility portfolio.

Whether that matters depends on your application. With constraints,
perhaps not.

An example in which it does not matter is in Section "1.3
Redundant assets" in
https://cran.r-project.org/web/packages/NMOF/vignettes/TAportfolio.pdf

There you also have an example of a solver. A lengthier discussion is in
Section "13.2.5 Repairing Matrices" in this book

@BOOK{Gilli2011b,
title        = {Numerical Methods and Optimization in Finance},
year         = 2011,
author       = {Gilli, Manfred and Maringer, Dietmar and Schumann,
Enrico}
}

of which [disclosure], I am a co-author.

--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

```