Skip to content

Commit

Permalink
STY: pep-8, flake-8
Browse files Browse the repository at this point in the history
  • Loading branch information
josef-pkt committed May 25, 2024
1 parent a6dca5b commit 539f80f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 38 deletions.
35 changes: 16 additions & 19 deletions statsmodels/robust/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import numpy as np
from scipy import stats, linalg
from scipy.linalg.lapack import dtrtri
from .scale import mad, qn_scale, _scale_iter
from .scale import mad
import statsmodels.robust.norms as rnorms
import statsmodels.robust.scale as rscale
import statsmodels.robust.tools as rtools
Expand Down Expand Up @@ -1266,12 +1266,12 @@ def _orthogonalize_det(x, corr, loc_func, scale_func):
cov = (transf0 * scale_z**2).dot(transf0.T)
# extra step in DetMCD, sphering data with new cov to compute center
# I think this is equivalent to scaling z
#loc_z = loc_func(z / scale_z) * scale_z # center of principal components
#loc = (transf0 * scale_z).dot(loc_z)
# loc_z = loc_func(z / scale_z) * scale_z # center of principal components
# loc = (transf0 * scale_z).dot(loc_z)
transf1 = (transf0 * scale_z).dot(transf0.T)
#transf1inv = (transf0 * scale_z**(-1)).dot(transf0.T)
# transf1inv = (transf0 * scale_z**(-1)).dot(transf0.T)

#loc = loc_func(x @ transf1inv) @ transf1
# loc = loc_func(x @ transf1inv) @ transf1
loc = loc_func((z / scale_z).dot(transf0.T)) @ transf1

return loc, cov
Expand Down Expand Up @@ -1369,7 +1369,6 @@ def __init__(self, data, norm_mean=None, norm_scatter=None,
else:
self.norm_scatter = norm_scatter


self.weights_mean = self.norm_mean.weights
self.weights_scatter = self.weights_mean
# self.weights_scatter = lambda d: self.norm_mean.rho(d) / d**2
Expand Down Expand Up @@ -1443,7 +1442,6 @@ def _fit_scale(self, maha, start_scale=None, maxiter=100, rtol=1e-5,
)
return scale


def fit(self, start_mean=None, start_shape=None, start_scale=None,
maxiter=100, update_scale=True):
"""Estimate mean, shape and scale parameters with MM-estimator.
Expand Down Expand Up @@ -1501,15 +1499,14 @@ def fit(self, start_mean=None, start_shape=None, start_scale=None,

for i in range(maxiter):
shape, mean = self._fit_mean_shape(mean_old, shape_old, scale_old)
# d = mahalanobis(self.data - mean, shape / scale_old**2, sqrt=True)
d = mahalanobis(self.data - mean, shape, sqrt=True)
if update_scale:
scale = self._fit_scale(d, start_scale=scale_old, maxiter=10)

if (np.allclose(scale, scale_old, rtol=1e-5) and
np.allclose(mean, mean_old, rtol=1e-5) and
np.allclose(shape, shape_old, rtol=1e-5)
):
): # noqa E124
converged = True
break
scale_old = scale
Expand Down Expand Up @@ -1582,7 +1579,7 @@ def _cstep(self, x, mean, cov, h, maxiter=2, tol=1e-8):

converged = False

for i in range(maxiter):
for _ in range(maxiter):
d = mahalanobis(x - mean, cov)
idx_sel = np.argpartition(d, h)[:h]
x_sel = x[idx_sel]
Expand Down Expand Up @@ -1623,8 +1620,8 @@ def _fit_one(self, x, idx, h, maxiter=2, mean=None, cov=None):
Notes
-----
This does not do any preprocessing of the data and returns the empirical mean
and covariance of evaluation set of the data ``x``.
This does not do any preprocessing of the data and returns the
empirical mean and covariance of evaluation set of the data ``x``.
"""
if idx is not None:
x_sel = x[idx]
Expand All @@ -1642,8 +1639,8 @@ def _fit_one(self, x, idx, h, maxiter=2, mean=None, cov=None):
return mean, cov, det, conv

def fit(self, h, *, h_start=None, mean_func=None, scale_func=None,
maxiter=100, options_start=None, reweight=True,
trim_frac=0.975, maxiter_step=100):
maxiter=100, options_start=None, reweight=True,
trim_frac=0.975, maxiter_step=100):
"""
Compute minimum covariance determinant estimate of mean and covariance.
Expand Down Expand Up @@ -1706,13 +1703,13 @@ def fit(self, h, *, h_start=None, mean_func=None, scale_func=None,
res = {}
for ii, ini in enumerate(starts):
idx_sel, method = ini
mean, cov, det, _ = self._fit_one(x, idx_sel, h,
maxiter=maxiter_step)
mean, cov, det, _ = self._fit_one(x, idx_sel, h,
maxiter=maxiter_step)
res[ii] = Holder(
mean=mean,
cov=cov * fac_trunc,
det_subset=det,
method=method
method=method,
)

det_all = np.array([i.det_subset for i in res.values()])
Expand All @@ -1725,7 +1722,7 @@ def fit(self, h, *, h_start=None, mean_func=None, scale_func=None,
# is with best 2 in original DetMCD
if maxiter_step < maxiter:
mean, cov, det, conv = self._fit_one(x, None, h, maxiter=maxiter,

Check warning on line 1724 in statsmodels/robust/covariance.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/covariance.py#L1724

Added line #L1724 was not covered by tests
mean=best.mean, cov=best.cov)
mean=best.mean, cov=best.cov)
best = Holder(

Check warning on line 1726 in statsmodels/robust/covariance.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/covariance.py#L1726

Added line #L1726 was not covered by tests
mean=mean,
cov=cov * fac_trunc,
Expand Down Expand Up @@ -1886,7 +1883,7 @@ def _fit_one(self, mean=None, shape=None, scale=None, maxiter=100):
return res

def fit(self, *, h_start=None, mean_func=None, scale_func=None,
maxiter=100, options_start=None, maxiter_step=5):
maxiter=100, options_start=None, maxiter_step=5):
"""Compute S-estimator of mean and covariance.
Parameters
Expand Down
2 changes: 1 addition & 1 deletion statsmodels/robust/norms.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ def get_tuning(cls, bp=None, eff=None):
"""
if ((bp is None and eff is None) or
(bp is not None and eff is not None)):
(bp is not None and eff is not None)):
raise ValueError("exactly one of bp and eff needs to be provided")

Check warning on line 923 in statsmodels/robust/norms.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/norms.py#L923

Added line #L923 was not covered by tests

if bp is not None:
Expand Down
3 changes: 1 addition & 2 deletions statsmodels/robust/resistant_linear_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from statsmodels.robust.robust_linear_model import RLM
import statsmodels.robust.norms as rnorms
import statsmodels.robust.scale as rscale
from statsmodels.robust.covariance import _get_detcov_startidx
from statsmodels.robust.covariance import _get_detcov_startidx

Check warning on line 16 in statsmodels/robust/resistant_linear_model.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/resistant_linear_model.py#L13-L16

Added lines #L13 - L16 were not covered by tests


class RLMDetS(Model):

Check warning on line 19 in statsmodels/robust/resistant_linear_model.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/resistant_linear_model.py#L19

Added line #L19 was not covered by tests
Expand Down Expand Up @@ -87,7 +87,6 @@ def _get_start_params(self, h):
start_params_all = [np.atleast_1d([q]) for q in quantiles]
return start_params_all

Check warning on line 88 in statsmodels/robust/resistant_linear_model.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/resistant_linear_model.py#L88

Added line #L88 was not covered by tests


starts = _get_detcov_startidx(

Check warning on line 90 in statsmodels/robust/resistant_linear_model.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/resistant_linear_model.py#L90

Added line #L90 was not covered by tests
self.data_start, h, options_start=None, methods_cov="all")

Expand Down
6 changes: 3 additions & 3 deletions statsmodels/robust/tests/test_covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def test_covdetmcd():
0.0223939863695 1.1384166802155 0.4315534571891 -0.2344041030201
0.7898958050933 0.4315534571891 1.8930117467493 -0.3292893001459
0.4060613360808 -0.2344041030201 -0.3292893001459 0.6179686100845
""".split(), float).reshape(4,4)
""".split(), float).reshape(4, 4)

mean_dmcd_r = np.array([1.7725, 2.2050, 1.5375, -0.0575])

Expand All @@ -250,7 +250,7 @@ def test_covdetmcd():
0.0928577020508 1.56769177397171 0.224929617385 -0.00516128856542
0.2520760101278 0.22492961738467 1.483829106079 -0.20275013775619
0.1387344440830 -0.00516128856542 -0.202750137756 0.43326701543885
""".split(), float).reshape(4,4)
""".split(), float).reshape(4, 4)

mod = robcov.CovDetMCD(dta_hbk)
res = mod.fit(40, maxiter_step=100) # default is reweight=True
Expand All @@ -272,7 +272,7 @@ def test_covdetmm():
0.06925842715939 1.74566218886362 0.22161135221404 -0.00517023660647
0.20781848922667 0.22161135221404 1.63937749762534 -0.17217102475913
0.10749343153015 -0.00517023660647 -0.17217102475913 0.48174480967136
""".split(), float).reshape(4,4)
""".split(), float).reshape(4, 4)

mean_dmm_r = np.array([1.5388643420460, 1.8027582110408, 1.6811517253521,
-0.0755069488908])
Expand Down
27 changes: 14 additions & 13 deletions statsmodels/robust/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Author: Josef Perktold
License: BSD-3
"""

# flake8: noqa E731
import numpy as np
from scipy import stats, integrate, optimize

Expand Down Expand Up @@ -57,8 +57,8 @@ def _var_normal(norm):
"""
num = stats.norm.expect(lambda x: norm.psi(x)**2)
denom = stats.norm.expect(lambda x: norm.psi_deriv(x))**2
num = stats.norm.expect(lambda x: norm.psi(x)**2) #noqa E731
denom = stats.norm.expect(lambda x: norm.psi_deriv(x))**2 #noqa E731
return num / denom


Expand Down Expand Up @@ -100,7 +100,7 @@ def _var_normal_jump(norm):
"""
num = stats.norm.expect(lambda x: norm.psi(x)**2)
num = stats.norm.expect(lambda x: norm.psi(x)**2) #noqa E731

def func(x):
# derivative normal pdf
Expand Down Expand Up @@ -142,7 +142,6 @@ def _get_tuning_param(norm, eff, kwd="c", kwargs=None, use_jump=False,
efficiency.
"""
kwds = {} if kwargs is None else kwargs
if bracket is None:
bracket = [0.1, 10]

Expand Down Expand Up @@ -216,12 +215,13 @@ def tuning_s_estimator_mean(norm, breakdown=None):
def func(c):
norm_ = norm
norm_._set_tuning_param(c, inplace=True)
bp = stats.norm.expect(lambda x : norm_.rho(x)) / norm_.max_rho()
bp = (stats.norm.expect(lambda x : norm_.rho(x)) / #noqa E731
norm_.max_rho())
return bp

res = []
for bp in bps:
c_bp = optimize.brentq(lambda c0: func(c0) - bp, 0.1, 10)
c_bp = optimize.brentq(lambda c0: func(c0) - bp, 0.1, 10) #noqa E731
norm._set_tuning_param(c_bp, inplace=True) # inplace modification
eff = 1 / _var_normal(norm)
b = stats.norm.expect(lambda x : norm.rho(x))
Expand Down Expand Up @@ -250,9 +250,9 @@ def scale_bias_cov_biw(c, k_vars):
This uses the chisquare distribution as reference distribution for the
squared Mahalanobis distance.
"""
p = k_vars # alias for formula
p = k_vars # alias for formula
chip, chip2, chip4, chip6 = stats.chi2.cdf(c**2, [p, p + 2, p + 4, p + 6])
b = p / 2 * chip2 - p * (p + 2) / (2 * c**2) * chip4
b = p / 2 * chip2 - p * (p + 2) / (2 * c**2) * chip4
b += p * (p + 2) * (p + 4) / (6 * c**4) * chip6 + c**2 / 6 * (1 - chip)
return b, b / (c**2 / 6)

Expand Down Expand Up @@ -294,6 +294,7 @@ def func(c):
return scale_bias_cov_biw(c, k_vars)[1] - breakdown_point
else:
norm = norm._set_tuning_param(2., inplace=False) # create copy

Check warning on line 296 in statsmodels/robust/tools.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/tools.py#L296

Added line #L296 was not covered by tests

def func(c):
norm._set_tuning_param(c, inplace=True)
return scale_bias_cov(norm, k_vars)[1] - breakdown_point

Check warning on line 300 in statsmodels/robust/tools.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/tools.py#L298-L300

Added lines #L298 - L300 were not covered by tests
Expand Down Expand Up @@ -337,7 +338,8 @@ def eff_mvmean(norm, k_vars):
"""
k = k_vars # shortcut

Check warning on line 339 in statsmodels/robust/tools.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/tools.py#L339

Added line #L339 was not covered by tests
f_alpha = lambda d: norm.psi(d)**2 / k
f_beta = lambda d: (1 - 1 / k) * norm.weights(d) + 1 / k * norm.psi_deriv(d)
f_beta = lambda d: ((1 - 1 / k) * norm.weights(d) +
1 / k * norm.psi_deriv(d))
alpha = stats.chi(k).expect(f_alpha)
beta = stats.chi(k).expect(f_beta)
return beta**2 / alpha, alpha, beta

Check warning on line 345 in statsmodels/robust/tools.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/tools.py#L343-L345

Added lines #L343 - L345 were not covered by tests
Expand Down Expand Up @@ -439,8 +441,6 @@ def func(c):
return p_tune

Check warning on line 441 in statsmodels/robust/tools.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/robust/tools.py#L440-L441

Added lines #L440 - L441 were not covered by tests




# ##### tables

tukeybiweight_bp = {
Expand All @@ -458,7 +458,7 @@ def func(c):
}

tukeybiweight_eff = {
#efficiency : (tuning parameter, breakdown point)
# efficiency : (tuning parameter, breakdown point)
0.65: (2.523102, 0.305646),
0.70: (2.697221, 0.280593),
0.75: (2.897166, 0.254790),
Expand Down Expand Up @@ -534,6 +534,7 @@ def _convert_to_dict_mvmean_effs(eff_mean=True):

return tp


tukeybiweight_mvmean_eff_d = _convert_to_dict_mvmean_effs(eff_mean=True)
tukeybiweight_mvshape_eff_d = _convert_to_dict_mvmean_effs(eff_mean=False)

Expand Down

0 comments on commit 539f80f

Please sign in to comment.