Skip to content

Multi MMM does not work with time_varying_intercept_ and custom priors #1575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
juanitorduz opened this issue Mar 20, 2025 · 4 comments
Open
Labels
bug Something isn't working MMM

Comments

@juanitorduz
Copy link
Collaborator

juanitorduz commented Mar 20, 2025

I was trying to run the example notebook from #1521 with

model_config = {
    "intercept": Prior("Normal", mu=0.5, sigma=0.2, dims="geo"),
    "gamma_control": Prior("Normal", mu=0, sigma=0.03, dims="control"),
    "gamma_fourier": Prior("Laplace", mu=0, b=0.2, dims=("geo", "fourier_mode")),
    'intercept_tvp_config': HSGPKwargs(m=200, L=None, eta_lam=1, ls_mu=5, ls_sigma=5),
    "likelihood": Prior(
        "TruncatedNormal",
        lower=0,
        sigma=Prior("HalfNormal", sigma=Prior("HalfNormal", sigma=1)),
        dims=("date", "geo"),
    ),
}

and I get the error at build time

File ~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1109, in MMM.build_model(self, X, y, **kwargs)
   [1104](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1104) if self.time_varying_intercept:
   [1105](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1105)     baseline_intercept = self.model_config["intercept"].create_variable(
   [1106](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1106)         "baseline_intercept"
   [1107](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1107)     )
-> [1109](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1109)     intercept_latent_process = SoftPlusHSGP.parameterize_from_data(
   [1110](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1110)         X=time_index,  # this is
   [1111](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1111)         dims=("date", *self.dims),
   [1112](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1112)         **self.model_config["intercept_tvp_config"],
   [1113](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1113)     ).create_variable("intercept_latent_process")
   [1115](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1115)     intercept = pm.Deterministic(
   [1116](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1116)         name="intercept_contribution",
   [1117](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1117)         var=baseline_intercept[None, ...] * intercept_latent_process,
   [1118](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1118)         dims=("date", *self.dims),
   [1119](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1119)     )
   [1120](https://file+.vscode-resource.vscode-cdn.net/Users/juanitorduz/Documents/pymc-marketing/docs/source/notebooks/mmm/~/Documents/pymc-marketing/pymc_marketing/mmm/multidimensional.py:1120) else:

TypeError: pymc_marketing.mmm.hsgp.HSGP.parameterize_from_data() argument after ** must be a mapping, not HSGPKwargs

I think the issue is we are not not expecting HSGPKwargs anywhere as the default config has 'intercept_tvp_config': {'ls_lower': 0.3, 'ls_upper': 2.0},, which is passed to

intercept_latent_process = SoftPlusHSGP.parameterize_from_data(
X=time_index, # this is
dims=("date", *self.dims),
**self.model_config["intercept_tvp_config"],
).create_variable("intercept_latent_process")

However, we are parsing the model_config at the init stage:

model_config = parse_model_config(
model_config, # type: ignore
hsgp_kwargs_fields=["intercept_tvp_config", "media_tvp_config"],
)

@juanitorduz juanitorduz added MMM bug Something isn't working and removed Needs Triage labels Mar 20, 2025
@juanitorduz
Copy link
Collaborator Author

@williambdean Regarding the design, how do you view the integration between SoftPlusHSGP (and other HSGP) and HSGPKwargs?

@williambdean
Copy link
Contributor

williambdean commented Mar 20, 2025

It might not accept that at the moment. Are we expecting to use the HSGPKwargs in the new MMMM class? CC: @cetagostini

I think the HSGPKwargs should be removed completely and work toward just passing a HSGP class itself (HSGP, SoftPlusHSGP, HSGPPeriodic). See #1432. But not there yet.

This would allow for the different parameter requirements for the classes to be handled within the class itself. It is more flexible with the cost that it becomes a bit more verbose to pass.

Maybe we can explore a different interface that makes use of the mu_effects

mmm = MMM(..., # no hsgp information here)
hsgp = HSGP.parameterize_from_data(...)
mmm.add_intercept(hsgp)

Bit of a chicken or egg for both of these solutions since the HSGP requires data (register_data method) would be called within the MMM class.

What are your thoughts?

@juanitorduz
Copy link
Collaborator Author

juanitorduz commented Mar 20, 2025

I agree with removing the HSHPKwargs! Please let me know if you want some support or if there is any way I could help!

@gdeleva
Copy link
Contributor

gdeleva commented Mar 27, 2025

Thank you for this great library.

I understand that we cannot define a custom prior, but would the default prior be a time varying intercept equal across all geographical regions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working MMM
Projects
None yet
Development

No branches or pull requests

3 participants