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?
Dmat <- Dmat + lambda*diag(ncol(Dmat))
wherelambda
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