Skip to content

Commit

Permalink
MAINT: stats.wilcoxon: fix bug with Ndim>1, shape[axis]>50, NaN, 'aut…
Browse files Browse the repository at this point in the history
…o' (#20592)
  • Loading branch information
mdhaber committed May 16, 2024
1 parent f2d4775 commit 224dea5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions scipy/stats/_wilcoxon.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def _wilcoxon_iv(x, y, zero_method, correction, alternative, method, axis):
"an instance of `stats.PermutationMethod`.")
if method not in methods:
raise ValueError(message)
output_z = True if method == 'approx' else False

# logic unchanged here for backward compatibility
n_zero = np.sum(d == 0, axis=-1)
Expand All @@ -127,7 +128,7 @@ def _wilcoxon_iv(x, y, zero_method, correction, alternative, method, axis):
if 0 < d.shape[-1] < 10 and method == "approx":
warnings.warn("Sample size too small for normal approximation.", stacklevel=2)

return d, zero_method, correction, alternative, method, axis
return d, zero_method, correction, alternative, method, axis, output_z


def _wilcoxon_statistic(d, zero_method='wilcox'):
Expand Down Expand Up @@ -196,7 +197,7 @@ def _wilcoxon_nd(x, y=None, zero_method='wilcox', correction=True,
alternative='two-sided', method='auto', axis=0):

temp = _wilcoxon_iv(x, y, zero_method, correction, alternative, method, axis)
d, zero_method, correction, alternative, method, axis = temp
d, zero_method, correction, alternative, method, axis, output_z = temp

if d.size == 0:
NaN = _get_nan(d)
Expand Down Expand Up @@ -232,6 +233,6 @@ def _wilcoxon_nd(x, y=None, zero_method='wilcox', correction=True,
z = -np.abs(z) if (alternative == 'two-sided' and method == 'approx') else z

res = _morestats.WilcoxonResult(statistic=statistic, pvalue=p[()])
if method == 'approx':
if output_z:
res.zstatistic = z[()]
return res
16 changes: 16 additions & 0 deletions scipy/stats/tests/test_morestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,22 @@ def test_permutation_method(self, size):
assert_equal(np.round(res.pvalue, 2), res.pvalue) # n_resamples used
assert_equal(res.pvalue, ref.pvalue) # random_state used

def test_method_auto_nan_propagate_ND_length_gt_50_gh20591(self):
# When method!='approx', nan_policy='propagate', and a slice of
# a >1 dimensional array input contained NaN, the result object of
# `wilcoxon` could (under yet other conditions) return `zstatistic`
# for some slices but not others. This resulted in an error because
# `apply_along_axis` would have to create a ragged array.
# Check that this is resolved.
rng = np.random.default_rng(235889269872456)
A = rng.normal(size=(51, 2)) # length along slice > exact threshold
A[5, 1] = np.nan
res = stats.wilcoxon(A)
ref = stats.wilcoxon(A, method='approx')
assert_allclose(res, ref)
assert hasattr(ref, 'zstatistic')
assert not hasattr(res, 'zstatistic')


# data for k-statistics tests from
# https://cran.r-project.org/web/packages/kStatistics/kStatistics.pdf
Expand Down

0 comments on commit 224dea5

Please sign in to comment.