Skip to content

QuantReg fails when a residual is exactly zero. #2377

Closed

Description

@chrish42

I would like to use quantile regression on a dataset with many factor variables. It seems it is easy for the model to get at least one residual that is exactly zero with this dataset... When this happens, I get nans in xstar, and the code raises an exception in the next round. The cause is that the first two lines in this snippet of the fit method

        mask = np.abs(resid) < .000001
        resid[mask] = np.sign(resid[mask]) * .000001
        resid = np.where(resid < 0, q * resid, (1-q) * resid)
        resid = np.abs(resid)
        xstar = exog / resid[:, np.newaxis]

reset residuals to a small number only if they are close to zero but not zero. This is because np.sign(0) is 0, so the second line does nothing when the residual is 0. In my mind, the easy solution is to arbitrarily choose a side (say, the positive one) when a residual is zero, How would that sound?

Activity

  1. josef-pkt commented on Apr 28, 2015

    @josef-pkt
    Member

    Thanks for catching this.
    Do you have a short example that can be used to test this case?
    Setting the the zeros positive sounds fine, but there is a chance that the solution becomes asymmetric in q and 1-q if there are many ties at zero (discretized or rounded observations instead of continuous observations)
    Do you have just one or very residuals at zero or some larger number?

    related aside: I was thinking of also trying a smoothed version (locally quadratic around zero) to avoid some problems with zero or near zero residuals. (there was a convergence cycle reported on stats.stackexchange)

  2. added this to the 0.7 milestone on Apr 28, 2015
  3. josef-pkt commented on Apr 28, 2015

    @josef-pkt
    Member

    we could just replace the sign function np.sign(resid[mask])

    by ((resid[mask]) >= 0) *2 - 1)

    for trimming to positive

  4. josef-pkt commented on Apr 28, 2015

    @josef-pkt
    Member

    another option to look into (as a more general solution to near zero residuals)

    regularize the division, i.e. limit the weights

    xstar = exog / (a0 + resid[:, np.newaxis]

    with a small a0 = 1e-6
    (Fan and Li 2001 do something similar with data dependent a0 as function of quantiles for L1 penalization around origin. I didn't read that part carefully.)

    I haven't checked whether near zero residuals also create other problems

    edit
    additive a0 is not just shifting the residuals, because resid at that point is positive resid_abs

  5. chrish42 commented on Apr 28, 2015

    @chrish42
    Author

    Thanks for the quick reply. The dataset I'm working with is kind of large, but it should be possible to reproduce this also with a synthetic dataset, no? The times I've looked at the number of zeroes in the debugger, it was just a few or more like one, actually. And given that my model matrix is very tall and not that predictable, it would be surprising to get a lot of zeroes for the residual during these iterations.

    I'll let people who are more knowledgeable about statistics than me select the best way of keeping that xstar variable not too large. I'd be fine with all the solutions proposed above.

    What do you need from me to help fix this bug?

  6. josef-pkt commented on Apr 28, 2015

    @josef-pkt
    Member

    It would be helpful if you could make up a small example that shows the behavior.
    One problem with these type of changes is that we need a collection of corner cases to verify and test the behavior.

    Replacing the sign function by the weak inequality would be a quick fix, but I would prefer the regularization solution, but I would have to do more testing for that.

  7. chrish42 commented on Apr 29, 2015

    @chrish42
    Author

    The following snippet of code reproduces the problem:

    import numpy as np
    from statsmodels.api import QuantReg
    
    X = np.array([[1, 0], [0, 1]], dtype=np.float64)
    y = np.array([0, 1], dtype=np.float64)
    
    QuantReg(y, X).fit(0.5)

    I confirmed (with a debugger) that the residuals are zero after the first iteration.

    The regularization solution looks like the better fix to me too, at least at an intuitive level.

  8. josef-pkt commented on Jun 29, 2015

    @josef-pkt
    Member

    @chrish42 Thanks for the example, I'm finally going back to this and will fix it for statsmodels 0.7

    A simple way to trigger this case is to include a outlier (one-observation) dummy variable, which makes it a pretty general problem.

  9. added a commit that references this issue on Jun 29, 2015
    ad9b5c3
  10. added a commit that references this issue on Jul 4, 2015
    ab167b7
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions