1
votes

I'm trying to solve a portfolio optimization problem with quadprog library, but solve.QP function returns this:

matrix D in quadratic function is not positive definite!

But, I'm defining Dmat as:

Dmat <- cov(diff(as.matrix(na.locf(prices))))

How can I turn Dmat in a positive definite matrix?

1
Maybe adding some regularization could help? Something on the lines of Dmat <- Dmat + lambda*diag(ncol(Dmat)) where lambda is a small constant (a hyperparameter you'd want to tune to find a good value). Although it would add some distortion to your results, it might be an acceptable tradeoff to improve the robustness of the method.Backlin
Try to check the eigenvalues by eigen(Dmat) and if one of them less than zero. There would be some problem with your matrix.Also, sometimes, a high dimension matrix would lead to a huge numerical error if the code is not prepared for such a high dimension problem or just poorly-written.Stan Yip
Try adding some small values and see what happens to the eigen values, as @galapagos suggested. To properly tune it you'd need some kind of performance metric that you optimize with cross-validation on similar. These type of questions are not really on topic for SO and I think you'd get better help at the cross-validated site though so I'll vote for a migration.Backlin
The hint by galapagos is along what is actually done. This article could be a good starting point in the literature: repositori.upf.edu/bitstream/handle/10230/560/691.pdfmts
Portfolio optimization problems use returns, not price differences so, if your prices object contains asset prices, then you'll need to compute returns using something like: temp <- as.matrix(na.locf(prices)); Dmat <- cov( diff(temp)/ head(temp,-1)). Alternatively you could convert prices to log prices and then diff would give you log returns.WaltS

1 Answers

0
votes

Thanks to the help. I discovered the cov.shrink function from corpcor library, and now I'm defining Dmat as:

cov.shrink(diff(as.matrix(na.locf(precos_mes))))

which works perfectly as a positive definite matrix.