Skip to content

Commit

Permalink
Merge pull request #7000 from esmucler/mad-center
Browse files Browse the repository at this point in the history
BUG: fix bug where mad ignores center if center is not callable
  • Loading branch information
bashtage committed Aug 27, 2020
2 parents d5748a5 + b3e76b9 commit 85b1829
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions statsmodels/robust/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ def mad(a, c=Gaussian.ppf(3/4.), axis=0, center=np.median):
"""
a = array_like(a, 'a', ndim=None)
c = float_like(c, 'c')
if callable(center) and a.size:
if not a.size:
center = 0.0
elif callable(center):
center = np.apply_over_axes(center, a, axis)
else:
center = 0.0
center = float_like(center, "center")

return np.median((np.abs(a-center)) / c, axis=axis)

Expand Down
7 changes: 7 additions & 0 deletions statsmodels/robust/tests/test_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from numpy.random import standard_normal
from numpy.testing import assert_almost_equal, assert_equal
from scipy.stats import norm as Gaussian
import pytest
# Example from Section 5.5, Venables & Ripley (2002)

Expand Down Expand Up @@ -81,6 +82,12 @@ def test_mad_empty(self):
def test_mad_center(self):
n = scale.mad(self.X, center=0)
assert_equal(n.shape, (10,))
with pytest.raises(TypeError):
scale.mad(self.X, center=None)
assert_almost_equal(scale.mad(self.X, center=1),
np.median(np.abs(self.X - 1),
axis=0)/Gaussian.ppf(3/4.),
DECIMAL)


class TestMadAxes(object):
Expand Down

0 comments on commit 85b1829

Please sign in to comment.