Skip to content

Commit 0ec0fb3

Browse files
authored
MAINT: stats.order_statistic: override support (scipy#22050)
* MAINT: stats.order_statistic: override `support` [skip ci] * MAINT: stats.OrderStatisticDistribution: override _update_parameters
1 parent 0b2496f commit 0ec0fb3

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

scipy/stats/_distribution_infrastructure.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4236,6 +4236,14 @@ class OrderStatisticDistribution(TransformedDistribution):
42364236
def __init__(self, dist, /, *args, r, n, **kwargs):
42374237
super().__init__(dist, *args, r=r, n=n, **kwargs)
42384238

4239+
def _support(self, *args, r, n, **kwargs):
4240+
return self._dist._support(*args, **kwargs)
4241+
4242+
def _process_parameters(self, r=None, n=None, **params):
4243+
parameters = self._dist._process_parameters(**params)
4244+
parameters.update(dict(r=r, n=n))
4245+
return parameters
4246+
42394247
def _overrides(self, method_name):
42404248
return method_name in {'_logpdf_formula', '_pdf_formula',
42414249
'_cdf_formula', '_ccdf_formula',

scipy/stats/tests/test_continuous.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,7 @@ def test_pow(self):
14961496
sample = Y.sample(10)
14971497
assert np.all(sample > 0)
14981498

1499+
class TestOrderStatistic:
14991500
@pytest.mark.fail_slow(20) # Moments require integration
15001501
def test_order_statistic(self):
15011502
rng = np.random.default_rng(7546349802439582)
@@ -1539,6 +1540,33 @@ def test_order_statistic(self):
15391540
with pytest.raises(ValueError, match=message):
15401541
stats.order_statistic(X, n=1.5, r=r)
15411542

1543+
def test_support_gh22037(self):
1544+
# During review of gh-22037, it was noted that the `support` of
1545+
# an `OrderStatisticDistribution` returned incorrect results;
1546+
# this was resolved by overriding `_support`.
1547+
Uniform = stats.make_distribution(stats.uniform)
1548+
X = Uniform()
1549+
Y = X*5 + 2
1550+
Z = stats.order_statistic(Y, r=3, n=5)
1551+
assert_allclose(Z.support(), Y.support())
1552+
1553+
def test_composition_gh22037(self):
1554+
# During review of gh-22037, it was noted that an error was
1555+
# raised when creating an `OrderStatisticDistribution` from
1556+
# a `TruncatedDistribution`. This was resolved by overriding
1557+
# `_update_parameters`.
1558+
Normal = stats.make_distribution(stats.norm)
1559+
TruncatedNormal = stats.make_distribution(stats.truncnorm)
1560+
a, b = [-2, -1], 1
1561+
r, n = 3, [[4], [5]]
1562+
x = [[[-0.3]], [[0.1]]]
1563+
X1 = Normal()
1564+
Y1 = stats.truncate(X1, a, b)
1565+
Z1 = stats.order_statistic(Y1, r=r, n=n)
1566+
X2 = TruncatedNormal(a=a, b=b)
1567+
Z2 = stats.order_statistic(X2, r=r, n=n)
1568+
np.testing.assert_allclose(Z1.cdf(x), Z2.cdf(x))
1569+
15421570

15431571
class TestFullCoverage:
15441572
# Adds tests just to get to 100% test coverage; this way it's more obvious

0 commit comments

Comments
 (0)