Skip to content

fix: return the smallest x with F(x) >= p in stats/base/dists/discrete-uniform/quantile - #15602

Open
Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/discrete-uniform-quantile
Open

Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/discrete-uniform-quantile

Conversation

@Abhist17

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • fixes stats/base/dists/discrete-uniform/quantile (JS, factory, and C) returning one past the quantile whenever p*(b-a+1) is an integer. That includes p = 1, where it returns b+1, outside the support.
var quantile = require( '@stdlib/stats/base/dists/discrete-uniform/quantile' );
var cdf = require( '@stdlib/stats/base/dists/discrete-uniform/cdf' );

quantile( 1.0, 1, 2 );  // 3, which is not in {1, 2}
quantile( 0.5, 1, 2 );  // 2, but cdf( 1, 1, 2 ) is already 0.5
quantile( 0.5, -8, 7 ); // 0, but cdf( -1, -8, 7 ) is already 0.5

The quantile is the smallest x with F(x) >= p. With F(x) = (x-a+1)/n and n = b-a+1, that is x = a - 1 + ceil(p*n), not a + floor(p*n). The two agree unless p*n is an integer, which is why the Julia fixtures didn't catch it: p = rand() never lands on k/n. The Julia runner's own quantile( DiscreteUniform( a, b ), p ) gives a - 1 + ceil(p*n), and the new implementation matches all 3,000 existing fixture values.

Two details:

  • Q(0) = a, since ceil(0) = 0 would otherwise give a-1.
  • p*n can round up past an integer, e.g. 0.3*10 = 3.0000000000000004. The code steps k back when (k-1)/n >= p, which is the same comparison cdf makes, so F(Q(p)) >= p > F(Q(p)-1) holds exactly.

The README formula is updated to match. I couldn't get make markdown-svg-equations-files to run locally (remark config error), so docs/img/equation_discrete_uniform_quantile_function.svg still shows the old formula and will need regenerating.

Related Issues

Does this pull request have any related issues?

No.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

  • New tests (JS, factory, native) check p = 0 and p = 1, and every p = k/n for n in 1..10 and a in {-3, 0, 3}. On develop, 167 assertions fail in test.quantile.js (all 165 p = k/n cases and both p = 1 checks), and the same 167 fail in test.factory.js. All of them pass with this change.
  • One doc example changes: quantile.factory( 0, 4 )( 0.8 ) now returns 3, since F(3) = 4/5 = 0.8. It is updated in the README, repl.txt, index.d.ts, and the lib/index.js JSDoc.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

This PR was written primarily by Claude Code. The bug was found by comparing every stats/base/dists function against SciPy on random parameters.


@stdlib-js/reviewers

…screte-uniform/quantile`

`a + floor(p*n)` is one past the quantile whenever `p*n` is an integer,
and returns `b+1`, outside the support, for `p = 1`. Use
`a - 1 + ceil(p*n)` instead, with `Q(0) = a` and a correction for `p*n`
rounding up past an integer.
@Abhist17
Abhist17 requested a review from a team September 27, 2026 22:48
@stdlib-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Sep 27, 2026
@stdlib-bot

stdlib-bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/discrete-uniform/quantile $\\color{red}321/325$
$\\color{green}+0.02\\%$
$\\color{red}33/35$
$\\color{green}+0.00\\%$
$\\color{green}4/4$
$\\color{green}+0.00\\%$
$\\color{red}321/325$
$\\color{green}+0.02\\%$

The above coverage report was generated for the changes in this PR.

@kgryte
kgryte requested a review from Planeshifter September 28, 2026 01:26
@kgryte

kgryte commented Sep 28, 2026

Copy link
Copy Markdown
Member

@Planeshifter Mind taking a look at this PR?

This branch has not been deployed

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

Labels

Needs Review A pull request which needs code review. Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants