Add n_samples=0 support and HDI_Prob in plot_hdi #2257
Add n_samples=0 support and HDI_Prob in plot_hdi #2257pkaf wants to merge 27 commits intopymc-labs:mainfrom
Conversation
PR SummaryMedium Risk Overview
Written by Cursor Bugbot for commit 47a8854. This will update automatically on new commits. Configure here. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2257 +/- ##
==========================================
- Coverage 93.17% 93.14% -0.03%
==========================================
Files 78 78
Lines 12460 12472 +12
==========================================
+ Hits 11609 11617 +8
- Misses 851 855 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thanks @pkaf . I will take a look into the pre-commit hooks error later 🙏 |
|
Hi @pkaf the pre-commit errors are pymc_marketing/plot.py:481: error: List item 0 has incompatible type "None"; expected "float" [list-item]
pymc_marketing/plot.py:483: error: List item 0 has incompatible type "float | None"; expected "float" [list-item]
pymc_marketing/plot.py:517: error: Incompatible return value type (got "tuple[Any, ndarray[tuple[Any, ...], dtype[Any]] | None]", expected "tuple[Any, ndarray[tuple[Any, ...], dtype[Any]]]") [return-value]
pymc_marketing/mmm/budget_optimizer.py:665: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
pymc_marketing/mmm/additive_effect.py:461: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
Found 3 errors in 1 file (checked 80 source files)You must ensure consistency of the types. You can test locally by running Please let me know if you get stuck so that I can try to fix it. |
I understood the issue regarding type setting, but I think am stuck. Was further confused as the other piece of code in a similar method was passing the test without a fuss. Can you please help me with this? |
| if isinstance(non_grid_names, str): | ||
| non_grid_names = {non_grid_names} | ||
| for ihdi_prob in hdi_prob: | ||
| current_hdi_kwargs = {**hdi_kwargs, **dict(hdi_prob=ihdi_prob)} |
There was a problem hiding this comment.
Dict merge order reversal silently overrides user-provided hdi_prob
Medium Severity
The dict merge order at current_hdi_kwargs = {**hdi_kwargs, **dict(hdi_prob=ihdi_prob)} was reversed compared to the old code ({**dict(hdi_prob=hdi_prob), **hdi_kwargs}). Now ihdi_prob always overrides any hdi_prob the user supplied in hdi_kwargs. When hdi_prob defaults to None, this silently discards a user-provided hdi_kwargs["hdi_prob"], replacing it with None (arviz default). Downstream callers like base.py:plot_curve_hdi, which only expose hdi_kwargs and not hdi_prob directly, lose the ability to control the HDI probability.
There was a problem hiding this comment.
@pkaf could you please look into this and add a test in https://github.com/pymc-labs/pymc-marketing/blob/main/tests/test_plot.py ?
|
Thanks @pkaf (and sorry for the late reply). Would you please add some basic tests to ensure the expected behaviour of the changes in https://github.com/pymc-labs/pymc-marketing/blob/main/tests/test_plot.py 🙏 ? |


feat(plot): Add n_samples=0 support, list hdi_prob in plot_hdi, and include_mean option #2225
Description
Previously passing n_samples=0 to plot_curve() would break the function. Now users are allow to put any positive integer for n_samples, 0 or None. For 0 or None users should be now able to plot just the HDI bands without any individual sample lines.
Implementation notes:
When n_samples=0 or None, plot_curve() skips calling plot_samples() entirely.
Previously plot_hdi() only accepted a single float value for hdi_prob. Not it can accept a list e.g. [0.5, 0.8, 0.95].
Loop from plot_curve is now redundant as plot_hdi can directly handle the loop.
Closes #
Related to feat(plot): Add n_samples=0 support, list hdi_prob in plot_hdi, and include_mean option #2225
Checked that the pre-commit linting/style checks pass. Feel free to comment
pre-commit.ci autofixto auto-fix.Included tests that prove the fix is effective or that the new feature works
Added necessary documentation (docstrings and/or example notebooks) using numpydoc format.
If you are a pro: each commit corresponds to a relevant logical change
📚 Documentation preview 📚: https://pymc-marketing--2257.org.readthedocs.build/en/2257/