Skip to content

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

Open
anshurajbisoyi98-ctrl wants to merge 1 commit into
stdlib-js:developfrom
anshurajbisoyi98-ctrl:fix-discrete-uniform-quantile
Open

anshurajbisoyi98-ctrl wants to merge 1 commit into
stdlib-js:developfrom
anshurajbisoyi98-ctrl:fix-discrete-uniform-quantile

Conversation

@anshurajbisoyi98-ctrl

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • fixes stats/base/dists/discrete-uniform/quantile so that it returns the smallest x with cdf( x ) >= p.
var quantile = require( '@stdlib/stats/base/dists/discrete-uniform/quantile' );

quantile( 1.0, 1, 4 );
// returns 5, expected 4

quantile( 0.25, 1, 4 );
// returns 2, expected 1 (cdf( 1, 1, 4 ) = 0.25)

The current formula a + floor( p*n ) returns one past the right value whenever p*n is an integer, which includes every step of the CDF and p = 1 (where it returns b+1, outside the support). I switched it to a - 1 + ceil( p*n ), with p = 0 returning a, which is what Julia's Distributions.jl (used for the test fixtures) does. SciPy's randint.ppf agrees except at p = 0. The same change is in main.js, factory.js and src/main.c, and manifest.json swaps floor for ceil.

The fixtures use random p and don't hit these cases, so I added tests to test.quantile.js, test.factory.js and test.native.js. The factory( 0, 4 )( 0.8 ) example in the README and docs said 4, but cdf( 3, 0, 4 ) is already 0.8, so I changed it to 3.

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.

No.

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.

I used Claude Code to run consistency checks across the discrete distributions in stats/base/dists (comparing quantile against cdf), which turned this up, and to make the change and tests. I checked the formula against Julia and SciPy, built the add-on locally, and ran the JS and native tests.


@stdlib-js/reviewers

…sts/discrete-uniform/quantile`

Signed-off-by: anshurajbisoyi98-ctrl <anshurajbisoyi98@gmail.com>
@anshurajbisoyi98-ctrl
anshurajbisoyi98-ctrl requested a review from a team November 29, 2026 17: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 Nov 29, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/discrete-uniform/quantile $\\color{green}305/305$
$\\color{green}+100.00\\%$
$\\color{green}31/31$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}305/305$
$\\color{green}+100.00\\%$

The above coverage report was generated for the changes in 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.

2 participants