2

我试图了解为什么在尝试使用 solnp 解决此问题时会收到警告消息?以下是我收到的消息 -

solnp--> Solution not reliable....Problem Inverting Hessian.
Warning message:
In p0 * vscale[(neq + 2):(nc + np + 1)] :
  longer object length is not a multiple of shorter object length

以下是代码

countw <- 100
Dmat = diag(1, 100, 100)
# Equality constraints
eq_A <- rep(1, countw)
eq_b <- 1

# Constraint wts greater than zero
ineq_A <- diag(x = 1, nrow = countw, ncol = countw)
ineq_b <- rep(0, countw)

# Combine constraints
heq <- eq_A
hin <- ineq_A

theta <- c(0.51, 0.49, rep(0, countw-2))

krdsolnp <- solnp(par = theta, 
                  fun = function(x) -c(t(x) %*% Dmat %*% x), 
                  ineqfun = function(x) c(hin %*% x),
                  ineqLB = rep(0, countw),
                  ineqUB = rep(1, countw),
                  eqfun = function(x) c(heq %*% x),
                  eqB = eq_b)
4

1 回答 1

1

这段代码相当于在问:如何最大化sum(x^2) ,同时保持x的系数在 0 和 1 之间,并保持sum(x)等于 1?

该库尝试使用目标函数的 Hessian 矩阵来解决此问题,即sum(x^2)相对于x的任何一对系数的偏导数矩阵。Hessian 通常是单位矩阵的 2 倍,这是可逆的。

然而,一些关于约束的东西把它扔掉了。您可以通过将初始条件theta中的 0 更改为 0.01来避免该错误。

于 2016-10-17T22:23:01.283 回答