Skip to content

Incorrect implementation of confusion matrix in pred_table for Logit model #2968

Closed

Description

@napsternxg

The confusion matrix given by the pred_table() function gives the incorrect value. E.g.

y = np.array([[1],
       [0],
       [1],
       [0],
       [0],
       [0],
       [0],
       [0],
       [0],
       [0]])
res = Logit(y,np.ones((10,1))).fit()
res.pred_table()

Outputs the following:

array([[ 0.,  8.],
       [ 0.,  2.]])

This output is wrong on multiple accounts:

  • Given that label 1 is considered true and label 0 is considered false:
TP = 0, FP = 2, FN = 0, TN = 8
  • The correct predictions for each class should fall on the diagonal, however, in this case the correct predictions appear to be on the off diagonal.
  • As mentioned in Issue classification performance measures #1577 tp, fp, fn, tn = map(float, pred_table.flatten()), if the true class is 0. However, when using Logit model the true class is almost always 1. And even if the true class is 0, then also TP is going to be cm[0,1] and not cm[0,0], where cm is the confusion matrix.

The model, the actual confusion matrix, considering label 1 as the positive class, should be:

[[0,2],
[0,8]]
#representing the traditional
[[TP, FN],
[FP, TN]]

Alternatively, it can also be simply:

[[8,0],
[2,0]]
#representing the sklearn implementation
[[Actual = 0, Predicted = 0, Actual = 0, Predicted = 1],
[Actual = 1, Predicted = 0, Actual = 1, Predicted = 1]]
# Ensuring that the diagonal is the correct class predictions counts. 

Activity

  1. josef-pkt commented on May 25, 2016

    @josef-pkt
    Member

    @napsternxg Thanks for reporting

    Yes, that's a bug, which AFAICS causes problems when a row or column is zero.
    The method just uses numpy histogram2d with bins=2, which produces the wrong bin limits if there are not both zeros and ones

    >>> np.histogram2d(res.model.endog, pred, bins=2)
    (array([[ 0.,  8.],
           [ 0.,  2.]]), array([ 0. ,  0.5,  1. ]), array([-0.5,  0. ,  0.5]))
    

    to fix this, I think it should be enough to use explicit bin boundaries

    >>> b1 = np.array([ 0. ,  0.5,  1. ])
    >>> np.histogram2d(res.model.endog, pred, bins=(b1, b1))
    (array([[ 8.,  0.],
           [ 2.,  0.]]), array([ 0. ,  0.5,  1. ]), array([ 0. ,  0.5,  1. ]))
    

    I think using the threshold in the bin directly would make the 0-1 prediction unnecessary, but I don't remember how numpy histogram handles values exactly at the bounds (in case of ties and points on the threshold).
    b1 = np.array([ 0. , threshold, 1. ])

  2. josef-pkt commented on May 25, 2016

    @josef-pkt
    Member

    Based on browsing the unit tests, test coverage for pred_table looks good. So, this problem might be only in this (untested) edge case.

    @napsternxg Do you want to prepare a PR with the fix and your example as unit test?

  3. napsternxg commented on May 25, 2016

    @napsternxg
    Author

    @josef-pkt
    I can prepare a PR and send by next week. However, I think we can use sklearn's API for creating the confusion matrix which will make it more consistent for users coming form ML background. I think in Issue #1650 there was already discussion to some code from sklearn api for doing this.

    Another option would be explicitly construct the Confusion matrix in the format such that the diagonal elements contain the true predictions for each class. In Logit case it would be just 2 classes such that 0 is the first class. The code would also be more readable as opposed to using np.histogram2d.

    What do you think ?

  4. josef-pkt commented on May 25, 2016

    @josef-pkt
    Member

    the problem is that any refactoring or enhancements will need decisions and review, while fixing the current implementation for the edge case with unit test can be merged right away.

    I think histogram usage is quite fine, as long as it has explicit intervals. It's a common usage for counting a binned continuous variable.

    Extensions along #1650 would be a good contribution, of course, but I'm not usually using this functionality and I'm not really familiar with what scikit-learn is doing in this area.
    So, I don't have a strong opinion about details and would mainly review for overall consistency with the statsmodels pattern.

    Overall predictive evaluation measures is not yet a strong, consistently supported area in statsmodels. I added the eval_measures for continuous data, but my usecase was more Monte Carlo studies than predictive cross-validation, plus some helper function for mainly internal usage.

    This means, that it would be very useful if you or someone else is pushing into this area.

  5. josef-pkt commented on Aug 17, 2016

    @josef-pkt
    Member

    MNLogit has the same problem if a zero column is at the boundary. There is a unit test for it with zeros in the middle. Zero columns at the boundary mess up the bin edges for histogram2d.

  6. added 3 commits that reference this issue on Aug 17, 2016
    1c6eff4
    5751c14
    fac13a3
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