Skip to content

Conversation

@ColtAllen
Copy link
Collaborator

@ColtAllen ColtAllen commented Oct 17, 2025

Description

This began as a bug fix for prior/posterior predictive checks in ShiftedBetaGeoModel, but I decided to expand this PR & add another predictive method while waiting for unrelated CI checks to be resolved.

Related Issue

Checklist


📚 Documentation preview 📚: https://pymc-marketing--2010.org.readthedocs.build/en/2010/

@ColtAllen ColtAllen linked an issue Oct 18, 2025 that may be closed by this pull request
@ColtAllen ColtAllen changed the title Fix RV bug in ShiftedBetaGeometric Distribution Block Add expected_residual_lifetime predictions to ShiftedBetaGeoModel Oct 18, 2025
@ColtAllen ColtAllen added the enhancement New feature or request label Oct 18, 2025
@github-actions github-actions bot added the good second issue Bit more involved but still doable for newcomers label Oct 18, 2025
@juanitorduz juanitorduz mentioned this pull request Oct 19, 2025
samples = rng.geometric(p, size=size)

return samples
# prevent log(0) by clipping small p samples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't do this. If the only reason is the test, relax it or seed it. I can easily make a test that still fails with this hack.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PPCs occasionally fail during notebook testing, and it's no different when reformulating the RV with the np.ceil(np.log(U) / np.log(1 - p))) trick.

That said, most users will be doing MAP fits with this model and evaluating with the post-inference predictive methods. Ardent Bayesians may be annoyed by the lack of PPCs, but this bug isn't something that would break a typical workflow.

Copy link
Contributor

@ricardoV94 ricardoV94 Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no longer opposed to this truncation, because numpy does an even more aggressive truncation so that it can return integer (as those can't represent infinity): https://github.com/numpy/numpy/blob/4992eaf12a8c7a3eeb0f6d13f832a05f21e3834c/numpy/random/src/distributions/distributions.c#L996-L997

I would open an issue to consider returning floats, in which case both infinity can be returned or larger values if one writes the geometric as geom = np.ceil(np.log(u) / np.log1p(-p)) without casting to integer

Copy link
Contributor

@ricardoV94 ricardoV94 Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact you can truncate as high as 1e-35 without changing the behavior, as that will still be higher than the cutoff point for any value drawn by numpy:

numpy_cutoff = 9.223372036854776e+18
largest_uniform = np.nextafter(1, 0)
beta_clip = 1e-35
assert np.log(largest_uniform) / np.log1p(-beta_clip)  >= numpy_cutoff

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just leave a comment in the code about this discussion?

@ColtAllen
Copy link
Collaborator Author

Have we hit a token limit in the PR labeler?

  ERROR: failed to build: failed to solve: ubuntu:latest: failed to resolve source metadata for docker.io/library/ubuntu:latest: failed to authorize: failed to fetch oauth token: unexpected status from POST request to https://auth.docker.io/token: 503 Service Unavailable: <html><body><h1>503 Service Unavailable</h1>

@ColtAllen ColtAllen removed good second issue Bit more involved but still doable for newcomers MMM labels Oct 20, 2025
Copy link
Contributor

@cetagostini cetagostini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me :)

@ColtAllen ColtAllen merged commit 178f07a into pymc-labs:main Oct 20, 2025
38 checks passed
TeemuSailynoja pushed a commit that referenced this pull request Oct 20, 2025
…#2010)

* fix sBG RV bug

* assert var>=0

* fixed mmm transformers numpy import

* ruff format

* init commit for add expected_residual_lifetime

* fix dim names in unit tests

* fix discount rate mypy error

* test_fixtures

* WIP sbg erl fixtures

* WIP fixtures

* fixed erl expression and nb tests pass

* tighten rtol and clean up nb

* add TODO  on float vs int in rng_fn

* update doc references for sbg

* update doc references from latest to stable

---------

Co-authored-by: Juan Orduz <juanitorduz@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working CLV docs Improvements or additions to documentation enhancement New feature or request tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug with TestShiftedBetaGeometric

5 participants