Skip to content

fix: handle negative t in stats/base/dists/weibull/mgf - #16212

Open
0PrashantYadav0 wants to merge 1 commit into
stdlib-js:developfrom
0PrashantYadav0:fix/weibull-mgf-negative-t
Open

0PrashantYadav0 wants to merge 1 commit into
stdlib-js:developfrom
0PrashantYadav0:fix/weibull-mgf-negative-t

Conversation

@0PrashantYadav0

Copy link
Copy Markdown
Member

Description

What is the purpose of this pull request?

This pull request:

  • fixes the series stopping criterion in stats/base/dists/weibull/mgf (main.js, factory.js, and src/main.c). The loop ran while ( summand / sum > EPS ). When t < 0, the terms alternate in sign, so the ratio is negative whenever a summand and the running sum have opposite signs, and the loop stopped after one or two terms. The criterion now uses abs( summand / sum ). For t >= 0 every term is positive, so results there do not change.
  • updates the mgf( -1.0, 4.0, 4.0 ) example from ~0.019 to ~0.045, and the weibull.mgf( -0.5 ) example in weibull/ctor from ~0.075 to ~0.109.
  • adds tests for negative t to test.mgf.js, test.factory.js, and test.native.js.

Examples:

call before after reference
mgf( -0.5, 1.0, 1.0 ) 0.5 0.6666666666666666 1/(1-tλ) = 2/3 (Weibull with k = 1 is exponential)
mgf( -0.3, 2.0, 4.0 ) 0.2737 0.3961554052634957 0.39615540526349546
mgf( -1.0, 4.0, 4.0 ) 0.0189 0.04475961134295209 0.04475961134294959

The reference values come from summing the same series in 1024-bit BigFloat arithmetic in Julia, and they agree with numerical integration of E[exp(tX)].

Related Issues

Does this pull request have any related issues?

No.

Questions

Any questions for reviewers of this pull request?

This fixes the stopping test, but the power series still has limits for t < 0 that this PR does not address:

  • for k < 1 the series diverges for any t != 0, so the function returns ±Infinity, although the MGF is finite for every t <= 0.
  • for k = 1, gamma( 1+n ) overflows before c underflows once |tλ| approaches 1 (e.g., mgf( -0.9, 1.0, 1.0 )), and at tλ <= -1 the series does not converge at all.
  • for large |tλ|, the alternating terms cancel badly. The error is ~4 ULP at |tλ| = 1.2, ~7700 ULP at |tλ| = 5, and the result is meaningless at mgf( -20.0, 3.0, 1.0 ).

For t < 0, evaluating the integral ∫₀^∞ exp( tλ u^(1/k) ) exp( -u ) du numerically would avoid all three. stdlib has no quadrature routine in math/base, so I kept this PR to the stopping test. Would you like a follow-up for this, or a note in the README in the meantime?

Other

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

The new tests use |tλ| <= 2, where the series agrees with the reference to within 4 ULP. They fail on develop and pass with this change, for both the JavaScript and native implementations.

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

I found this bug manually while reviewing other PRs. The code changes were generated with Claude Code, and I reviewed them and verified the results.


@stdlib-js/reviewers

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown_pkg_readmes
    status: passed
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: passed
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: missing_dependencies
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: passed
  - task: lint_license_headers
    status: passed
---
@0PrashantYadav0
0PrashantYadav0 requested a review from a team October 17, 2026 16:01
@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 17, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/weibull/ctor $\\color{red}486/487$
$\\color{green}+99.79\\%$
$\\color{green}33/33$
$\\color{green}+100.00\\%$
$\\color{red}18/19$
$\\color{green}+94.74\\%$
$\\color{red}486/487$
$\\color{green}+99.79\\%$
stats/base/dists/weibull/mgf $\\color{green}346/346$
$\\color{green}+100.00\\%$
$\\color{green}29/29$
$\\color{green}+100.00\\%$
$\\color{green}4/4$
$\\color{green}+100.00\\%$
$\\color{green}346/346$
$\\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