Skip to content

fix: return correct MGF value when t is zero in stats/base/dists/cosine/mgf - #16425

Open
harsh-thakkar7 wants to merge 1 commit into
stdlib-js:developfrom
harsh-thakkar7:fix/cosine-mgf-zero-t
Open

harsh-thakkar7 wants to merge 1 commit into
stdlib-js:developfrom
harsh-thakkar7:fix/cosine-mgf-zero-t

Conversation

@harsh-thakkar7

Copy link
Copy Markdown
Contributor

Description

This pull request:

  • fixes stats/base/dists/cosine/mgf to return 1 when t is zero; previously it returned NaN for every parameter combination, because the evaluation divides by st = s*t, producing 0/0 at t = 0;

  • the limit as t approaches zero is 1 (sinh(st) ~ st cancels the st factor, and M(0) = E[e^{0*X}] = 1 by definition), and the same change is applied to the C implementation in src/main.c.

Related Issues

This pull request has the following related issues:

  • none; found while checking distribution MGFs against their limiting values

Questions

No.

Other

Verification:

  • mgf( 0.0, 1.0, 2.0 ) returned NaN before and returns 1 after; mgf( 1e-12, 1.0, 2.0 ) already returned 1.000000000001, confirming continuity;
  • mgf.factory( 2.0, 5.0 )( 0.0 ) likewise returns 1;
  • all previously documented example values are unchanged (e.g., mgf( 0.5, 0.0, 1.0 ) is still ~1.016, mgf( 2.0, 0.0, 3.0 ) is still ~7.234);
  • NaN and s <= 0 handling is unchanged, since the new check runs after input validation.

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 was executed locally to verify the values above.


@stdlib-js/reviewers

@harsh-thakkar7
harsh-thakkar7 requested a review from a team November 1, 2026 05:34
@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
@stdlib-bot

stdlib-bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/cosine/mgf $\\color{red}296/300$
$\\color{green}+0.00\\%$
$\\color{red}21/23$
$\\color{green}+0.00\\%$
$\\color{green}4/4$
$\\color{green}+0.00\\%$
$\\color{red}296/300$
$\\color{green}+0.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