Unsupported dtype for TensorType: object while using PyMC for Hierarchical Market Mix Modeling

14 Views Asked by At

I am trying to reproduce the example in the blog :

https://towardsdatascience.com/bayesian-hierarchical-marketing-mix-modeling-in-pymc-684f6024e57a#:~:text=Bayesian%20Marketing%20Mix%20Modeling%20in%20Python%20via%20PyMC3&text=The%20Bayesian%20approach%20works%20exceptionally,example%2C%20spendings%20across%20several%20countries%3F

However I get an object error where the channel contributions are getting calculated , commented for ease of discovery. Below is the whole code :

'''
import aesara.tensor as at
def saturate(x, a):
    return 1 - at.exp(-a*x)
def carryover(x, strength, length=21):
    w = at.as_tensor_variable(
        [at.power(strength, i) for i in range(length)]
    )
    
    x_lags = at.stack(
        [at.concatenate([
            at.zeros(i),
            x[:x.shape[0]-i]
        ]) for i in range(length)]
    )
    
    return at.dot(w, x_lags)

''''

with pm.Model() as bhmmm:
    coef_lam = pm.Exponential("coef_lam", lam=10)
    sat_lam = pm.Exponential("sat_lam", lam=10)
    car_alpha = pm.Exponential("car_alpha", lam=0.01)
    car_beta = pm.Exponential("car_beta", lam=0.01)
    base_lam = pm.Exponential("base_lam", lam=10)

    for country in X["Country"].unique():
        X_ = X[X["Country"] == country]
        channel_contributions = []

        for channel in ["TV", "Radio", "Banners"]:
            coef = pm.Exponential(f"coef_{channel}_{country}", lam=coef_lam)
            sat = pm.Exponential(f"sat_{channel}_{country}", lam=sat_lam)
            car = pm.Beta(f"car_{channel}_{country}", alpha=car_alpha, beta=car_beta)

            channel_data = X_[channel].values
            channel_contribution = pm.Deterministic(
                f"contribution_{channel}_{country}",
                coef * saturate(carryover(channel_data, car), sat),# Error
            )

            channel_contributions.append(channel_contribution)

        base = pm.Exponential(f"base_{country}", lam=base_lam)
        noise = pm.Exponential(f"noise_{country}", lam=0.001)

        sales = pm.Normal(
            f"sales_{country}",
            mu=sum(channel_contributions) + base,
            sigma=noise,
            observed=y[X_.index].values,
        )

    trace = pm.sample(tune=3000)

The link to the dataset in github is : https://raw.githubusercontent.com/Garve/datasets/fdb81840fb96faeda5a874efa1b9bbfb83ce1929/bhmmm.csv

Can anyone please help me resolve this? I have bitten away almost half of my nails in trying to fix this!!

0

There are 0 best solutions below