Github user actuaryzhang commented on a diff in the pull request:
https://github.com/apache/spark/pull/16131#discussion_r90784498
--- Diff:
mllib/src/main/scala/org/apache/spark/ml/regression/GeneralizedLinearRegression.scala
---
@@ -505,7 +505,7 @@ object GeneralizedLinearRegression extends
DefaultParamsReadable[GeneralizedLine
override def initialize(y: Double, weight: Double): Double = {
require(y >= 0.0, "The response variable of Poisson family " +
s"should be non-negative, but got $y")
- y
+ y + 0.1
--- End diff --
The issue is initializing mu = y for the case of y = 0 can lead to almost
zero weight in subsequent IWLS. I'll explain in detail below.
The following initialization method from `FamilyAndLink` shows that the
mean `mu` is initialized using `family.initialize`. So `mu` could be zero if `y
= 0`, which is incorrect since the mean of the Poisson can never be zero.
```
val newInstances = instances.map { instance =>
val mu = family.initialize(instance.label, instance.weight)
val eta = predict(mu)
Instance(eta, instance.weight, instance.features)
}
```
In each iteration of the reweighted least squares, the weight fed to WLS is
defined as follows (`reweightFunc`):
```
val eta = model.predict(instance.features)
val mu = fitted(eta)
val offset = eta + (instance.label - mu) * link.deriv(mu)
val weight = instance.weight / (math.pow(this.link.deriv(mu), 2.0)
* family.variance(mu))
```
Let's use one observation as an example. Suppose mu = y = 0. Then running
the above prints:
```
offset: -32.43970761868737 weight: 2.2177289643212133E-14
```
The weight is almost zero, which is the cause of the issue. To see why, in
the poisson case with log link, we have `(math.pow(this.link.deriv(mu), 2.0) *
family.variance(mu))` = 1/mu where `link.deriv(mu)` = 1/mu^2 and
`family.variance(mu)` is `mu`. So the weight is basically mu.
That also explains why using small epsilon as initialization may not be
helpful. I added 0.1 because that is how R does it:
```
> poisson()$initialize
expression({
if (any(y < 0))
stop("negative values not allowed for the 'Poisson' family")
n <- rep.int(1, nobs)
mustart <- y + 0.1
})
```
@srowen
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]