Looking for a way to do this in Python. scipy.optimize.nnls forces all coefficients to be positive.

Some additional context: I have a data frame with a some explanatory variables and a response variable. When I run a regular linear regression, the coefficients of some explanatory variables become negative. This is okay for some variables, but not for all. I want to prevent the coefficients of specific variables from becoming negative.

I want to force these coefficients to be positive because they should have a positive contribution to the response. The variables I want to have positive coefficients are investment dollars in a few different channels. The response is revenue. I don't want my model to say that investing more dollars in a certain channel lowers revenue (even if that creates a more accurate model).

  • $\begingroup$ Hi, welcome to the forum. You need to add more context so that people can give you good answers. $\endgroup$
    – Peter
    Nov 1 '19 at 20:33
  • $\begingroup$ @Peter I added some more context. Let me know if there is anything else I should answer. $\endgroup$
    – Kyle Zengo
    Nov 1 '19 at 20:50
  • $\begingroup$ Why do you want to prevent variables to become zero? Kuhn-Tucker is for a very specific type of (production) function. Can you add code/plots? Still not clear to me what the actual problem is, Cheers... en.m.wikipedia.org/wiki/Karush–Kuhn–Tucker_conditions $\endgroup$
    – Peter
    Nov 1 '19 at 20:59
  • $\begingroup$ @Peter I added in my motivation. I don't have much code written yet. $\endgroup$
    – Kyle Zengo
    Nov 1 '19 at 21:12

Sorry, but on the surface, this sounds like a terrible idea to me: if linear regression gives you negative coefficients for some explanatory variables that you think should be positive, then it means that either your data is "wrong" (typically noisy or too small) or your intuition is misguided.

I can't see any good reason why one would use a data-driven approach if the goal is to manually force the model in a particular way. This is the equivalent of breaking the thermometer to hide the fever.

I'd suggest the following instead:

  1. In general an unexpected outcome is arguably a good thing, in the sense that it tells us something we didn't know about the data. That's a cue to investigate what happens in the data. Linear regression is simple enough to analyze: one can look at the correlation, plot the relation between the variables etc.
  2. If there's really something suspicious going on with some variables, maybe some errors in the data which make them behave in a way they shouldn't, then it's much better to discard them altogether from the model rather than fixing their coefficient, because this way the model won't rely on them at all.
  • 1
    $\begingroup$ Data is most often too noisy and too small. Having such a regression model helps to filter the noise or "anomalies", which is exactly what we want to do in some cases. In those cases, a better answer may be found here: datascience.stackexchange.com/a/5019/1281 $\endgroup$
    – Trylks
    Sep 9 '20 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.