Skip to content

BUG: fix robust.norm.Hampel - #8801

Merged
josef-pkt merged 5 commits into
statsmodels:mainfrom
josef-pkt:bug_robust_norms
Apr 16, 2023
Merged

josef-pkt merged 5 commits into
statsmodels:mainfrom
josef-pkt:bug_robust_norms

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Apr 15, 2023 •

Copy link
Copy Markdown
Member

this copies the hampel and biweight fixes from #1341 and adds additional fixes that the unit tests founc

closes #8800
closes #8697
closes #8696

one issue
using mask to fill results array needs a dtype for it.
zeros_like(z) does not work for int
zeros(shape, float) does not allow complex step derivative
we need at least float

However, complex step derivatives are not supported in norms because of the widespread use of np.abs, which does not do the computation needed for cs derivatives

@josef-pkt

Copy link
Copy Markdown
Member Author

from #7603 (comment)

at least dtype can be implemented with np.promote_types

np.promote_types(np.array([990000000, 2100000000], np.int32).dtype, np.float64)
dtype('float64')

np.promote_types(np.array([990000000, 2100000000], np.complex128).dtype, np.float64)
dtype('complex128')

@josef-pkt

josef-pkt commented Apr 15, 2023 •

Copy link
Copy Markdown
Member Author

only unrelated randomly failing test
FAILED statsmodels/tsa/statespace/tests/test_dynamic_factor.py::TestDynamicFactor_ar2_errors::test_mle

still style check failures
next cleaning up to have consistent pattern across methods

aside: np.abs remove complex dtype
Needs additional check that complex step derivatives work with this. We might need abs of real and complex components separately for that.

update
a unit tests with cs derivative fails, result is zero, so np.abs is not compatible with cs derivatives
psid = _approx_fprime_cs_scalar(x, norm.rho)

If I replace np.abs usage by cabs, then complex step derivatives works for numerical derivatives of rho and psi

def cabs(x):
    sign = (x.real >= 0) *2 - 1
    return sign * x

Note: this sign computation cannot be replaced by np.sign because np.sign is trivariate with np.sign(0) = 0
Complex part could be nonzero, even if real part is zero.
However, I'm not sure what should happen with negative complex parts, what is needed for complex step derivatives.
I'm giving up on complex step derivatives of norms for 0.14. We don't have a usecase yet.

(x+x*1j)
array([-9.-9.j, -6.-6.j, -2.-2.j, -1.-1.j,  0.+0.j,  1.+1.j,  2.+2.j,
        6.+6.j,  9.+9.j])
(x+x*1j)
(x+x*1j) * ((x.real > 0) * 2 - 1)
array([ 9.+9.j,  6.+6.j,  2.+2.j,  1.+1.j, -0.+0.j,  1.+1.j,  2.+2.j,
        6.+6.j,  9.+9.j])

update2

The subset selection also uses np.abs across almost all norms. So, there are more places to change to get complex step derivatives to work correctly.

@josef-pkt

Copy link
Copy Markdown
Member Author

one more design decision: do we support scalar input in methods?
Currently breaks, except for the methods that I already rewrote to use z = np.atleast_1d(z)

@josef-pkt

Copy link
Copy Markdown
Member Author

another minor bug
HuberT.psi_deriv returns boolean dtype
do we return int or float?

I guess float for consistency across norms

@josef-pkt

Copy link
Copy Markdown
Member Author

The remaining norms don't overlap with R robustbase AFAICS.
the result_rlm mentions SAS, so those seem to be modelled after SAS

I can add generic consistency tests using the definitions, so the RLM unit test would indirectly also verify the remaining norm methods.

@josef-pkt

Copy link
Copy Markdown
Member Author

Also bug in AndrewsWave (and not yet investigate results consistency failure in TrimmedMean)

weights looks correct with SAS
psi is missing multiplication by self.a

I had to multiply the results in every method except weights to fix AdrewsWave.
However, I think this bug/inconsistency does not affect the RLM results.
Multiplying weights or objective function by a fixed factor does not change the estimation results, params and bse, (I'm pretty sure, based on M-estimation and WLS/IRLS). Making the change does not cause RLM unit test failures.

TrimmedMean was false alarm
In the test case the approx_fprime was evaluating at a jump point. All tests pass after I change args to avoid evaluating at jump points.

@josef-pkt

Copy link
Copy Markdown
Member Author

This should be finished now.

maybe formulas in docstring need now corrections.

@josef-pkt

josef-pkt commented Apr 16, 2023 •

Copy link
Copy Markdown
Member Author

still a bug
AndrewWave does not have rho(0) = 0 after my changes. I need to shift it up

scaling is a bit arbitrary.
I don't find any reference (not yet) for all method formulas for Andrews' Wave/Sin.
Scaling and parameterization does not seem to be consistent across references, books and articles that I found.

aside:
SAS mentions that most default args for the norms are defined to reach 95% efficiency (at normal distribution)
IIRC, I had looked at computing relative efficiency in one of the robust PRs.

update
It's missing an assert that weights(0) = 1 or max(weights(x)) == 1 if 0 in x.
I guess, if we require internal consistency then weights(0) = 1 will pin down the scale through weights = psi / x

@josef-pkt

Copy link
Copy Markdown
Member Author

aside:
some tests like rho(0) == 0 will not apply to MQuantileNorm. (Maybe that's still true?)
Similarly would be test for symmetry rho(x) == rho(-1) or anti-symmetry psi(x) = -psi(-x), but I have not added those checks.

I think this is the end, unless I missed some style failures.

@josef-pkt

Copy link
Copy Markdown
Member Author

all green, I guess the doc build crash/error is an azur problem to fetch the data and not related to this PR
merging

@josef-pkt

Copy link
Copy Markdown
Member Author

I found now a reference that includes AndrewsWave in the same form as I changed it to.
I have not looked at the other parts of the article.

Khan, Dost Muhammad, Muhammad Ali, Zubair Ahmad, Sadaf Manzoor, and Sundus Hussain. “A New Efficient Redescending M-Estimator for Robust Fitting of Linear Regression Models in the Presence of Outliers.” Mathematical Problems in Engineering 2021 (November 22, 2021): e3090537. https://doi.org/10.1155/2021/3090537.

@josef-pkt

Copy link
Copy Markdown
Member Author

Default parameter in SAS
https://documentation.sas.com/doc/en/statcdc/14.2/statug/statug_rreg_syntax01.htm#statug.rreg.robustregftab3

Note, the default args for Hampel are different from this in R robustbase

Weight Function Option Default a, b, c
Andrews WF=ANDREWS <(C=c)> 1.339
Bisquare WF=BISQUARE <(C=c)> 4.685
Cauchy WF=CAUCHY <(C=c)> 2.385
Fair WF=FAIR <(C=c)> 1.4
Hampel WF=HAMPEL <(<A=a> <B=b> <C=c>)> 2, 4, 8
Huber WF=HUBER <(C=c)> 1.345
Logistic WF=LOGISTIC <(C=c)> 1.205
Median WF=MEDIAN <(C=c)> 0.01
Talworth WF=TALWORTH <(C=c)> 2.795
Welsch WF=WELSCH <(C=c)> 2.985

@josef-pkt

josef-pkt commented Apr 17, 2023 •

Copy link
Copy Markdown
Member Author

another reference to confirm the parameterization changes

Eviews 10 manual p 422
has c**2 in Andrews rho function, as I changed it to
Talworth which is our TrimmedMean, has rho continuity at break point, outside points rho is c**2/2 (SAS also calls it Talworth)

@josef-pkt

Copy link
Copy Markdown
Member Author

another reference with 48 robust norms for M-estimators, rho and psi functions

Menezes, D. Q. F. de, D. M. Prata, A. R. Secchi, and J. C. Pinto. 2021. “A Review on Robust M-Estimators for Regression Analysis.” Computers & Chemical Engineering 147 (April): 107254. https://doi.org/10.1016/j.compchemeng.2021.107254.

They include tuning parameters for 95% and in appendix for 90%, 98%, 99% relative efficiency.

However, their scaling factor * rho looks arbitrary, i.e. not fixed so that weight(0) = 1.
The rho scaling is only relevant for weights, and does not affect numerical optimization (with standard optimizers) or relative efficiency and similar properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant