Skip to content

fix: use log1p above the mode in stats/base/dists/triangular/logcdf - #15777

Open
Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/triangular-logcdf-upper
Open

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/triangular-logcdf-upper

Conversation

@Abhist17

@Abhist17 Abhist17 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • uses log1p( -(b-x)^2 / ((b-a)(b-c)) ) instead of ln( 1 - (b-x)^2 / ((b-a)(b-c)) ) for c < x < b in stats/base/dists/triangular/logcdf (JS, factory and C).

Close to b, the CDF is close to 1 and the log-CDF is close to 0. Forming 1 - (b-x)^2/... before taking ln loses those digits, and once the fraction is below 2^-53 the result is 0:

var logcdf = require( '@stdlib/stats/base/dists/triangular/logcdf' );

logcdf( 1.0-1.0e-9, 0.0, 1.0, 0.5 );
// develop: 0.0
// this PR: -1.9999998868722757e-18 (exact for that `x`)

Fixtures and tests:

  • runner.jl now evaluates the textbook piecewise formula in 2048-bit BigFloat instead of Distributions.logcdf, which has the same log(1 - ...). Distributions is dropped from REQUIRE, and the three fixtures are regenerated with the same input ranges.
  • There's a new near_b.json with b - x log-spaced from 1e-8 to 0.1 times b - c.
  • All four fixtures pass at 4 ULP or less in JS and native. The tolerances were 6, 0 and 11 against the old fixtures, and are now 4, 4 and 4, plus 2 for near_b.
  • On develop, 1,007 of the 4,012 assertions in test.logcdf.js fail. With this change they all pass.

Related Issues

Does this pull request have any related issues?

None.

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. 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. I found the bug by scanning stats/base/dists for ln( 1.0 - ... ).


@stdlib-js/reviewers

`ln( 1 - (b-x)^2/((b-a)(b-c)) )` loses the digits of a result close to
`0` and returns `0` once the fraction is below `2^-53`. The fixtures are
now generated from the textbook formula in extended precision.
@Abhist17
Abhist17 requested a review from a team October 1, 2026 23:47
@stdlib-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Oct 1, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/triangular/logcdf $\\color{green}358/358$
$\\color{green}+100.00\\%$
$\\color{green}36/36$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}358/358$
$\\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