镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

gamma samples were off by a factor of beta^2 - #8750

Open
rootkiller6788 wants to merge 2 commits into
tensorflow:masterfrom
rootkiller6788:fix-gamma-beta-scaling
Open

rootkiller6788 wants to merge 2 commits into
tensorflow:masterfrom
rootkiller6788:fix-gamma-beta-scaling

Conversation

@rootkiller6788

Copy link
Copy Markdown

tf.randomGamma(x, alpha, beta) was sampling with a mean of alpha * beta instead of alpha / beta.

The class stores the scale, i.e. 1/beta, in this.beta, but nextValue() finished with v = (1 / this.beta) * this.d * v - multiplying by the rate a second time. Every sample came out beta^2 too large. Since beta defaults to 1 this is invisible unless you pass a real rate, and the existing tests only assert the values are somewhere in [0, 30], which any scaling passes.

I noticed it while checking the sampler's mean against the distribution. Sample mean over 400k draws:

alpha=2,  beta=2   -> 3.9934  (should be 1.0)
alpha=2,  beta=0.5 -> 1.0007  (should be 4.0)
alpha=0.5,beta=3   -> 1.4971  (should be 0.1667)
alpha=2,  beta=1   -> 2.0035  (unchanged, default path)

So the fix is the single line, and it leaves the default beta=1 behaviour alone.

I also added the mean/std checks to rand_test.ts and random_gamma_test.ts, because a range check alone can't tell a correct scale factor from a wrong one. With the fix the seeded check gives mean 1.0020 / std 0.7114; against the old code the same check fails with 4.0079 vs 1.

I couldn't run the bazel test suite here (no bazel on this machine), so I verified by compiling the changed rand_util.ts on its own against a real seedrandom and comparing both versions, plus the table above.

RandGamma stores the scale (1/beta) in this.beta, but nextValue multiplied
by 1/this.beta, i.e. by the rate again. So tf.randomGamma(x, alpha, beta)
sampled a mean of alpha * beta instead of alpha / beta.

With the default beta = 1 this cancels out, which is why it went unnoticed.
The existing gamma tests only check that the values land in a range, which
any scaling passes. Check the sample mean and std against alpha and beta
instead, at both the RandGamma and the tf.randomGamma level.
@rootkiller6788
rootkiller6788 marked this pull request as ready for review October 16, 2026 03:52

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant