pymc-labs / pymc-marketing

Bayesian marketing toolbox in PyMC. Media Mix (MMM), customer lifetime value (CLV), buy-till-you-die (BTYD) models and more.
https://www.pymc-marketing.io/
Apache License 2.0
651 stars 178 forks source link

Help defining priors and derived variables from them #875

Closed jgzga closed 1 week ago

jgzga commented 1 month ago

I have the following code where I define certain priors and derived variables with these priors. However, it seems that they are not working because, despite restricting the distribution to be positive (in this case with a HalfNormal), they are not turning out positive. The documentation provides some information about the Prior class but nothing related to how to create a variable with those priors.

with pm.Model(coords=coords) as model:
    # Priors
    a = pm.HalfNormal(name="a", sigma=4)
    b_trend = pm.HalfNormal(name="b_trend", sigma=2)
    b_fourier = pm.Laplace(name="b_fourier", mu=0, b=2, dims="fourier_mode")
    b_z = pm.HalfNormal(name="b_z", sigma=2, dims="channel")
    sigma = pm.HalfNormal(name="sigma", sigma=0.5)
    nu = pm.Gamma(name="nu", alpha=25, beta=2)

    # Model components
    trend = pm.Deterministic(name="trend", var=a + b_trend * data['t'], dims="date")
    seasonality = pm.Deterministic(
        name="seasonality", var=pm.math.dot(fourier_features.values, b_fourier), dims="date"
    )
    z_effect = pm.Deterministic(
        name="z_effect", 
        var=pm.math.sum(b_z * data[[f'{col}_adstock_saturated' for col in media_columns]].values, axis=1), 
        dims="date"
    )
    mu = pm.Deterministic(name="mu", var=trend + seasonality + z_effect, dims="date")

    # Likelihood
    pm.StudentT(name="likelihood", nu=nu, mu=mu, sigma=sigma, observed=data['y'], dims="date")

# Configure the MMM model
mmm = MMM(
    model=model,
    date_column="date_week",
    adstock="geometric",
    saturation="logistic",
    channel_columns=[f'{col}_adstock_saturated' for col in media_columns],
    control_columns=['t'],
    adstock_max_lag=8,
    yearly_seasonality=2,
)

The priors and variables are based on this post: https://juanitorduz.github.io/pymc_mmm/ Any help would be appreciated. Thank you

jgzga commented 1 month ago

I saw this example on documentation:

 """Create a PyMC variable from the prior.

        Must be used in a PyMC model context.

        Parameters
        ----------
        name : str
            The name of the variable.

        Returns
        -------
        pt.TensorVariable
            The PyMC variable.

        Examples
        --------
        Create a hierarchical normal variable in larger PyMC model.

        .. code-block:: python

            dist = Prior(
                "Normal",
                mu=Prior("Normal"),
                sigma=Prior("HalfNormal"),
                dims="channel",
            )

            coords = {"channel": ["C1", "C2", "C3"]}
            with pm.Model(coords=coords):
                var = dist.create_variable("var")

        """

But I don't know how can I include this variable called "var" in the class MMM

AlfredoJF commented 1 month ago

Hi @jgzga

I recommend that you follow the latest notebook mmm_example.ipynb in case you want to start playing around with PyMC-Marketing and use the out-of-the-box model.

If you need to build a custom model, follow this notebook mmm_components.ipynb then.

Both examples use the same Prior class you mentioned and will be available in release 0.8.0.

In the meanwhile, install the main branch using this cmd to use the latest updates in your code pip install git+https://github.com/pymc-labs/pymc-marketing.git

jgzga commented 1 month ago

I'm installing the library with this command. pip install git+https://github.com/pymc-labs/pymc-marketing.git

And importing the Prior class as in this notebook with command from pymc_marketing.prior import Prior

But gives me this error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
[<ipython-input-2-895cc36dd95d>](https://localhost:8080/#) in <cell line: 11>()
      9 from pymc_marketing.mmm import MMM
     10 #from pymc_marketing.mmm.plot import plot_curve
---> 11 from pymc_marketing.prior import Prior
     12 from pymc_marketing.mmm.components.adstock import geometric_adstock
     13 from pymc_marketing.mmm.components.saturation import logistic_saturation

ModuleNotFoundError: No module named 'pymc_marketing.prior'
AlfredoJF commented 1 month ago

@jgzga

Try to uninstall and re-install the package as the main branch is labelled as 0.7.0 and might be causing issues when installing directly from the main Github branch.


pip uninstall pymc-marketing
pip install git+https://github.com/pymc-labs/pymc-marketing.git
wd60622 commented 1 month ago

I'm installing the library with this command.

pip install git+https://github.com/pymc-labs/pymc-marketing.git

And importing the Prior class as in this notebook with command

from pymc_marketing.prior import Prior

But gives me this error:


---------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)

[<ipython-input-2-895cc36dd95d>](https://localhost:8080/#) in <cell line: 11>()

      9 from pymc_marketing.mmm import MMM

     10 #from pymc_marketing.mmm.plot import plot_curve

---> 11 from pymc_marketing.prior import Prior

     12 from pymc_marketing.mmm.components.adstock import geometric_adstock

     13 from pymc_marketing.mmm.components.saturation import logistic_saturation

ModuleNotFoundError: No module named 'pymc_marketing.prior'

You will likely have to pip uninstall pymc-marketing first then run command again

Related to #860

jgzga commented 1 month ago

After doing this, it worked.

pip uninstall pymc-marketing
pip install git+https://github.com/pymc-labs/pymc-marketing.git

Thanks

wd60622 commented 1 month ago

Hi @jgzga, did this resolve? The original question may have gotten lost. If you have an further questions, let me know.