Skip to content

Conversation

@juanitorduz
Copy link
Collaborator

@juanitorduz juanitorduz commented Oct 17, 2025

Vibe coded some components.


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

@codecov
Copy link

codecov bot commented Oct 17, 2025

Codecov Report

❌ Patch coverage is 93.75000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.89%. Comparing base (47d500c) to head (621e665).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
pymc_marketing/customer_choice/nested_logit.py 93.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2011      +/-   ##
==========================================
- Coverage   93.89%   93.89%   -0.01%     
==========================================
  Files          67       67              
  Lines        9221     9220       -1     
==========================================
- Hits         8658     8657       -1     
  Misses        563      563              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

# Select rows for this nest's alternatives
nest_alt_indices = nest_indices[level][nest]
if n_alts_in_nest == 1:
# For single alternative nests, explicitly maintain 2D shape
Copy link
Contributor

Choose a reason for hiding this comment

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

Use expand dims instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant instead of the slice indexing thing. You can index the same but expand_dims after the case where it goes 1d

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mmm something like ad6c5e9 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't know how this is related to the new PyTensor though

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

mmm I still do not know either, this is me trying to make broadcasting work again 🙈

Copy link
Contributor

Choose a reason for hiding this comment

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

What was the error?

@NathanielF
Copy link
Contributor

Looks like the notebooks all ran successfully too?

Copy link
Contributor

@NathanielF NathanielF left a comment

Choose a reason for hiding this comment

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

I don't see any major issues with the code changes, but i can take a look at running the models tomorrow and making sure the results look right. Just want to understand what issue it was?

y_nest = U[:, nest_indices[level][nest]]
nest_idx = nest_indices[level][nest]
y_nest = U[:, nest_idx]
n_alts_in_nest = len(nest_idx)
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks fine. Just renaming

I_nest = pm.Deterministic(
f"I_{nest}", pm.math.logsumexp((y_nest - max_y_nest) / lambda_, axis=-1)
)
W_nest = w_nest + pt.expand_dims(I_nest * lambda_, axis=-1)
Copy link
Contributor

Choose a reason for hiding this comment

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

was it this part that was failing?

@ricardoV94
Copy link
Contributor

@juanitorduz what test/nb failed with the nested logit and with what error?

@juanitorduz
Copy link
Collaborator Author

@juanitorduz what test/nb failed with the nested logit and with what error?

This one #2010 (comment) (a test)

@NathanielF
Copy link
Contributor

Just to say i'm trying to re-run now and encountered issue. I installed a fresh environment and it seems there is some incompatibility with tqdm

image
ERROR:2025-10-18 10:42:31,769:jax._src.callback:441: jax.io_callback failed
Traceback (most recent call last):
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/tqdm/notebook.py", line 262, in update
    return super().update(n=n)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/tqdm/std.py", line 1242, in update
    self.refresh(lock_args=self.lock_args)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/tqdm/std.py", line 1347, in refresh
    self.display()
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/tqdm/notebook.py", line 168, in display
    ltext.value = left
    ^^^^^^^^^^^
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/traitlets/traitlets.py", line 716, in __set__
    self.set(obj, value)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/traitlets/traitlets.py", line 706, in set
    obj._notify_trait(self.name, old_value, new_value)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/traitlets/traitlets.py", line 1513, in _notify_trait
    self.notify_change(
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/ipywidgets/widgets/widget.py", line 686, in notify_change
    self.send_state(key=name)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/ipywidgets/widgets/widget.py", line 555, in send_state
    self._send(msg, buffers=buffers)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/ipywidgets/widgets/widget.py", line 818, in _send
    self.comm.send(data=msg, buffers=buffers)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/comm/base_comm.py", line 144, in send
    self.publish_msg(
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/ipykernel/comm/comm.py", line 42, in publish_msg
    parent=self.kernel.get_parent(),
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/ipykernel/kernelbase.py", line 797, in get_parent
    return self._shell_parent.get()
           ^^^^^^^^^^^^^^^^^^^^^^^^
LookupError: <ContextVar name='shell_parent' at 0x105aa62f0>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/jax/_src/callback.py", line 439, in io_callback_impl
    return tree_util.tree_map(np.asarray, callback(*args))
                                          ^^^^^^^^^^^^^^^
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/jax/_src/callback.py", line 70, in __call__
    return tree_util.tree_leaves(self.callback_func(*args, **kwargs))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/numpyro/util.py", line 226, in _update_tqdm
    tqdm_bars[chain].update(increment)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/tqdm/notebook.py", line 267, in update
    self.disp(bar_style='danger')
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/tqdm/notebook.py", line 171, in display
    rtext.value = right
    ^^^^^^^^^^^
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/traitlets/traitlets.py", line 716, in __set__
    self.set(obj, value)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/traitlets/traitlets.py", line 706, in set
    obj._notify_trait(self.name, old_value, new_value)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/traitlets/traitlets.py", line 1513, in _notify_trait
    self.notify_change(
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/ipywidgets/widgets/widget.py", line 686, in notify_change
    self.send_state(key=name)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/ipywidgets/widgets/widget.py", line 555, in send_state
    self._send(msg, buffers=buffers)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/ipywidgets/widgets/widget.py", line 818, in _send
    self.comm.send(data=msg, buffers=buffers)
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/comm/base_comm.py", line 144, in send
    self.publish_msg(
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/ipykernel/comm/comm.py", line 42, in publish_msg
    parent=self.kernel.get_parent(),
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/nathanielforde/mambaforge/envs/pymc-marketing-dev/lib/python3.12/site-packages/ipykernel/kernelbase.py", line 797, in get_parent
    return self._shell_parent.get()
           ^^^^^^^^^^^^^^^^^^^^^^^^
LookupError: <ContextVar name='shell_parent' at 0x105aa62f0>

I'm not sure this actually impacts the sampling and just the render of the progress bar. But i'm going to let the model run for five minutes and if i don't get an update i'll try an re-install a cleaner environment

@NathanielF
Copy link
Contributor

Confirmed @juanitorduz 's code changes will run on nutpie and vanilla pymc. But identifiability of the fixed parameters seems effected.

Formulas with fixed parameters e.g. income

image

Formulas without fixed parameters i.e. just alternative specific parameters.

image

It's also very annoying that the progress bar doesn't seem to work anymore for nutpie, sampling still seems to work though. However with numpyro the sampling itself seems broken due to progress bar incompatibility.

I will need to spend more time debugging properly. I'll try tonight.

@juanitorduz
Copy link
Collaborator Author

Sure! Take your time! Feel free to push to this branch I create a new PR :)

import numpy.typing as npt
import pymc as pm
import pytensor.tensor as pt
from numpy.core.multiarray import normalize_axis_index
Copy link
Contributor

Choose a reason for hiding this comment

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

would this be conditional import? Are we supporting both versions?

@NathanielF
Copy link
Contributor

Opened a fresh pull request here: #2015

Was easier for me to work through the difference from main. Requested a review, but i think we can close this one.

@juanitorduz juanitorduz deleted the fix-errors-pymc-new branch October 19, 2025 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants