Skip to content

fix: return MGF for the number of failures in stats/base/dists/geometric/mgf - #16424

Open
harsh-thakkar7 wants to merge 1 commit into
stdlib-js:developfrom
harsh-thakkar7:fix/geometric-mgf-failures-convention
Open

harsh-thakkar7 wants to merge 1 commit into
stdlib-js:developfrom
harsh-thakkar7:fix/geometric-mgf-failures-convention

Conversation

@harsh-thakkar7

@harsh-thakkar7 harsh-thakkar7 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #15780.

Description

This pull request:

  • fixes stats/base/dists/geometric/mgf to return p / (1 - (1-p)e^t), the MGF for the number of failures before the first success, which is the parameterization used by the rest of the geometric packages (pmf, mean, variance, etc.);

  • previously it returned pe^t / (1 - (1-p)e^t), the MGF for the number of trials, so every value was off by a factor of e^t;

  • applies the same fix to the C implementation in src/main.c, and updates the affected doctests in lib/index.js, lib/native.js, lib/factory.js, the README equation, and the README/C example values;

  • regenerates the expected values in test/fixtures/julia/{small_p,large_p}.json, which had been computed with the old formula (the x and p inputs are unchanged);

  • updates the p = 1 test expectation in test.mgf.js / test.native.js: X is degenerate at 0 when p = 1, so the MGF is 1, not e^t.

Related Issues

This pull request has the following related issues:

Questions

No.

Other

Verification with p = 0.3:

  • mgf(0.1, 0.3) now returns 1.3252033149682356, matching the numerically integrated sum_k e^{0.1k} pmf(k, 0.3) from the issue (1.3252);
  • the numeric derivative (mgf(1e-6, 0.3) - mgf(-1e-6, 0.3)) / 2e-6 is 2.3333, matching mean(0.3) (2.3333); before the fix it was 3.3333;
  • mgf(0.5, 1.0) now returns 1 (X is always 0 when p = 1); before the fix it returned 1.6487;
  • the domain check is unchanged (mgf(0.8, 0.5) still returns NaN).

Checklist

AI Assistance

  • Yes

  • No

  • Code generation

  • Documentation (including examples)

  • Research and understanding

Disclosure

This PR was prepared with Qoder AI assistance under the contributor's direction. All changed code and examples were executed locally to verify the values above.


@stdlib-js/reviewers

@harsh-thakkar7
harsh-thakkar7 requested a review from a team November 1, 2026 05:23
@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 1, 2026
@harsh-thakkar7
harsh-thakkar7 force-pushed the fix/geometric-mgf-failures-convention branch 2 times, most recently from 2222797 to fae8a48 Compare November 1, 2026 05:45
@harsh-thakkar7
harsh-thakkar7 force-pushed the fix/geometric-mgf-failures-convention branch from fae8a48 to 9375595 Compare November 1, 2026 05:52
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/geometric/mgf $\\color{green}288/288$
$\\color{green}+100.00\\%$
$\\color{green}19/19$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}288/288$
$\\color{green}+100.00\\%$

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

@stdlib-bot stdlib-bot added the Potential Duplicate There might be another pull request resolving the same issue. label Nov 2, 2026

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. Potential Duplicate There might be another pull request resolving the same issue. 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/geometric/mgf returns the MGF for the number of trials instead of the number of failures

2 participants