You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Running a huber estimate on the data in the attached script, produces NaN for the new scale estimate on the first huber iteration due to the fact that the SQRT gets a negative result as input.
The separate class for mean and scale estimation are not well tested.
I get a convergence failure, which raises an exception that maxiter has been reached, which doesn't allow seeing any results.
Using RLM with just a constant as regressor works for me:
>>> from statsmodels.robust.robust_linear_model import RLM
>>> res = RLM(a, np.ones(len(a))).fit()
>>> print(res.summary())
Robust linear Model Regression Results
==============================================================================
Dep. Variable: y No. Observations: 25
Model: RLM Df Residuals: 24
Method: IRLS Df Model: 0
Norm: HuberT
Scale Est.: mad
Cov Type: H1
Date: Fri, 22 Jun 2018
Time: 08:09:35
No. Iterations: 42
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
const 1.31e-05 9.06e-07 14.460 0.000 1.13e-05 1.49e-05
==============================================================================
If the model instance has been used for another fit with different fit
parameters, then the fit options might not be the correct ones anymore .
The joint estimation of loc and scale in Huber().__call__ is almost certaintly wrong, and doesn't align with HuberScale().__call__ (which appears to be right). In this example, it quickly produces a nan and then cannot recover.
In a quick look, I had
# TODO: This is wrong and doesn't match up with the scale estimator below
nscale = np.sum(subset * (a - nmu)**2, axis)
denom = n * self.gamma - (a.shape[axis] - card) * self.c**2
if denom <= 0:
raise RuntimeError('The number of observations clipped is too large to estimate the scale.')
nscale = np.sqrt(nscale / denom)
from statsmodels.robust.robust_linear_model import RLM
res = RLM(a, np.ones(len(a))).fit(scale_est=HuberScale())
print(res.summary())
Robust linear Model Regression Results
==============================================================================================================================
Dep. Variable: y No. Observations: 25
Model: RLM Df Residuals: 24
Method: IRLS Df Model: 0
Norm: HuberT
Scale Est.: <statsmodels.robust.scale.HuberScale object at 0x0000018DA5F070D0>
Cov Type: H1
Date: Mon, 13 Feb 2023
Time: 15:03:29
No. Iterations: 19
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
const 1.367e-05 1.15e-06 11.920 0.000 1.14e-05 1.59e-05
==============================================================================
If the model instance has been used for another fit with different fit parameters, then the fit options might not be the correct ones anymore .
It looks like one problem is using mad as initial scale if it is (almost) zero.
Huber converges if I use a larger initscale.
Note: initscale needs to be ndarray. float does not work because of all the squeeze, unsqueeze and reshape.
(This might have been written as python/numpy beginner code)
I went through the scale iteration in Huber and don't understand the computation.
If I change it to something standard, ie. scale_num is winsorized sum of squares, then I get convergence although it requires larger maxiter than default for the data in this issue.
huber fit/__call__ does not have maxiter and tol option in "fit", but those are attributes that can be changed. update maxiter, tol are __init__ keywords, e.g. unit test has scale.Huber(maxiter=1000, tol=1.0e-05)
(My current change still causes shape errors in unit tests ValueError: cannot reshape array of size 48000 into shape (40,1,30)) axis option is a pain in the code, too many squeeze and unsqueeze, and requires that mu and initscale need to be arrays.
related issue #9171 and upcoming PR for fixes and MScale enhancements.
aside:
HuberScale has a different default than Huber
The matching RLM would be the following but scale estimate is much smaller than the huber proposal 2 estimate.
BUG:
I got a convergence failure in unit test test_scale.py::TestHuberAxes::test_axis1 after my changes.
current Huber.__call__ has hardcoded axis=0 when computing nobs/df_resid n
if mu is None:
n = a.shape[0] - 1
mu = np.median(a, axis=axis)
est_mu = True
else:
n = a.shape[0]
mu = mu
est_mu = False
unit tests don't fail on this because they only check shape and not correctness of numbers
after fixing the axis here, I don't get convergence failures anymore, even if I reduce maxiter from 1000 to 100.
(maxiter=1000 was working around the bug in the unit test).
Fixed via PR #9210 (commit dc0844f): replaced a hardcoded axis=0 with a.shape[axis], and replaced a scale-update formula that could go negative (feeding sqrt a negative value) with an always-positive winsorized-sum-of-squares formula in robust.scale.Huber (statsmodels/robust/scale.py). Closing as fixed.
Activity
josef-pkt commentedon Jun 22, 2018
The separate class for mean and scale estimation are not well tested.
I get a convergence failure, which raises an exception that maxiter has been reached, which doesn't allow seeing any results.
Using RLM with just a constant as regressor works for me:
bashtage commentedon May 10, 2019
The joint estimation of loc and scale in
Huber().__call__is almost certaintly wrong, and doesn't align withHuberScale().__call__(which appears to be right). In this example, it quickly produces ananand then cannot recover.In a quick look, I had
josef-pkt commentedon Feb 13, 2023
aside: HuberScale in summary looks awful
josef-pkt commentedon Feb 13, 2023
bump for 0.15
(plus unblackening the math code. Current code is very difficult to read, e.g.)
josef-pkt commentedon Feb 13, 2023
It looks like one problem is using
madas initial scale if it is (almost) zero.Huber converges if I use a larger initscale.
Note: initscale needs to be ndarray. float does not work because of all the squeeze, unsqueeze and reshape.
(This might have been written as python/numpy beginner code)
compared to
Using RLM with HuberScale()
using RLM with mad scale_est
josef-pkt commentedon Apr 15, 2024
I went through the scale iteration in Huber and don't understand the computation.
If I change it to something standard, ie.
scale_numis winsorized sum of squares, then I get convergence although it requires larger maxiter than default for the data in this issue.huber fit/
__call__does not have maxiter and tol option in "fit", but those are attributes that can be changed.update maxiter, tol are
__init__keywords, e.g. unit test hasscale.Huber(maxiter=1000, tol=1.0e-05)(My current change still causes shape errors in unit tests
ValueError: cannot reshape array of size 48000 into shape (40,1,30))axisoption is a pain in the code, too many squeeze and unsqueeze, and requires that mu and initscale need to be arrays.related issue #9171 and upcoming PR for fixes and MScale enhancements.
aside:
HuberScale has a different default than Huber
The matching RLM would be the following but scale estimate is much smaller than the
huberproposal 2 estimate.I don't know why there is not a closer match in the estimates between RLM and scale.Huber.
(unless precision issues introduce several local minima)
josef-pkt commentedon Apr 15, 2024
BUG:
I got a convergence failure in unit test
test_scale.py::TestHuberAxes::test_axis1after my changes.current
Huber.__call__has hardcoded axis=0 when computing nobs/df_residnunit tests don't fail on this because they only check shape and not correctness of numbers
after fixing the axis here, I don't get convergence failures anymore, even if I reduce maxiter from 1000 to 100.
(maxiter=1000 was working around the bug in the unit test).
BUG: fix iteration loop and axis handling in scale.Huber, see statsmo…
bashtage commentedon Jul 30, 2026
Fixed via PR #9210 (commit dc0844f): replaced a hardcoded axis=0 with a.shape[axis], and replaced a scale-update formula that could go negative (feeding sqrt a negative value) with an always-positive winsorized-sum-of-squares formula in robust.scale.Huber (statsmodels/robust/scale.py). Closing as fixed.