Skip to content

Allow for box constraints on coefficients? #14

Description

@tomwenseleers

I was wondering if it would be possible by any chance to support extra arguments lower.limits and upper.limits with vectors of lower & upper box constraints on the coefficients. I would like to fit a nonnegative Poisson GLM, but in the absence of nonnegativity constraints this always blows up. In my experience, just imposing these constraints in each IRLS iteration by clipping coefficients to the allowed range works quite well. Otherwise, the osqp quadratic programming solver is also quite efficient and would allow formal box constraints & works both for dense or sparse covariate matrices :

constrainedLS_osqp <- function(y, X,
                               lower=rep(0, ncol(X)), upper=rep(Inf, ncol(X)),
                               x.start = NULL, y.start = NULL) {
  require(osqp)
  require(Matrix)
  XtX = crossprod(X,X)
  Xty = crossprod(X,y)
  
  settings = osqpSettings(verbose = FALSE, eps_abs = 1e-8, eps_rel = 1e-8, linsys_solver = 0L,
                          warm_start = FALSE)
  pff = .sparseDiagonal(ncol(X))
  model <- osqp(XtX, -Xty, pff, l=lower, u=upper, pars=settings)
  
  if (!is.null(x.start)) model$WarmStart(x=x.start, y=y.start)

  coefs = model$Solve()$x # fitted coefficients

  coefs = pmax(lower, pmin(coefs, upper) ) # fitted coefficients sometimes go very slightly outside constraint zone due to numerical inaccuracies in solver - this is fixed here via clipping

  return(coefs)
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions