You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Decrease abolute tolerance in allclose to detect perfect separation earlier #659
exog is singular
looks like we don't have consistent behavior across optimizers
it also looks like bfgs is getting a pinv solution, and is not checking for perfect separation
>>> res = sm.Logit(yes, data).fit(method="bfgs")
Optimization terminated successfully.
Current function value: 0.000008
Iterations: 18
Function evaluations: 19
Gradient evaluations: 19
>>> res.params
array([ 7.14622756, -24.69727545, 7.14622756])
>>> res.predict()
array([ 0.99999938, 0.99999938, 0. , 0.00003029])
Right. Like I said, I didn't even look at it. So one issue here is BFGS does not check for perfect separation. The other issue is that the we're not dropping perfectly collinear columns.
Yeah, not trying to rehash this right now, but I'm not very happy with the current state of affairs here. We get a condition number warning in summary on pretty much any real data, but we don't handle perfectly collinear variables. Seems like a wart to me.
Except for solutions similar to using pinv which result in parameter estimates that are difficult to interpret, we will get into scikit-learn territory with variable selection, and soon will get k_vars > nobs.
Except for solutions similar to using pinv which result in parameter
estimates that are difficult to interpret, we will get into scikit-learn
territory with variable selection, and soon will get k_vars > nobs.
Maybe we should at least provide a helper function to identify near perfect
collinearity in variables, even if we don't use it by default. I recall
Chuck posting an example in the past that could be fixed up to do this.
I'll have to look back at some of my linear algebra notes though. That way,
we have it for identifying variables and can point users to it without too
much of a hassle.
I think the second point is a bit of a non-sequitur. Stata drops collinear
variables, but I don't think anyone would accuse stata of doing too much
hidden variable selection. And ill-defined problems is also another issue.
Not sure where else to park this, but I was just reading about perfect prediction.
Perfect separation occurs when for
y_t = 0 X_t \beta < 0
y_t = 1 X_t \beta > 0
So in the binary case, we can test for when max(np.dot(X,params)[y == 0]) < min(np.dot(X,params))[y == 1]
Reference Stokes (2004) "On the advantage of using two or more econometric software systems to solve the same problem." There is a good discussion and some examples in there.
Maybe this was obvious to you, but this just hit me while thinking about similar. As you said, there are two issues here. 1 is the perfect separation and 2 is the multicollinearity.
For 2, the model has converged to a global maximum, it's just not unique and with perfect collinearity the Hessian is rank deficient (or numerically so) since moving in the same direction for multiple parameters changes the objective in the same way. Makes sense and it explains why the estimators that use the hessian fail.
For 1, I think this is a bug since it doesn't raise the perfect separation error.
The regularize fit has the weight on the penalty as 0 by default, so that's not surprising. I think the error message could be a little more informative here though that the Hessian is singular, try increasing the penalty, or something.
A nice quality of life improvement regarding PerfectSeparationError would be to identify the variables causing perfect separation so users don't have to do ad-hoc analysis to identify them.
For BFGS above, it's just that the result is within the absolute tolerance of allclose. Since, we're comparing to 0, relative tolerance doesn't matter. This is either working as intended, or we should lower the tolerance for the check. I would be fine with the latter. That seems close enough to me and we probably would never have false positives or especially with realer data.
In [117]: np.allclose(fittedvalues, mod_fit.model.endog, atol=1e-4)
Out[117]: True
In [118]: fittedvalues - mod_fit.model.endog
Out[118]:
array([-0.0000006206765542, -0.0000006206765542, 0.0000000000000006,
0.0000302852240728])
changed the title [-]Decrease abolute tolerance to detect perfect separation earlier[/-][+]Decrease abolute tolerance in allclose to detect perfect separation earlier[/+]on Mar 14, 2021
Activity
josef-pkt commentedon Feb 11, 2013
exog is singular
looks like we don't have consistent behavior across optimizers
it also looks like bfgs is getting a pinv solution, and is not checking for perfect separation
jseabold commentedon Feb 11, 2013
Right. Like I said, I didn't even look at it. So one issue here is BFGS does not check for perfect separation. The other issue is that the we're not dropping perfectly collinear columns.
josef-pkt commentedon Feb 11, 2013
To the last point, I don't think we should drop columns automatically, but we need a consistent warning or exception for the singular exog case.
jseabold commentedon Feb 11, 2013
Yeah, not trying to rehash this right now, but I'm not very happy with the current state of affairs here. We get a condition number warning in summary on pretty much any real data, but we don't handle perfectly collinear variables. Seems like a wart to me.
josef-pkt commentedon Feb 11, 2013
That's a separate discussion and won't be easy.
Except for solutions similar to using pinv which result in parameter estimates that are difficult to interpret, we will get into scikit-learn territory with variable selection, and soon will get k_vars > nobs.
josef-pkt commentedon Feb 11, 2013
looks like fit_regularized calculates cov_params already in fit, and also breaks on the example
jseabold commentedon Feb 11, 2013
On Mon, Feb 11, 2013 at 11:48 AM, Josef Perktold
[email protected]:
Maybe we should at least provide a helper function to identify near perfect
collinearity in variables, even if we don't use it by default. I recall
Chuck posting an example in the past that could be fixed up to do this.
I'll have to look back at some of my linear algebra notes though. That way,
we have it for identifying variables and can point users to it without too
much of a hassle.
I think the second point is a bit of a non-sequitur. Stata drops collinear
variables, but I don't think anyone would accuse stata of doing too much
hidden variable selection. And ill-defined problems is also another issue.
jseabold commentedon Apr 5, 2013
Not sure where else to park this, but I was just reading about perfect prediction.
Perfect separation occurs when for
y_t = 0 X_t \beta < 0
y_t = 1 X_t \beta > 0
So in the binary case, we can test for when max(np.dot(X,params)[y == 0]) < min(np.dot(X,params))[y == 1]
Reference Stokes (2004) "On the advantage of using two or more econometric software systems to solve the same problem." There is a good discussion and some examples in there.
jseabold commentedon Mar 5, 2015
Maybe this was obvious to you, but this just hit me while thinking about similar. As you said, there are two issues here. 1 is the perfect separation and 2 is the multicollinearity.
For 2, the model has converged to a global maximum, it's just not unique and with perfect collinearity the Hessian is rank deficient (or numerically so) since moving in the same direction for multiple parameters changes the objective in the same way. Makes sense and it explains why the estimators that use the hessian fail.
For 1, I think this is a bug since it doesn't raise the perfect separation error.
jseabold commentedon Mar 5, 2015
The regularize fit has the weight on the penalty as 0 by default, so that's not surprising. I think the error message could be a little more informative here though that the Hessian is singular, try increasing the penalty, or something.
Gabriel808 commentedon Mar 14, 2018
A nice quality of life improvement regarding PerfectSeparationError would be to identify the variables causing perfect separation so users don't have to do ad-hoc analysis to identify them.
josef-pkt commentedon Mar 14, 2018
@Gabriel808 Do you have an idea how to identify those, or what additional information to provide?
In general there might be just some linear combination (or separating hyper plane) that separates or discriminates some observations.
We can add helper function or methods to help in the separation diagnostics.
josef-pkt commentedon Mar 14, 2018
There might be something that we can do with the contribution to predicting the separated set of observations, but I never looked into that.
Gabriel808 commentedon Mar 14, 2018
I need to dig into the package a bit to identify the error path, but these variables must already be identified for the error to get raised.
josef-pkt commentedon Mar 14, 2018
We actually check for perfect prediction, and we don't use any information about what might causing it https://github.com/statsmodels/statsmodels/blob/master/statsmodels/discrete/discrete_model.py#L197
jseabold commentedon Mar 14, 2021
For BFGS above, it's just that the result is within the absolute tolerance of
allclose. Since, we're comparing to 0, relative tolerance doesn't matter. This is either working as intended, or we should lower the tolerance for the check. I would be fine with the latter. That seems close enough to me and we probably would never have false positives or especially with realer data.[-]perfect separation what's going on?[/-][+]Decrease abolute tolerance to detect perfect separation earlier[/+][-]Decrease abolute tolerance to detect perfect separation earlier[/-][+]Decrease abolute tolerance in allclose to detect perfect separation earlier[/+]