Hierarchical MMM for intercept in pymc3

I have a dataframe df which would contain scaled impressions for some media variables (marketing activities), and my target variable is the number of acquisitions for a particular week.

This is weekly level data for 3 years for 4 geographical locations.

I want to create a Bayesian model using pymc3 which would have different intercepts for all those 4 geo locations but same coefficients for all the other media variables.

My dataset has 624 records (156 records for each geo location).

Through my research I found that we could use a shape argument along with my priors and use indexing to align those priors with the target variable.

The command that we are using right now for a single intercept across all 624 records:

intercept = pm.Normal(‘coeff_intercept’, mu=prior_mean_intercept[0], sd=prior_std_intercept[0])

// Here we are introducing our intercept as our normal distribution.

mu_likelihood = intercept

// mu_likelihood is our observed random variable and all the other media variable distributions are added further

New command for intercept with 4 coefficients (1 for each geo location):

weeks_per_dma = 156

dma_idx = np.repeat(np.arange(4), weeks_per_dma)

intercept = pm.Normal(‘coeff_intercept’, mu=prior_mean_intercept[0], sd=prior_std_intercept[0], shape = 4)

mu_likelihood = intercept[dma_idx]

I am getting very high loss in the new command in comparison to the method where we have only 1 intercept. please help me in identifying the issue. Is there something else that I am missing out on?

You could try looking at a non-centered parameterisation (useful/classic notebook here) , and also couple that with a ZeroSumNormal (useful notebook here).

1 Like