Skip to content

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

Open
SajalDevX wants to merge 2 commits into
stdlib-js:developfrom
SajalDevX:fix/stats-geometric-mgf
Open

SajalDevX wants to merge 2 commits into
stdlib-js:developfrom
SajalDevX:fix/stats-geometric-mgf

Conversation

@SajalDevX

Copy link
Copy Markdown

Resolves #15780.

Description

What is the purpose of this pull request?

This pull request:

  • fixes stats/base/dists/geometric/mgf (JS and C) so that it returns the MGF of the number of failures before the first success, p / (1 - (1-p) e^t). That is the convention used by the other geometric packages, such as pmf, mean, and variance. Before this fix it returned p e^t / (1 - (1-p) e^t), the MGF of the number of trials, so every value was off by a factor of e^t. For example, mgf( 0.5, 1.0 ) returned e^0.5 instead of 1, and M'(0) was 1/p instead of mean( p ) = (1-p)/p.
  • regenerates the expected values in test/fixtures/julia/{small,large}_p.json, keeping the same x and p inputs, and changes the p = 1 test to expect 1.
  • updates the documented equation and the example outputs in geometric/mgf and geometric/ctor.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

  • #{{TODO: add related issue number}}

Questions

Any questions for reviewers of this pull request?

The original fixtures were generated with Distributions.jl 0.23.8 (per REQUIRE). That version used the same trials-based formula, which is why the old tests passed. I didn't have Julia available, so I regenerated expected with 60-digit decimal arithmetic for p / (1 - (1-p) e^t), rounded to the nearest double. Current Distributions.jl computes the same quantity as p / (p - (1-p) expm1(t)). Should I bump REQUIRE to a newer Distributions.jl version, or would you rather regenerate the fixtures with runner.jl?

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

  • Testing: I ran test.js, test.mgf.js, and test.factory.js for geometric/mgf, plus geometric/ctor/test/test.js. With the new fixtures and the old code, 2000 fixture assertions failed in each of test.mgf.js and test.factory.js. After the fix, everything passes (2016/2016 and 2014/2014; ctor 67/67). I couldn't build the native add-on, so test.native.js was skipped. To check the C change, I compiled src/main.c on its own with libm stand-ins for exp, ln, and is_nan. It gives the same results as the JS version: 1.2843610871738946 for (0.2, 0.5) and 1 for (0.5, 1.0).
  • The equation SVG in mgf/docs/img still shows the old formula. I only changed the raw equation text in the README, so the SVG will need to be regenerated with the usual tooling.

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.".

Used Claude to help analyze the code and draft the fix; I reviewed and tested the change myself.


@stdlib-js/reviewers

…/mgf`

The geometric distribution in stdlib models the number of failures
before the first success (support 0, 1, 2, ...), as documented by
`pmf`, `cdf`, `mean`, and `variance`. Its MGF is therefore
`p / (1 - (1-p) e^t)`. The implementation returned
`p e^t / (1 - (1-p) e^t)`, which is the MGF of the number of trials
(support 1, 2, ...), so every value was off by a factor of `e^t`.

For example, `mgf( 0.5, 1.0 )` returned `e^0.5` instead of `1`, and the
derivative at `t = 0` was `1/p` instead of `mean( p ) = (1-p)/p`.

This commit fixes the JavaScript and C implementations, regenerates the
test fixtures, updates the `p = 1` test, and corrects the documented
equation and examples, including those in `geometric/ctor`.
@SajalDevX
SajalDevX requested a review from a team October 2, 2026 01:59
@stdlib-bot stdlib-bot added the Statistics Issue or pull request related to statistical functionality. label Oct 2, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

👋 Hi there! 👋

And thank you for opening your first pull request! We will review it shortly. 🏃 💨

Getting Started

Next Steps

  1. A project maintainer will approve GitHub Actions workflows for your PR.
  2. All CI checks must pass before your submission can be fully reviewed.
  3. You'll need to address any failures in linting or unit tests.

Running Tests Locally

You can use make to run any of the CI commands locally from the root directory of the stdlib repository:

# Run tests for all packages in the math namespace:
make test TESTS_FILTER=".*/@stdlib/math/.*"

# Run benchmarks for a specific package:
make benchmark BENCHMARKS_FILTER=".*/@stdlib/math/base/special/sin/.*"

If you haven't heard back from us within two weeks, please ping us by tagging the "reviewers" team in a comment on this PR.

If you have any further questions while waiting for a response, please join our Zulip community to chat with project maintainers and other community members.

We appreciate your contribution!

Documentation Links

@stdlib-bot stdlib-bot added First-time Contributor A pull request from a contributor who has never previously committed to the project repository. Needs Review A pull request which needs code review. labels Oct 2, 2026
@kgryte

kgryte commented Oct 2, 2026

Copy link
Copy Markdown
Member

I commented on the original issue: #15780 (comment)

We first need to determine whether it is actually a bug and whether we want to keep the current formulation.

@kgryte kgryte added Needs Discussion Needs further discussion. and removed Needs Review A pull request which needs code review. labels Oct 2, 2026
@kgryte
kgryte requested a review from Planeshifter October 2, 2026 03:13
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Oct 2, 2026
@Planeshifter

Copy link
Copy Markdown
Member

We should bump REQUIRE to a newer Distributions.jl version here and regenerate the fixtures from that. Would you like to give that a shot, @SajalDevX, or shall we take this PR over? Thanks for your contribution!

@SajalDevX

Copy link
Copy Markdown
Author

Sure, done! I bumped REQUIRE to Distributions 0.25.131 / julia 1.10.10 / JSON 1.10.0 and regenerated large_p.json and small_p.json with runner.jl.

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

First-time Contributor A pull request from a contributor who has never previously committed to the project repository. Needs Discussion Needs further discussion. 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.

[BUG]: stats/base/dists/geometric/mgf returns the MGF for the number of trials instead of the number of failures

4 participants