forked from sibylhe/mmm_stan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmm_stan.py
921 lines (816 loc) · 29.7 KB
/
mmm_stan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# Implementation of Multiplicative Marketing Mix Model, Adstock and Diminishing Return
# Author: Sibyl He
# Pystan Installation Tips (mac, anaconda3)
# 1. In bash:
# (create a stan environment, install pystan, current version is 2.19)
# conda create -n stan_env python=3.7 -c conda-forge
# conda activate stan_env
# conda install pystan -c conda-forge
# (install gcc5, pystan 2.19 requires gcc4.9.3 and above)
# brew install gcc@5
# (look for 'gcc-10', 'g++-10')
# ls /usr/local/bin | grep gcc
# ls /usr/local/bin | grep g++
#
# 2. Open Anaconda Navigator > Home > Applications on: select stan_env as environment, launch Notebook
#
# 3. In python:
# import os
# os.environ['CC'] = 'gcc-10'
# os.environ['CXX'] = 'g++-10'
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import pandas as pd
import sys
import time
from datetime import datetime
from datetime import timedelta
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
sns.color_palette("husl")
sns.set_style('darkgrid')
# Data
# Four years' (209 weeks) records of sales, media impression and media spending at weekly level.
df = pd.read_csv('data.csv')
# 1. media variables
# media impression
mdip_cols=[col for col in df.columns if 'mdip_' in col]
# media spending
mdsp_cols=[col for col in df.columns if 'mdsp_' in col]
# 2. control variables
# macro economics variables
me_cols = [col for col in df.columns if 'me_' in col]
# store count variables
st_cols = ['st_ct']
# markdown/discount variables
mrkdn_cols = [col for col in df.columns if 'mrkdn_' in col]
# holiday variables
hldy_cols = [col for col in df.columns if 'hldy_' in col]
# seasonality variables
seas_cols = [col for col in df.columns if 'seas_' in col]
base_vars = me_cols+st_cols+mrkdn_cols+hldy_cols+seas_cols
# 3. sales variables
sales_cols =['sales']
df[['wk_strt_dt']+mdip_cols+['sales']].head()
# EDA - correlation, distribution plots
plt.figure(figsize=(24,20))
sns.heatmap(df[mdip_cols+['sales']].corr(), square=True, annot=True, vmax=1, vmin=-1, cmap='RdBu')
plt.figure(figsize=(50,50))
sns.pairplot(df[mdip_cols+['sales']], vars=mdip_cols+['sales'])
# 1.1 Adstock
def apply_adstock(x, L, P, D):
'''
params:
x: original media variable, array
L: length
P: peak, delay in effect
D: decay, retain rate
returns:
array, adstocked media variable
'''
x = np.append(np.zeros(L-1), x)
weights = np.zeros(L)
for l in range(L):
weight = D**((l-P)**2)
weights[L-1-l] = weight
adstocked_x = []
for i in range(L-1, len(x)):
x_array = x[i-L+1:i+1]
xi = sum(x_array * weights)/sum(weights)
adstocked_x.append(xi)
adstocked_x = np.array(adstocked_x)
return adstocked_x
def adstock_transform(df, md_cols, adstock_params):
'''
params:
df: original data
md_cols: list, media variables to be transformed
adstock_params: dict,
e.g., {'sem': {'L': 8, 'P': 0, 'D': 0.1}, 'dm': {'L': 4, 'P': 1, 'D': 0.7}}
returns:
adstocked df
'''
md_df = pd.DataFrame()
for md_col in md_cols:
md = md_col.split('_')[-1]
L, P, D = adstock_params[md]['L'], adstock_params[md]['P'], adstock_params[md]['D']
xa = apply_adstock(df[md_col].values, L, P, D)
md_df[md_col] = xa
return md_df
# # plot adstock with varying decay
# fig, ax = plt.subplots(figsize=(15,5))
# psets = [
# [8, 1, 0.1],
# [8, 1, 0.9]
# ]
# xm = df['mdip_vidtr'].values
# sns.lineplot(x=range(52), y=xm[-52:], ax=ax, label='original')
# for i in range(len(psets)):
# p = psets[i]
# L, P, D = p[0], p[1], p[2]
# xm_adstocked = apply_adstock(xm, L, P, D)
# sns.lineplot(x=range(52), y=xm_adstocked[-52:], ax=ax,
# label='L=%d, P=%d, D=%.1f'%(L, P, D))
# ax.lines[i+1].set_linestyle("--")
# ax.set_title('Adstock Parameter: Decay', fontsize=16)
# # plot adstock with varying length
# fig, ax = plt.subplots(figsize=(15,5))
# psets = [
# [4, 1, 0.9],
# [12, 1, 0.9]
# ]
# xm = df['mdip_vidtr'].values
# sns.lineplot(x=range(52), y=xm[-52:], ax=ax, label='original')
# for i in range(len(psets)):
# p = psets[i]
# L, P, D = p[0], p[1], p[2]
# xm_adstocked = apply_adstock(xm, L, P, D)
# sns.lineplot(x=range(52), y=xm_adstocked[-52:], ax=ax,
# label='L=%d, P=%d, D=%.1f'%(L, P, D))
# ax.lines[i+1].set_linestyle("--")
# ax.set_title('Adstock Parameter: Length', fontsize=16)
# 1.2 Diminishing Return
def hill_transform(x, ec, slope):
return 1 / (1 + (x / ec)**(-slope))
# # plot hill function with varying K and S
# fig, ax = plt.subplots(figsize=(9,6))
# psets = [
# [0.5, 0.5],
# [0.5, 1.0],
# [0.95, 1.0],
# [0.95, 3.0]
# ]
# xm = np.arange(0,2,0.05)
# for i in range(len(psets)):
# p = psets[i]
# ec, slope = p[0], p[1]
# sns.lineplot(x=xm, y=hill_transform(xm, ec, slope), ax=ax,
# label='K=%.2f, S=%.1f'%(ec, slope))
# #ax.lines[i+1].set_linestyle("--")
# ax.set_title('Hill Function', fontsize=16)
# 2. Model Implementation
# The model is built in a stacked way. Three models are trained:
# - Control Model
# - Marketing Mix Model
# - Diminishing Return Model
# 2.1 Control Model / Base Sales Model
import pystan
import os
os.environ['CC'] = 'gcc-10'
os.environ['CXX'] = 'g++-10'
# helper functions
from sklearn.metrics import mean_squared_error
def mean_absolute_percentage_error(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
def apply_mean_center(x):
mu = np.mean(x)
xm = x/mu
return xm, mu
def mean_center_trandform(df, cols):
'''
returns:
mean-centered df
scaler, dict
'''
df_new = pd.DataFrame()
sc = {}
for col in cols:
x = df[col].values
df_new[col], mu = apply_mean_center(x)
sc[col] = mu
return df_new, sc
def mean_log1p_trandform(df, cols):
'''
returns:
mean-centered, log1p transformed df
scaler, dict
'''
df_new = pd.DataFrame()
sc = {}
for col in cols:
x = df[col].values
xm, mu = apply_mean_center(x)
sc[col] = mu
df_new[col] = np.log1p(xm)
return df_new, sc
import json
def save_json(data, file_name):
with open(file_name, 'w') as fp:
json.dump(data, fp)
def load_json(file_name):
with open(file_name, 'r') as fp:
data = json.load(fp)
return data
# mean-centralize: sales, numeric base_vars
df_ctrl, sc_ctrl = mean_center_trandform(df, ['sales']+me_cols+st_cols+mrkdn_cols)
df_ctrl = pd.concat([df_ctrl, df[hldy_cols+seas_cols]], axis=1)
# variables positively related to sales: macro economy, store count, markdown, holiday
pos_vars = [col for col in base_vars if col not in seas_cols]
X1 = df_ctrl[pos_vars].values
# variables may have either positive or negtive impact on sales: seasonality
pn_vars = seas_cols
X2 = df_ctrl[pn_vars].values
ctrl_data = {
'N': len(df_ctrl),
'K1': len(pos_vars),
'K2': len(pn_vars),
'X1': X1,
'X2': X2,
'y': df_ctrl['sales'].values,
'max_intercept': min(df_ctrl['sales'])
}
ctrl_code1 = '''
data {
int N; // number of observations
int K1; // number of positive predictors
int K2; // number of positive/negative predictors
real max_intercept; // restrict the intercept to be less than the minimum y
matrix[N, K1] X1;
matrix[N, K2] X2;
vector[N] y;
}
parameters {
vector<lower=0>[K1] beta1; // regression coefficients for X1 (positive)
vector[K2] beta2; // regression coefficients for X2
real<lower=0, upper=max_intercept> alpha; // intercept
real<lower=0> noise_var; // residual variance
}
model {
// Define the priors
beta1 ~ normal(0, 1);
beta2 ~ normal(0, 1);
noise_var ~ inv_gamma(0.05, 0.05 * 0.01);
// The likelihood
y ~ normal(X1*beta1 + X2*beta2 + alpha, sqrt(noise_var));
}
'''
sm1 = pystan.StanModel(model_code=ctrl_code1, verbose=True)
fit1 = sm1.sampling(data=ctrl_data, iter=2000, chains=4)
fit1_result = fit1.extract()
# extract control model parameters and predict base sales -> df['base_sales']
def extract_ctrl_model(fit_result, pos_vars=pos_vars, pn_vars=pn_vars,
extract_param_list=False):
ctrl_model = {}
ctrl_model['pos_vars'] = pos_vars
ctrl_model['pn_vars'] = pn_vars
ctrl_model['beta1'] = fit_result['beta1'].mean(axis=0).tolist()
ctrl_model['beta2'] = fit_result['beta2'].mean(axis=0).tolist()
ctrl_model['alpha'] = fit_result['alpha'].mean()
if extract_param_list:
ctrl_model['beta1_list'] = fit_result['beta1'].tolist()
ctrl_model['beta2_list'] = fit_result['beta2'].tolist()
ctrl_model['alpha_list'] = fit_result['alpha'].tolist()
return ctrl_model
def ctrl_model_predict(ctrl_model, df):
pos_vars, pn_vars = ctrl_model['pos_vars'], ctrl_model['pn_vars']
X1, X2 = df[pos_vars], df[pn_vars]
beta1, beta2 = np.array(ctrl_model['beta1']), np.array(ctrl_model['beta2'])
alpha = ctrl_model['alpha']
y_pred = np.dot(X1, beta1) + np.dot(X2, beta2) + alpha
return y_pred
base_sales_model = extract_ctrl_model(fit1_result, pos_vars=pos_vars, pn_vars=pn_vars)
base_sales = ctrl_model_predict(base_sales_model, df_ctrl)
df['base_sales'] = base_sales*sc_ctrl['sales']
# evaluate control model
print('mape: ', mean_absolute_percentage_error(df['sales'], df['base_sales']))
# np.savetxt("base_sales_pred.csv", df['base_sales'].values, delimiter=",")
# save_json(base_sales_model, 'ctrl_model.json')
# df['base_sales'] = pd.read_csv('base_sales_pred.csv', header=None).values
# 2.2 Marketing Mix Model
df_mmm, sc_mmm = mean_log1p_trandform(df, ['sales', 'base_sales'])
mu_mdip = df[mdip_cols].apply(np.mean, axis=0).values
max_lag = 8
num_media = len(mdip_cols)
# padding zero * (max_lag-1) rows
X_media = np.concatenate((np.zeros((max_lag-1, num_media)), df[mdip_cols].values), axis=0)
X_ctrl = df_mmm['base_sales'].values.reshape(len(df),1)
model_data2 = {
'N': len(df),
'max_lag': max_lag,
'num_media': num_media,
'X_media': X_media,
'mu_mdip': mu_mdip,
'num_ctrl': X_ctrl.shape[1],
'X_ctrl': X_ctrl,
'y': df_mmm['sales'].values
}
model_code2 = '''
functions {
// the adstock transformation with a vector of weights
real Adstock(vector t, row_vector weights) {
return dot_product(t, weights) / sum(weights);
}
}
data {
// the total number of observations
int<lower=1> N;
// the vector of sales
real y[N];
// the maximum duration of lag effect, in weeks
int<lower=1> max_lag;
// the number of media channels
int<lower=1> num_media;
// matrix of media variables
matrix[N+max_lag-1, num_media] X_media;
// vector of media variables' mean
real mu_mdip[num_media];
// the number of other control variables
int<lower=1> num_ctrl;
// a matrix of control variables
matrix[N, num_ctrl] X_ctrl;
}
parameters {
// residual variance
real<lower=0> noise_var;
// the intercept
real tau;
// the coefficients for media variables and base sales
vector<lower=0>[num_media+num_ctrl] beta;
// the decay and peak parameter for the adstock transformation of
// each media
vector<lower=0,upper=1>[num_media] decay;
vector<lower=0,upper=ceil(max_lag/2)>[num_media] peak;
}
transformed parameters {
// the cumulative media effect after adstock
real cum_effect;
// matrix of media variables after adstock
matrix[N, num_media] X_media_adstocked;
// matrix of all predictors
matrix[N, num_media+num_ctrl] X;
// adstock, mean-center, log1p transformation
row_vector[max_lag] lag_weights;
for (nn in 1:N) {
for (media in 1 : num_media) {
for (lag in 1 : max_lag) {
lag_weights[max_lag-lag+1] <- pow(decay[media], (lag - 1 - peak[media]) ^ 2);
}
cum_effect <- Adstock(sub_col(X_media, nn, media, max_lag), lag_weights);
X_media_adstocked[nn, media] <- log1p(cum_effect/mu_mdip[media]);
}
X <- append_col(X_media_adstocked, X_ctrl);
}
}
model {
decay ~ beta(3,3);
peak ~ uniform(0, ceil(max_lag/2));
tau ~ normal(0, 5);
for (i in 1 : num_media+num_ctrl) {
beta[i] ~ normal(0, 1);
}
noise_var ~ inv_gamma(0.05, 0.05 * 0.01);
y ~ normal(tau + X * beta, sqrt(noise_var));
}
'''
sm2 = pystan.StanModel(model_code=model_code2, verbose=True)
fit2 = sm2.sampling(data=model_data2, iter=1000, chains=3)
fit2_result = fit2.extract()
# extract mmm parameters
def extract_mmm(fit_result, max_lag=max_lag,
media_vars=mdip_cols, ctrl_vars=['base_sales'],
extract_param_list=True):
mmm = {}
mmm['max_lag'] = max_lag
mmm['media_vars'], mmm['ctrl_vars'] = media_vars, ctrl_vars
mmm['decay'] = decay = fit_result['decay'].mean(axis=0).tolist()
mmm['peak'] = peak = fit_result['peak'].mean(axis=0).tolist()
mmm['beta'] = fit_result['beta'].mean(axis=0).tolist()
mmm['tau'] = fit_result['tau'].mean()
if extract_param_list:
mmm['decay_list'] = fit_result['decay'].tolist()
mmm['peak_list'] = fit_result['peak'].tolist()
mmm['beta_list'] = fit_result['beta'].tolist()
mmm['tau_list'] = fit_result['tau'].tolist()
adstock_params = {}
media_names = [col.replace('mdip_', '') for col in media_vars]
for i in range(len(media_names)):
adstock_params[media_names[i]] = {
'L': max_lag,
'P': peak[i],
'D': decay[i]
}
mmm['adstock_params'] = adstock_params
return mmm
mmm = extract_mmm(fit2, max_lag=max_lag,
media_vars=mdip_cols, ctrl_vars=['base_sales'])
# save_json(mmm, 'mmm1.json')
# plot media coefficients' distributions
# red line: mean, green line: median
beta_media = {}
for i in range(len(mmm['media_vars'])):
md = mmm['media_vars'][i]
betas = []
for j in range(len(mmm['beta_list'])):
betas.append(mmm['beta_list'][j][i])
beta_media[md] = np.array(betas)
f = plt.figure(figsize=(18,15))
for i in range(len(mmm['media_vars'])):
ax = f.add_subplot(5,3,i+1)
md = mmm['media_vars'][i]
x = beta_media[md]
mean_x = x.mean()
median_x = np.median(x)
ax = sns.distplot(x)
ax.axvline(mean_x, color='r', linestyle='-')
ax.axvline(median_x, color='g', linestyle='-')
ax.set_title(md)
# Decompose sales to media channels' contribution
# Each media channel's contribution = total sales - sales upon removal the channel
# decompose sales to media contribution
def mmm_decompose_contrib(mmm, df, original_sales=df['sales']):
# adstock params
adstock_params = mmm['adstock_params']
# coefficients, intercept
beta, tau = mmm['beta'], mmm['tau']
# variables
media_vars, ctrl_vars = mmm['media_vars'], mmm['ctrl_vars']
num_media, num_ctrl = len(media_vars), len(ctrl_vars)
# X_media2: adstocked, mean-centered media variables + 1
X_media2 = adstock_transform(df, media_vars, adstock_params)
X_media2, sc_mmm2 = mean_center_trandform(X_media2, media_vars)
X_media2 = X_media2 + 1
# X_ctrl2, mean-centered control variables + 1
X_ctrl2, sc_mmm2_1 = mean_center_trandform(df[ctrl_vars], ctrl_vars)
X_ctrl2 = X_ctrl2 + 1
# y_true2, mean-centered sales variable + 1
y_true2, sc_mmm2_2 = mean_center_trandform(df, ['sales'])
y_true2 = y_true2 + 1
sc_mmm2.update(sc_mmm2_1)
sc_mmm2.update(sc_mmm2_2)
# X2 <- media variables + ctrl variable
X2 = pd.concat([X_media2, X_ctrl2], axis=1)
# 1. compute each media/control factor:
# log-log model: log(sales) = log(X[0])*beta[0] + ... + log(X[13])*beta[13] + tau
# multiplicative model: sales = X[0]^beta[0] * ... * X[13]^beta[13] * e^tau
# each factor = X[i]^beta[i]
# intercept = e^tau
factor_df = pd.DataFrame(columns=media_vars+ctrl_vars+['intercept'])
for i in range(num_media):
colname = media_vars[i]
factor_df[colname] = X2[colname] ** beta[i]
for i in range(num_ctrl):
colname = ctrl_vars[i]
factor_df[colname] = X2[colname] ** beta[num_media+i]
factor_df['intercept'] = np.exp(tau)
# 2. calculate the product of all factors -> y_pred
# baseline = intercept * control factor = e^tau * X[13]^beta[13]
y_pred = factor_df.apply(np.prod, axis=1)
factor_df['y_pred'], factor_df['y_true2'] = y_pred, y_true2
factor_df['baseline'] = factor_df[['intercept']+ctrl_vars].apply(np.prod, axis=1)
# 3. calculate each media factor's contribution
# media contribution = total volume – volume upon removal of the media factor
mc_df = pd.DataFrame(columns=media_vars+['baseline'])
for col in media_vars:
mc_df[col] = factor_df['y_true2'] - factor_df['y_true2']/factor_df[col]
mc_df['baseline'] = factor_df['baseline']
mc_df['y_true2'] = factor_df['y_true2']
# 4. scale contribution
# predicted total media contribution: product of all media factors
mc_df['mc_pred'] = mc_df[media_vars].apply(np.sum, axis=1)
# true total media contribution: total volume - baseline
mc_df['mc_true'] = mc_df['y_true2'] - mc_df['baseline']
# predicted total media contribution is slightly different from true total media contribution
# scale each media factor’s contribution by removing the delta volume proportionally
mc_df['mc_delta'] = mc_df['mc_pred'] - mc_df['mc_true']
for col in media_vars:
mc_df[col] = mc_df[col] - mc_df['mc_delta']*mc_df[col]/mc_df['mc_pred']
# 5. scale mc_df based on original sales
mc_df['sales'] = original_sales
for col in media_vars+['baseline']:
mc_df[col] = mc_df[col]*mc_df['sales']/mc_df['y_true2']
print('rmse (log-log model): ',
mean_squared_error(np.log(y_true2), np.log(y_pred)) ** (1/2))
print('mape (multiplicative model): ',
mean_absolute_percentage_error(y_true2, y_pred))
return mc_df
# calculate media contribution percentage
def calc_media_contrib_pct(mc_df, media_vars=mdip_cols, sales_col='sales', period=52):
'''
returns:
mc_pct: percentage over total sales
mc_pct2: percentage over incremental sales (sales contributed by media channels)
'''
mc_pct = {}
mc_pct2 = {}
s = 0
if period is None:
for col in (media_vars+['baseline']):
mc_pct[col] = (mc_df[col]/mc_df[sales_col]).mean()
else:
for col in (media_vars+['baseline']):
mc_pct[col] = (mc_df[col]/mc_df[sales_col])[-period:].mean()
for m in media_vars:
s += mc_pct[m]
for m in media_vars:
mc_pct2[m] = mc_pct[m]/s
return mc_pct, mc_pct2
mc_df = mmm_decompose_contrib(mmm, df, original_sales=df['sales'])
adstock_params = mmm['adstock_params']
mc_pct, mc_pct2 = calc_media_contrib_pct(mc_df, period=52)
# mc_df.to_csv('mc_df1.csv', index=False)
# save_json(adstock_params, 'adstock_params1.json')
# pd.concat([
# pd.DataFrame.from_dict(mc_pct, orient='index', columns=['mc_pct']),
# pd.DataFrame.from_dict(mc_pct2, orient='index', columns=['mc_pct2'])
# ], axis=1).to_csv('mc_pct_df1.csv')
# 2.3 Diminishing Return Model
def create_hill_model_data(df, mc_df, adstock_params, media):
y = mc_df['mdip_'+media].values
L, P, D = adstock_params[media]['L'], adstock_params[media]['P'], adstock_params[media]['D']
x = df['mdsp_'+media].values
x_adstocked = apply_adstock(x, L, P, D)
# centralize
mu_x, mu_y = x_adstocked.mean(), y.mean()
sc = {'x': mu_x, 'y': mu_y}
x = x_adstocked/mu_x
y = y/mu_y
model_data = {
'N': len(y),
'y': y,
'X': x
}
return model_data, sc
model_code3 = '''
functions {
// the Hill function
real Hill(real t, real ec, real slope) {
return 1 / (1 + (t / ec)^(-slope));
}
}
data {
// the total number of observations
int<lower=1> N;
// y: vector of media contribution
vector[N] y;
// X: vector of adstocked media spending
vector[N] X;
}
parameters {
// residual variance
real<lower=0> noise_var;
// regression coefficient
real<lower=0> beta_hill;
// ec50 and slope for Hill function of the media
real<lower=0,upper=1> ec;
real<lower=0> slope;
}
transformed parameters {
// a vector of the mean response
vector[N] mu;
for (i in 1:N) {
mu[i] <- beta_hill * Hill(X[i], ec, slope);
}
}
model {
slope ~ gamma(3, 1);
ec ~ beta(2, 2);
beta_hill ~ normal(0, 1);
noise_var ~ inv_gamma(0.05, 0.05 * 0.01);
y ~ normal(mu, sqrt(noise_var));
}
'''
# pipeline for training one hill model for a media channel
def train_hill_model(df, mc_df, adstock_params, media, sm):
'''
params:
df: original data
mc_df: media contribution df derived from MMM
adstock_params: adstock parameter dict output by MMM
media: 'dm', 'inst', 'nsp', 'auddig', 'audtr', 'vidtr', 'viddig', 'so', 'on', 'sem'
sm: stan model object
returns:
a dict of model data, scaler, parameters
'''
data, sc = create_hill_model_data(df, mc_df, adstock_params, media)
fit = sm.sampling(data=data, iter=2000, chains=4)
fit_result = fit.extract()
hill_model = {
'beta_hill_list': fit_result['beta_hill'].tolist(),
'ec_list': fit_result['ec'].tolist(),
'slope_list': fit_result['slope'].tolist(),
'sc': sc,
'data': {
'X': data['X'].tolist(),
'y': data['y'].tolist(),
}
}
return hill_model
# extract params by mean or median
# almost no difference, choose either one
def extract_hill_model_params(hill_model, method='mean'):
if method=='mean':
hill_model_params = {
'beta_hill': np.mean(hill_model['beta_hill_list']),
'ec': np.mean(hill_model['ec_list']),
'slope': np.mean(hill_model['slope_list'])
}
elif method=='median':
hill_model_params = {
'beta_hill': np.median(hill_model['beta_hill_list']),
'ec': np.median(hill_model['ec_list']),
'slope': np.median(hill_model['slope_list'])
}
return hill_model_params
def hill_model_predict(hill_model_params, x):
beta_hill, ec, slope = hill_model_params['beta_hill'], hill_model_params['ec'], hill_model_params['slope']
y_pred = beta_hill * hill_transform(x, ec, slope)
return y_pred
def evaluate_hill_model(hill_model, hill_model_params):
x = np.array(hill_model['data']['X'])
y_true = np.array(hill_model['data']['y']) * hill_model['sc']['y']
y_pred = hill_model_predict(hill_model_params, x) * hill_model['sc']['y']
print('mape on original data: ',
mean_absolute_percentage_error(y_true, y_pred))
return y_true, y_pred
# train hill models for all media channels
sm3 = pystan.StanModel(model_code=model_code3, verbose=True)
hill_models = {}
to_train = ['dm', 'inst', 'nsp', 'auddig', 'audtr', 'vidtr', 'viddig', 'so', 'on', 'sem']
for media in to_train:
print('training for media: ', media)
hill_model = train_hill_model(df, mc_df, adstock_params, media, sm3)
hill_models[media] = hill_model
# extract params by mean
hill_model_params_mean, hill_model_params_med = {}, {}
for md in list(hill_models.keys()):
hill_model = hill_models[md]
params1 = extract_hill_model_params(hill_model, method='mean')
params1['sc'] = hill_model['sc']
hill_model_params_mean[md] = params1
# params2 = extract_hill_model_params(hill_model, method='median')
# params2['sc'] = hill_model['sc']
# hill_model_params_med[md] = params2
# save_json(hill_model_params_med, 'hill_model_params_med.json')
# save_json(hill_model_params_mean, 'hill_model_params_mean.json')
# evaluate model params extracted by mean
for md in list(hill_models.keys()):
print('evaluating media: ', md)
hill_model = hill_models[md]
hill_model_params = hill_model_params_mean[md]
_ = evaluate_hill_model(hill_model, hill_model_params)
# evaluate model params extracted by median
# for md in list(hill_models.keys()):
# print('evaluating media: ', md)
# hill_model = hill_models[md]
# hill_model_params = hill_model_params_med[md]
# _ = evaluate_hill_model(hill_model, hill_model_params)
# plot ec distribution
f = plt.figure(figsize=(18,12))
hm_keys = list(hill_models.keys())
for i in range(len(hm_keys)):
ax = f.add_subplot(4,3,i+1)
md = hm_keys[i]
x = hill_models[md]['ec_list']
mean_x = np.mean(x)
median_x = np.median(x)
ax = sns.distplot(x)
ax.axvline(mean_x, color='r', linestyle='-', alpha=0.5)
ax.axvline(median_x, color='g', linestyle='-', alpha=0.5)
ax.set_title(md)
# plot slope distribution
f = plt.figure(figsize=(18,12))
hm_keys = list(hill_models.keys())
for i in range(len(hm_keys)):
ax = f.add_subplot(4,3,i+1)
md = hm_keys[i]
x = hill_models[md]['slope_list']
mean_x = np.mean(x)
median_x = np.median(x)
ax = sns.distplot(x)
ax.axvline(mean_x, color='r', linestyle='-', alpha=0.5)
ax.axvline(median_x, color='g', linestyle='-', alpha=0.5)
ax.set_title(md)
# plot fitted hill function
f = plt.figure(figsize=(18,16))
hm_keys = list(hill_models.keys())
for i in range(len(hm_keys)):
ax = f.add_subplot(4,3,i+1)
md = hm_keys[i]
hm = hill_models[md]
hmp = hill_model_params_mean[md]
x, y = hm['data']['X'], hm['data']['y']
#mu_x, mu_y = hm['sc']['x'], hm['sc']['y']
ec, slope = hmp['ec'], hmp['slope']
x_sorted = np.array(sorted(x))
y_fit = hill_model_predict(hmp, x_sorted)
ax = sns.scatterplot(x=x, y=y, alpha=0.2)
ax = sns.lineplot(x=x_sorted, y=y_fit, color='r',
label='ec=%.2f, slope=%.2f'%(ec, slope))
ax.set_title(md)
# Calculate overall ROAS and weekly ROAS
# - Overall ROAS = total contribution / total spending
# - Weekly ROAS = weekly contribution / weekly spending
# adstocked media spending
ms_df = pd.DataFrame()
for md in list(hill_models.keys()):
hill_model = hill_models[md]
x = np.array(hill_model['data']['X']) * hill_model['sc']['x']
ms_df['mdsp_'+md] = x
# ms_df.to_csv('ms_df1.csv', index=False)
# calc overall ROAS of a given period
def calc_roas(mc_df, ms_df, period=None):
roas = {}
md_names = [col.split('_')[-1] for col in ms_df.columns]
for i in range(len(md_names)):
md = md_names[i]
sp, mc = ms_df['mdsp_'+md], mc_df['mdip_'+md]
if period is None:
md_roas = mc.sum()/sp.sum()
else:
md_roas = mc[-period:].sum()/sp[-period:].sum()
roas[md] = md_roas
return roas
# calc weekly ROAS
def calc_weekly_roas(mc_df, ms_df):
weekly_roas = pd.DataFrame()
md_names = [col.split('_')[-1] for col in ms_df.columns]
for md in md_names:
weekly_roas[md] = mc_df['mdip_'+md]/ms_df['mdsp_'+md]
weekly_roas.replace([np.inf, -np.inf, np.nan], 0, inplace=True)
return weekly_roas
roas_1y = calc_roas(mc_df, ms_df, period=52)
weekly_roas = calc_weekly_roas(mc_df, ms_df)
roas1y_df = pd.DataFrame(index=weekly_roas.columns.tolist())
roas1y_df['roas_mean'] = weekly_roas[-52:].apply(np.mean, axis=0)
roas1y_df['roas_median'] = weekly_roas[-52:].apply(np.median, axis=0)
# # plot weekly ROAS distribution
# # median: green line, mean: red line
# f = plt.figure(figsize=(18,12))
# for i in range(len(weekly_roas.columns)):
# md = weekly_roas.columns[i]
# ax = f.add_subplot(4,3,i+1)
# x = weekly_roas[md]
# mean_x = np.mean(x)
# median_x = np.median(x)
# ax = sns.distplot(x)
# ax.axvline(mean_x, color='r', linestyle='-', alpha=0.5)
# ax.axvline(median_x, color='g', linestyle='-', alpha=0.5)
# ax.set(xlabel=None)
# ax.set_title(md)
# plot weekly ROAS distribution of past 1 year
# median: green line, mean: red line
f = plt.figure(figsize=(18,12))
for i in range(len(weekly_roas.columns)):
md = weekly_roas.columns[i]
ax = f.add_subplot(4,3,i+1)
x = weekly_roas[md][-52:]
mean_x = np.mean(x)
median_x = np.median(x)
ax = sns.distplot(x)
ax.axvline(mean_x, color='r', linestyle='-', alpha=0.5)
ax.axvline(median_x, color='g', linestyle='-', alpha=0.5)
ax.set(xlabel=None)
ax.set_title(md)
# Calculate mROAS
# 1. Current spending level (cur_sp) is represented by mean or median of weekly spending.
# Next spending level (next_sp) is increasing cur_sp by 1%.
# 2. Plug cur_sp and next_sp into the Hill function:
# Current media contribution: cur_mc = Hill(cur_sp)
# Next-level media contribution next_mc = Hill(next_sp)
# 3. mROAS = (next_mc - cur_mc) / (0.01 * cur_sp)
def calc_mroas(hill_model, hill_model_params, period=52):
'''
calculate mROAS for a media
params:
hill_model: a dict containing model data and scaling factor
hill_model_params: a dict containing beta_hill, ec, slope
period: in weeks, the period used to calculate ROAS and mROAS. 52 is last one year.
return:
mROAS value
'''
mu_x, mu_y = hill_model['sc']['x'], hill_model['sc']['y']
# get current media spending level over the period specified
cur_sp = np.asarray(hill_model['data']['X'])
if period is not None:
cur_sp = cur_sp[-period:]
cur_mc = sum(hill_model_predict(hill_model_params, cur_sp) * mu_y)
# next spending level: increase by 1%
next_sp = cur_sp * 1.01
# media contribution under next spending level
next_mc = sum(hill_model_predict(hill_model_params, next_sp) * mu_y)
# mROAS
delta_mc = next_mc - cur_mc
delta_sp = sum(next_sp * mu_x) - sum(cur_sp * mu_x)
mroas = delta_mc/delta_sp
return mroas
# calc mROAS of recent 1 year
mroas_1y = {}
for md in list(hill_models.keys()):
hill_model = hill_models[md]
hill_model_params = hill_model_params_mean[md]
mroas_1y[md] = calc_mroas(hill_model, hill_model_params, period=52)
roas1y_df = pd.concat([
roas1y_df[['roas_mean', 'roas_median']],
pd.DataFrame.from_dict(mroas_1y, orient='index', columns=['mroas']),
pd.DataFrame.from_dict(roas_1y, orient='index', columns=['roas_avg'])
], axis=1)
# roas1y_df.to_csv('roas1y_df1.csv')
roas1y_df
# **ROAS & mROAS**
# 'roas_avg': overall ROAS = total contribution / total spending
# 'roas_mean': mean of weekly ROAS
# 'roas_median': median of weekly ROAS
# 'mroas': mROAS calculated based on increasing current spending level by 1%