我稍微更改了您的代码:
library(Rsolnp)
set.seed(1)
sample.data <- matrix(rnorm(10*1000),ncol=10)
gamma <- 1
#OBJECTIVE FUNCTION TO MINIMIZE
fn <- function(x){
cov.Rt <- cov(sample.data);
as.numeric(t(x) %*% cov.Rt %*% x)
}
#EQUALITY CONSTRAINT
eqn <- function(x){
one.vec <- matrix(1, ncol=10, nrow=1)
as.numeric(one.vec %*% x)
}
constraints <- 1
#INEQUALITY CONSTRAINT
ineq <- function(x){
one.vec <- matrix(1, ncol=10, nrow=1);
z1<-one.vec %*% abs(x)
as.numeric(z1)
}
uh <- gamma #UPPER BOUND
lb <- 0 #LOWER BOUND
x0 <- matrix(1, 10, 1) #STARTING PARAMETER VECTOR (NOT SURE WHAT STARTING VALUES TO PUT HERE)
sol1 <- solnp(x0, fun=fn, eqfun=eqn, eqB=constraints)
当我们运行上面的代码时,我们得到了一个解决方案:
Iter: 1 fn: 0.09624 Pars: 0.08355 0.08307 0.10154 0.09108 0.11745 0.12076 0.09020 0.09435 0.10884 0.10918
Iter: 2 fn: 0.09624 Pars: 0.08354 0.08308 0.10153 0.09107 0.11746 0.12078 0.09021 0.09434 0.10883 0.10918
solnp--> Completed in 2 iterations
但是当我们将不等式限制添加到优化问题中时,我们就会遇到问题:
> sol2 <- solnp(x0, fun=fn, eqfun=eqn, eqB=constraints,
ineqfun=ineq, ineqUB=gamma, ineqLB=lb)
Iter: 1 fn: 0.09624 Pars: 0.08356 0.08305 0.10153 0.09111 0.11748 0.12078 0.09021 0.09431 0.10881 0.10916
solnp-->The linearized problem has no feasible
solnp-->solution. The problem may not be feasible.
Iter: 2 fn: 272.5459 Pars: 4.44541 4.42066 5.40272 4.84595 6.25082 6.42718 4.80020 5.02004 5.79138 5.81029
solnp-->The linearized problem has no feasible
solnp-->solution. The problem may not be feasible.
Iter: 3 fn: 272.5459 Pars: 4.44547 4.42070 5.40274 4.84596 6.25078 6.42712 4.80023 5.02006 5.79138 5.81023
Iter: 4 fn: 0.09624 Pars: 0.08357 0.08304 0.10157 0.09106 0.11744 0.12074 0.09021 0.09432 0.10886 0.10918
Iter: 5 fn: 0.09625 Pars: 0.08354 0.08308 0.10153 0.09107 0.11747 0.12078 0.09021 0.09434 0.10883 0.10919
Iter: 6 fn: 0.09717 Pars: 0.08394 0.08347 0.10201 0.09150 0.11803 0.12135 0.09064 0.09479 0.10935 0.10971
Iter: 7 fn: 0.09624 Pars: 0.08353 0.08307 0.10153 0.09106 0.11747 0.12078 0.09020 0.09433 0.10883 0.10919
Iter: 8 fn: 0.09624 Pars: 0.08353 0.08307 0.10153 0.09106 0.11747 0.12078 0.09020 0.09433 0.10883 0.10919
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
让我们尝试稍微改变一下 gamma:
gamma <- 1.01
> sol2 <- solnp(x0, fun=fn, eqfun=eqn, eqB=constraints,
ineqfun=ineq, ineqUB=gamma, ineqLB=lb)
Iter: 1 fn: 0.09624 Pars: 0.08355 0.08307 0.10153 0.09108 0.11745 0.12076 0.09020 0.09435 0.10884 0.10918
Iter: 2 fn: 0.09624 Pars: 0.08354 0.08308 0.10153 0.09107 0.11746 0.12078 0.09021 0.09434 0.10883 0.10918
solnp--> Completed in 2 iterations
所以你的不等式约束似乎完全围绕着等式约束。还把这两个约束放在一起看,我觉得有点奇怪。我的猜测是,您可能错误地指定了您的不等式约束,并且只是想要卖空约束之类的东西,例如。权重介于 0 和 1 之间。这可以按照您的方法通过以下方式归档:
ineq <- function(x){ return(x) }
uh <- rep(gamma, 10) #UPPER BOUND
lb <- rep(0, 10) #LOWER BOUND
sol3 <- solnp(x0, fun=fn, eqfun=eqn, eqB=constraints, ineqfun=ineq, ineqUB=uh, ineqLB=lb)