Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[Bug]: rayleigh/mgf returns wrong values due to misplaced parenthesis

未关闭 适合新手
#15,456 3 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
领域
backend, tooling

调研方向

该错误位于 lib/main.js、lib/factory.js 和 src/main.c 中的 Rayleigh MGF 公式中。首先验证数学公式和当前实现。更新这三个文件以移动括号,然后更新 README.md、docs/repl.txt、docs/types/index.d.ts、lib/index.js 和 lib/native.js 中的文档。运行 test.mgf.js 和 test.factory.js 中的现有测试以确保它们仍然通过,并考虑添加数值夹具以进行更强大的验证。

由索引模型根据 Issue 内容生成。

描述

Bug
Description

I encountered incorrect output from @stdlib/stats/base/dists/rayleigh/mgf while cross-checking the MGF implementations against their closed forms. The Rayleigh implementation multiplies the whole (1 + ...) expression by the √(π/2)·(1 + erf) factor, when that factor should only multiply the σt term.

The current code in lib/main.js (and identically in lib/factory.js and src/main.c) reads as follows:

sigmat = t * sigma;
out = 1.0 + (sigmat * exp( sigmat*sigmat / 2.0 ));
out *= SQRT_HALF_PI * ( erf( sigmat / SQRT2 ) + 1.0 );

The closed form for the Rayleigh MGF is M(t) = 1 + σt·√(π/2)·e^((σt)²/2)·(1 + erf(σt/√2)), so the trailing factor ends up being applied to the leading 1 as well. The quickest way to see the problem is that mgf(0, σ) returns √(π/2) ≈ 1.2533 instead of 1, and every moment-generating function must equal 1 at t = 0. The error also flips signs in places: mgf(-1, 4) currently returns -0.947, whereas the correct value is +0.053.
The JSDoc and README examples (mgf(1.0, 3.0) → ~678.508 and similar) match the buggy formula, so the incorrect values are baked into the documentation as well. I checked the neighboring mgf implementations (geometric, negative-binomial, Poisson, gamma, normal, and others), and this problem looks isolated to Rayleigh.

Proposed Approach

The fix is a small regrouping of the existing expression so that the √(π/2)·(1 + erf) factor multiplies only the σt term, applied in all three places that carry the formula:

sigmat = t * sigma;
out = 1.0 + ( sigmat * SQRT_HALF_PI * exp( sigmat*sigmat / 2.0 ) * ( erf( sigmat / SQRT2 ) + 1.0 ) );

Concretely, I propose updating lib/main.js, lib/factory.js, and src/main.c, along with the corresponding doc examples in README.md, docs/repl.txt, docs/types/index.d.ts, lib/index.js, and lib/native.js. The corrected example outputs, verified against stdlib's own erf implementation, are mgf(1.0, 3.0) → ~677.005, mgf(1.0, 2.0) → ~37.2, mgf(-1.0, 4.0) → ~0.053, mgf.factory(0.5)(1.0) → ~1.982, and mgf.factory(0.5)(0.5) → ~1.387. I have these changes ready locally and fully verified .

I’m happy to prepare a PR for this if the team is aligned , however please let me know if there’s another direction you'd prefer to take...! :)

Related Issues

Related to my earlier discussion https://github.com/stdlib-js/stdlib/discussions/15298

Questions

I have no blocking questions. I would, however, appreciate guidance on whether the pull request should also add numeric fixtures to test.mgf.js and test.factory.js, since those tests currently only assert non-NaN output, which is how this slipped through unnoticed.

Demo

N/A

Reproduction
  • var mgf = require( '@stdlib/stats/base/dists/rayleigh/mgf' );
  • mgf( 0.0, 1.0 ); // returns ~1.253 instead of 1
  • mgf( 1.0, 3.0 ); // returns ~678.508 instead of ~677.005
  • mgf( -1.0, 4.0 ); // returns ~-0.947 instead of ~0.053
Expected Results
1
677.0046886610472
0.05339046834575567
Actual Results
1.2533141373155003
678.5079332431045
-0.9465301436239729
Version

0.4.1 (develop)

Environments

Node.js

Browser Version

N/A

Node.js / npm Version

Node v26.5.0 (the numbers were verified against stdlib's own erf, so the discrepancy lies in the formula rather than the environment).

Platform

macOS (darwin). This is a formula-level bug and is platform-independent.

Checklist
  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.
主要语言
JavaScript
星标
6k
派生
1.3k
平均合并
1 天 3 小时
30 天内合并 PR
585

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

stdlib-js/stdlib 的其他 Issue

查看 stdlib-js/stdlib 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。