0

I am using SLSQP function in NLOPTR for portfolio construction using 34 low volatility stocks. The stocks are not important. What I am trying to do is being able to impose a minimum weight and a maximum weight to each stock.

The hin function, imposes the minimum weight constraint - as can be seen here - that each weight must be at least 1% in the construction of the portfolio; this is seen by the '(x - 0.01)'

 hin <- function(x){
      return(x - 0.01)
    }

I don't know how to now add a maximum constraint (of say 15% max weight per stock) to the function as well as having the minimum constraint. Thus will have a weighting constraint of minimum 1% and maximum 15%.

Can someone help me to create a minimum and a maximum constraint.

My code for running the optimisation is as follows:

modvol <- subset(matret, select=modvolreturns)
covmodvol <- cov(modvol)
var(modvol[,1])

size <- 34

fn <- function(x){
  return(t(x)%*%covmodvol%*%x)
}

hin <- function(x){
  return(x - 0.01)
}

heq <- function(x){
  return(sum(x)-1)
}

startx <- vector("numeric",size)
startx[1:size] <- 1/size

mvportmodvol <- slsqp(startx,fn,hin = hin,heq = heq)
  • You can pass on lower and upper bounds on the variables to the `slsqp` call through the `lower` and `upper` parameters. – Erwin Kalvelagen Sep 28 '16 at 04:14
  • @ErwinKalvelagen thank you very much. Do you by any chance know of a shortcut so I don't have to write out the upper bound for all 34 variables like I have done below. `upper <- c(0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15,0.15)` – Nicholas Olds Sep 28 '16 at 10:20
  • `upper <- rep(0.15,34)` – Erwin Kalvelagen Sep 28 '16 at 11:11
  • @ErwinKalvelagen Thank you so much! – Nicholas Olds Sep 30 '16 at 06:57

0 Answers0