Skip to content

Decrease abolute tolerance in allclose to detect perfect separation earlier #659

Open

Description

@jseabold

Toy example parked for consideration. Haven't thought about or looked at this.

yes = [1, 1, 0, 0.]
data = [[1, 0, 1],[1, 0, 1],[1, 2, 1],[1, 1, 1.]]
sm.Logit(yes, data).fit(method="bfgs") # warning, why are we inverting when we have analytic results?
sm.Logit(yes, data).fit(method="powell") # PerfectSeparationError
sm.Logit(yes, data).fit() # LinAlgError

Edit: The last now raises a PerfectSeparationError. BFGS still does not.

Activity

  1. josef-pkt commented on Feb 11, 2013

    @josef-pkt
    Member

    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])
    
  2. jseabold commented on Feb 11, 2013

    @jseabold
    MemberAuthor

    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.

  3. josef-pkt commented on Feb 11, 2013

    @josef-pkt
    Member

    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.

  4. jseabold commented on Feb 11, 2013

    @jseabold
    MemberAuthor

    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.

  5. josef-pkt commented on Feb 11, 2013

    @josef-pkt
    Member

    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.

  6. josef-pkt commented on Feb 11, 2013

    @josef-pkt
    Member

    looks like fit_regularized calculates cov_params already in fit, and also breaks on the example

    >>> res2 = sm.Logit(yes, data).fit_regularized()
    Optimization terminated successfully.    (Exit mode 0)
                Current function value: 1.12892750712e-10
                Iterations: 35
                Function evaluations: 35
                Gradient evaluations: 35
    Traceback (most recent call last):
      File "<pyshell#17>", line 1, in <module>
        res2 = sm.Logit(yes, data).fit_regularized()
      File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new\statsmodels\statsmodels\discrete\discrete_model.py", line 391, in fit_regularized
        size_trim_tol=size_trim_tol, qc_tol=qc_tol, **kwargs)
      File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new\statsmodels\statsmodels\discrete\discrete_model.py", line 304, in fit_regularized
        cov_params_func=cov_params_func, **kwargs)
      File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new\statsmodels\statsmodels\base\model.py", line 335, in fit
        Hinv = cov_params_func(self, xopt, retvals)
      File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new\statsmodels\statsmodels\discrete\discrete_model.py", line 324, in cov_params_func_l1
        H_restricted_inv = np.linalg.inv(-H_restricted)
      File "C:\Python26\lib\site-packages\numpy\linalg\linalg.py", line 445, in inv
        return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
      File "C:\Python26\lib\site-packages\numpy\linalg\linalg.py", line 328, in solve
        raise LinAlgError, 'Singular matrix'
    LinAlgError: Singular matrix
    
  7. jseabold commented on Feb 11, 2013

    @jseabold
    MemberAuthor

    On Mon, Feb 11, 2013 at 11:48 AM, Josef Perktold
    [email protected]:

    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.

    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.

  8. jseabold commented on Apr 5, 2013

    @jseabold
    MemberAuthor

    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.

  9. jseabold commented on Mar 5, 2015

    @jseabold
    MemberAuthor

    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.

  10. jseabold commented on Mar 5, 2015

    @jseabold
    MemberAuthor

    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.

  11. Gabriel808 commented on Mar 14, 2018

    @Gabriel808

    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.

  12. josef-pkt commented on Mar 14, 2018

    @josef-pkt
    Member

    @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.

  13. josef-pkt commented on Mar 14, 2018

    @josef-pkt
    Member

    There might be something that we can do with the contribution to predicting the separated set of observations, but I never looked into that.

  14. Gabriel808 commented on Mar 14, 2018

    @Gabriel808

    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.

  15. josef-pkt commented on Mar 14, 2018

    @josef-pkt
    Member
  16. jseabold commented on Mar 14, 2021

    @jseabold
    MemberAuthor

    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])
    
  17. changed the title [-]perfect separation what's going on?[/-] [+]Decrease abolute tolerance to detect perfect separation earlier[/+] on Mar 14, 2021
  18. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions