2

I have a regression of the form model = sm.GLM(y, X, w = weight).

Which ends up being a simple weighted OLS. (note that specificying w as the error weights array actually works in sm.GLM identically to sm.WLS despite it not being in the documentation).

I'm using GLM because this allows me to fit with some additional constraints using fit_constrained(). My X consists of 6 independent variables, 2 of which i want to constrain the resulting coeffecients to be positive. But i can not seem to figure out the syntax to get fit_constrained() to work. The documentation is extremely bare and i can not find any good examples anywhere. All i really need is the correct syntax for imputing these constraints. Thanks!

wj wj
  • 75
  • 6

1 Answers1

2

The function you see is meant for linear constraints, that is a combination of your coefficients fulfill some linear equalities, not meant for defining boundaries.

The closest you can get is using scipy least squares and defining the boundaries, for example, we set up some dataset with 6 coefficients:

from scipy.optimize import least_squares
import numpy as np

np.random.seed(100)
x = np.random.uniform(0,1,(30,6))
y = np.random.normal(0,2,30)

The function to basically matrix multiply and return error:

def fun(b, x, y):
    return b[0] + np.matmul(x,b[1:]) - y

The first coefficient is the intercept. Let's say we require the 2nd and 6th to be always positive:

res_lsq = least_squares(fun, [1,1,1,1,1,1,1], args=(x, y),
                        bounds=([-np.inf,0,-np.inf,-np.inf,-np.inf,-np.inf,0],+np.inf))

And we check the result:

res_lsq.x

array([-1.74342242e-01,  2.09521327e+00, -2.02132481e-01,  2.06247855e+00,
       -3.65963504e+00,  6.52264332e-01,  5.33657765e-20])
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • correction to first sentence should be: "fulfill some linear equalities", statsmodels does not support inequality constraints that can be binding. inference is nonstandard and difficult in that case – Josef Dec 10 '20 at 21:47
  • I already found a workaround for this issue but thanks for this succint explanation nonetheless! – wj wj Dec 11 '20 at 21:55
  • thanks @josef for the correction. and glad you have also found a workaround wj wj – StupidWolf Dec 11 '20 at 22:19