Repository navigation
Conversation
|
|
||
| def mahalanobis(data, cov=None, cov_inv=None): | ||
| """Mahalanobis distance | ||
|
|
There was a problem hiding this comment.
note: this does not return the sqrt
|
|
||
| def cov_gk1(x, y, scale_func=mad): | ||
| """Gnanadesikan and Kettenring covariance between two variables | ||
|
|
There was a problem hiding this comment.
maybe add option for general version, i.e. a, b coefficient as in Huber/Ronchetti book (?)
or normalized by scale of x and of y.
| cf = 2 * ((1 - c**2) * stats.norm.cdf(c) - c * stats.norm.pdf(c) | ||
| + c**2) - 1 | ||
| #return Holder(loc=mean, scale=np.sqrt(var / cf)) | ||
| return mean, np.sqrt(var / cf) |
|
|
||
| # calculate MAD only once if needed | ||
| if start_cov is None or shrinkage_factor is None: | ||
| scale_mad = sm.robust.mad(x, center=0) |
There was a problem hiding this comment.
drop sm (pylint error)
| if diff < eps: | ||
| break | ||
|
|
||
| return c, i, shrinkage_factor, corr |
There was a problem hiding this comment.
return Holder, CovResults instead
| for it in range(maxiter): | ||
| dist = mahalanobis(data, cov=cov) | ||
| w = weights_func(dist, *weights_args) | ||
| cov, mean = cov_weighted(data, w) |
There was a problem hiding this comment.
in general we need different options for denominator in weighted cov.
standard M does not rescale cov by wsum
| s = np.median(dist) / stats.chi2.ppf(0.5, k_vars) | ||
| cov *= s | ||
|
|
||
| return cov, mean, w, dist, it, converged |
There was a problem hiding this comment.
return Holder or CovResults for consistent pattern. This is partially a tools function.
| def _scale_iter(data, scale0='mad', maxiter=10, rtol=1e-6, atol=1e-8, | ||
| meef_scale=None, scale_bias=None): | ||
| """iterative scale estimate base on "rho" function | ||
|
|
There was a problem hiding this comment.
docstring, requirement for meef_scale
is this a standard M-estimator of scale?
| scale_old = scale | ||
| for i in range(maxiter): | ||
| x_scaled = x / scale | ||
| weights_scale = meef_scale(x_scaled) / (1e-50 + x_scaled**2) |
There was a problem hiding this comment.
this looks like an unnecessary detour: either use weights_scale as argument or usemeef_scale (i.e. "rho") in the iteration.
| def _scale_iter(data, scale0='mad', maxiter=10, rtol=1e-6, atol=1e-8, | ||
| meef_scale=None, scale_bias=None): | ||
| """iterative scale estimate base on "rho" function | ||
|
|
There was a problem hiding this comment.
rename scale_bias?
need consistent terminology for various consistency or normalization "constants"
807158c to
df6d678
Compare
|
rebased, a few merge conflict in test_scale |
| x_scaled = x / scale | ||
| weights_scale = meef_scale(x_scaled) / (1e-50 + x_scaled**2) | ||
| if debug: | ||
| print('weights sum', weights_scale.sum(), end=" ") |
There was a problem hiding this comment.
print syntax is not py 2 compatible
f698b3d to
450a606
Compare
450a606 to
6f124aa
Compare
|
We need to start to add future division everywhere (like scipy), that's more efficient than chasing every failure to see which module is missing it. |
|
This pull request introduces 2 alerts - view on lgtm.com new alerts:
Comment posted by lgtm.com |
Codecov Report
@@ Coverage Diff @@
## master #3230 +/- ##
==========================================
- Coverage 79.32% 79.31% -0.01%
==========================================
Files 544 548 +4
Lines 81718 82295 +577
Branches 9255 9339 +84
==========================================
+ Hits 64822 65274 +452
- Misses 14802 14884 +82
- Partials 2094 2137 +43
Continue to review full report at Codecov.
|
|
(When I was waiting at the dentists office I saw that I had |
|
unit test coverage is low but not too bad 75% for both main modules with unit tests against R package rrcov, we should be able to get good enough coverage for merging. |
|
many merge conflicts in |
|
rebased version in #8129 |
|
closing #8129 has been merged |
see #3221
getting started, some parts are almost clean
for other parts I need to collect functions from various scripts so I can start on a consistent collection
contains:
_scale_iter: basic iterative M-estimator for scale, IIRC, givenrhofunction, approximate unit testtrimmed_scale: another old function, not reviewed againproblems to address
madas initial estimate, we get scale=0 if more than half of the observations fit perfectly. see REF/ENH RLM and robust scale for almost perfect prediction #1341Terminology
scale_trimmedreturns standard deviation.(difference scale in distribution is std (sigma), scale in regression is var (sigma**2)
coveither covariance or scatter matrix, as returned by functions, scaling depends on options or implementation detailsmeanreturned by functions for the center, i.e. mean or expected value of data excluding outliersok for symmetric distribution where mean is the center, e.g. median estimates the mean.
Need to be more explicit for skewed distributions, but main value of interest is mean (expected value), but we also have median and location parameters which might differ.