0

Due to the lack of knowledge of CVXPY, I have a problem with reconciling a simple optimization problem's results when using different versions of it.

When I use CVXPY with version 0.4.5 I wrote my problem as:

import numpy as np
from cvxpy import * 

n = 5
np.random.seed(123)
g1 = np.random.rand(2*n, 1)
H1 = np.eye(2*n)
w = Variable(2*n)
gamma = Parameter(sign="positive")
ret = -g1.T*w
risk = quad_form(w, H1)
prob = Problem(Maximize(ret - gamma*risk), 
               [w >= 0])
gamma.value = 0.5
prob.solve()
res = w.value

and the res equals to:

 res = [[  2.86653834e-12],
    [  2.47912037e-11],
    [  3.73027873e-11],
    [  7.13532730e-12],
    [  2.31133274e-12],
    [  1.27710498e-11],
    [ -2.50944234e-12],
    [  3.15803733e-12],
    [  9.90353521e-12],
    [  1.46452182e-11]]

However, When I use CVXPY with version 1.0.8, I type almost the same codes as follows:

n = 5 
np.random.seed(123)
g1 = np.random.rand(2*n, 1)
H1 = np.eye(2*n)
w = Variable(2*n)
gamma = Parameter(nonneg=True)
ret = -g1.T*w
risk = quad_form(w, H1)
prob = Problem(Maximize(ret - gamma*risk), 
               [w >= 0])
gamma.value = 0.5
prob.solve()
res = w.value

The result is:

(Pdb) res
    array([6.66098380e-25, 2.73633363e-25, 2.16955532e-25, 5.27275998e-25,
   6.88070573e-25, 4.04646723e-25, 9.37904145e-25, 6.54954091e-25,
   4.60002892e-25, 3.75018828e-25])

The only difference I made when using version 1.0.8 of CVXPY is that I use attribute 'nonneg=True' instead of 'sign=positive' which I think they are essentially the same thing. Can someone help me out here? What are the possible reasons that the results are so different?

Many thanks

user45668
  • 31
  • 5
  • The results look approximately the same to me: 0 for all w's. The difference is noise. – Erwin Kalvelagen Nov 16 '18 at 21:41
  • It's possible the difference is noise, since if I flip the sign of g1 then the results become the same. But what is weird is the noise shouldn't be this large. They are certainly not on the same scale. And if I tested other cases, the difference will be larger to the extent affecting the decision making of the application. – user45668 Nov 16 '18 at 22:29
  • If this is the noise I guess my question is when can we trust the results in similar cases since those noises can give us pretty different results. – user45668 Nov 16 '18 at 22:59
  • Note that interior point solvers require that variables stay strictly positive when we have a bound `x>=0`. How close they will get to the bound depend on many things. You can solve with the verbose option to see the solver logs. There may be some indications what is different. But again, for interior point solvers it is normal that variables are somewhat inside their bounds. – Erwin Kalvelagen Nov 17 '18 at 10:40

1 Answers1

1

CVXPY 1.0 uses the OSQP solver for problems like yours, whereas CVXPY 0.4 uses ECOS. That's why the results differ. But ultimately numbers very close to zero should be treated as zero. If your program behaves differently if the output is -1e-12 versus 1e-12 you may want to make the program less sensitive.

steven
  • 671
  • 4
  • 8