Skip to content

fix: avoid cancellation for small sigma in stats/base/dists/lognormal/kurtosis - #15776

Open
Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/lognormal-kurtosis-small-sigma
Open

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/lognormal-kurtosis-small-sigma

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:

  • rewrites stats/base/dists/lognormal/kurtosis so it doesn't cancel for small sigma (JS and C).

The excess kurtosis was computed as e^{4s} + 2e^{3s} + 3e^{2s} - 6 with s = sigma^2. The sum is about 6 + 16s, so subtracting 6 cancels. For s < 2^-55 the result is 0:

var kurtosis = require( '@stdlib/stats/base/dists/lognormal/kurtosis' );

kurtosis( 0.0, 1.0e-9 );
// develop: 0.0
// this PR: 1.6e-17

kurtosis( 0.0, 1.0e-5 );
// develop: 1.6000001323845936e-9
// this PR: 1.6000000002300001e-9

With u = e^s - 1, evaluated by expm1, the same expression expands exactly to u*(16 + u*(15 + u*(6 + u))). All its terms are positive, so it can't cancel. The derivation is in a new ## Method section in the JSDoc and the C doc comment. kurtosis( 0.0, 1.0 ) still returns 110.93639217631151.

Fixtures and tests:

  • runner.jl now evaluates the textbook formula in 2048-bit BigFloat instead of Distributions.kurtosis. Distributions is dropped from REQUIRE, and data.json is regenerated with the same input ranges.
  • There's a new small_sigma.json with sigma log-spaced over [1e-150, 0.1]. It passes at 1 ULP in JS and native.
  • On data.json, the max error is 61 ULP (develop: 62). It comes at sigma ≈ 4.3, where the rounding of sigma^2 is multiplied by 4*sigma^2 ≈ 73 in e^{4s}. That's the conditioning of the function.
  • The tests used an EPS-relative delta. They now use isAlmostSameValue like the other migrated packages.
  • On develop, 1,001 of the 1,110 assertions in test.js fail. With this change they all pass.

Related Issues

Does this pull request have any related issues?

None. Same family as #15774 (lognormal/variance) and #15775 (lognormal/skewness).

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 exp(...) - 1-style cancellation.


@stdlib-js/reviewers

…mal/kurtosis`

`e^{4s} + 2e^{3s} + 3e^{2s} - 6` cancels for small `s = sigma^2` and is
`0` for `s < 2^-55`. With `u = expm1(s)` it is exactly
`u*(16 + u*(15 + u*(6 + u)))`, whose terms are all positive. The
fixtures are now generated from the textbook formula in extended
precision and the tests use ULP-based assertions.
@Abhist17
Abhist17 requested a review from a team October 1, 2026 23:46
@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/lognormal/kurtosis $\\color{green}194/194$
$\\color{green}+100.00\\%$
$\\color{green}9/9$
$\\color{green}+100.00\\%$
$\\color{green}2/2$
$\\color{green}+100.00\\%$
$\\color{green}194/194$
$\\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