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
QuantReg fails when a residual is exactly zero. #2377
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
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?
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)
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
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.
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.
Activity
josef-pkt commentedon Apr 28, 2015
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)
josef-pkt commentedon Apr 28, 2015
we could just replace the sign function
np.sign(resid[mask])by
((resid[mask]) >= 0) *2 - 1)for trimming to positive
josef-pkt commentedon Apr 28, 2015
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_abschrish42 commentedon Apr 28, 2015
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
xstarvariable not too large. I'd be fine with all the solutions proposed above.What do you need from me to help fix this bug?
josef-pkt commentedon Apr 28, 2015
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.
chrish42 commentedon Apr 29, 2015
The following snippet of code reproduces the problem:
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.
josef-pkt commentedon Jun 29, 2015
@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.
BUG Quantile Regression zero residuals, closes statsmodels#2377
BUG Quantile Regression zero residuals, closes statsmodels#2377