R error QP:not 正定矩阵?

R error QP:not a positive definite matrix?

我正在尝试使用 quadprog 包在 R 中解决以下问题:

min: vec %*% p + t(p) %*% mat %*% p 
st: p >= 0

哪里

mat <- matrix(c(1162296,0,0,0,0,1,0,0,951.7089,0,1,0,-951.7089,0,0,1),4)
vec <- c(6341934541.1,175800.1,-356401.7,14398073047.1)

我用过

libary(quadprog)
solve.QP(2*mat,-vec, diag(4), integer(4))

但我不断收到以下错误:

Error in solve.QP(2*mat, -vec, diag(4), integer(4)) : 
  matrix D in quadratic function is not positive definite!

然而,cleary

> eigen(mat)$values > 0
[1] TRUE TRUE TRUE TRUE

我做错了什么?为什么这个错误一直出现?

您的矩阵 mat 不对称。 quadprog 包旨在求解二次规划,根据定义,二次规划需要最高阶项中的对称矩阵。例如,参见 here

要解决所写的这个问题,您将需要使用通用的约束优化算法。例如,您可以这样尝试 constrOptim

# system matrices
mat <- matrix(c(1162296,0,0,0,0,1,0,0,951.7089,0,1,0,-951.7089,0,0,1),4)
vec <- c(6341934541.1,175800.1,-356401.7,14398073047.1)

# an initial value
p0 <- c(1,1,1,1)

# the objective function
objective <- function(p) {
  vec %*% p + t(p) %*% mat %*% p
}

# solve -- warning!  without additional work you won't know if this is a global minimum solution.
solution <- constrOptim(p0, objective, NULL, diag(4), c(0,0,0,0))