what optimizer I can use in R to solve the following portfolio optimization problem:

1. $ -a \le \sum_{i=1} ^{n} x(i) \le b$
2. $ -c \le x(i) \le d$
3. $ e \le \sum _{i=1} ^n |x(i)| \le f$
4. $\sqrt{x^t\Sigma x} \le g$

a,b,c,d,e,f,g - positive. f is a vector. x - weights. $\Sigma$ var-covar matrix estimated from historical data. The problem without constrain 4 can be solved with linear solver ( with some tricks for condition 3). Condition 4 makes constrain non-linear, but quadratic. Any suggestion would be helpful. Thanks.


1 Answer 1


You can try the solnp() function from the package Rsolnp, which is a solver for nonlinear optimization. First, you define your function that is to be minimized:

fun1 <- function(x) ( return(as.numeric(fvec %*% x)))

fvec is your fixed vector $f$.

Then you define the inequality function for the inequalities 1,3 and 4 - inequality 2 is covered by the parameter boundaries in the solver.

ineqfun1 <- function(x){
  ineq1 <- sum(x);
  ineq2 <- sum(abs(x));
  ineq3 <- sqrt(x %*% vcovmat %*% x);

vcovmat is your var-cov matrix $\Sigma$.

After additionally defining the boundaries a to g and a starting estimate for x, which we call xstart, you can do the optimization:

sol <- solnp(pars=xstart, fun=fun1, ineqfun=ineqfun1,
             LB=-c, UB=d)

If the solver converges, you can get the resulting weight estimates with sol$pars.
I have no data to try it, but the code should work. You will have to choose a sensible starting value and appropriate boundaries a-g, though.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.