1

Some problems come up when I use the function lsqlincon() of the package pracma in R. I want to solve a quadratic optimization with linear equality and inequality constraints. For example, the least square solution with the constraint that every entry of the solution must be larger or equal to zero and the sum of all entries of the solution must be one. I use the following code:

library(MASS)
set.seed(1)
X <- mvrnorm(n = 100, mu = rep(0, 10), Sigma = diag(1, 10))
y <- rnorm(100)

library(pracma)
C <- X
d <- y
Aeq <- matrix(rep(1, ncol(C)), 1)
beq <- 1
lb <- rep(0, ncol(C))
(weight <- lsqlincon(C = C, d = d, Aeq = Aeq, beq = beq, lb = lb))

[1]  1.711997e-01 -7.383626e-21  1.707092e-01  1.361470e-01  1.475174e-01
[6]  8.595312e-02  1.217463e-01  1.391139e-01  2.761332e-02  0.000000e+00

But the second entry of the weight return is -7.383626e-21, and it's a negative number. If I try to use lb <- rep(1e-18, ncol(C)), the problem above still exists:

lb <- rep(1e-18, ncol(C))
(weight <- lsqlincon(C = C, d = d, Aeq = Aeq, beq = beq, lb = lb))

[1]  1.711997e-01 -7.383626e-21  1.707092e-01  1.361470e-01  1.475174e-01
[6]  8.595312e-02  1.217463e-01  1.391139e-01  2.761332e-02  0.000000e+00

It's really strange and I don't know why. How could I fix this? Treat -7.383626e-21 as 0?

JWen
  • 11
  • 1
  • https://stackoverflow.com/q/9508518/1412059 – Roland Sep 28 '20 at 09:16
  • Yes, you should present the results as rounded values. `format.pval` is often used to do this for p-values. It might give you some inspiration. – Roland Sep 28 '20 at 09:18
  • Thank you. I found a similar question here: [Nonnegative constraints give a solution with negative entries #406](https://github.com/cvxgrp/cvxpy/issues/406), maybe it's fine behavior and I can treat the very very small negative number as zero. I try to find some theoretical, or concise explanation and haven't found it yet. – JWen Sep 29 '20 at 01:57

0 Answers0