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
{{ message }}
Repository navigation
[BUG]: @stdlib/stats-wilcoxon: exact two-sided p-value is greater than 1 when the statistic equals n(n+1)/4 #16663
BugSomething isn't working.Numerical AccuracyIssue or pull request concerns numerical accuracy.StatisticsIssue or pull request related to statistical functionality.
Encountered an incorrect result when computing an exact two-sided Wilcoxon signed-rank test with @stdlib/stats-wilcoxon. For data without ties or zeros, the returned p-value exceeds 1 when the test statistic equals its null mean n(n+1)/4. This happens with default options.
Cause: in lib/main.js, the exact two-sided branch is
When stat equals len*(len+1)/4, signrankCDF( stat, len ) is larger than one half, so the doubled value is above 1. Clamping the result with min( 1.0, pval ) gives 1 for these inputs and leaves other inputs unchanged.
Scope: on 3,334 inputs with distinct nonzero integer values (n = 2 to 100, exact: true), 36 inputs with a statistic equal to n(n+1)/4 gave a two-sided p-value above 1 (largest n: 75). Five further n = 6 inputs returned 1.0000000000000002. The file is unchanged on develop as of 2026-11-17.
Related Issues
None found. Searched issues and pull requests for "wilcoxon" and "signrank".
The ranks are 1, 2, 3 and the statistic is 3. Of the 8 sign assignments, 5 give a statistic of at most 3 and 5 give a statistic of at least 3. Both one-sided probabilities are 5/8, so the two-sided value is min(1, 2 * 5/8) = 1. R returns 1 for wilcox.test(c(1, 2, -3), exact = TRUE).
And thank you for opening your first issue! We will get back to you shortly. 🏃 💨
If you have any further questions while waiting for a response, please join our Zulip community to chat with project maintainers and other community members.
changed the title [-]`@stdlib/stats-wilcoxon`: exact two-sided p-value is greater than 1 when the statistic equals n(n+1)/4[/-][+][BUG]: `@stdlib/stats-wilcoxon`: exact two-sided p-value is greater than 1 when the statistic equals n(n+1)/4[/+]on Nov 17, 2026
Here's a rough fix in case it's useful. I have it on a branch and can open a PR if you'd like one, or you can take it from here.
Cause: the exact null distribution is discrete, so at the null mean n(n+1)/4 the lower tail P(W <= s) is at least one half (strictly more than half at s = 3, n = 3). Doubling it then gives a value above 1. The CDF's accumulated rounding can also push one-sided values just past 1, e.g. 1.0000000000000107 for n = 55.
Minimal fix: clamp to one, in lib/main.js:
var abs = require( '@stdlib/math/base/special/abs' );
+var min = require( '@stdlib/math/base/special/min' );
var Float64Array = require( '@stdlib/array/float64' );
} else {
pval = 2.0 * signrankCDF( stat, len );
}
+ pval = min( 1.0, pval );
} else if ( alt === 'greater' ) {
Test:test/test.js L349-L367 checks the repro, [ 1, 2, -3 ] (statistic exactly at the mean), and [ 1, 2, 3, 4, -5, -6 ] (statistic 10, just below the mean of 10.5, where the CDF is slightly above one half). Both now return exactly 1, which matches R's wilcox.test.
That said, I think the clamp alone isn't the whole story, bc the upper branch has the cancellation problem in #16664. So, the fix I'd actually propose handles both: compute each tail directly and clamp every alternative, not just the two-sided one. Details on that issue.
BugSomething isn't working.Numerical AccuracyIssue or pull request concerns numerical accuracy.StatisticsIssue or pull request related to statistical functionality.
Activity
stdlib-bot commentedon Nov 17, 2026
👋 Hi there! 👋
And thank you for opening your first issue! We will get back to you shortly. 🏃 💨
If you have any further questions while waiting for a response, please join our Zulip community to chat with project maintainers and other community members.
batpigandme commentedon Nov 17, 2026
Related upstream fixes (including Julia's identical p > 1 bug, fixed with a doubled-and-capped tail) are collected in #16664 (#16664 (comment)).
kgryte commentedon Nov 17, 2026
@Planeshifter Given you wrote the affected package, would you mind taking a look?
[-]`@stdlib/stats-wilcoxon`: exact two-sided p-value is greater than 1 when the statistic equals n(n+1)/4[/-][+][BUG]: `@stdlib/stats-wilcoxon`: exact two-sided p-value is greater than 1 when the statistic equals n(n+1)/4[/+]batpigandme commentedon Nov 17, 2026
Here's a rough fix in case it's useful. I have it on a branch and can open a PR if you'd like one, or you can take it from here.
Cause: the exact null distribution is discrete, so at the null mean
n(n+1)/4the lower tailP(W <= s)is at least one half (strictly more than half ats = 3,n = 3). Doubling it then gives a value above 1. The CDF's accumulated rounding can also push one-sided values just past 1, e.g.1.0000000000000107forn = 55.Minimal fix: clamp to one, in
lib/main.js:var abs = require( '@stdlib/math/base/special/abs' ); +var min = require( '@stdlib/math/base/special/min' ); var Float64Array = require( '@stdlib/array/float64' );} else { pval = 2.0 * signrankCDF( stat, len ); } + pval = min( 1.0, pval ); } else if ( alt === 'greater' ) {Commit: batpigandme@bedf444
Test:
test/test.jsL349-L367 checks the repro,[ 1, 2, -3 ](statistic exactly at the mean), and[ 1, 2, 3, 4, -5, -6 ](statistic 10, just below the mean of 10.5, where the CDF is slightly above one half). Both now return exactly1, which matches R'swilcox.test.That said, I think the clamp alone isn't the whole story, bc the upper branch has the cancellation problem in #16664. So, the fix I'd actually propose handles both: compute each tail directly and clamp every alternative, not just the two-sided one. Details on that issue.