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 info 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/foremost/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 include detrimental values. When publicity metrics are usually not obtainable, use the identical as in media spend.
- Media spend : Containing the media spending per channel and time span. The media information and media spend should have the identical dimensions.
When must you use spends vs execution ?
It’s often really helpful to make use of publicity metrics as direct inputs into the mannequin as they characterize how media exercise has been consumed by shoppers. Nevertheless, nobody plans a funds utilizing execution information. Should you use MMM to optimize funds planning, my recommendation can be to make use of information you management, ie spends.
Loading the info
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 kind in order that Meridian can perceive them. The CoordToColumns
object will assist us do this, and requires necessary info :
time
: the time column (often 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 wish to predict gross sales.media
: the media execution information (impressions, clicks, and many others.) or the spends if we’ve 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’ve a number of teams (geographies for ex.), inhabitants
, attain
, frequency
. Particulars about these are out of this scope however the documentation may be discovered right here.
We are able to subsequently 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 an information 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 info
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. Pattern 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 reducing development for newspaper correlated with an rising development for Social Media. Spends appear to be additionally rising at or simply earlier than Christmas.
Specifying the Mannequin
Constructing the mannequin and choosing the proper parameters may be fairly advanced as there are numerous choices obtainable. I’ll share right here my findings however be happy 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 deal with 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’ll need to adapt the code to make use of a special 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,
title=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,
# title=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
embrace within the Adstock calculation. I like to recommend selecting between 2 and 6.paid_media_prior_type
: in case you select to mannequin thebeta_m
, then selectcoefficient
. Else, selectroi
ormroi
.knots
: Meridian applies computerized seasonality adjustment via a time-varying intercept method, managed by theknots
worth. You possibly can set a worth of 1 (fixed intercept, no seasonality modelling), or equal to a given quantity that have to be decrease than the size of the info. A low worth might result in a low baseline, a excessive worth might 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 doable to outline a train-test cut up to keep away from overfitting by way of the holdout_id
parameter. I gained’t cowl it right here, however it’s a finest observe to have this cut up achieved 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 may be gradual when you’ve got numerous information factors and variables. I like to recommend to start out 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 collection of checks to make sure that we will use it confidently.
- R-hat
R-hat near 1.0 point out convergence. R-hat < 1.2 signifies approximate convergence and is an inexpensive threshold for a lot of issues.
An absence of convergence sometimes has one in all two culprits. Both the mannequin may be very poorly misspecified for the info, which may be within the chance (mannequin specification) or within the prior. Or, there may be not sufficient burnin, which means n_adapt + n_burnin shouldn’t be giant sufficient.
from meridian.evaluation import visualizermodel_diagnostics = visualizer.ModelDiagnostics(mmm)
model_diagnostics.plot_rhat_boxplot()
We see that every one r-hat values are beneath 1.02, which signifies no divergence or concern throughout coaching.
2. Mannequin hint
The mannequin hint comprises the pattern values from the chains. A pleasant hint is when the 2 posterior distributions (as we’ve 2 chains) for a given parameter overlap properly. Within the diagram beneath, you’ll be able to see that blue and black strains on the left-hand facet properly overlap :
3. Prior vs Posterior distributions
To know if our mannequin has realized 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 subsequently has most likely not realized something, or that the priors have been misspecified. To ensure our mannequin has realized, 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 guage our mannequin match. You most likely learn 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 might 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 info.
Let’s now plot the mannequin:
model_fit = visualizer.ModelFit(mmm)
model_fit.plot_model_fit()
Contributions of media to gross sales
Keep in mind that one of many goals of MMM is to offer you media contributions vs your gross sales. That is what we’ll take a look at with a waterfall diagram :
media_summary = visualizer.MediaSummary(mmm)
media_summary.plot_contribution_waterfall_chart()
What we often anticipate is to have a baseline between 60 and 80%. Remember the fact that this worth may be very delicate and rely upon the mannequin specification and parameters. I encourage you to play with totally different knots
values and priors and see the impression it could 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 most important. MMM shouldn’t be a precise reply : it provides you values AND uncertainty related to these. My opinion right here is that uncertainty intervals are very giant. Possibly we must always use extra sampling steps or add extra variables to the mannequin.
Optimizing our funds
Keep in mind that one of many goals of the MMM is to suggest an optimum allocation of spends to maximise income. This may be achieved first by 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, progress is slower, which means a 2x enhance in spend won’t translate to a 2x incremental income.
The aim of the optimization might be to take these curves and navigate to seek out the perfect 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 perform.
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 (state of affairs 1). We are going to use the default parameters right here however it’s doable 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 by way of income ?
3% enhance in income simply by rebalancing our funds ! After all this conclusion is a bit hasty. First, replaying the previous is simple. You don’t have 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’ve used right here is an easy extra mannequin, however some approaches use a log-log multiplicative mannequin to account for interactions between variables. Third, there may be 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 to be the image beneath and optimizing beneath uncertainty turns into much more advanced :
Nevertheless, it nonetheless provides you an thought of the place you’re perhaps over or under-spending.
MMM is a fancy however highly effective instrument that may uncover insights out of your advertising and marketing information, aid you perceive your advertising and marketing effectivity and help you in funds planning. The brand new strategies counting on Bayesian inference present good function resembling adstock and saturation modelling, incorporation of geographic-level information, uncertainty ranges and optimization capabilities. Pleased coding.