Skip to content

ENH: robust: tools and more norms - #9186

Merged
josef-pkt merged 4 commits into
statsmodels:mainfrom
josef-pkt:robust_tools_norms
Apr 12, 2024
Merged

josef-pkt merged 4 commits into
statsmodels:mainfrom
josef-pkt:robust_tools_norms

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

related
#8808
#1372

this adds tools for asymptotic efficency, breakdown point computation and choice of tuning parameter of norms.
currently only for univariate

this adds some additional norms, TukeyQuartic and StudentT (I also have Cauchy norm, but might leave it out as special case of StudentT with df=1)

plan for next, either in this PR or next PR are more general (M-)scale estimators #9171

@pep8speaks

pep8speaks commented Mar 29, 2024 •

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:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2024-04-11 20:53:04 UTC

Comment thread statsmodels/robust/tools.py Resolved
@josef-pkt
josef-pkt force-pushed the robust_tools_norms branch 2 times, most recently from e56e873 to 64c0305 Compare March 29, 2024 20:36
Comment thread statsmodels/robust/tools.py Fixed
@josef-pkt
josef-pkt force-pushed the robust_tools_norms branch from 64c0305 to 6cad5ad Compare March 30, 2024 17:28
@josef-pkt

josef-pkt commented Mar 31, 2024 •

Copy link
Copy Markdown
Member Author

extra keywords are a bit of a pain when we need to create norm instances in helper function

currently

  • tuning parameter is set in __init__, So, we need to create new instance of a Norm class to change the tuning parameter
    Additional complication is that name and keyword for tuning parameter is not consistent across norms.
  • some Norms have extra parameters that define the norm, e.g. df in new StudentT and k in TukeyQuartic norm.
    • If we need to create a new norm instance, we need to add the extra keywords.

It looks like _get_tuning_param(norm, eff, kwd="c", kwargs=None, ...) handles this with kwd and kwargs.
But it's a mess to add those to tuning_s_estimator_mean which currently only applies if extra keyword is default and tuning parameter name is "c".

one possibility would be to add a setter or update method for the tuning parameter.
Then we can work with an existing instance and don't need to create new instances to change the tuning parameter.
Disadvantage is that we need to be careful not to get the "wrong state" in the instance. Some attributes might need to be updated if we change the norm or tuning parameters.

One of these will be max_rho that is needed for the breakdown computation. This is also currently hardcoded for only norms that have "c" as tuning parameter and attribution.
max_rho should be cheap to compute and is not used very often, so we could keep it as method instead of attribute, which would automatically choose the "current" tuning parameter.

If we want to attach relative efficiency, breakdown point or similar properties of the norm, then those should be cached because they are more expensive to compute, especially when we need numerical integration and/or rootfinding.
If we use those as cached attributes, then we need to invalidate the cache when updating/setting the tuning parameters.

What we might be able to use for the extra kwargs is something like a partial function, but for instance creation.
That sounds too complex just for this use case.

Parameterized link function for GLM might have similar applications, but we don't have yet any code that needs to change the Link parameters. (Users can just create new link instances in e.g. a profile likelihood optimization.)

Sounds like adding _set_tunig_param and max_rho to the Norms is the easiest way to make generic, reusable code without kwds and kwargs to handle inconsistences or differences between norm classes.

update
Another disadvantage, now that I try this out in the code.
The tools functions including those called internally all modify the same norm instance. This is very much like using one global variable across functions.
That's very fragile if we use the norm without first setting the tuning parameter to what we need locally.
This means the tuning parameter essentially becomes a required parameter. We cannot rely on whatever part has set it to some specific or arbitrary value. Additionally it is the instance the the original user provides, i.e. the caller to tuning_s_estimator_mean or to similar tools functions

@josef-pkt

josef-pkt commented Mar 31, 2024 •

Copy link
Copy Markdown
Member Author

aside typo in docstring for Andrews rho
rho(z) & = 2a^2, |z|>a\pi had a missing power ^2 and wrong letter in threshold
i.e. max_rho = 2 * a**2, and argmax rho = a * pi (threshold is not the tuning param directly)

@josef-pkt

Copy link
Copy Markdown
Member Author

Currently there is no boundary and feasibility checking for the tools functions.
Can we get breakdown point larger than 50% ? e.g. we want to detect clusters if there are 3 roughly equal sized cluster?
The robust literature usually has maximal breakdown point close to 50%, a bit less in small samples.

We might run into corner or feasibility problems, but so far I only check "standard" case.

There are some conditions on max_rho, breakdown point and feasible solution for M-scale in the literature but I never paid close attention to those.
There will be conditions when M-scale is infeasible and is taken as zero.

I will leave those for when the problems actually show up in examples.

@josef-pkt

josef-pkt commented Mar 31, 2024 •

Copy link
Copy Markdown
Member Author

another issue: attributes for properties of norms

e.g. redescending

  • RamsayE and Cauchy are weakly (softly) redescending. It looks like max_rho = inf, but psi function goes to zero as abs(x) -> inf
    • I don't know about StudentT for different df > 1.
  • HuberT and LeastSquares are monotone, i.e. non-redescending
  • all others are (hard) redescending with finite threshold and finite max_rho

Only redescending norms (finite max_rho) are used in M-scale with large breakdown point.
We need a check in M-scale breakdown point if norm is redescender.
Either we use isfinite(norm.max_rho()) or add an attribute redescending, maybe with 3 levels, True, False and "weak" (?).
(maybe not good if user use if not redescending which includes weak redescenders.)
(The literature refers to "redescender" without qualifier usually to mean hard redescenders, not including soft redescenders.)

However, AFAIU, we can compute the M-scale also for non-redescending norms, although I have not seen that used in the literature. Using leastsquares norm for M-scale should just give variance.

reminder (I just remembered):
We need to be careful whether computation for, e.g., M-scale are for scale (std) or scale**2 (var).
scale in robust is defined as corresponding to std (and not variance)

update
I need an attribute to indicate whether the second derivative, psi_deriv is continuous or has jumps. That information is required to decide whether to use the jump version of var_normal.
currently that is TrimmedMean and Hampel, AFAICS.
(update to update I added a class attribute continuous with values 1 or 2 for continuous first or second derivative.
)

Aside: I added _set_tuning_param to Hampel, which changes c, and a and b are adjusted as constant fractions of c., c/4, resp. c/2
This follows Menenez.

return bp

res = []
for bp in bps:

Check failure

Code scanning / CodeQL

Non-iterable used in for loop

This for-loop may attempt to iterate over a [non-iterable instance](1) of class [float](2). This for-loop may attempt to iterate over a [non-iterable instance](3) of class [float](2).
@josef-pkt

Copy link
Copy Markdown
Member Author

numerical problems

test_tools takes now (next commit) 1 min.
Integration warnings with Andrews and Hampel on python 3.11, only with Hampel on python 3.9 on my computer (maybe changes in scipy).
unit test pass with small adjustments of numbers for Hampel in Menenez.

that means
results are ok for now.
We need faster computation or faster unit tests.
E.g. provide more help to numerical integration (kinks, ...) and rootfinding (more appropriate, tighter bounds). If possible we could get explicit solutions to integration if or when they exist.

For actual use by users, I'm planning to add hardcoded, precomputed tables with common values for efficiency and breakdown points, e.g. 5% intervals.
If we have the table, then we could also easily find tight bounds for brentq (using searchsorted on table values) for points in between tabled values.

Also there might be a lower bound on efficiency for non-redescending norms.
For example, rootfinding for HuberT requiring 50% efficiency fails. (What's the efficiency of LAD, i.e. HuberT as c->0?

unit tests are only for effs = [0.9, 0.95, 0.98, 0.99] for which tuning parameters are in Menenez.

@josef-pkt

Copy link
Copy Markdown
Member Author

checking breakdown point for norm M-scale, S-estimator

Not what I expected Quartic: has lower efficiency than biweight at the same breakdown point.
I guess, it's because we have to lower the threshold c to achieve the breakdown point, max_rho is increasing in k

bps = [0.5, 0.75]
​
norm_redesc = [TukeyBiweight(), TukeyQuartic(k=2), TukeyQuartic(k=4), TukeyQuartic(k=6)]
for norm in norm_redesc:
    res = robt.tuning_s_estimator_mean(norm, breakdown=bps)
    print(norm)
    print(res.all.T[..., :3])  # 'breakdown', 'eff', 'param'
<statsmodels.robust.norms.TukeyBiweight object at 0x000001CEF22B96D0>
[[0.5        0.28682612 1.54764498]
 [0.75       0.03936289 0.70416864]]
<statsmodels.robust.norms.TukeyQuartic object at 0x000001CEF22B98E0>
[[0.5        0.28682612 1.54764498]
 [0.75       0.03936289 0.70416864]]
<statsmodels.robust.norms.TukeyQuartic object at 0x000001CEF22B9850>
[[0.5        0.2671216  1.34768341]
 [0.75       0.03545058 0.61827138]]
<statsmodels.robust.norms.TukeyQuartic object at 0x000001CEF22B9CA0>
[[0.5        0.2558479  1.26108016]
 [0.75       0.03347108 0.58023609]]

If we use the same c in the norms, then eff is increasing in k. But that's not really relevant because we always adapt the tuning parameter to the norm.

This makes the Quartic norms a lot less interesting, max_rho for it is too large to provide a better S-estimator.
Maybe I need to look at the "optimal" norm after all.

The quartic norm has a larger almost linear segment around 0 than biweight, so it might still be useful for "something".

@josef-pkt
josef-pkt force-pushed the robust_tools_norms branch from 94a6068 to 25976ac Compare April 1, 2024 17:12
@josef-pkt
josef-pkt force-pushed the robust_tools_norms branch 2 times, most recently from 18b6ced to 05703b4 Compare April 11, 2024 13:23
@josef-pkt
josef-pkt force-pushed the robust_tools_norms branch from 05703b4 to 2fa3396 Compare April 11, 2024 20:52
@josef-pkt

Copy link
Copy Markdown
Member Author

all green, merging

not included in this PR:
Efficiency and breakdown point are not accessible through norm classes.
The tools functions are standalone and not yet reused internally.
No hard coded table of tuning parameters for common values of efficiency and breakdown points.

next
MScale class (I'm not sure yet what the api for choosing different types of rho/psi_scale functions should be.)
or additional helper functions that I have in draft versions.

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.

3 participants