简体   繁体   中英

R minimize portfolio function with gradient

I want to minimize function

f <- function(u){
  return(-(1+u[1]+u[2]+u[3]+u[4]))
}

with gradient grad

And I have constraints:

1) u[1]+u[2]+u[3]+u[4] = 1

2) 0<=u[1]<=1, 0<=u[2]<=1, 0<=u[3]<=1, 0<=u[4]<=1

How to make it correctly? I can make it only for 2 constraint

optim(par=c(0,0,0,0), fn=f,lower=c(0, 0, 0, 0), upper=c(1, 1, 1, 1),method="L-BFGS-B")

But 1 constraint is not true in this case

Maybe you can try fmincon from package pracma like below

pracma::fmincon(c(0,0,0,0), 
        f, 
        gr = grad, 
        Aeq = cbind(1,1,1,1), 
        beq = 1,
        lb = c(0,0,0,0), 
        ub = c(1,1,1,1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM