-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathtransformers.py
1129 lines (941 loc) · 38 KB
/
transformers.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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2022 - 2025 The PyMC Labs Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Media transformation functions for Marketing Mix Models."""
from enum import Enum
from typing import Any, NamedTuple
import numpy as np
import numpy.typing as npt
import pymc as pm
import pytensor.tensor as pt
from pymc.distributions.dist_math import check_parameters
from pytensor.tensor.random.utils import params_broadcast_shapes
class ConvMode(str, Enum):
"""Convolution mode for the convolution."""
# TODO: use StrEnum when we upgrade to python 3.11
After = "After"
Before = "Before"
Overlap = "Overlap"
class WeibullType(str, Enum):
"""Weibull type for the Weibull adstock."""
# TODO: use StrEnum when we upgrade to python 3.11
PDF = "PDF"
CDF = "CDF"
def batched_convolution(
x,
w,
axis: int = 0,
mode: ConvMode | str = ConvMode.After,
):
R"""Apply a 1D convolution in a vectorized way across multiple batch dimensions.
.. plot::
:context: close-figs
import matplotlib.pyplot as plt
import numpy as np
import arviz as az
from pymc_marketing.mmm.transformers import batched_convolution, ConvMode
plt.style.use('arviz-darkgrid')
spends = np.array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0])
w = np.array([0.75, 0.25, 0.125, 0.125])
x = np.arange(-5, 6)
ax = plt.subplot(111)
for mode in [ConvMode.Before, ConvMode.Overlap, ConvMode.After]:
y = batched_convolution(spends, w, mode=mode).eval()
suffix = "\n(default)" if mode == ConvMode.After else ""
plt.plot(x, y, label=f'{mode.value}{suffix}')
plt.xlabel('time since spend', fontsize=12)
plt.ylabel('f(time since spend)', fontsize=12)
plt.title(f"1 spend at time 0 and {w = }", fontsize=14)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
Parameters
----------
x : tensor_like
The array to convolve.
w : tensor_like
The weight of the convolution. The last axis of ``w`` determines the number of steps
to use in the convolution.
axis : int
The axis of ``x`` along witch to apply the convolution
mode : ConvMode, optional
The convolution mode determines how the convolution is applied at the boundaries
of the input signal, denoted as "x." The default mode is ConvMode.After.
- ConvMode.After: Applies the convolution with the "Adstock" effect, resulting in a trailing decay effect.
- ConvMode.Before: Applies the convolution with the "Excitement" effect, creating a leading effect
similar to the wow factor.
- ConvMode.Overlap: Applies the convolution with both "Pull-Forward" and "Pull-Backward" effects,
where the effect overlaps with both preceding and succeeding elements.
Returns
-------
y : tensor_like
The result of convolving ``x`` with ``w`` along the desired axis. The shape of the
result will match the shape of ``x`` up to broadcasting with ``w``. The convolved
axis will show the results of left padding zeros to ``x`` while applying the
convolutions.
"""
# We move the axis to the last dimension of the array so that it's easier to
# reason about parameter broadcasting. We will move the axis back at the end
orig_ndim = x.ndim
axis = axis if axis >= 0 else orig_ndim + axis
w = pt.as_tensor(w)
x = pt.moveaxis(x, axis, -1)
l_max = w.type.shape[-1]
if l_max is None:
try:
l_max = w.shape[-1].eval()
except Exception: # noqa: S110
pass
# Get the broadcast shapes of x and w but ignoring their last dimension.
# The last dimension of x is the "time" axis, which doesn't get broadcast
# The last dimension of w is the number of time steps that go into the convolution
x_shape, w_shape = params_broadcast_shapes([x.shape, w.shape], [1, 1])
x = pt.broadcast_to(x, x_shape)
w = pt.broadcast_to(w, w_shape)
x_time = x.shape[-1]
# Make a tensor with x at the different time lags needed for the convolution
x_shape = x.shape
# Add the size of the kernel to the time axis
shape = (*x_shape[:-1], x_shape[-1] + w.shape[-1] - 1, w.shape[-1])
padded_x = pt.zeros(shape, dtype=x.dtype)
if l_max is None: # pragma: no cover
raise NotImplementedError(
"At the moment, convolving with weight arrays that don't have a concrete shape "
"at compile time is not supported."
)
# The window is the slice of the padded array that corresponds to the original x
if l_max <= 1:
window = slice(None)
elif mode == ConvMode.Before:
window = slice(l_max - 1, None)
elif mode == ConvMode.After:
window = slice(None, -l_max + 1)
elif mode == ConvMode.Overlap:
# Handle even and odd l_max differently if l_max is odd then we can split evenly otherwise we drop from the end
window = slice((l_max // 2) - (1 if l_max % 2 == 0 else 0), -(l_max // 2))
else:
raise ValueError(f"Wrong Mode: {mode}, expected of ConvMode")
for i in range(l_max):
padded_x = pt.set_subtensor(padded_x[..., i : x_time + i, i], x)
padded_x = padded_x[..., window, :]
# The convolution is treated as an element-wise product, that then gets reduced
# along the dimension that represents the convolution time lags
conv = pt.sum(padded_x * w[..., None, :], axis=-1)
# Move the "time" axis back to where it was in the original x array
return pt.moveaxis(conv, -1, axis + conv.ndim - orig_ndim)
def geometric_adstock(
x,
alpha: float = 0.0,
l_max: int = 12,
normalize: bool = False,
axis: int = 0,
mode: ConvMode = ConvMode.After,
):
R"""Geometric adstock transformation.
Adstock with geometric decay assumes advertising effect peaks at the same
time period as ad exposure. The cumulative media effect is a weighted average
of media spend in the current time-period (e.g. week) and previous `l_max` - 1
periods (e.g. weeks). `l_max` is the maximum duration of carryover effect.
.. plot::
:context: close-figs
import matplotlib.pyplot as plt
import numpy as np
import arviz as az
from pymc_marketing.mmm.transformers import geometric_adstock
plt.style.use('arviz-darkgrid')
l_max = 12
params = [
(0.01, False),
(0.5, False),
(0.9, False),
(0.5, True),
(0.9, True),
]
spend = np.zeros(15)
spend[0] = 1
ax = plt.subplot(111)
x = np.arange(len(spend))
for a, normalize in params:
y = geometric_adstock(spend, alpha=a, l_max=l_max, normalize=normalize).eval()
plt.plot(x, y, label=f'alpha = {a}\nnormalize = {normalize}')
plt.xlabel('time since spend', fontsize=12)
plt.title(f'Geometric Adstock with l_max = {l_max}', fontsize=14)
plt.ylabel('f(time since spend)', fontsize=12)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.65, box.height])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
Parameters
----------
x : tensor
Input tensor.
alpha : float, by default 0.0
Retention rate of ad effect. Must be between 0 and 1.
l_max : int, by default 12
Maximum duration of carryover effect.
normalize : bool, by default False
Whether to normalize the weights.
axis : int
The axis of ``x`` along witch to apply the convolution
mode : ConvMode, optional
The convolution mode determines how the convolution is applied at the boundaries
of the input signal, denoted as "x." The default mode is ConvMode.After.
- ConvMode.After: Applies the convolution with the "Adstock" effect, resulting in a trailing decay effect.
- ConvMode.Before: Applies the convolution with the "Excitement" effect, creating a leading effect
similar to the wow factor.
- ConvMode.Overlap: Applies the convolution with both "Pull-Forward" and "Pull-Backward" effects,
where the effect overlaps with both preceding and succeeding elements.
Returns
-------
tensor
Transformed tensor.
References
----------
.. [1] Jin, Yuxue, et al. "Bayesian methods for media mix modeling
with carryover and shape effects." (2017).
"""
alpha = check_parameters(
alpha, [pt.ge(alpha, 0), pt.le(alpha, 1)], msg="0 <= alpha <= 1"
)
w = pt.power(pt.as_tensor(alpha)[..., None], pt.arange(l_max, dtype=x.dtype))
w = w / pt.sum(w, axis=-1, keepdims=True) if normalize else w
return batched_convolution(x, w, axis=axis, mode=mode)
def delayed_adstock(
x,
alpha: float = 0.0,
theta: int = 0,
l_max: int = 12,
normalize: bool = False,
axis: int = 0,
mode: ConvMode = ConvMode.After,
):
R"""Delayed adstock transformation.
This transformation is similar to geometric adstock transformation, but it
allows for a delayed peak of the effect. The peak is assumed to occur at `theta`.
.. plot::
:context: close-figs
import matplotlib.pyplot as plt
import numpy as np
import arviz as az
from pymc_marketing.mmm.transformers import delayed_adstock
plt.style.use('arviz-darkgrid')
params = [
(0.25, 0, False),
(0.25, 5, False),
(0.75, 5, False),
(0.75, 5, True)
]
spend = np.zeros(15)
spend[0] = 1
x = np.arange(len(spend))
ax = plt.subplot(111)
for a, t, normalize in params:
y = delayed_adstock(spend, alpha=a, theta=t, normalize=normalize).eval()
plt.plot(x, y, label=f'alpha = {a}\ntheta = {t}\nnormalize = {normalize}')
plt.xlabel('time since spend', fontsize=12)
plt.ylabel('f(time since spend)', fontsize=12)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.65, box.height])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
Parameters
----------
x : tensor
Input tensor.
alpha : float, by default 0.0
Retention rate of ad effect. Must be between 0 and 1.
theta : float, by default 0
Delay of the peak effect. Must be between 0 and `l_max` - 1.
l_max : int, by default 12
Maximum duration of carryover effect.
normalize : bool, by default False
Whether to normalize the weights.
axis : int
The axis of ``x`` along witch to apply the convolution
mode : ConvMode, optional
The convolution mode determines how the convolution is applied at the boundaries
of the input signal, denoted as "x." The default mode is ConvMode.After.
- ConvMode.After: Applies the convolution with the "Adstock" effect, resulting in a trailing decay effect.
- ConvMode.Before: Applies the convolution with the "Excitement" effect, creating a leading effect
similar to the wow factor.
- ConvMode.Overlap: Applies the convolution with both "Pull-Forward" and "Pull-Backward" effects,
where the effect overlaps with both preceding and succeeding elements.
Returns
-------
tensor
Transformed tensor.
References
----------
.. [1] Jin, Yuxue, et al. "Bayesian methods for media mix modeling
with carryover and shape effects." (2017).
"""
w = pt.power(
pt.as_tensor(alpha)[..., None],
(pt.arange(l_max, dtype=x.dtype) - pt.as_tensor(theta)[..., None]) ** 2,
)
w = w / pt.sum(w, axis=-1, keepdims=True) if normalize else w
return batched_convolution(x, w, axis=axis, mode=mode)
def weibull_adstock(
x,
lam=1,
k=1,
l_max: int = 12,
axis: int = 0,
mode: ConvMode = ConvMode.After,
type: WeibullType | str = WeibullType.PDF,
normalize: bool = False,
):
R"""Weibull Adstocking Transformation.
This transformation is similar to geometric adstock transformation but has more
degrees of freedom, adding more flexibility.
.. plot::
:context: close-figs
import matplotlib.pyplot as plt
import numpy as np
import arviz as az
from pymc_marketing.mmm.transformers import WeibullType, weibull_adstock
plt.style.use('arviz-darkgrid')
spend = np.zeros(50)
spend[0] = 1
shapes = [0.5, 1., 1.5, 5.]
scales = [10, 20, 40]
modes = [WeibullType.PDF, WeibullType.CDF]
fig, axes = plt.subplots(
len(shapes), len(modes), figsize=(12, 8), sharex=True, sharey=True
)
fig.suptitle("Effect of Changing Weibull Adstock Parameters", fontsize=16)
for m, mode in enumerate(modes):
axes[0, m].set_title(f"Mode: {mode.value}")
for i, shape in enumerate(shapes):
for j, scale in enumerate(scales):
adstock = weibull_adstock(
spend, lam=scale, k=shape, type=mode, l_max=len(spend)
).eval()
axes[i, m].plot(
np.arange(len(spend)),
adstock,
label=f"Scale={scale}",
linestyle="-",
)
fig.legend(
*axes[0, 0].get_legend_handles_labels(),
loc="center right",
bbox_to_anchor=(1.2, 0.85),
)
plt.tight_layout(rect=[0, 0, 0.9, 1])
plt.show()
Parameters
----------
x : tensor
Input tensor.
lam : float, by default 1.
Scale parameter of the Weibull distribution. Must be positive.
k : float, by default 1.
Shape parameter of the Weibull distribution. Must be positive.
l_max : int, by default 12
Maximum duration of carryover effect.
axis : int
The axis of ``x`` along witch to apply the convolution
mode : ConvMode, optional
The convolution mode determines how the convolution is applied at the boundaries
of the input signal, denoted as "x." The default mode is ConvMode.After.
- ConvMode.After: Applies the convolution with the "Adstock" effect, resulting in a trailing decay effect.
- ConvMode.Before: Applies the convolution with the "Excitement" effect, creating a leading effect
similar to the wow factor.
- ConvMode.Overlap: Applies the convolution with both "Pull-Forward" and "Pull-Backward" effects,
where the effect overlaps with both preceding and succeeding elements.
type : WeibullType or str, by default WeibullType.PDF
Type of Weibull adstock transformation to be applied (PDF or CDF).
normalize : bool, by default False
Whether to normalize the weights.
Returns
-------
tensor
Transformed tensor based on Weibull adstock transformation.
"""
lam = pt.as_tensor(lam)[..., None]
k = pt.as_tensor(k)[..., None]
t = pt.arange(l_max, dtype=x.dtype) + 1
if type == WeibullType.PDF:
w = pt.exp(pm.Weibull.logp(t, k, lam))
w = (w - pt.min(w, axis=-1)[..., None]) / (
pt.max(w, axis=-1)[..., None] - pt.min(w, axis=-1)[..., None]
)
elif type == WeibullType.CDF:
w = 1 - pt.exp(pm.Weibull.logcdf(t, k, lam))
shape = (*w.shape[:-1], w.shape[-1] + 1)
padded_w = pt.ones(shape, dtype=w.dtype)
padded_w = pt.set_subtensor(padded_w[..., 1:], w)
w = pt.cumprod(padded_w, axis=-1)
else:
raise ValueError(f"Wrong WeibullType: {type}, expected of WeibullType")
w = w / pt.sum(w, axis=-1, keepdims=True) if normalize else w
return batched_convolution(x, w, axis=axis, mode=mode)
def logistic_saturation(x, lam: npt.NDArray | float = 0.5):
r"""Logistic saturation transformation.
.. math::
f(x) = \frac{1 - e^{-\lambda x}}{1 + e^{-\lambda x}}
.. plot::
:context: close-figs
import matplotlib.pyplot as plt
import numpy as np
import arviz as az
from pymc_marketing.mmm.transformers import logistic_saturation
plt.style.use('arviz-darkgrid')
lam = np.array([0.25, 0.5, 1, 2, 4])
x = np.linspace(0, 5, 100)
ax = plt.subplot(111)
for l in lam:
y = logistic_saturation(x, lam=l).eval()
plt.plot(x, y, label=f'lam = {l}')
plt.xlabel('spend', fontsize=12)
plt.ylabel('f(spend)', fontsize=12)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
Parameters
----------
x : tensor
Input tensor.
lam : float or array-like, optional, by default 0.5
Saturation parameter.
Returns
-------
tensor
Transformed tensor.
"""
return (1 - pt.exp(-lam * x)) / (1 + pt.exp(-lam * x))
def inverse_scaled_logistic_saturation(
x, lam: npt.NDArray | float = 0.5, eps: float = np.log(3)
):
r"""Inverse scaled logistic saturation transformation.
It offers a more intuitive alternative to logistic_saturation,
allowing for lambda to be interpreted as the half saturation point
when using default value for eps.
.. math::
f(x) = \\frac{1 - e^{-x*\epsilon/\lambda}}{1 + e^{-x*\epsilon/\lambda}}
.. plot::
:context: close-figs
import matplotlib.pyplot as plt
import numpy as np
import arviz as az
from pymc_marketing.mmm.transformers import inverse_scaled_logistic_saturation
plt.style.use('arviz-darkgrid')
lam = np.array([0.25, 0.5, 1, 2, 4])
x = np.linspace(0, 5, 100)
ax = plt.subplot(111)
for l in lam:
y = inverse_scaled_logistic_saturation(x, lam=l).eval()
plt.plot(x, y, label=f'lam = {l}')
plt.xlabel('spend', fontsize=12)
plt.ylabel('f(spend)', fontsize=12)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
Parameters
----------
x : tensor
Input tensor.
lam : float or array-like, optional, by default 0.5
Saturation parameter.
eps : float or array-like, optional, by default ln(3)
Scaling parameter. ln(3) results in halfway saturation at lam
Returns
-------
tensor
Transformed tensor.
"""
return logistic_saturation(x, eps / lam)
class TanhSaturationParameters(NamedTuple):
"""Container for tanh saturation parameters.
Parameters
----------
b : pt.TensorLike
Saturation
c : pt.TensorLike
Customer Aquisition Cost at 0.
"""
b: pt.TensorLike
c: pt.TensorLike
def baseline(self, x0: pt.TensorLike) -> "TanhSaturationBaselinedParameters":
"""Change the parameterization to baselined at :math:`x_0`.
Parameters
----------
x0 : pt.TensorLike
Baseline spend.
Returns
-------
TanhSaturationBaselinedParameters
Baselined parameters.
"""
y_ref = tanh_saturation(x0, self.b, self.c)
gain_ref = y_ref / x0
r_ref = y_ref / self.b
return TanhSaturationBaselinedParameters(x0, gain_ref, r_ref)
class TanhSaturationBaselinedParameters(NamedTuple):
"""Representation of tanh saturation parameters in baselined form.
Parameters
----------
x0 : pt.TensorLike
Baseline spend.
gain : pt.TensorLike
ROAS at :math:`x_0`.
r : pt.TensorLike
Overspend Fraction.
"""
x0: pt.TensorLike
gain: pt.TensorLike
r: pt.TensorLike
def debaseline(self) -> TanhSaturationParameters:
"""Change the parameterization to baselined to be classic saturation and cac.
Returns
-------
TanhSaturationParameters
Classic saturation and cac parameters.
"""
saturation = (self.gain * self.x0) / self.r
cac = self.r / (self.gain * pt.arctanh(self.r))
return TanhSaturationParameters(saturation, cac)
def rebaseline(self, x1: pt.TensorLike) -> "TanhSaturationBaselinedParameters":
"""Change the parameterization to baselined at :math:`x_1`."""
params = self.debaseline()
return params.baseline(x1)
def tanh_saturation(
x: pt.TensorLike,
b: pt.TensorLike = 0.5,
c: pt.TensorLike = 0.5,
) -> pt.TensorVariable:
R"""Tanh saturation transformation.
.. math::
f(x) = b \tanh \left( \frac{x}{bc} \right)
.. plot::
:context: close-figs
import matplotlib.pyplot as plt
import numpy as np
import arviz as az
from pymc_marketing.mmm.transformers import tanh_saturation
plt.style.use('arviz-darkgrid')
params = [
(0.75, 0.25),
(0.75, 1.5),
(1, 0.25),
(1, 1),
(1, 1.5),
]
x = np.linspace(0, 5, 100)
ax = plt.subplot(111)
for b, c in params:
y = tanh_saturation(x, b=b, c=c).eval()
plt.plot(x, y, label=f'b = {b}\nc = {c}')
plt.xlabel('spend', fontsize=12)
plt.ylabel('f(spend)', fontsize=12)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
# plt.legend()
plt.show()
Parameters
----------
x : tensor
Input tensor.
b : float, by default 0.5
Number of users at saturation. Must be non-negative.
c : float, by default 0.5
Initial cost per user. Must be non-zero.
Returns
-------
tensor
Transformed tensor.
References
----------
See https://www.pymc-labs.io/blog-posts/reducing-customer-acquisition-costs-how-we-helped-optimizing-hellofreshs-marketing-budget/ # noqa: E501
""" # noqa: E501
return b * pt.tanh(x / (b * c))
def tanh_saturation_baselined(
x: pt.TensorLike,
x0: pt.TensorLike,
gain: pt.TensorLike = 0.5,
r: pt.TensorLike = 0.5,
) -> pt.TensorVariable:
r"""Baselined Tanh Saturation.
This parameterization that is easier than :func:`tanh_saturation`
to use for industry applications where domain knowledge is an essence.
In a nutshell, it is an alternative parameterization of the reach function is given by:
.. math::
\begin{align}
c_0 &= \frac{r}{g \cdot \arctan(r)} \\
\beta &= \frac{g \cdot x_0}{r} \\
\operatorname{saturation}(x, \beta, c_0) &= \beta \cdot \tanh \left( \frac{x}{c_0 \cdot \beta} \right)
\end{align}
where:
- :math:`x_0` is the "reference point". This is a point chosen
by the user (not given a prior) where they expect most of their data to lie.
For example, if you're spending between 50 and 150 dollars on a particular channel,
you might choose :math:`x_0 = 100`.
Suggested value is median channel spend: ``np.median(spend)``.
- :math:`g` is the "gain", which is the value of the CAC (:math:`c_0`) at the reference point.
You have to set a prior on what you think the CAC is when you spend :math:`x_0 = 100`.
Imagine you have four advertising channels, and you acquired 1000 new users.
If each channel performed equally well, and advertising drove all sales, you might expect
that you gained 250 users from each channel. Here, your "gain" would be :math:`250 / 100 = 2.5`.
Suggested prior is ``pm.Exponential``
- :math:`r`, the overspend fraction is telling you where the reference point is.
- :math:`0` - we can increase our budget by a lot to reach the saturated region,
the diminishing returns are not visible yet.
- :math:`1` - the reference point is already in the saturation region
and additional dollar spend will not lead to any new users.
- :math:`0.8`, you can still increase acquired users by :math:`50\%` as much
you get in the reference point by increasing the budget.
:math:`x_0` effect is 20% away from saturation point
Suggested prior is ``pm.Beta``
.. note::
The reference point :math:`x_0` has to be set within the range of the actual spends.
As in, you buy ads three times and spend :math:`5`, :math:`6` and :math:`7` dollars,
:math:`x_0` has to be set within :math:`[5, 7]`, so not :math:`4` not :math:`8`.
Otherwise the posterior of r and gain becomes a skinny diagonal line.
It could be very relevant if there is very little spend observations for a particular channel.
The original reach or saturation function used in an MMM is formulated as
.. math::
\operatorname{saturation}(x, \beta, c_0) = \beta \cdot \tanh \left( \frac{x}{c_0 \cdot \beta} \right)
where:
- :math:`\beta` is the saturation, or the limit of the total number
of new users obtained when an infinite number of dollars are spent on that channel.
- :math:`c_0` is the cost per acquisition (CAC0), so the initial cost per new user.
- :math:`\frac{1}{c_0}` is the inverse of the CAC0, so it's the number of new
users we might expect after spending our first dollar.
.. plot::
:context: close-figs
import matplotlib.pyplot as plt
import numpy as np
import arviz as az
from pymc_marketing.mmm.transformers import (
tanh_saturation_baselined,
tanh_saturation,
TanhSaturationBaselinedParameters,
)
gain = 1
overspend_fraction = 0.7
x_baseline = 400
params = TanhSaturationBaselinedParameters(x_baseline, gain, overspend_fraction)
x = np.linspace(0, 1000)
y = tanh_saturation_baselined(x, *params).eval()
saturation, cac0 = params.debaseline()
cac0 = cac0.eval()
saturated_ref = tanh_saturation(x_baseline, saturation, cac0).eval()
plt.plot(x, y);
plt.axvline(x_baseline, linestyle="dashed", color="red", label="baseline")
plt.plot(x, x * gain, linestyle="dashed", label="gain (slope)");
plt.axhline(saturated_ref, linestyle="dashed", label="f(reference)")
plt.plot(x, x / cac0, linestyle="dotted", label="1/cac (slope)");
plt.axhline(saturation, linestyle="dotted", label="saturation")
plt.fill_between(x, saturated_ref, saturation, alpha=0.1, label="underspend fraction")
plt.fill_between(x, saturated_ref, alpha=0.1, label="overspend fraction")
plt.legend()
plt.show()
Examples
--------
.. code-block:: python
import pymc as pm
import numpy as np
x_in = np.exp(3+np.random.randn(100))
true_cac = 1
true_saturation = 100
y_out = abs(np.random.normal(tanh_saturation(x_in, true_saturation, true_cac).eval(), 0.1))
with pm.Model() as model_reparam:
r = pm.Uniform("r")
gain = pm.Exponential("gain", 1)
input = pm.ConstantData("spent", x_in)
response = pm.ConstantData("response", y_out)
sigma = pm.HalfNormal("n")
output = tanh_saturation_baselined(input, np.median(x_in), gain, r)
pm.Normal("output", output, sigma, observed=response)
trace = pm.sample()
Parameters
----------
x : tensor
Input tensor.
x0: tensor
Baseline for saturation.
gain : tensor, by default 0.5
ROAS at the baseline point, mathematically as :math:`gain = f(x0) / x0`.
r : tensor, by default 0.5
The overspend fraction, mathematically as :math:`r = f(x0) / \text{saturation}`.
Returns
-------
tensor
Transformed tensor.
References
----------
Developed by Max Kochurov and Aziz Al-Maeeni doing innovative work in `PyMC Labs <pymc-labs.com>`_.
"""
return gain * x0 * pt.tanh(x * pt.arctanh(r) / x0) / r
def michaelis_menten(
x: float | np.ndarray | npt.NDArray,
alpha: float | np.ndarray | npt.NDArray,
lam: float | np.ndarray | npt.NDArray,
) -> float | Any:
r"""Evaluate the Michaelis-Menten function for given values of x, alpha, and lambda.
The Michaelis-Menten function models enzyme kinetics and describes how the rate of
a chemical reaction increases with substrate concentration until it reaches its
maximum value.
.. math::
\alpha \cdot \frac{x}{\lambda + x}
where:
- :math:`x`: Channel spend or substrate concentration.
- :math:`\alpha`: Maximum contribution or efficiency factor.
- :math:`\lambda` (k): Michaelis constant, representing the threshold substrate concentration.
.. plot::
:context: close-figs
import numpy as np
import matplotlib.pyplot as plt
from pymc_marketing.mmm.transformers import michaelis_menten
x = np.linspace(0, 100, 500)
alpha = 10
lam = 50
y = michaelis_menten(x, alpha, lam)
plt.plot(x, y)
plt.xlabel('Spend/Impressions (x)')
plt.ylabel('Contribution (y)')
plt.title('Michaelis-Menten Function')
plt.show()
.. plot::
:context: close-figs
import numpy as np
import matplotlib.pyplot as plt
from pymc_marketing.mmm.transformers import michaelis_menten
x = np.linspace(0, 100, 500)
alpha_values = [5, 10, 15] # Different values of alpha
lam_values = [25, 50, 75] # Different values of lam
# Plot varying lam
plt.figure(figsize=(8, 6))
for lam in lam_values:
y = michaelis_menten(x, alpha_values[0], lam)
plt.plot(x, y, label=f"lam={lam}")
plt.xlabel('Spend/Impressions (x)')
plt.ylabel('Contribution (y)')
plt.title('Michaelis-Menten Function (Varying lam)')
plt.legend()
plt.show()
# Plot varying alpha
plt.figure(figsize=(8, 6))
for alpha in alpha_values:
y = michaelis_menten(x, alpha, lam_values[0])
plt.plot(x, y, label=f"alpha={alpha}")
plt.xlabel('Spend/Impressions (x)')
plt.ylabel('Contribution (y)')
plt.title('Michaelis-Menten Function (Varying alpha)')
plt.legend()
plt.show()
Parameters
----------
x : float
The spent on a channel.
alpha : float
The maximum contribution a channel can make.
lam : float
The Michaelis constant for the given enzyme-substrate system.
Returns
-------
float
The value of the Michaelis-Menten function given the parameters.
"""
return alpha * x / (lam + x)
def hill_function(
x: pt.TensorLike, slope: pt.TensorLike, kappa: pt.TensorLike
) -> pt.TensorVariable:
r"""Hill Function.
.. math::
f(x) = 1 - \frac{\kappa^s}{\kappa^s + x^s}
where:
- :math:`s` is the slope of the hill.
- :math:`\kappa` is the half-saturation point as :math:`f(\kappa) = 0.5` for any value of :math:`s` and :math:`\kappa`.
- :math:`x` is the independent variable and must be non-negative.
Hill function from Equation (5) in the paper [1]_.
.. plot::
:context: close-figs
import numpy as np
import matplotlib.pyplot as plt
from pymc_marketing.mmm.transformers import hill_function
x = np.linspace(0, 10, 100)
# Varying slope
slopes = [0.3, 0.7, 1.2]
fig, axes = plt.subplots(1, 3, figsize=(12, 4), sharey=True)
for i, slope in enumerate(slopes):
plt.subplot(1, 3, i+1)
y = hill_function(x, slope, 2).eval()
plt.plot(x, y)
plt.xlabel('x')
plt.title(f'Slope = {slope}')
plt.subplot(1,3,1)
plt.ylabel('Hill Saturation Sigmoid')
plt.tight_layout()
plt.show()
# Varying kappa
kappas = [1, 5, 10]
fig, axes = plt.subplots(1, 3, figsize=(12, 4), sharey=True)
for i, kappa in enumerate(kappas):
plt.subplot(1, 3, i+1)
y = hill_function(x, 1, kappa).eval()
plt.plot(x, y)
plt.xlabel('x')
plt.title(f'Kappa = {kappa}')
plt.subplot(1,3,1)
plt.ylabel('Hill Saturation Sigmoid')
plt.tight_layout()
plt.show()
Parameters
----------
x : float or array-like
The independent variable, typically representing the concentration of a
substrate or the intensity of a stimulus.
slope : float
The slope of the hill. Must pe non-positive.
kappa : float
The half-saturation point as :math:`f(\kappa) = 0.5` for any value of :math:`s` and :math:`\kappa`.
Returns
-------
float
The value of the Hill function given the parameters.
References
----------
.. [1] Jin, Yuxue, et al. “Bayesian methods for media mix modeling with carryover and shape effects.” (2017).
""" # noqa: E501
return pt.as_tensor_variable(
1 - pt.power(kappa, slope) / (pt.power(kappa, slope) + pt.power(x, slope))
)
def hill_saturation_sigmoid(
x: pt.TensorLike,
sigma: pt.TensorLike,
beta: pt.TensorLike,
lam: pt.TensorLike,
) -> pt.TensorVariable:
r"""Hill Saturation Sigmoid Function.
.. math::
f(x) = \frac{\sigma}{1 + e^{-\beta(x - \lambda)}} - \frac{\sigma}{1 + e^{\beta\lambda}}