Skip to content

REF/ENH RLM and robust scale for almost perfect prediction - #1341

Open
josef-pkt wants to merge 19 commits into
statsmodels:mainfrom
josef-pkt:fix_robust_scale
Open

josef-pkt wants to merge 19 commits into
statsmodels:mainfrom
josef-pkt:fix_robust_scale

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

see #55

This makes the code more robust for the cases where we have almost perfect prediction, or where mad of the sample or residual is zero.

There are also small fixes to missing asarray and integer division in robust/scale

The code adds some extra calculation if scale == 0:

Explicit, verifying test for HuberScale are still missing. I get the same results with an independent new implementation.
New unit tests include a "sanity" check for all norm, scale combinations:
no exceptions, params is reasonably (no nan, close to median) estimated in RLM in all examples
a few nans in bse, and in standalone scale estimates for some corner cases

update

This now includes also some other fixes and enhancements:

  • BUG robust.norms.Hampel.rho was incorrect
  • BUG robust.norms.Hampel.psi had a sign error
    (this should have caused problems in existing code, for some values in the negative range)
  • REF robust.norms.TukeyBiweight shift function so that rho(0) == 0
  • ENH: add rsquared, aic, bic, deviance to RLMResults (based on SAS manual)
  • ENH: compare_lrtype LR/Ftest like test for nested models

for scale

  • BUG: robust.scale.Huber use mu if given

TODO

  • regression test for robust scale with almost perfect prediction is outdated,
    needs another check and new regression numbers.
  • add comment to compare_lrtype
    Maronna, Martin and Yohai 2006 p.106 use empirical cdf for chi2_factor
    problem
    MMY don't divide by df
    (only test case against SAS has df=1, so we cannot verify)
    Hampel et all 2005 (1986?) p.346 divide by df and by nobs

@josef-pkt

Copy link
Copy Markdown
Member Author

forgot one change: Huber should use center=mu in mad if mu is given (found while looking at #55)

@josef-pkt

Copy link
Copy Markdown
Member Author

also: I think adding a Warning in some of the scale==0 cases will be appropriate, mainly when we set weights to be 0 or 1.

@josef-pkt

Copy link
Copy Markdown
Member Author

good, the pickle test fails, which means it's working
https://travis-ci.org/statsmodels/statsmodels/jobs/18282752

need to register the new (nobs,) array rho for removal.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 8980f20 on josef-pkt:fix_robust_scale into 034f232 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

see #1459 for results when we have perfect prediction with OLS
With RLM we will get similar results if we have perfect prediction of observations with weights > 0

y3 = 4 + np.array([ 0, 0, 0, 0, 0, 0, -1.5, 1])
y4 = 4. + np.zeros(10)

endogs = [y1 - 27 + 4, 4 + y2, y3, y4]#[:-1]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commented-out code should either be removed or explained

try:
rlm = RLM(y, np.ones(len(y)), M=norm())
res = rlm.fit(scale_est=scale)
#print res.params, res.bse, res.scale

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

success.append([norm, scale, res.params, res.bse, res.scale])
except Exception as e:
fail.append([y, norm, scale, res.params, res.bse, res.scale])
print ' using ', norm, scale

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

py3 incompatible.

from results.results_rlm import BiSquare
self.res2 = BiSquare()

def test_other(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split into tests each testing one thing

@@ -0,0 +1,129 @@
# -*- coding: utf-8 -*-

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this redundant with check_rlm_perfect_prediction?

@josef-pkt

Copy link
Copy Markdown
Member Author

@jbrockmendel Thanks for reviewing.

The main problem with this PR is that I couldn't make up my mind when scale=0 with perfect prediction is a bug and when it is a feature. Another problem is that it combines debatable bug fixes with trivial bug-fixes which get blocked because we cannot merge the good with the (possibly) bad.

(Since then, I have read large parts of 4 or 5 text books and of 50 to 100 articles in the robust estimation neighborhood. Maybe I could make up my mind now.)

@josef-pkt josef-pkt modified the milestones: 0.8, 0.10 Apr 21, 2018
@blalterman

Copy link
Copy Markdown

Is there a status update on this?

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
@josef-pkt josef-pkt reopened this Jan 4, 2020
@pep8speaks

Copy link
Copy Markdown

Hello @josef-pkt! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 42:9: E999 SyntaxError: invalid syntax

Line 57:42: E999 SyntaxError: invalid syntax

@josef-pkt

josef-pkt commented Jan 4, 2020 •

Copy link
Copy Markdown
Member Author

It's not easily visible what of this PR is included in #5967

It is not all, for example rsquared #1351 , compare_lrtype, aic and similar are missing

@jseabold jseabold removed this from the 0.10 milestone Apr 2, 2021
@josef-pkt josef-pkt added this to the 0.14 milestone Nov 3, 2021
@josef-pkt

josef-pkt commented Nov 3, 2021 •

Copy link
Copy Markdown
Member Author

bump for 0.14
https://stackoverflow.com/questions/31655196/how-to-get-r-squared-for-robust-regression-rlm-in-statsmodels

  • parts of scale=0 handling is obsolete
  • extra results like rsquared, aic are not yet in main
  • bugfixes: need checking, Some might not yet be in main

This PR uses greek, e.g. rho which IIRC is the objective function

aside on aic:
I have more recent work on info criteria for misspecified models and non-MLE models, TIC, ... #6516 #7441

@josef-pkt

Copy link
Copy Markdown
Member Author

an alternative to current behavior in main
use huber scale to avoid perfect prediction PR #1355

I guess we still want options for scale if mad == 0

@josef-pkt

Copy link
Copy Markdown
Member Author

most likely we cannot rebase this.
needs part by part comparison to current main and decide on conflicting defaults.
missing features not yet in main will be easier to merge

need more unit test, e.g. for norms, to check that bugs there are fixed.

@josef-pkt

Copy link
Copy Markdown
Member Author

This has essentially 3 parts AFAICS

  • mad = 0, has some fixes in master, check fixed scale
  • bugfix, in norms, and ???
  • enhancements in RLMResults, needs references, I need to find from where I got those extra results (SAS Manual, ...)

looks like many merge conflicts.

Maybe split out bugfix and Results enhancements.
mad=0 and scale handling will have the most merge conflicts and will conflict with already merged adjustments.

@josef-pkt josef-pkt mentioned this pull request Nov 29, 2022
6 tasks
@josef-pkt josef-pkt modified the milestones: 0.14, 0.15 Nov 30, 2022
@josef-pkt

Copy link
Copy Markdown
Member Author

The norm bug fixes for hampel and biweight have been manually copied to #8801 and merged
Now obsolete/superseded in this PR

@josef-pkt

Copy link
Copy Markdown
Member Author

Eviews manual for version 10 p. 425
has 3 rsquared for RLM M-estimator and robust AIC and BIC

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.

6 participants