Skip to content

FIX: simulate_glm(sample=True) for gamma returns exp(mu) instead of gamma samples - #401

Merged
jasmainak merged 1 commit into
glm-tools:masterfrom
rpinho:fix-gamma-simulate
Nov 8, 2026
Merged

jasmainak merged 1 commit into
glm-tools:masterfrom
rpinho:fix-gamma-simulate

Conversation

@rpinho

@rpinho rpinho commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

With distr='gamma' and sample=True, simulate_glm returns np.exp(mu): the same value for every seed, with no noise, and the wrong mean. For example, a mean of 1.103 gives 3.014 for every observation.

This PR draws random_state.gamma(shape=1.0, scale=mu), i.e. shape-1 gamma observations with mean mu, matching the likelihood used for fitting. With 20,000 draws at that mean, the sample mean is 1.104 and the variance 1.19 (expected about 1.103 and 1.22). The same seed reproduces the same draws. sample=False is unchanged.

Test: test_simulate_gamma_samples in tests/test_pyglmnet.py fails on master and passes here. The rest of the suite is unchanged (same pre-existing failures on master as noted in the Bernoulli PR).

Open PR #390's GammaSoftplus.simulate also returns np.exp(mu), so the same change would apply there.

It returned exp(mu) deterministically. Draw shape-1 gamma samples with
mean mu, matching the likelihood used for fitting.
@jasmainak
jasmainak merged commit 72a83f6 into glm-tools:master Nov 8, 2026
@jasmainak

Copy link
Copy Markdown
Member

Thanks @rpinho . I realized that the docstring of simulate_glm is not super clear about the return value's dependence on sample ... would be good to clarify at some point.

@rpinho

rpinho commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. Agreed, the docstring doesn't say what sample changes in the return value. I can add a few lines for that.

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.

2 participants