Skip to content

fix: evaluate both tails without cancellation in stats/base/dists/cauchy/logcdf - #15966

Open
Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/cauchy-logcdf-left-tail
Open

Abhist17 wants to merge 2 commits into
stdlib-js:developfrom
Abhist17:fix/cauchy-logcdf-left-tail

Conversation

@Abhist17

@Abhist17 Abhist17 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • evaluates both tails of stats/base/dists/cauchy/logcdf without cancellation (JS, factory and C).

ln( 0.5 + atan(z)/pi ) cancels left of the median: the sum loses everything once atan(z)/pi is within an ULP of -0.5, so the function returns -Infinity from about x = x0 - 1e16*gamma on, and is off by hundreds of ULP well before that.

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

logcdf( -1.0e16, 0.0, 1.0 );
// develop: -Infinity
// this PR: -37.98609137375413

logcdf( -1.0e30, 0.0, 1.0 );
// develop: -Infinity
// this PR: -70.22228267567077
  • Below z = -1: 0.5 + atan(z)/pi equals atan2( gamma, x0-x )/pi exactly, with no subtraction.
  • Above z = 1: the result is ln of a number approaching 1, so it uses log1p( -atan2( gamma, x-x0 )/pi ).
  • In between, the original expression is kept. Applied there too, the identities were less accurate than the sum (atan2 is close to pi/2 and picks up the rounding of 1/pi): on large_gamma the first version was further from exact than develop on 325 points; gated, it's 2. x = x0 still gives ln(0.5); ±Infinity are unchanged.

The fixtures had to change. They were generated with Distributions.jl's logcdf, which evaluates the same formula in Float64. Against mpmath, on the 1,046 points where this PR and the fixtures disagree, the fixtures (and develop) are up to 782 ULP off, and this PR is within 2. develop only passed because it reproduces the reference's own error. gen now evaluates the textbook formula in 2048-bit BigFloat, as in #15772 and #15774. All three existing fixtures were regenerated with that, plus a new left_tail.json with x from 1e6 to 1e296 scale units below x0.

With the new fixtures, every point is within 2 ULP (the existing tolerances of 6–12 ULP are unchanged; the new left-tail blocks use 2). test.logcdf.js / test.factory.js / test.native.js pass 4,014 / 4,018 / 4,014. On develop, 1,211 assertions in test.logcdf.js fail.

Related Issues

Does this pull request have any related issues?

None.

Questions

Any questions for reviewers of this pull request?

…uchy/logcdf`

`ln( 0.5 + atan(z)/pi )` cancels left of the median and returns -Infinity
from about `x = x0 - 1e16*gamma`; `logcdf( -1e30, 0, 1 )` is about -70.22.
Use the equivalent `atan2( gamma, x0-x )/pi` there, and
`log1p( -atan2( gamma, x-x0 )/pi )` right of the median, where the result
is `ln` of a number close to 1.

The fixtures were generated with Distributions.jl's Float64 logcdf, which
uses the same formula and is up to 782 ULP off; they are now evaluated in
2048-bit BigFloat, plus a new far-left-tail fixture. All four fixtures are
within 2 ULP; develop fails 1211 of the assertions.
@Abhist17
Abhist17 requested a review from a team October 7, 2026 21:30
@stdlib-bot stdlib-bot added the Statistics Issue or pull request related to statistical functionality. label Oct 7, 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 added Needs Review A pull request which needs code review. and removed Needs Review A pull request which needs code review. labels Oct 7, 2026
@stdlib-bot

stdlib-bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/cauchy/logcdf $\\color{green}307/307$
$\\color{green}+100.00\\%$
$\\color{green}27/27$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}307/307$
$\\color{green}+100.00\\%$

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

Near the median the original `ln( 0.5 + atan(z)/pi )` doesn't cancel and is
the more accurate form; the tail identities only pay off once the result is
far from `ln(0.5)`. Gate them at `z < -1` and `z > 1`: on `large_gamma`
this PR was further from exact than develop on 325 points, now 2.
@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!

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