Let’s now use the Meridian library with information. Step one is to put in Meridian with both pip or poetry : pip set up google-meridian
or poetry add google-meridian
We are going to then get the information and begin defining columns that are of curiosity to us.
import pandas as pdraw_df = pd.read_csv("https://uncooked.githubusercontent.com/sibylhe/mmm_stan/fundamental/information.csv")
For the management variables, we’ll use all the holidays variables within the dataset. Our KPI might be gross sales, and the time granularity might be weekly.
Subsequent, we’ll choose our Media variables. Meridian makes a distinction between media information and media spends:
- Media information (or “execution”) : Accommodates the publicity metric per channel and time span (resembling impressions per time interval). Media values should not comprise unfavorable values. When publicity metrics are usually not out there, use the identical as in media spend.
- Media spend : Containing the media spending per channel and time span. The media information and media spend will need to have the identical dimensions.
When must you use spends vs execution ?
It’s normally really useful to make use of publicity metrics as direct inputs into the mannequin as they characterize how media exercise has been consumed by customers. Nonetheless, nobody plans a funds utilizing execution information. When you use MMM to optimize funds planning, my recommendation can be to make use of information you management, ie spends.
Loading the information
In our use case, we’ll solely use the spends from 5 channels: Newspaper, Radio, TV, Social Media and On-line Show.
# 1. management variables
CONTROL_COLS = [col for col in raw_df.columns if 'hldy_' in col]# 2. media variables
spends_mapping = {
"mdsp_nsp": "Newspaper",
"mdsp_audtr": "Radio",
"mdsp_vidtr": "TV",
"mdsp_so": "Social Media",
"mdsp_on": "On-line Show",
}
MEDIA_COLS = checklist(spends_mapping.keys())
# 3. gross sales variables
SALES_COL = "gross sales"
# 4. Date column
DATE_COL = "wk_strt_dt"
data_df = raw_df[[DATE_COL, SALES_COL, *MEDIA_COLS, *CONTROL_COLS]]
data_df[DATE_COL] = pd.to_datetime(data_df[DATE_COL])
We are going to then map the columns to their information sort in order that Meridian can perceive them. The CoordToColumns
object will assist us try this, and requires necessary data :
time
: the time column (normally a date, day or week)controls
: the management variableskpi
: the response we wish the mannequin to foretell. In our case, we’ll give it the worthincome
since we need to predict gross sales.media
: the media execution information (impressions, clicks, and many others.) or the spends if we now have no execution information. In our case, we’ll put the spends.media_spends
: the media spends.
There a number of different parameters which can be utilized, specifically the geo
parameter if we now have a number of teams (geographies for ex.), inhabitants
, attain
, frequency
. Particulars about these are out of this scope however the documentation might be discovered right here.
We are able to due to this fact create our column mappings :
from meridian.information import loadcoord_to_columns = load.CoordToColumns(
time=DATE_COL,
controls=CONTROL_COLS,
kpi=SALES_COL,
media=MEDIA_COLS,
media_spend=MEDIA_COLS,
)
Subsequent, we’ll use our dataframe and the columns mappings to create a knowledge object for use by the mannequin.
loader = load.DataFrameDataLoader(
df=data_df,
kpi_type='income',
coord_to_columns=coord_to_columns,
media_to_channel=spends_mapping,
media_spend_to_channel=spends_mapping
)
information = loader.load()
Exploring the information
Gross sales
fig, ax = plt.subplots()
data_df.set_index("wk_strt_dt")[SALES_COL].plot(coloration=COLORS[1], ax=ax)
ax.set(title="Gross sales", xlabel='date', ylabel="gross sales");
fig.tight_layout();
There appears to be a pleasant seasonality with peaks round Christmas. Development is total fixed with a degree oscillating between 50 and 150M.
Media Spends
fig, ax = plt.subplots(5, figsize=(20,30))for axis, channel in zip(ax, spends_columns_raw):
data_df.set_index("wk_strt_dt")[channel].plot(ax=axis, coloration=COLORS[1])
axis.legend(title="Channel", fontsize=12)
axis.set(title=spends_mapping[channel], xlabel="Date", ylabel="Spend");
fig.tight_layout()
We observe a clearly lowering pattern for newspaper correlated with an growing pattern for Social Media. Spends appear to be additionally growing at or simply earlier than Christmas.
Specifying the Mannequin
Constructing the mannequin and selecting the best parameters might be fairly complicated as there are plenty of choices out there. I’ll share right here my findings however be at liberty to discover by your self.
The primary half is to decide on the priors for our media spends. We are going to use the PriorDistribution
class which permits us to outline a number of variables. You possibly can change the priors of just about any parameter of the mannequin (mu, tau, gamma, beta, and many others…), however for now we’ll solely give attention to the beta that are the coefficients of our media variables. My suggestion is, if you’re utilizing spends solely, to make use of the beta_m
. You possibly can select the roi_m
or mroi_m
however you will want to adapt the code to make use of a unique prior.
import tensorflow_probability as tfp
from meridian import constants
from meridian.mannequin import prior_distributionprior = prior_distribution.PriorDistribution(
beta_m=tfp.distributions.HalfNormal(
0.2,
identify=constants.BETA_M,
# If you wish to use the ROI imaginative and prescient as a substitute of the coefficients method
# roi_m=tfp.distributions.HalfNormal(
# 0.2,
# identify=constants.ROI_M
)
)
When defining the mannequin specs, you’ll then have the ability to outline :
- the priors (cf above).
max_len
: the utmost variety of lag durations (≥ `0`) to
embody within the Adstock calculation. I like to recommend selecting between 2 and 6.paid_media_prior_type
: should you select to mannequin thebeta_m
, then selectcoefficient
. Else, selectroi
ormroi
.knots
: Meridian applies automated seasonality adjustment by way of a time-varying intercept method, managed by theknots
worth. You possibly can set a price of 1 (fixed intercept, no seasonality modelling), or equal to a given quantity that have to be decrease than the size of the information. A low worth may result in a low baseline, a excessive worth may result in overfitting and result in a baseline consuming the whole lot. I like to recommend to set it to 10% of the variety of information factors
Additionally it is attainable to outline a train-test cut up to keep away from overfitting through the holdout_id
parameter. I gained’t cowl it right here, however it’s a greatest follow to have this cut up performed for mannequin choice.
In a nutshell:
from meridian.mannequin import spec
from meridian.mannequin import mannequinmodel_spec = spec.ModelSpec(
prior=prior,
max_lag=6,
knots=int(0.1*len(data_df)),
paid_media_prior_type='coefficient',
)
mmm = mannequin.Meridian(input_data=information, model_spec=model_spec)
Operating the mannequin
Becoming the mannequin might be gradual when you’ve got a lot of information factors and variables. I like to recommend to begin with 2 chains, and depart the default variety of samples:
mmm.sample_prior(500)
mmm.sample_posterior(n_chains=2, n_adapt=500, n_burnin=500, n_keep=1000)
Mannequin Diagnostics
As soon as the mannequin is completed operating, we’ll carry out a sequence of checks to make sure that we will use it confidently.
- R-hat
R-hat near 1.0 point out convergence. R-hat
A scarcity of convergence sometimes has considered one of two culprits. Both the mannequin could be very poorly misspecified for the information, which might be within the probability (mannequin specification) or within the prior. Or, there’s not sufficient burnin, which means n_adapt + n_burnin shouldn’t be massive sufficient.
from meridian.evaluation import visualizermodel_diagnostics = visualizer.ModelDiagnostics(mmm)
model_diagnostics.plot_rhat_boxplot()
We see that each one r-hat values are under 1.02, which signifies no divergence or concern throughout coaching.
2. Mannequin hint
The mannequin hint accommodates the pattern values from the chains. A pleasant hint is when the 2 posterior distributions (as we now have 2 chains) for a given parameter overlap properly. Within the diagram under, you possibly can see that blue and black strains on the left-hand aspect properly overlap :
3. Prior vs Posterior distributions
To know if our mannequin has discovered throughout becoming, we’ll examine prior vs posterior distribution. In the event that they completely overlap, which means our mannequin has not shifted its prior distributions and due to this fact has in all probability not discovered something, or that the priors had been misspecified. To verify our mannequin has discovered, we wish to see a slight shift in distributions :
We clearly that that the priors and posteriors don’t overlap. For TV and Social Media for ex, we see that the orange HalfNormal priors have shifted to the blue quasi-Regular distributions.
4. R2 and Mannequin Match
Lastly, we’ll use metrics to judge our mannequin match. You in all probability find out about metrics like R2, MAPE, and many others., so let’s take a look at these values:
model_diagnostics = visualizer.ModelDiagnostics(mmm)
model_diagnostics.predictive_accuracy_table()
Clearly, a R2 of 0.54 shouldn’t be nice in any respect. We may enhance that by both including extra knots within the baseline, or extra information to the mannequin, or play with the priors to attempt to seize extra data.
Let’s now plot the mannequin:
model_fit = visualizer.ModelFit(mmm)
model_fit.plot_model_fit()
Contributions of media to gross sales
Do not forget that one of many goals of MMM is to give you media contributions vs your gross sales. That is what we’ll have a look at with a waterfall diagram :
media_summary = visualizer.MediaSummary(mmm)
media_summary.plot_contribution_waterfall_chart()
What we normally count on is to have a baseline between 60 and 80%. Take into account that this worth might be very delicate and rely on the mannequin specification and parameters. I encourage you to play with completely different knots
values and priors and see the affect it could actually have on the mannequin.
Spends vs Contributions
The spend versus contribution chart compares the spend and incremental income or KPI cut up between channels. The inexperienced bar highlights the return on funding (ROI) for every channel.
media_summary.plot_roi_bar_chart()
We see that the best ROI comes from Social Media, adopted by TV. However that is additionally the place the uncertainty interval is the biggest. MMM shouldn’t be an actual reply : it provides you values AND uncertainty related to these. My opinion right here is that uncertainty intervals are very massive. Perhaps we must always use extra sampling steps or add extra variables to the mannequin.
Optimizing our funds
Do not forget that one of many goals of the MMM is to suggest an optimum allocation of spends to maximise income. This may be performed first by taking a look at what we name response curves. Response curves describe the connection between advertising and marketing spend and the ensuing incremental income.
We are able to see there that :
- incremental income will increase because the spend will increase
- for some touchpoints like newspaper, development is slower, which means a 2x improve in spend is not going to translate to a 2x incremental income.
The objective of the optimization might be to take these curves and navigate to seek out the very best mixture of worth that maximize our gross sales equation. We all know that gross sales = f(media, management, baseline), and we’re looking for the media* values that maximize our operate.
We are able to select between a number of optimization issues, for ex:
- How can I attain the sames gross sales degree with much less funds ?
- Given the identical funds, what’s the most income I can get ?
Let’s use Meridian to optimize our funds and maximize gross sales (situation 1). We are going to use the default parameters right here however it’s attainable to fine-tune the constraints on every channel to restrict the search scope.
from meridian.evaluation import optimizerbudget_optimizer = optimizer.BudgetOptimizer(mmm)
optimization_results = budget_optimizer.optimize()
# Plot the response curves earlier than and after
optimization_results.plot_response_curves()
We are able to see that the optimizer recommends to lower the spends for Newspaper, On-line Show and recommends to extend spends for Radio, Social Media and TV.
How does it translate when it comes to income ?
3% improve in income simply by rebalancing our funds ! In fact this conclusion is a bit hasty. First, replaying the previous is straightforward. You haven’t any assure that your baseline gross sales (60%) would behave the identical subsequent 12 months. Consider Covid. Second, our mannequin doesn’t account for interactions between channels. What we now have used right here is a straightforward extra mannequin, however some approaches use a log-log multiplicative mannequin to account for interactions between variables. Third, there’s uncertainty in our response curves which isn’t dealt with by the optimizer, because it solely takes the typical response curve for every channel. Response curves with uncertainty appear like the image under and optimizing below uncertainty turns into much more complicated :
Nonetheless, it nonetheless provides you an thought of the place you might be perhaps over or under-spending.
MMM is a posh however highly effective software that may uncover insights out of your advertising and marketing information, assist you perceive your advertising and marketing effectivity and help you in funds planning. The brand new strategies counting on Bayesian inference present good characteristic resembling adstock and saturation modelling, incorporation of geographic-level information, uncertainty ranges and optimization capabilities. Completely satisfied coding.