Skip to content

Add check for convergence - #5585

Closed
jpfeiffg wants to merge 1 commit into
statsmodels:masterfrom
jpfeiffg:rlm
Closed

jpfeiffg wants to merge 1 commit into
statsmodels:masterfrom
jpfeiffg:rlm

Conversation

@jpfeiffg

Copy link
Copy Markdown

Currently in robust_linear_model, we do one iteration, rescale, and then enter the iterations loop.

This can be a problem if the first iteration is a perfect fit or nearly a perfect fit. When dividing by scale, we get either NaN or an ill-conditioned problem.

@bashtage

Copy link
Copy Markdown
Member

Needs a test that exits here.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.009%) to 84.541% when pulling 3284ca5 on jpfeiffg:rlm into b8be04d on statsmodels:master.

@codecov

codecov Bot commented Apr 30, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #5585 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #5585   +/-   ##
=======================================
  Coverage   82.03%   82.03%           
=======================================
  Files         588      588           
  Lines       93103    93103           
  Branches    10323    10323           
=======================================
  Hits        76379    76379           
  Misses      14359    14359           
  Partials     2365     2365
Impacted Files Coverage Δ
statsmodels/robust/robust_linear_model.py 79.51% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b8be04d...3284ca5. Read the comment docs.

@jpfeiffg

Copy link
Copy Markdown
Author

Needs a test that exits here.

Thanks, let me add one and get back to you. I see the tests in the robust/tests directory; I need to figure out their data format and then add a test for x ~ x.

@bashtage

Copy link
Copy Markdown
Member

You can simulate the data as part of the test, something like

rs = np.random.RandomState(0)
x = rs.standard_normal(100)
y = x + 1e-16*rs.standard_normal(100)  # or just y = x

It is best to use a dedicated RandomState so that the test is the same on every run.

@bashtage

Copy link
Copy Markdown
Member

I don't think this can ever stop at this point since the initial criterial is always np.inf, see

history = dict(params = [np.inf], scale = [])

I think you always have to run a couple of iterations to know a model has converged.

In particular on the first iteration,

not (np.any(np.fabs(criterion[iteration] - criterion[iteration-1]) > tol) and iteration < maxiter)

is

not (np.any(np.fabs(criterion[iteration] - np.inf) > tol) and iteration < maxiter)

which is always a nonstop. AFAICT The only way it could stop here would be is maxiter is 1 or less.

@thequackdaddy

thequackdaddy commented May 10, 2019 •

Copy link
Copy Markdown
Member

For funsies, I remember dealing with this in GLM a long, long time ago.

05afd28

xref #2803

@bashtage

Copy link
Copy Markdown
Member

I think to beleive it has converged you need to estimate the model at least twice, so that is OK. An looking again, if maxiter == 0, then it will ignore this and always run one, so this check seems to be OK here.

@jpfeiffg The only thing the test needs to do is to fit(maxiter=0) and verify that it exits immediately with iteration=1

@jpfeiffg

jpfeiffg commented May 10, 2019 •

Copy link
Copy Markdown
Author

Hey, thank you for the response. I'm sorry for the delay, I have been working on other stuff and found it hard to write the test :(.

Are you sure the change is always a no-op? I added some prints to my test case (which is, fitting "a ~ b - 1" when a and b are both == range(10)).

The _check_convergence call in this case is:
_check_convergence([inf, nan], 1, 1e-08, 50).

It evaluates to True:

_check_convergence([inf, nan], 1, 1e-08, 50) =
not (np.any(np.fabs(criterion[1] - criterion[0]) > 1e-08) and 1 < 50) =
not (np.any(np.fabs(nan - inf) > 1e-08) and 1 < 50)
>>> not (np.any(np.fabs(np.nan - np.inf) > 1e-08) and 1 < 50)
True
>>> np.nan > 1e-08
False

So, I think if we believe that exiting after 1 iteration is a sensible thing to do here, then the code change in this pull request, is sensible.

As to whether we should exit after 1 iteration, I don't know the module well enough. I see that we're attempting to re-weight the LS in the iteration which seems fine. The problem is, in my example where we are fitting X to itself, we divide by 0 in the wls_results.resid/self.scale line, because self.scale can be 0.

Question for those who know the module well - does self.scale == 0 imply that we have in fact converged?

@bashtage

bashtage commented May 10, 2019 via email

Copy link
Copy Markdown
Member

@bashtage

bashtage commented May 10, 2019 via email

Copy link
Copy Markdown
Member

@jpfeiffg

Copy link
Copy Markdown
Author

OK. We could explicitly check for scale==0. I think what's happening in this case is that deviance is very large, NaN in my example when compiled with -copt=-mavx, or just large numbers when compiled normally.

I think it makes sense to treat scale == 0 or scale ~ 0 identically?

@josef-pkt

Copy link
Copy Markdown
Member

No, if the initial estimate of scale=0, then we don't necessarily have converged.

based on what I remember
The initial estimate uses MAD which has scale = 0 if at least half of the observattions have perfect fit. However, the estimate for the scale in the iteration is based on Huber or other methods and not MAD.
original ancient issue #55

Is perfect fit to 50% of the data a feature or a bug?
There should be some more recent issues on this. The "hardcore" robust literature considers it a feature, I think it's most often not desired, and Huber estimator doesn't have that feature.

@jpfeiffg

Copy link
Copy Markdown
Author

@josef-pkt , thanks for the links to older bugs, the history / context is useful.

I would agree that users usually don't want to get NaN back if they passed in reasonable data such as a perfect fit or 50%+ perfect fit. Can we reach agreement on what to do in this case, at an API level? If we agree on that I think it will be straightforward to make the code changes involved.

I propose a good thing for RLM.fit(partial_perfect_fit) to do, is to proceed until we hit a scale of 0 since it's not meaningful. I would be love to hear other approaches since I'm not familiar with robust regression. I claim in general that we shouldn't return NaN here but would also be open to understanding why this is reasonable.

Is there anyone else who should be brought in to comment? Besides @bashtage and @josef-pkt I see that @jseabold has done a lot of work in robust/ over time.

@josef-pkt

Copy link
Copy Markdown
Member

When I cannot make up my mind, which happened in this case with 50% perfect fit, then I like to push it off to the user.

So, I think we could add a keyword option to stop the iteration if estimated scale = 0. That would depend on the breakdown point of the chosen scale estimator or (maybe) initial scale estimator, around 50% for MAD and something higher for iqr.
Huber scale is always nonzero, IIRC, but might have strange behavior if a large fraction of observations has zero residuals and the others have residuals with large magnitude, i.e. the distribution of the data is far away from a normal distribution.

If scale=0, then we still have params, but I think all standard errors will be nan or zero. (I don't remember if I looked at that part, or if the references that I had used say anything about inference in the perfect fit case.)

Correction after reading the docstring of RLM.fit again.
The default scale is MAD (and not HuberScale as I remembered). In the past I was looking mainly at the case when initial scale estimate is MAD and iteration scale estimate is HuberScale. IIRC

If user specified MAD scale_est, then I also think it will have converged at perfect fit if the scale=0.

IIRC, I didn't look much at that case because MAD has low efficiency as scale estimator, and I was more interested in cases where we don't have a very high breakdown point. (Default RLM is not really designed for high breakdown point, i.e. something close to 50%)

so

  • if scale_est == 'mad' and scale==0, then stop as converged with perfect fit.
  • if scale_est == HuberScale and scale==0, then make continuation depend on user option
    default is to continue to convergence and handle initial estimated scale=0 with something nonzero

@bashtage

bashtage commented May 21, 2019 via email

Copy link
Copy Markdown
Member

@josef-pkt

josef-pkt commented Jun 4, 2019 •

Copy link
Copy Markdown
Member

terminology: "exact fit property"
used in
Ricardo A. Maronna, Douglas R. Martin, Victor J. Yohai 2006 Robust Statistics: Theory and Methods

I was rereading the sections in the book. It is defined as having at least the number of observations corresponding to the breakdown point in a linear subspace.

AFAIR, M-estimator with Huber loss function does not have a breakdown point < 1.
But breakdown point of MAD for scale is 0.5, IIRC.
I don't remember what determines the breakdown point of redescending loss functions.

there is a new, Jan 2019, edition of the book using R instead of S

https://www.wiley.com/en-ca/Robust+Statistics%3A+Theory+and+Methods+%28with+R%29%2C+2nd+Edition-p-9781119214663

with R package https://cran.r-project.org/web/packages/RobStatTM/index.html

@jpfeiffg

jpfeiffg commented Jun 5, 2019

Copy link
Copy Markdown
Author

I saw this was referenced as a release blocker - I can work on this at some point but I don't have time to do anything in the near term. Happy to hand it off if that helps; if not, I may come back to it later.

@bashtage

bashtage commented Jun 5, 2019

Copy link
Copy Markdown
Member

Would be nice to have the right fix, but not a blocker.

@bashtage

bashtage commented Jun 5, 2019

Copy link
Copy Markdown
Member

What does R do in this data? Could just follow their lead.

bashtage added a commit to bashtage/statsmodels that referenced this pull request Jul 17, 2019
Ensure RLM doesn't crash if perfect fit
Clean norms so that they don't crash either
Avoid calculations the load to nan
Improve testing of RLM
Replace fabs with abs

closes statsmodels#5585
closes statsmodels#1341
closes statsmodels#5878
closes statsmodels#5356
closes statsmodels#3319
xref statsmodels#55
bashtage added a commit to bashtage/statsmodels that referenced this pull request Jul 17, 2019
Ensure RLM doesn't crash if perfect fit
Clean norms so that they don't crash either
Avoid calculations the load to nan
Improve testing of RLM
Replace fabs with abs

closes statsmodels#5585
closes statsmodels#1341
closes statsmodels#5878
closes statsmodels#5356
closes statsmodels#3319
xref statsmodels#55
bashtage added a commit to bashtage/statsmodels that referenced this pull request Jul 17, 2019
Ensure RLM doesn't crash if perfect fit
Clean norms so that they don't crash either
Avoid calculations the load to nan
Improve testing of RLM
Replace fabs with abs

closes statsmodels#5585
closes statsmodels#1341
closes statsmodels#5878
closes statsmodels#5356
closes statsmodels#3319
xref statsmodels#55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants