LightGBM icon indicating copy to clipboard operation
LightGBM copied to clipboard

[python package]: Poisson regression returns negative values

Open j-mark-hou opened this issue 9 years ago • 9 comments

This is strange because poisson regression should only return positive values. Consider the example below.

A few points:

  1. treating both X columns as continuous instead of categoricals makes the bug go away.
  2. setting the size of the poisson noise lam to 1 makes the bug go away
  3. making the X and Y less skewed (e.g. just poisson instead of this lognormal thing) makes the bug go away

This is a synthetic example, and very few of the outputs are negative here. In some real life examples I've encountered, the number of negatives has been much larger, say ~10% of all observations. These observations for which the output is negative tend to be observations where the y-value should intuitively be small, so that just substituting 0 is not such a bad stopgap measure. However, given that poisson regression just should never return negatives, I'm a bit puzzled by this behavior.

import lightgbm as lgb
import numpy as np
import pandas as pd

n=100000
lam = .01
X = np.floor(np.random.lognormal(size=(n,2))).astype(int)
y = np.maximum(X[:,0],X[:,1])+np.random.poisson(lam=lam, size=n)

train_inds = np.arange(int(n/3))
val_inds =   np.arange(int(n/3), int(2*n/3))
test_inds =   np.arange(int(2*n/3), int(n))

X_test, y_test = X[test_inds,:], y[test_inds]

ds = lgb.Dataset(X,y, categorical_feature=[1])

ds_train = ds.subset(train_inds)
ds_val = ds.subset(val_inds)

params = {'objective':'poisson',
         'metric':'rmse',
         'learning_rate':.1
         }
gbm = lgb.train(params, ds_train, num_boost_round=300, early_stopping_rounds=20, valid_sets=[ds_val, ds_train], 
                verbose_eval=100, categorical_feature=[1])

yhat = gbm.predict(X_test)
print('neg obs:', len(yhat[yhat<0]))

# Training until validation scores don't improve for 20 rounds.
# [100]	training's rmse: 0.813582	valid_0's rmse: 0.87278
# [200]	training's rmse: 0.532022	valid_0's rmse: 0.672167
# [300]	training's rmse: 0.4245	valid_0's rmse: 0.615395
# neg obs: 9

j-mark-hou avatar Aug 08 '17 22:08 j-mark-hou

I'm thinking Poisson regression is missing an exponential, because the gradient is currently the L2/MSE gradient: https://github.com/Microsoft/LightGBM/blob/master/src/objective/regression_objective.hpp#L317-L332

ping @henry0312

Laurae2 avatar Aug 08 '17 22:08 Laurae2

i should mention that that the mean values of the predicted values match up quite well with the mean values of the actual target y, so in particular the problem is not that predict() is returning the log of the mean. perhaps I'm misunderstanding you?

j-mark-hou avatar Aug 08 '17 22:08 j-mark-hou

@j-mark-hou Gradient descent for Poisson regression is supposed to optimize for y - exp(x) (Poisson gradient), not y - x (L2 gradient).

Laurae2 avatar Aug 08 '17 22:08 Laurae2

ah, understood. good find!

j-mark-hou avatar Aug 08 '17 22:08 j-mark-hou

It looks fixable by simply implementing the ConvertOutput method as the exp(). But I'm not sure it is the best way since it implies a bunch of silly log(exp(score)) calculations at all points.

olofer avatar Aug 09 '17 16:08 olofer

@henry0312

guolinke avatar Aug 09 '17 23:08 guolinke

I'm sorry for late response. I've been in my short summer vacation, so I'll check and reply next week (as soon as possible).

henry0312 avatar Aug 11 '17 14:08 henry0312

Guys, we can refer:

@olofer I'm sorry, I can't understand what you mean. Please give me some sample codes (or diffs).

henry0312 avatar Aug 15 '17 01:08 henry0312

Take a look at this edit: https://github.com/olofer/LightGBM/commit/8782ae9efc60b6ea635cb694e18a87ea9be75de0

I have not tried it. Look at the code comment in the file for an explanation what it is supposed to do.

olofer avatar Aug 16 '17 01:08 olofer