-
Notifications
You must be signed in to change notification settings - Fork 99
/
metrics.py
899 lines (643 loc) · 37.1 KB
/
metrics.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
import numpy as np
import pandas as pd
from sklearn.metrics import auc
from sklearn.utils.extmath import stable_cumsum
from sklearn.utils.validation import check_consistent_length
from sklearn.metrics import make_scorer
from ..utils import check_is_binary
def make_uplift_scorer(metric_name, treatment, **kwargs):
"""Make uplift scorer which can be used with the same API as ``sklearn.metrics.make_scorer``.
Args:
metric_name (string): Name of desirable uplift metric. Raise ValueError if invalid.
treatment (pandas.Series): A Series from original DataFrame which
contains original index and treatment group column.
kwargs (additional arguments): Additional parameters to be passed to metric func.
For example: `negative_effect`, `strategy`, `k` or somtething else.
Returns:
scorer (callable): An uplift scorer with passed treatment variable (and kwargs, optionally) that returns a scalar score.
Raises:
ValueError: if `metric_name` does not present in metrics list.
ValueError: if `treatment` is not a pandas Series.
Example::
from sklearn.model_selection import cross_validate
from sklift.metrics import make_uplift_scorer
# define X_cv, y_cv, trmnt_cv and estimator
# Use make_uplift_scorer to initialize new `sklearn.metrics.make_scorer` object
qini_scorer = make_uplift_scorer("qini_auc_score", trmnt_cv)
# or pass additional parameters if necessary
uplift50_scorer = make_uplift_scorer("uplift_at_k", trmnt_cv, strategy='overall', k=0.5)
# Use this object in model selection functions
cross_validate(estimator,
X=X_cv,
y=y_cv,
fit_params={'treatment': trmnt_cv}
scoring=qini_scorer,
)
"""
metrics_dict = {
'uplift_auc_score': uplift_auc_score,
'qini_auc_score': qini_auc_score,
'uplift_at_k': uplift_at_k,
'weighted_average_uplift': weighted_average_uplift,
}
if metric_name not in metrics_dict.keys():
raise ValueError(
f"'{metric_name}' is not a valid scoring value. "
f"List of valid metrics: {list(metrics_dict.keys())}"
)
if not isinstance(treatment, pd.Series):
raise TypeError("Expected pandas.Series in treatment vector, got %s" % type(treatment))
def scorer(y_true, uplift, treatment_value, **kwargs):
t = treatment_value.loc[y_true.index]
return metrics_dict[metric_name](y_true, uplift, t, **kwargs)
return make_scorer(scorer, treatment_value=treatment, **kwargs)
def uplift_curve(y_true, uplift, treatment):
"""Compute Uplift curve.
For computing the area under the Uplift Curve, see :func:`.uplift_auc_score`.
Args:
y_true (1d array-like): Correct (true) binary target values.
uplift (1d array-like): Predicted uplift, as returned by a model.
treatment (1d array-like): Treatment labels.
Returns:
array (shape = [>2]), array (shape = [>2]): Points on a curve.
See also:
:func:`.uplift_auc_score`: Compute normalized Area Under the Uplift curve from prediction scores.
:func:`.perfect_uplift_curve`: Compute the perfect Uplift curve.
:func:`.plot_uplift_curve`: Plot Uplift curves from predictions.
:func:`.qini_curve`: Compute Qini curve.
References:
Devriendt, F., Guns, T., & Verbeke, W. (2020). Learning to rank for uplift modeling. ArXiv, abs/2002.05897.
"""
check_consistent_length(y_true, uplift, treatment)
check_is_binary(treatment)
check_is_binary(y_true)
y_true, uplift, treatment = np.array(y_true), np.array(uplift), np.array(treatment)
desc_score_indices = np.argsort(uplift, kind="mergesort")[::-1]
y_true, uplift, treatment = y_true[desc_score_indices], uplift[desc_score_indices], treatment[desc_score_indices]
y_true_ctrl, y_true_trmnt = y_true.copy(), y_true.copy()
y_true_ctrl[treatment == 1] = 0
y_true_trmnt[treatment == 0] = 0
distinct_value_indices = np.where(np.diff(uplift))[0]
threshold_indices = np.r_[distinct_value_indices, uplift.size - 1]
num_trmnt = stable_cumsum(treatment)[threshold_indices]
y_trmnt = stable_cumsum(y_true_trmnt)[threshold_indices]
num_all = threshold_indices + 1
num_ctrl = num_all - num_trmnt
y_ctrl = stable_cumsum(y_true_ctrl)[threshold_indices]
curve_values = (np.divide(y_trmnt, num_trmnt, out=np.zeros_like(y_trmnt), where=num_trmnt != 0) -
np.divide(y_ctrl, num_ctrl, out=np.zeros_like(y_ctrl), where=num_ctrl != 0)) * num_all
if num_all.size == 0 or curve_values[0] != 0 or num_all[0] != 0:
# Add an extra threshold position if necessary
# to make sure that the curve starts at (0, 0)
num_all = np.r_[0, num_all]
curve_values = np.r_[0, curve_values]
return num_all, curve_values
def perfect_uplift_curve(y_true, treatment):
"""Compute the perfect (optimum) Uplift curve.
This is a function, given points on a curve. For computing the
area under the Uplift Curve, see :func:`.uplift_auc_score`.
Args:
y_true (1d array-like): Correct (true) binary target values.
treatment (1d array-like): Treatment labels.
Returns:
array (shape = [>2]), array (shape = [>2]): Points on a curve.
See also:
:func:`.uplift_curve`: Compute the area under the Qini curve.
:func:`.uplift_auc_score`: Compute normalized Area Under the Uplift curve from prediction scores.
:func:`.plot_uplift_curve`: Plot Uplift curves from predictions.
"""
check_consistent_length(y_true, treatment)
check_is_binary(treatment)
check_is_binary(y_true)
y_true, treatment = np.array(y_true), np.array(treatment)
cr_num = np.sum((y_true == 1) & (treatment == 0)) # Control Responders
tn_num = np.sum((y_true == 0) & (treatment == 1)) # Treated Non-Responders
# express an ideal uplift curve through y_true and treatment
summand = y_true if cr_num > tn_num else treatment
perfect_uplift = 2 * (y_true == treatment) + summand
return uplift_curve(y_true, perfect_uplift, treatment)
def uplift_auc_score(y_true, uplift, treatment):
"""Compute normalized Area Under the Uplift Curve from prediction scores.
By computing the area under the Uplift curve, the curve information is summarized in one number.
For binary outcomes the ratio of the actual uplift gains curve above the diagonal to that of
the optimum Uplift Curve.
Args:
y_true (1d array-like): Correct (true) binary target values.
uplift (1d array-like): Predicted uplift, as returned by a model.
treatment (1d array-like): Treatment labels.
Returns:
float: Area Under the Uplift Curve.
See also:
:func:`.uplift_curve`: Compute Uplift curve.
:func:`.perfect_uplift_curve`: Compute the perfect (optimum) Uplift curve.
:func:`.plot_uplift_curve`: Plot Uplift curves from predictions.
:func:`.qini_auc_score`: Compute normalized Area Under the Qini Curve from prediction scores.
"""
check_consistent_length(y_true, uplift, treatment)
check_is_binary(treatment)
check_is_binary(y_true)
y_true, uplift, treatment = np.array(y_true), np.array(uplift), np.array(treatment)
x_actual, y_actual = uplift_curve(y_true, uplift, treatment)
x_perfect, y_perfect = perfect_uplift_curve(y_true, treatment)
x_baseline, y_baseline = np.array([0, x_perfect[-1]]), np.array([0, y_perfect[-1]])
auc_score_baseline = auc(x_baseline, y_baseline)
auc_score_perfect = auc(x_perfect, y_perfect) - auc_score_baseline
auc_score_actual = auc(x_actual, y_actual) - auc_score_baseline
return auc_score_actual / auc_score_perfect
def qini_curve(y_true, uplift, treatment):
"""Compute Qini curve.
For computing the area under the Qini Curve, see :func:`.qini_auc_score`.
Args:
y_true (1d array-like): Correct (true) binary target values.
uplift (1d array-like): Predicted uplift, as returned by a model.
treatment (1d array-like): Treatment labels.
Returns:
array (shape = [>2]), array (shape = [>2]): Points on a curve.
See also:
:func:`.uplift_curve`: Compute the area under the Qini curve.
:func:`.perfect_qini_curve`: Compute the perfect Qini curve.
:func:`.plot_qini_curves`: Plot Qini curves from predictions..
:func:`.uplift_curve`: Compute Uplift curve.
References:
Nicholas J Radcliffe. (2007). Using control groups to target on predicted lift:
Building and assessing uplift model. Direct Marketing Analytics Journal, (3):14–21, 2007.
Devriendt, F., Guns, T., & Verbeke, W. (2020). Learning to rank for uplift modeling. ArXiv, abs/2002.05897.
"""
check_consistent_length(y_true, uplift, treatment)
check_is_binary(treatment)
check_is_binary(y_true)
y_true, uplift, treatment = np.array(y_true), np.array(uplift), np.array(treatment)
desc_score_indices = np.argsort(uplift, kind="mergesort")[::-1]
y_true = y_true[desc_score_indices]
treatment = treatment[desc_score_indices]
uplift = uplift[desc_score_indices]
y_true_ctrl, y_true_trmnt = y_true.copy(), y_true.copy()
y_true_ctrl[treatment == 1] = 0
y_true_trmnt[treatment == 0] = 0
distinct_value_indices = np.where(np.diff(uplift))[0]
threshold_indices = np.r_[distinct_value_indices, uplift.size - 1]
num_trmnt = stable_cumsum(treatment)[threshold_indices]
y_trmnt = stable_cumsum(y_true_trmnt)[threshold_indices]
num_all = threshold_indices + 1
num_ctrl = num_all - num_trmnt
y_ctrl = stable_cumsum(y_true_ctrl)[threshold_indices]
curve_values = y_trmnt - y_ctrl * np.divide(num_trmnt, num_ctrl, out=np.zeros_like(num_trmnt), where=num_ctrl != 0)
if num_all.size == 0 or curve_values[0] != 0 or num_all[0] != 0:
# Add an extra threshold position if necessary
# to make sure that the curve starts at (0, 0)
num_all = np.r_[0, num_all]
curve_values = np.r_[0, curve_values]
return num_all, curve_values
def perfect_qini_curve(y_true, treatment, negative_effect=True):
"""Compute the perfect (optimum) Qini curve.
For computing the area under the Qini Curve, see :func:`.qini_auc_score`.
Args:
y_true (1d array-like): Correct (true) binary target values.
treatment (1d array-like): Treatment labels.
negative_effect (bool): If True, optimum Qini Curve contains the negative effects
(negative uplift because of campaign). Otherwise, optimum Qini Curve will not
contain the negative effects.
Returns:
array (shape = [>2]), array (shape = [>2]): Points on a curve.
See also:
:func:`.qini_curve`: Compute Qini curve.
:func:`.qini_auc_score`: Compute the area under the Qini curve.
:func:`.plot_qini_curves`: Plot Qini curves from predictions..
"""
check_consistent_length(y_true, treatment)
check_is_binary(treatment)
check_is_binary(y_true)
n_samples = len(y_true)
y_true, treatment = np.array(y_true), np.array(treatment)
if not isinstance(negative_effect, bool):
raise TypeError(f'Negative_effects flag should be bool, got: {type(negative_effect)}')
# express an ideal uplift curve through y_true and treatment
if negative_effect:
x_perfect, y_perfect = qini_curve(
y_true, y_true * treatment - y_true * (1 - treatment), treatment
)
else:
ratio_random = (y_true[treatment == 1].sum() - len(y_true[treatment == 1]) *
y_true[treatment == 0].sum() / len(y_true[treatment == 0]))
x_perfect, y_perfect = np.array([0, ratio_random, n_samples]), np.array([0, ratio_random, ratio_random])
return x_perfect, y_perfect
def qini_auc_score(y_true, uplift, treatment, negative_effect=True):
"""Compute normalized Area Under the Qini curve (aka Qini coefficient) from prediction scores.
By computing the area under the Qini curve, the curve information is summarized in one number.
For binary outcomes the ratio of the actual uplift gains curve above the diagonal to that of
the optimum Qini curve.
Args:
y_true (1d array-like): Correct (true) binary target values.
uplift (1d array-like): Predicted uplift, as returned by a model.
treatment (1d array-like): Treatment labels.
negative_effect (bool): If True, optimum Qini Curve contains the negative effects
(negative uplift because of campaign). Otherwise, optimum Qini Curve will not contain the negative effects.
.. versionadded:: 0.2.0
Returns:
float: Qini coefficient.
See also:
:func:`.qini_curve`: Compute Qini curve.
:func:`.perfect_qini_curve`: Compute the perfect (optimum) Qini curve.
:func:`.plot_qini_curves`: Plot Qini curves from predictions..
:func:`.uplift_auc_score`: Compute normalized Area Under the Uplift curve from prediction scores.
References:
Nicholas J Radcliffe. (2007). Using control groups to target on predicted lift:
Building and assessing uplift model. Direct Marketing Analytics Journal, (3):14–21, 2007.
"""
# TODO: Add Continuous Outcomes
check_consistent_length(y_true, uplift, treatment)
check_is_binary(treatment)
check_is_binary(y_true)
y_true, uplift, treatment = np.array(y_true), np.array(uplift), np.array(treatment)
if not isinstance(negative_effect, bool):
raise TypeError(f'Negative_effects flag should be bool, got: {type(negative_effect)}')
x_actual, y_actual = qini_curve(y_true, uplift, treatment)
x_perfect, y_perfect = perfect_qini_curve(y_true, treatment, negative_effect)
x_baseline, y_baseline = np.array([0, x_perfect[-1]]), np.array([0, y_perfect[-1]])
auc_score_baseline = auc(x_baseline, y_baseline)
auc_score_perfect = auc(x_perfect, y_perfect) - auc_score_baseline
auc_score_actual = auc(x_actual, y_actual) - auc_score_baseline
return auc_score_actual / auc_score_perfect
def uplift_at_k(y_true, uplift, treatment, strategy, k=0.3):
"""Compute uplift at first k observations by uplift of the total sample.
Args:
y_true (1d array-like): Correct (true) binary target values.
uplift (1d array-like): Predicted uplift, as returned by a model.
treatment (1d array-like): Treatment labels.
k (float or int): If float, should be between 0.0 and 1.0 and represent the proportion of the dataset
to include in the computation of uplift. If int, represents the absolute number of samples.
strategy (string, ['overall', 'by_group']): Determines the calculating strategy.
* ``'overall'``:
The first step is taking the first k observations of all test data ordered by uplift prediction
(overall both groups - control and treatment) and conversions in treatment and control groups
calculated only on them. Then the difference between these conversions is calculated.
* ``'by_group'``:
Separately calculates conversions in top k observations in each group (control and treatment)
sorted by uplift predictions. Then the difference between these conversions is calculated
.. versionchanged:: 0.1.0
* Add supporting absolute values for ``k`` parameter
* Add parameter ``strategy``
Returns:
float: Uplift score at first k observations of the total sample.
See also:
:func:`.uplift_auc_score`: Compute normalized Area Under the Uplift curve from prediction scores.
:func:`.qini_auc_score`: Compute normalized Area Under the Qini Curve from prediction scores.
"""
# TODO: checker all groups is not empty
check_consistent_length(y_true, uplift, treatment)
check_is_binary(treatment)
check_is_binary(y_true)
y_true, uplift, treatment = np.array(y_true), np.array(uplift), np.array(treatment)
strategy_methods = ['overall', 'by_group']
if strategy not in strategy_methods:
raise ValueError(f'Uplift score supports only calculating methods in {strategy_methods},'
f' got {strategy}.'
)
n_samples = len(y_true)
order = np.argsort(uplift, kind='mergesort')[::-1]
_, treatment_counts = np.unique(treatment, return_counts=True)
n_samples_ctrl = treatment_counts[0]
n_samples_trmnt = treatment_counts[1]
k_type = np.asarray(k).dtype.kind
if (k_type == 'i' and (k >= n_samples or k <= 0)
or k_type == 'f' and (k <= 0 or k >= 1)):
raise ValueError(f'k={k} should be either positive and smaller'
f' than the number of samples {n_samples} or a float in the '
f'(0, 1) range')
if k_type not in ('i', 'f'):
raise ValueError(f'Invalid value for k: {k_type}')
if strategy == 'overall':
if k_type == 'f':
n_size = int(n_samples * k)
else:
n_size = k
# ToDo: _checker_ there are observations among two groups among first k
score_ctrl = y_true[order][:n_size][treatment[order][:n_size] == 0].mean()
score_trmnt = y_true[order][:n_size][treatment[order][:n_size] == 1].mean()
else: # strategy == 'by_group':
if k_type == 'f':
n_ctrl = int((treatment == 0).sum() * k)
n_trmnt = int((treatment == 1).sum() * k)
else:
n_ctrl = k
n_trmnt = k
if n_ctrl > n_samples_ctrl:
raise ValueError(f'With k={k}, the number of the first k observations'
' bigger than the number of samples'
f'in the control group: {n_samples_ctrl}'
)
if n_trmnt > n_samples_trmnt:
raise ValueError(f'With k={k}, the number of the first k observations'
' bigger than the number of samples'
f'in the treatment group: {n_samples_ctrl}'
)
score_ctrl = y_true[order][treatment[order] == 0][:n_ctrl].mean()
score_trmnt = y_true[order][treatment[order] == 1][:n_trmnt].mean()
return score_trmnt - score_ctrl
def response_rate_by_percentile(y_true, uplift, treatment, group, strategy='overall', bins=10):
"""Compute response rate (target mean in the control or treatment group) at each percentile.
Args:
y_true (1d array-like): Correct (true) binary target values.
uplift (1d array-like): Predicted uplift, as returned by a model.
treatment (1d array-like): Treatment labels.
group (string, ['treatment', 'control']): Group type for computing response rate: treatment or control.
* ``'treatment'``:
Values equal 1 in the treatment column.
* ``'control'``:
Values equal 0 in the treatment column.
strategy (string, ['overall', 'by_group']): Determines the calculating strategy. Default is 'overall'.
* ``'overall'``:
The first step is taking the first k observations of all test data ordered by uplift prediction
(overall both groups - control and treatment) and conversions in treatment and control groups
calculated only on them. Then the difference between these conversions is calculated.
* ``'by_group'``:
Separately calculates conversions in top k observations in each group (control and treatment)
sorted by uplift predictions. Then the difference between these conversions is calculated.
bins (int): Determines the number of bins (and relative percentile) in the data. Default is 10.
Returns:
array (shape = [>2]), array (shape = [>2]), array (shape = [>2]):
response rate at each percentile for control or treatment group,
variance of the response rate at each percentile,
group size at each percentile.
"""
check_consistent_length(y_true, uplift, treatment)
check_is_binary(treatment)
check_is_binary(y_true)
group_types = ['treatment', 'control']
strategy_methods = ['overall', 'by_group']
n_samples = len(y_true)
if group not in group_types:
raise ValueError(f'Response rate supports only group types in {group_types},'
f' got {group}.')
if strategy not in strategy_methods:
raise ValueError(f'Response rate supports only calculating methods in {strategy_methods},'
f' got {strategy}.')
if not isinstance(bins, int) or bins <= 0:
raise ValueError(f'Bins should be positive integer. Invalid value bins: {bins}')
if bins >= n_samples:
raise ValueError(f'Number of bins = {bins} should be smaller than the length of y_true {n_samples}')
y_true, uplift, treatment = np.array(y_true), np.array(uplift), np.array(treatment)
order = np.argsort(uplift, kind='mergesort')[::-1]
trmnt_flag = 1 if group == 'treatment' else 0
if strategy == 'overall':
y_true_bin = np.array_split(y_true[order], bins)
trmnt_bin = np.array_split(treatment[order], bins)
group_size = np.array([len(y[trmnt == trmnt_flag]) for y, trmnt in zip(y_true_bin, trmnt_bin)])
response_rate = np.array([np.mean(y[trmnt == trmnt_flag]) for y, trmnt in zip(y_true_bin, trmnt_bin)])
else: # strategy == 'by_group'
y_bin = np.array_split(y_true[order][treatment[order] == trmnt_flag], bins)
group_size = np.array([len(y) for y in y_bin])
response_rate = np.array([np.mean(y) for y in y_bin])
variance = np.multiply(response_rate, np.divide((1 - response_rate), group_size))
return response_rate, variance, group_size
def weighted_average_uplift(y_true, uplift, treatment, strategy='overall', bins=10):
"""Weighted average uplift.
It is an average of uplift by percentile.
Weights are sizes of the treatment group by percentile.
Args:
y_true (1d array-like): Correct (true) binary target values.
uplift (1d array-like): Predicted uplift, as returned by a model.
treatment (1d array-like): Treatment labels.
strategy (string, ['overall', 'by_group']): Determines the calculating strategy. Default is 'overall'.
* ``'overall'``:
The first step is taking the first k observations of all test data ordered by uplift prediction
(overall both groups - control and treatment) and conversions in treatment and control groups
calculated only on them. Then the difference between these conversions is calculated.
* ``'by_group'``:
Separately calculates conversions in top k observations in each group (control and treatment)
sorted by uplift predictions. Then the difference between these conversions is calculated
bins (int): Determines the number of bins (and the relative percentile) in the data. Default is 10.
Returns:
float: Weighted average uplift.
"""
check_consistent_length(y_true, uplift, treatment)
check_is_binary(treatment)
check_is_binary(y_true)
strategy_methods = ['overall', 'by_group']
n_samples = len(y_true)
if strategy not in strategy_methods:
raise ValueError(f'Response rate supports only calculating methods in {strategy_methods},'
f' got {strategy}.')
if not isinstance(bins, int) or bins <= 0:
raise ValueError(f'Bins should be positive integer.'
f' Invalid value bins: {bins}')
if bins >= n_samples:
raise ValueError(f'Number of bins = {bins} should be smaller than the length of y_true {n_samples}')
response_rate_trmnt, variance_trmnt, n_trmnt = response_rate_by_percentile(
y_true, uplift, treatment, group='treatment', strategy=strategy, bins=bins)
response_rate_ctrl, variance_ctrl, n_ctrl = response_rate_by_percentile(
y_true, uplift, treatment, group='control', strategy=strategy, bins=bins)
uplift_scores = response_rate_trmnt - response_rate_ctrl
weighted_avg_uplift = np.dot(n_trmnt, uplift_scores) / np.sum(n_trmnt)
return weighted_avg_uplift
def uplift_by_percentile(y_true, uplift, treatment, strategy='overall',
bins=10, std=False, total=False, string_percentiles=True):
"""Compute metrics: uplift, group size, group response rate, standard deviation at each percentile.
Metrics in columns and percentiles in rows of pandas DataFrame:
- ``n_treatment``, ``n_control`` - group sizes.
- ``response_rate_treatment``, ``response_rate_control`` - group response rates.
- ``uplift`` - treatment response rate substract control response rate.
- ``std_treatment``, ``std_control`` - (optional) response rates standard deviation.
- ``std_uplift`` - (optional) uplift standard deviation.
Args:
y_true (1d array-like): Correct (true) binary target values.
uplift (1d array-like): Predicted uplift, as returned by a model.
treatment (1d array-like): Treatment labels.
strategy (string, ['overall', 'by_group']): Determines the calculating strategy. Default is 'overall'.
* ``'overall'``:
The first step is taking the first k observations of all test data ordered by uplift prediction
(overall both groups - control and treatment) and conversions in treatment and control groups
calculated only on them. Then the difference between these conversions is calculated.
* ``'by_group'``:
Separately calculates conversions in top k observations in each group (control and treatment)
sorted by uplift predictions. Then the difference between these conversions is calculated
std (bool): If True, add columns with the uplift standard deviation and the response rate standard deviation.
Default is False.
total (bool): If True, add the last row with the total values. Default is False.
The total uplift computes as a total response rate treatment - a total response rate control.
The total response rate is a response rate on the full data amount.
bins (int): Determines the number of bins (and the relative percentile) in the data. Default is 10.
string_percentiles (bool): type of percentiles in the index: float or string. Default is True (string).
Returns:
pandas.DataFrame: DataFrame where metrics are by columns and percentiles are by rows.
"""
check_consistent_length(y_true, uplift, treatment)
check_is_binary(treatment)
check_is_binary(y_true)
strategy_methods = ['overall', 'by_group']
n_samples = len(y_true)
if strategy not in strategy_methods:
raise ValueError(f'Response rate supports only calculating methods in {strategy_methods},'
f' got {strategy}.')
if not isinstance(total, bool):
raise ValueError(f'Flag total should be bool: True or False.'
f' Invalid value total: {total}')
if not isinstance(std, bool):
raise ValueError(f'Flag std should be bool: True or False.'
f' Invalid value std: {std}')
if not isinstance(bins, int) or bins <= 0:
raise ValueError(f'Bins should be positive integer.'
f' Invalid value bins: {bins}')
if bins >= n_samples:
raise ValueError(f'Number of bins = {bins} should be smaller than the length of y_true {n_samples}')
if not isinstance(string_percentiles, bool):
raise ValueError(f'string_percentiles flag should be bool: True or False.'
f' Invalid value string_percentiles: {string_percentiles}')
y_true, uplift, treatment = np.array(y_true), np.array(uplift), np.array(treatment)
response_rate_trmnt, variance_trmnt, n_trmnt = response_rate_by_percentile(
y_true, uplift, treatment, group='treatment', strategy=strategy, bins=bins)
response_rate_ctrl, variance_ctrl, n_ctrl = response_rate_by_percentile(
y_true, uplift, treatment, group='control', strategy=strategy, bins=bins)
uplift_scores = response_rate_trmnt - response_rate_ctrl
uplift_variance = variance_trmnt + variance_ctrl
percentiles = [round(p * 100 / bins) for p in range(1, bins + 1)]
if string_percentiles:
percentiles = [f"0-{percentiles[0]}"] + \
[f"{percentiles[i]}-{percentiles[i + 1]}" for i in range(len(percentiles) - 1)]
df = pd.DataFrame({
'percentile': percentiles,
'n_treatment': n_trmnt,
'n_control': n_ctrl,
'response_rate_treatment': response_rate_trmnt,
'response_rate_control': response_rate_ctrl,
'uplift': uplift_scores
})
if total:
response_rate_trmnt_total, variance_trmnt_total, n_trmnt_total = response_rate_by_percentile(
y_true, uplift, treatment, strategy=strategy, group='treatment', bins=1)
response_rate_ctrl_total, variance_ctrl_total, n_ctrl_total = response_rate_by_percentile(
y_true, uplift, treatment, strategy=strategy, group='control', bins=1)
df.loc[-1, :] = ['total', n_trmnt_total, n_ctrl_total, response_rate_trmnt_total,
response_rate_ctrl_total, response_rate_trmnt_total - response_rate_ctrl_total]
if std:
std_treatment = np.sqrt(variance_trmnt)
std_control = np.sqrt(variance_ctrl)
std_uplift = np.sqrt(uplift_variance)
if total:
std_treatment = np.append(std_treatment, np.sum(std_treatment))
std_control = np.append(std_control, np.sum(std_control))
std_uplift = np.append(std_uplift, np.sum(std_uplift))
df.loc[:, 'std_treatment'] = std_treatment
df.loc[:, 'std_control'] = std_control
df.loc[:, 'std_uplift'] = std_uplift
df = df \
.set_index('percentile', drop=True, inplace=False) \
.astype({'n_treatment': 'int32', 'n_control': 'int32'})
return df
def treatment_balance_curve(uplift, treatment, winsize):
"""Compute the treatment balance curve: proportion of treatment group in the ordered predictions.
Args:
uplift (1d array-like): Predicted uplift, as returned by a model.
treatment (1d array-like): Treatment labels.
winsize(int): Size of the sliding window for calculating the balance between treatment and control.
Returns:
array (shape = [>2]), array (shape = [>2]): Points on a curve.
"""
check_consistent_length(uplift, treatment)
check_is_binary(treatment)
uplift, treatment = np.array(uplift), np.array(treatment)
desc_score_indices = np.argsort(uplift, kind="mergesort")[::-1]
treatment = treatment[desc_score_indices]
balance = np.convolve(treatment, np.ones(winsize), 'valid') / winsize
idx = np.linspace(1, 100, len(balance))
return idx, balance
def average_squared_deviation(y_true_train, uplift_train, treatment_train, y_true_val,
uplift_val, treatment_val, strategy='overall', bins=10):
"""Compute the average squared deviation.
The average squared deviation (ASD) is a model stability metric that shows how much the model overfits
the training data. Larger values of ASD mean greater overfit.
Args:
y_true_train (1d array-like): Correct (true) target values for training set.
uplift_train (1d array-like): Predicted uplift for training set, as returned by a model.
treatment_train (1d array-like): Treatment labels for training set.
y_true_val (1d array-like): Correct (true) target values for validation set.
uplift_val (1d array-like): Predicted uplift for validation set, as returned by a model.
treatment_val (1d array-like): Treatment labels for validation set.
strategy (string, ['overall', 'by_group']): Determines the calculating strategy. Default is 'overall'.
* ``'overall'``:
The first step is taking the first k observations of all test data ordered by uplift prediction
(overall both groups - control and treatment) and conversions in treatment and control groups
calculated only on them. Then the difference between these conversions is calculated.
* ``'by_group'``:
Separately calculates conversions in top k observations in each group (control and treatment)
sorted by uplift predictions. Then the difference between these conversions is calculated
bins (int): Determines the number of bins (and the relative percentile) in the data. Default is 10.
Returns:
float: average squared deviation
References:
René Michel, Igor Schnakenburg, Tobias von Martens. Targeting Uplift. An Introduction to Net Scores.
"""
check_consistent_length(y_true_train, uplift_train, treatment_train)
check_is_binary(treatment_train)
check_consistent_length(y_true_val, uplift_val, treatment_val)
check_is_binary(treatment_val)
strategy_methods = ['overall', 'by_group']
n_samples_train = len(y_true_train)
n_samples_val = len(y_true_val)
min_n_samples = min(n_samples_train, n_samples_val)
if strategy not in strategy_methods:
raise ValueError(
f'Response rate supports only calculating methods in {strategy_methods},'
f' got {strategy}.')
if not isinstance(bins, int) or bins <= 0:
raise ValueError(
f'Bins should be positive integer. Invalid value bins: {bins}')
if bins >= min_n_samples:
raise ValueError(
f'Number of bins = {bins} should be smaller than the length of y_true_train {n_samples_train}'
f'and length of y_true_val {n_samples_val}')
uplift_by_percentile_train = uplift_by_percentile(y_true_train, uplift_train, treatment_train,
strategy=strategy, bins=bins)
uplift_by_percentile_val = uplift_by_percentile(y_true_val, uplift_val, treatment_val,
strategy=strategy, bins=bins)
return np.mean(np.square(uplift_by_percentile_train['uplift'] - uplift_by_percentile_val['uplift']))
def max_prof_uplift(df_sorted, treatment_name, churn_name, pos_outcome, benefit, c_incentive, c_contact, a_cost=0):
"""Compute the maximum profit generated from an uplift model decided campaign
This can be visualised by plotting plt.plot(perc, cumulative_profit)
Args:
df_sorted (pandas dataframe): dataframe with descending uplift predictions for each customer (i.e. highest 1st)
treatment_name (string): column name of treatment columm (assuming 1 = treated)
churn_name (string): column name of churn column
pos_outcome (int or float): 1 or 0 value in churn column indicating a positive outcome (i.e. purchase = 1, whereas churn = 0)
benefit (int or float): the benefit of retaining a customer (e.g., the average customer lifetime value)
c_incentive (int or float): the cost of the incentive if a customer accepts the offer
c_contact (int or float): the cost of contacting a customer regardless of conversion
a_cost (int or float): the fixed administration cost for the campaign
Returns:
1d array-like: the incremental increase in x, for plotting
1d array-like: the cumulative profit per customer
References:
Floris Devriendt, Jeroen Berrevoets, Wouter Verbeke. Why you should stop predicting customer churn and start using uplift models.
"""
# VARIABLES
# n_ct0 no. people not treated
# n_ct1 no. people treated
# n_y1_ct0 no. people not treated with +ve outcome
# n_y1_ct1 no. people treated with +ve outcome
# r_y1_ct0 mean of not treated people with +ve outcome
# r_y1_ct1 mean of treated people with +ve outcome
# cs cumsum() of each variable
n_ct0 = np.where(df_sorted[treatment_name] == 0, 1, 0)
cs_n_ct0 = pd.Series(n_ct0.cumsum())
n_ct1 = np.where(df_sorted[treatment_name] == 1, 1, 0)
cs_n_ct1 = pd.Series(n_ct1.cumsum())
if pos_outcome == 0:
n_y1_ct0 = np.where((df_sorted[treatment_name] == 0) & (df_sorted[churn_name] == 0), 1, 0)
n_y1_ct1 = np.where((df_sorted[treatment_name] == 1) & (df_sorted[churn_name] == 0), 1, 0)
elif pos_outcome == 1:
n_y1_ct0 = np.where((df_sorted[treatment_name] == 0) & (df_sorted[churn_name] == 1), 1, 0)
n_y1_ct1 = np.where((df_sorted[treatment_name] == 1) & (df_sorted[churn_name] == 1), 1, 0)
cs_n_y1_ct0 = pd.Series(n_y1_ct0.cumsum())
cs_n_y1_ct1 = pd.Series(n_y1_ct1.cumsum())
cs_r_y1_ct0 = (cs_n_y1_ct0 / cs_n_ct0).fillna(0)
cs_r_y1_ct1 = (cs_n_y1_ct1 / cs_n_ct1).fillna(0)
cs_uplift = cs_r_y1_ct1 - cs_r_y1_ct0
# Dataframe of all calculated variables
df = pd.concat([cs_n_ct0,cs_n_ct1,cs_n_y1_ct0,cs_n_y1_ct1, cs_r_y1_ct0, cs_r_y1_ct1, cs_uplift], axis=1)
df.columns = ['cs_n_ct0', 'cs_n_ct1', 'cs_n_y1_ct0', 'cs_n_y1_ct1', 'cs_r_y1_c0', 'cs_r_y1_ct1', 'cs_uplift']
x = cs_n_ct0 + cs_n_ct1
max = cs_n_ct0.max() + cs_n_ct1.max()
t_profit = (x * cs_uplift * benefit) - (c_incentive * x * cs_r_y1_ct1) - (c_contact * x) - a_cost
perc = x / max
cumulative_profit = t_profit / max
return perc, cumulative_profit