Skip to content

fix(stats/base/dists/f/cdf): swap betainc branches to avoid cancellation for small x - #16660

Open
wanjinhao1 wants to merge 2 commits into
stdlib-js:developfrom
wanjinhao1:agent/fix-16655-f-cdf-small-x
Open

wanjinhao1 wants to merge 2 commits into
stdlib-js:developfrom
wanjinhao1:agent/fix-16655-f-cdf-small-x

Conversation

@wanjinhao1

@wanjinhao1 wanjinhao1 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #16655.

Description

What is the purpose of this pull request?

This pull request:

  • Swaps the two betainc branches in the F distribution CDF (lib/main.js, lib/factory.js, src/main.c) so that when d1*x <= d2 the regularized incomplete beta is evaluated at the small argument (d1*x)/(d2+d1*x) instead of its nearly-one complement d2/(d2+d1*x), which lost most significant digits for small x and returned exactly 0 once d1*x/d2 fell below ~1e-16. This aligns the branch selection with Boost's fisher_f CDF.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

Reference Packages

Did the content of this pull request follow one or more existing packages? Answering is optional.

This pull request followed these packages:

  • Boost Math fisher_f CDF branch selection

What carried over from the reference(s)?

  • The numerically stable choice of evaluating the incomplete beta at the small argument.

Verification

  • cdf(1e-20, 2, 3) → 1e-20; cdf(1e-12, 2, 3) → 9.999999999991665e-13; cdf(1e-12, 10, 30) → 4.785185185158598e-59 (matches the mpmath/SciPy reference values in [BUG]: stats/base/dists/f/cdf loses accuracy for small x and returns 0 for very small x #16655).
  • All 3000 Julia fixture points (large_d1, large_d2, both_large) pass with worst relative error < 4e-15 for both main and factory exports (local check; the packaged tape runner was unavailable in this environment).
  • gcc -fsyntax-only passes on src/main.c.

AI assistance disclosure: this change was prepared with AI assistance under the direction of the account owner (wanjinhao1), who reviewed and verified the results.

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 codecode, but the proposed changes were fully authored manually by myself.".

This PR was implemented with AI assistance under the direction of the account owner (wanjinhao1), who reviewed the diff and verified the numerical results against the issue's reference values and the package fixtures.


…ion for small x

For d1*x <= d2, evaluate the regularized incomplete beta at the small
argument (d1*x)/(d2+d1*x) instead of its nearly-one complement, which lost
most significant digits for small x and returned exactly 0 once d1*x/d2
fell below ~1e-16. Aligns the branch selection with Boost's fisher_f CDF.

Fixes stdlib-js#16655
@wanjinhao1
wanjinhao1 requested a review from a team November 16, 2026 18:22
@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 16, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Hello! Thank you for your contribution to stdlib.

We noticed that the contributing guidelines acknowledgment is missing from your pull request. Here's what you need to do:

  1. Please read our contributing guidelines.

  2. Update your pull request description to include this checked box:

    - [x] Read, understood, and followed the [contributing guidelines](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md)

This acknowledgment confirms that you've read the guidelines, which include:

  • The developer's certificate of origin
  • Your agreement to license your contributions under the project's terms

We can't review or accept contributions without this acknowledgment.

Thank you for your understanding and cooperation. We look forward to reviewing your contribution!

@stdlib-bot

stdlib-bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/f/cdf $\\color{green}337/337$
$\\color{green}+0.00\\%$
$\\color{green}33/33$
$\\color{green}+0.00\\%$
$\\color{green}4/4$
$\\color{green}+0.00\\%$
$\\color{green}337/337$
$\\color{green}+0.00\\%$

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

@kgryte

kgryte commented Nov 16, 2026

Copy link
Copy Markdown
Member

@wanjinhao1 Mind providing a link to the line in Boost source code which aligns with this change? Cheers!

@kgryte kgryte added the Numerical Accuracy Issue or pull request concerns numerical accuracy. label Nov 16, 2026
@kgryte

kgryte commented Nov 16, 2026

Copy link
Copy Markdown
Member

@anandkaranubc Once the Boost source link is provided, would you mind taking a look at this? I am wondering if we also need to adjust test tolerances.

@kgryte
kgryte requested a review from anandkaranubc November 16, 2026 22:31
@wanjinhao1

Copy link
Copy Markdown
Contributor Author

Sure! The change aligns with Boost's fisher_f CDF here:

https://github.com/boostorg/math/blob/develop/include/boost/math/distributions/fisher_f.hpp#L376-L384

return v1x > df2
   ? BOOST_MATH_NAMESPACE::ibetac(df2 / 2, df1 / 2, df2 / (df2 + v1x), Policy())
   : BOOST_MATH_NAMESPACE::ibeta(df1 / 2, df2 / 2, v1x / (df2 + v1x), Policy());

i.e., Boost uses the direct (non-complemented) incomplete beta when d1*x <= d2 and the complemented form with swapped shape parameters otherwise. The previous stdlib branch condition (d1*x > d2 selecting the direct form) was inverted relative to this, which is what this PR fixes (the "Better to switch things around so we're passing 1-z instead" comment just above the branch explains the rationale for the split).

@wanjinhao1

Copy link
Copy Markdown
Contributor Author

The contributing-guidelines acknowledgment checkbox is checked in the PR description, but the Check Contributing Guidelines Acceptance check ran before the description was updated and still reports failure. Could a maintainer please re-run this check? (I don't have permission to rerun it.)

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. Numerical Accuracy Issue or pull request concerns numerical accuracy. Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: stats/base/dists/f/cdf loses accuracy for small x and returns 0 for very small x

4 participants