Repository navigation
BUG: Add finite check for _MinimalWLS - #4960
Conversation
Codecov Report
@@ Coverage Diff @@
## master #4960 +/- ##
==========================================
- Coverage 82% 81.54% -0.47%
==========================================
Files 588 569 -19
Lines 93362 89641 -3721
Branches 10372 10032 -340
==========================================
- Hits 76566 73096 -3470
+ Misses 14392 14273 -119
+ Partials 2404 2272 -132
Continue to review full report at Codecov.
|
| self.exog = exog | ||
| self.weights = weights | ||
| if check_finite: | ||
| msg = 'NaN/inf detected in {0], estimation infeasible.' |
dc55eb6 to
42902bc
Compare
8794875 to
db7e079
Compare
| self.weights = weights | ||
| if check_finite: | ||
| msg = 'NaN/inf detected in {0}, estimation infeasible.' | ||
| if not np.all(np.isfinite(endog)): |
There was a problem hiding this comment.
the fastest way to check this is to use np.isfinite(endog.ptp()) according to a stackoverflow (?) question
(the only disadvantage is that it causes a numpy warning if there is a nan or inf, e.g. in the unit tests)
np.all(np.isfinite( needs to loop through the array twice and needs to create an intermediate elementwise array
There was a problem hiding this comment.
%timeit np.all(np.isfinite(endog))
748 µs ± 33.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit np.isfinite(endog.ptp())
1.02 ms ± 47.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
There was a problem hiding this comment.
%timeit np.isfinite(endog)
641 µs ± 24.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit endog.ptp()
982 µs ± 7.85 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit endog.sum()
924 µs ± 8.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
There was a problem hiding this comment.
ptp is slow because it loops across the entire array twice, once for the max and once for the min.
There was a problem hiding this comment.
that's what I get in my older setup
x = np.random.randn(100000, 5)
%timeit np.all(np.isfinite(x))
%timeit np.isfinite(x.ptp())
1000 loops, best of 3: 962 µs per loop
1000 loops, best of 3: 227 µs per loop
a quick stackoverflow search finds https://stackoverflow.com/questions/6736590/fast-check-for-nan-in-numpy
but I don't remember from where I got the ptp part
There was a problem hiding this comment.
On your example
%timeit np.all(np.isfinite(x))
283 µs ± 5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit np.isfinite(x.ptp())
365 µs ± 5.35 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
| raise ValueError(msg.format('endog')) | ||
| if not np.all(np.isfinite(weights)): | ||
| raise ValueError(msg.format('weights')) | ||
|
|
There was a problem hiding this comment.
I think to catch algorithmic problems, e.g. negative weights, we need to do the check on w_half
There was a problem hiding this comment.
Is there a scenario where w_half is not OK but weights is? Computing w_half when there is an issue will likely raise a warning before the exception is thrown.
There was a problem hiding this comment.
The problems is negative weights which cause nans in w_half, which will cause a Warning but doesn't prevent the nans to go into linalg.
I don't remember or know in which part the problem was in my example when I ran into this, the reason for the problem was "user error".
IIRC, we should get it in a linear probability model for GLM Binomial when predicted is outside open interval (0, 1), but maybe we "sanitize" it in this case by clipping.
There is a set of issues related to this #2208 where we intentionally don't have a properly defined model.
|
another issue, AFAICS: |
|
one example case for the performance impact would be @thequackdaddy case with 5 to 10 Million observations. |
|
With 10M obs it adds about 25ms per iteration. Is suspect this is negligible compared to the actual cost of the fit. But I can make the tests separately optional. |
db7e079 to
32fee36
Compare
|
These are mostly to prevent hangs that are unexplainable by end users, rather than to be a method to detect algorithmic problems. |
32fee36 to
75cafd8
Compare
|
I'm copying part of my inline comment here for easier reference -- -- The problem is that we intentionally allow misspecified models. |
75cafd8 to
dcb9440
Compare
|
Some benchmarks, % slowdown with checks Check is O(n), iterative estimator is O(n^2) so negligible for large models. |
| """ | ||
|
|
||
| def __init__(self, endog, exog, weights=1.0): | ||
| msg = 'NaN, inf or invalid value detected in {0}, estimation infeasible.' |
There was a problem hiding this comment.
why is this a class attribute?
There was a problem hiding this comment.
Probably an unnecessary optimization preventing the string from being re-created in each instance.
There was a problem hiding this comment.
Thanks. I've been porting a bunch of your PRs to sm2, will probably put this inside __init__ there.
Add optional finite check for _MinimalWLS Add separate checks for endo and weights and adjust use upstream closes statsmodels#4566
dcb9440 to
b7452c7
Compare
Add optional finite check for _MinimalWLS
closes #4566