This repository has been archived by the owner on Nov 26, 2022. It is now read-only.
/
empc.py
267 lines (230 loc) · 8.7 KB
/
empc.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
import warnings
import numpy as np
from sklearn.metrics import roc_curve
from scipy.spatial import ConvexHull
from scipy.spatial.qhull import QhullError
from scipy.special import beta as beta_func
from scipy.special import betainc
from .utils import NUM_TYPE
class EMPChurn(object):
"""
Expected Maximum Profit Measure for Customer Churn (EMPC)
=========================================================
Parameters
----------
y_true : 1D array-like or label indicator array, shape=(n_samples,)
Binary target values. Churners have a ``cases_label`` value.
y_score : 1D array-like, shape=(n_samples,)
Target scores, can either be probability estimates or
non-thresholded decision values.
alpha : float (default: 6)
Alpha parameter of uni-modal beta distribution (alpha > 1).
beta : float (default: 14)
Beta parameter of uni-modal beta distribution (beta > 1).
clv : float (default: 200)
Constant customer lifetime value per retained customer (clv > d).
d : float (default: 10)
Constant cost of retention offer (d > 0).
f : float (default: 1)
Constant cost of contact (f > 0).
case_label : str or int or float (default: 1)
Label for the identification of cases (i.e., churners).
Note
----
The EMPC requires that the churn class is encoded as 0, and it is NOT
interchangeable (see [3, p. 37]). However, this implementation assumes
the standard notation ('churn': 1, 'no churn': 0) and makes the necessary
changes internally. An equivalent R implementation is available in [2].
References
----------
[1] Verbraken, T., Verbeke, W. and Baesens, B. (2013).
A Novel Profit Maximizing Metric for Measuring Classification
Performance of Customer Churn Prediction Models. IEEE Transactions on
Knowledge and Data Engineering, 25(5), 961-973. Available Online:
http://ieeexplore.ieee.org/iel5/69/6486492/06165289.pdf?arnumber=6165289
[2] Bravo, C. and Vanden Broucke, S. and Verbraken, T. (2015).
EMP: Expected Maximum Profit Classification Performance Measure.
R package version 2.0.1. Available Online:
http://cran.r-project.org/web/packages/EMP/index.html
[3] Verbraken, T. (2013). Business-Oriented Data Analytics:
Theory and Case Studies. Ph.D. dissertation, Dept. LIRIS, KU Leuven,
Leuven, Belgium, 2013.
"""
def __init__(
self,
y_true,
y_score=None,
alpha=6,
beta=14,
clv=200,
d=10,
f=1,
case_label=1,
):
"""Constructor for EMPChurn"""
assert isinstance(alpha, NUM_TYPE) and alpha > 1
assert isinstance(beta, NUM_TYPE) and beta > 1
assert isinstance(clv, NUM_TYPE) and clv > 0
assert isinstance(d, NUM_TYPE) and d > 0
assert isinstance(f, NUM_TYPE) and f > 0
assert clv > d
assert isinstance(case_label, (str, NUM_TYPE))
self._yt = None
self._ys = None
self._gamma_needs_update = None
y = np.asarray(y_true)
self.y_true = y == case_label # Make it binary
self.n_samples = len(self.y_true)
if y_score is not None:
self.y_score = np.asarray(y_score)
assert len(self.y_true) == len(self.y_score)
self.alpha = alpha
self.beta = beta
self.clv = clv
self.d = d
self.f = f
# Recall: cases are labeled as 0s in the EMP framework
self.pi0 = np.sum(self.y_true) / self.n_samples
self.pi1 = 1 - self.pi0
self._delta = self.d / self.clv
self._phi = self.f / self.clv
self._egamma = alpha / (alpha + beta)
self._gamma = None
self._F0 = None
self._F1 = None
self._empc = None
@property
def y_true(self):
"""Getter: y_true"""
return self._yt
@y_true.setter
def y_true(self, value):
"""Setter: y_true"""
y_true = np.asarray(value)
assert y_true.ndim == 1
assert len(np.unique(y_true)) == 2
self._yt = y_true
@property
def y_score(self):
"""Getter: y_score"""
return self._ys
@y_score.setter
def y_score(self, value):
"""Setter: y_score"""
if value is not None:
y_score = np.asarray(value)
assert y_score.ndim == 1
self._gamma_needs_update = True
self._ys = y_score
def empc(self, y_score=None):
"""
Compute EMPC.
Parameters
----------
y_score : 1D array-like, shape=(n_samples,)
Target scores, can either be probability estimates or
non-thresholded decision values. Must have the same length
as y_true.
Returns
-------
empc : float
Empirical EMP estimate for customer churn prediction.
"""
if y_score is not None:
self.y_score = y_score
assert self._gamma_needs_update is not None and self._ys is not None
assert len(self._yt) == len(self._ys)
# Check _empc
if not self._gamma_needs_update and self._empc:
# Gamma hasn't changed and empc has already been computed
return self._empc
else:
# Gamma has been changed, empc needs to be recomputed
assert self._gamma_needs_update
self._empc = self._compute_empc()
return self._empc
def _compute_empc(self):
self._compute_gamma()
gammaii = self._gamma[:-1]
gammaie = self._gamma[1:]
F0 = self._F0[range(len(gammaii))]
F1 = self._F1[range(len(gammaii))]
def betafn(x, a, b):
return betainc(a, b, x) * beta_func(a, b)
contr0 = (
(self.clv * (1 - self._delta) * self.pi0 * F0)
* (
betafn(gammaie, self.alpha + 1, self.beta)
- betafn(gammaii, self.alpha + 1, self.beta)
)
/ betafn(1, self.alpha, self.beta)
)
tmp = (
betafn(gammaie, self.alpha, self.beta)
- betafn(gammaii, self.alpha, self.beta)
) / betafn(1, self.alpha, self.beta)
contr1 = (
-self.clv
* (
self._phi * self.pi0 * F0
+ (self._delta + self._phi) * self.pi1 * F1
)
) * tmp
empc = np.sum(np.add(contr0, contr1))
return empc
def _compute_gamma(self):
self._emp_roc_info()
numerator = self.pi1 * (self._delta + self._phi) * np.diff(
self._F1
) + self.pi0 * self._phi * np.diff(self._F0)
denominator = self.pi0 * (1 - self._delta) * np.diff(self._F0)
with warnings.catch_warnings():
# Ignore RuntimeWarning: division by zero
# It is taken care of in the next gamma line
warnings.simplefilter("ignore")
gamma = np.append([0], numerator / denominator)
self._gamma = np.append(gamma[gamma < 1], [1])
self._gamma_needs_update = False
def _emp_roc_info(self):
# Compute ROC Convex Hull
def compute_roc_convex_hull(y_true, y_score):
fpr, tpr, _ = roc_curve(
y_true, y_score, pos_label=True, drop_intermediate=False,
)
if fpr[0] != 0 or tpr[0] != 0:
fpr = np.concatenate([[0], fpr])
tpr = np.concatenate([[0], tpr])
# For testing
fpr = (
np.concatenate([fpr, [1]])
if fpr[-1] != 1 or tpr[-1] != 1
else fpr
)
tpr = (
np.concatenate([tpr, [1]])
if fpr[-1] != 1 or tpr[-1] != 1
else tpr
)
is_finite = np.isfinite(fpr) & np.isfinite(tpr)
fpr = fpr[is_finite]
tpr = tpr[is_finite]
assert fpr.shape[0] >= 2, "Too few distinct predictions for ROCCH"
try:
points = np.c_[fpr, tpr]
ind = ConvexHull(points).vertices
ch_fpr = fpr[ind]
ch_tpr = tpr[ind]
ind_upper_triangle = ch_fpr < ch_tpr
ch_fpr = np.concatenate([[0], ch_fpr[ind_upper_triangle], [1]])
ch_tpr = np.concatenate([[0], ch_tpr[ind_upper_triangle], [1]])
ind = np.argsort(ch_fpr)
ch_fpr = ch_fpr[ind]
ch_tpr = ch_tpr[ind]
except QhullError:
ch_fpr = np.array([0, 1])
ch_tpr = np.array([0, 1])
return ch_fpr, ch_tpr
G0, G1 = compute_roc_convex_hull(self._yt, self._ys)
# Recall: cases are labeled as 0s in the EMP framework
self._F0 = G1
self._F1 = G0