Skip to content

[BUG]: @stdlib/stats-wilcoxon: exact two-sided p-value is greater than 1 when the statistic equals n(n+1)/4 #16663

Open

Description

@tasuku-kobayashi

Description

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

if ( stat > ( len * ( len+1 ) / 4 ) ) {
	pval = 2.0 * ( 1 - signrankCDF( stat - 1, len ) );
} else {
	pval = 2.0 * signrankCDF( stat, len );
}

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".

Questions

No.

Demo

No response

Reproduction

var wilcoxon = require( '@stdlib/stats-wilcoxon' );

var out = wilcoxon( [ 1, 2, -3 ] );
console.log( out.pValue );

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).

Expected Results

1

Actual Results

1.2500000000000004

Version

@stdlib/stats-wilcoxon 0.2.3

Environments

Node.js

Browser Version

No response

Node.js / npm Version

Node.js v22.23.4

Platform

Linux x86-64 (Ubuntu 24.04)

Checklist

  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.

Activity

  1. stdlib-bot commented on Nov 17, 2026

    @stdlib-bot
    Contributor

    👋 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.

  2. batpigandme commented on Nov 17, 2026

    @batpigandme
    Member

    Related upstream fixes (including Julia's identical p > 1 bug, fixed with a doubled-and-capped tail) are collected in #16664 (#16664 (comment)).

  3. kgryte commented on Nov 17, 2026

    @kgryte
    Member

    @Planeshifter Given you wrote the affected package, would you mind taking a look?

  4. added
    Numerical Accuracy
    on Nov 17, 2026
  5. 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
  6. added
    Bug
    Statistics
    on Nov 17, 2026
  7. batpigandme commented on Nov 17, 2026

    @batpigandme
    Member

    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' ) {

    Commit: batpigandme@bedf444

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugNumerical AccuracyStatistics

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions