-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathab_plot.py
More file actions
377 lines (321 loc) · 13 KB
/
ab_plot.py
File metadata and controls
377 lines (321 loc) · 13 KB
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
import numpy as np
import matplotlib.pyplot as plt
# import pandas as pd
import scipy.stats as scs
from stats import pooled_SE, confidence_interval, ab_dist, p_val, z_val
plt.style.use('ggplot')
def plot_norm_dist(ax, mu, sig, with_CI=False, sig_level=0.05, label=None):
"""Adds a normal distribution to the axes provided
Example:
plot_norm_dist(ax, 0, 1) # plots a standard normal distribution
Parameters:
ax (matplotlib axes)
mu (float): mean of the normal distribution
sig (float): standard deviation of the normal distribution
Returns:
None: the function adds a plot to the axes object provided
"""
x = np.linspace(mu - 12 * sig, mu + 12 * sig, 1000)
y = scs.norm(mu, sig).pdf(x)
ax.plot(x, y, label=label)
if with_CI:
plot_CI(ax, mu, sig, sig_level=sig_level)
def plot_binom_dist(ax, n, p, label=None):
"""Adds a binomial distribution to the axes provided
Example:
plot_norm_dist(ax, 0, 1) # plots a standard normal distribution
Parameters:
ax (matplotlib axes)
mu (float): mean of the normal distribution
sig (float): standard deviation of the normal distribution
Returns:
None: the function adds a plot to the axes object provided
"""
x = np.linspace(0, n, n+1)
y = scs.binom(n, p).pmf(x)
ax.plot(x, y, label=label)
def plot_CI(ax, mu, s, sig_level=0.05, color='grey'):
"""Calculates the two-tailed confidence interval and adds the plot to
an axes object.
Example:
plot_CI(ax, mu=0, s=stderr, sig_level=0.05)
Parameters:
ax (matplotlib axes)
mu (float): mean
s (float): standard deviation
Returns:
None: the function adds a plot to the axes object provided
"""
# z = scs.norm().ppf(1 - sig_level/2)
# left = mu - z * s
# right = mu + z * s
left, right = confidence_interval(sample_mean=mu, sample_std=s,
sig_level=sig_level)
ax.axvline(left, c=color, linestyle='--', alpha=0.5)
ax.axvline(right, c=color, linestyle='--', alpha=0.5)
def plot_null(ax, stderr):
"""Plots the null hypothesis distribution where if there is no real change,
the distribution of the differences between the test and the control groups
will be normally distributed.
The confidence band is also plotted.
Example:
plot_null(ax, stderr)
Parameters:
ax (matplotlib axes)
stderr (float): the pooled standard error of the control and test group
Returns:
None: the function adds a plot to the axes object provided
"""
plot_norm_dist(ax, 0, stderr, label="Null")
plot_CI(ax, mu=0, s=stderr, sig_level=0.05)
def plot_alt(ax, stderr, d_hat):
"""Plots the alternative hypothesis distribution where if there is a real
change, the distribution of the differences between the test and the
control groups will be normally distributed and centered around d_hat
The confidence band is also plotted.
Example:
plot_alt(ax, stderr, d_hat=0.025)
Parameters:
ax (matplotlib axes)
stderr (float): the pooled standard error of the control and test group
Returns:
None: the function adds a plot to the axes object provided
"""
plot_norm_dist(ax, d_hat, stderr, label="Alternative")
# plot_CI(ax, mu=d_hat, s=stderr, sig_level=0.05)
def abplot(N_A, N_B, bcr, d_hat, sig_level=0.05, show_power=False,
show_alpha=False, show_beta=False, show_p_value=False,
show_legend=True):
"""Example plot of AB test
Example:
abplot(n=4000, bcr=0.11, d_hat=0.03)
Parameters:
n (int): total sample size for both control and test groups (N_A + N_B)
bcr (float): base conversion rate; conversion rate of control
d_hat: difference in conversion rate between the control and test
groups, sometimes referred to as **minimal detectable effect** when
calculating minimum sample size or **lift** when discussing
positive improvement desired from launching a change.
Returns:
None: the function plots an AB test as two distributions for
visualization purposes
"""
# create a plot object
fig, ax = plt.subplots(figsize=(12, 6))
# define parameters to find pooled standard error
X_A = bcr * N_A
X_B = (bcr + d_hat) * N_B
stderr = pooled_SE(N_A, N_B, X_A, X_B)
# plot the distribution of the null and alternative hypothesis
plot_null(ax, stderr)
plot_alt(ax, stderr, d_hat)
# set extent of plot area
ax.set_xlim(-3 * d_hat, 3 * d_hat)
# shade areas according to user input
if show_power:
show_area(ax, d_hat, stderr, sig_level, area_type='power')
if show_alpha:
show_area(ax, d_hat, stderr, sig_level, area_type='alpha')
if show_beta:
show_area(ax, d_hat, stderr, sig_level, area_type='beta')
# show p_value based on the binomial distributions for the two groups
if show_p_value:
null = ab_dist(stderr, 'control')
p_val = p_value(N_A, N_B, bcr, bcr+d_hat)
ax.text(3 * stderr, null.pdf(0),
'p-value = {0:.3f}'.format(p_val),
fontsize=12, ha='left')
# option to show legend
if show_legend:
plt.legend()
plt.xlabel('d')
plt.ylabel('PDF')
plt.show()
def show_area(ax, d_hat, stderr, sig_level, area_type='power'):
"""Fill between upper significance boundary and distribution for
alternative hypothesis
"""
left, right = confidence_interval(sample_mean=0, sample_std=stderr,
sig_level=sig_level)
x = np.linspace(-12 * stderr, 12 * stderr, 1000)
null = ab_dist(stderr, 'control')
alternative = ab_dist(stderr, d_hat, 'test')
# if area_type is power
# Fill between upper significance boundary and distribution for alternative
# hypothesis
if area_type == 'power':
ax.fill_between(x, 0, alternative.pdf(x), color='green', alpha='0.25',
where=(x > right))
ax.text(-3 * stderr, null.pdf(0),
'power = {0:.3f}'.format(1 - alternative.cdf(right)),
fontsize=12, ha='right', color='k')
# if area_type is alpha
# Fill between upper significance boundary and distribution for null
# hypothesis
if area_type == 'alpha':
ax.fill_between(x, 0, null.pdf(x), color='green', alpha='0.25',
where=(x > right))
ax.text(-3 * stderr, null.pdf(0),
'alpha = {0:.3f}'.format(1 - null.cdf(right)),
fontsize=12, ha='right', color='k')
# if area_type is beta
# Fill between distribution for alternative hypothesis and upper
# significance boundary
if area_type == 'beta':
ax.fill_between(x, 0, alternative.pdf(x), color='green', alpha='0.25',
where=(x < right))
ax.text(-3 * stderr, null.pdf(0),
'beta = {0:.3f}'.format(alternative.cdf(right)),
fontsize=12, ha='right', color='k')
def zplot(area=0.95, two_tailed=True, align_right=False):
"""Plots a z distribution with common annotations
Example:
zplot(area=0.95)
zplot(area=0.80, two_tailed=False, align_right=True)
Parameters:
area (float): The area under the standard normal distribution curve.
align (str): The area under the curve can be aligned to the center
(default) or to the left.
Returns:
None: A plot of the normal distribution with annotations showing the
area under the curve and the boundaries of the area.
"""
# create plot object
fig = plt.figure(figsize=(12, 6))
ax = fig.subplots()
# create normal distribution
norm = scs.norm()
# create data points to plot
x = np.linspace(-5, 5, 1000)
y = norm.pdf(x)
ax.plot(x, y)
# code to fill areas
# for two-tailed tests
if two_tailed:
left = norm.ppf(0.5 - area / 2)
right = norm.ppf(0.5 + area / 2)
ax.vlines(right, 0, norm.pdf(right), color='grey', linestyle='--')
ax.vlines(left, 0, norm.pdf(left), color='grey', linestyle='--')
ax.fill_between(x, 0, y, color='grey', alpha='0.25',
where=(x > left) & (x < right))
plt.xlabel('z')
plt.ylabel('PDF')
plt.text(left, norm.pdf(left), "z = {0:.3f}".format(left), fontsize=12,
rotation=90, va="bottom", ha="right")
plt.text(right, norm.pdf(right), "z = {0:.3f}".format(right),
fontsize=12, rotation=90, va="bottom", ha="left")
# for one-tailed tests
else:
# align the area to the right
if align_right:
left = norm.ppf(1-area)
ax.vlines(left, 0, norm.pdf(left), color='grey', linestyle='--')
ax.fill_between(x, 0, y, color='grey', alpha='0.25',
where=x > left)
plt.text(left, norm.pdf(left), "z = {0:.3f}".format(left),
fontsize=12, rotation=90, va="bottom", ha="right")
# align the area to the left
else:
right = norm.ppf(area)
ax.vlines(right, 0, norm.pdf(right), color='grey', linestyle='--')
ax.fill_between(x, 0, y, color='grey', alpha='0.25',
where=x < right)
plt.text(right, norm.pdf(right), "z = {0:.3f}".format(right),
fontsize=12, rotation=90, va="bottom", ha="left")
# annotate the shaded area
plt.text(0, 0.1, "shaded area = {0:.3f}".format(area), fontsize=12,
ha='center')
# axis labels
plt.xlabel('z')
plt.ylabel('PDF')
plt.show()
def abplot_CI_bars(N, X, sig_level=0.05, dmin=None):
"""Returns a confidence interval bar plot for multivariate tests
Parameters:
N (list or tuple): sample size for all groups
X (list or tuple): number of conversions for each variant
sig_level (float): significance level
dmin (float): minimum desired lift; a red and green dashed lines are
shown on the plot if dmin is provided.
Returns:
None: A plot of the confidence interval bars is returned inline.
"""
# initiate plot object
fig, ax = plt.subplots(figsize=(12, 3))
# get control group values
N_A = N[0]
X_A = X[0]
# initiate containers for standard error and differences
SE = []
d = []
# iterate through X and N and calculate d and SE
for idx in range(1, len(N)):
X_B = X[idx]
N_B = N[idx]
d.append(X_B / N_B - X_A / N_A)
SE.append(pooled_SE(N_A, N_B, X_A, X_B))
# convert to numpy arrays
SE = np.array(SE)
d = np.array(d)
y = np.arange(len(N)-1)
# get z value
z = z_val(sig_level)
# confidence interval values
ci = SE * z
# bar to represent the confidence interval
ax.hlines(y, d-ci, d+ci, color='blue', alpha=0.35, lw=10, zorder=1)
# marker for the mean
ax.scatter(d, y, s=300, marker='|', lw=10, color='magenta', zorder=2)
# vertical line to represent 0
ax.axvline(0, c='grey', linestyle='-')
# plot veritcal dashed lines if dmin is provided
if dmin is not None:
ax.axvline(-dmin, c='red', linestyle='--', alpha=0.75)
ax.axvline(dmin, c='green', linestyle='--', alpha=0.75)
# invert y axis to show variant 1 at the top
ax.invert_yaxis()
# label variants on y axis
labels = ['variant{}'.format(idx+1) for idx in range(len(N)-1)]
plt.yticks(np.arange(len(N)-1), labels)
def funnel_CI_plot(A, B, sig_level=0.05):
"""Returns a confidence interval bar plot for multivariate tests
Parameters:
A (list of tuples): (sample size, conversions) for control group funnel
B (list of tuples): (sample size, conversions) for test group funnel
sig_level (float): significance level
Returns:
None: A plot of the confidence interval bars is returned inline.
"""
# initiate plot object
fig, ax = plt.subplots(figsize=(12, 3))
# initiate containers for standard error and differences
SE = []
d = []
# iterate through X and N and calculate d and SE
for idx in range(len(A)):
X_A = A[idx][1]
N_A = A[idx][0]
X_B = B[idx][1]
N_B = B[idx][0]
d.append(X_B / N_B - X_A / N_A)
SE.append(pooled_SE(N_A, N_B, X_A, X_B))
# convert to numpy arrays
SE = np.array(SE)
d = np.array(d)
print(d)
y = np.arange(len(A))
# get z value
z = z_val(sig_level)
# confidence interval values
ci = SE * z
# bar to represent the confidence interval
ax.hlines(y, d-ci, d+ci, color='blue', alpha=0.35, lw=10, zorder=1)
# marker for the mean
ax.scatter(d, y, s=300, marker='|', lw=10, color='magenta', zorder=2)
# vertical line to represent 0
ax.axvline(0, c='grey', linestyle='-')
# invert y axis to show variant 1 at the top
ax.invert_yaxis()
# label variants on y axis
labels = ['metric{}'.format(idx+1) for idx in range(len(A))]
plt.yticks(np.arange(len(A)), labels)