-
Notifications
You must be signed in to change notification settings - Fork 0
/
ab_simulation.py
221 lines (175 loc) · 8.73 KB
/
ab_simulation.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
import matplotlib.pyplot as plt
import numpy as np
import random
from scipy.stats import chi2, norm
def histogram(parameters):
hist, bins = np.histogram(parameters['data'], parameters['bins'])
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.figure(parameters['figure'])
plt.xlabel(parameters['xLabel'])
plt.ylabel(parameters['yLabel'])
plt.suptitle(parameters['title'])
plt.bar(center, hist, align='center', width=width)
def calculate_sample_size(baseline_odds, alpha, beta, minimum_effect_size):
if baseline_odds is None or alpha is None or beta is None or minimum_effect_size is None:
return None
# f(alpha, beta, baseline_odds, minimum_effect_size) = samples_per_trial
# http://sphweb.bumc.bu.edu/otlt/MPH-Modules/BS/BS704_Power/BS704_Power_print.html
# http://www.itl.nist.gov/div898/handbook/prc/section2/prc222.htm
return int(2 * baseline_odds * (1 - baseline_odds) * ((norm.ppf(1-alpha/2) + norm.ppf(1-beta)) / minimum_effect_size)**2)
# run_simulation()
# simulate an A/B experiment by modeling a series of coin flips (draw samples from a binomial distribution)
#
# inputs
# baseline_odds: float between 0 and 1; the control threshold for heads
# test_odds_delta: float between 0 and 1; baseline_odds + test_odds_delta indicates the test threshold for heads
# alpha: float between 0 and 1; false positive rate
# beta: float between 0 and 1; false negative rate
# minimum_effect_size: float between 0 and 1; min. desired effect size to detect if the control and treatment are different
# total_trials: integer; total number of trials to run (each trial draws a dynamically calculated number of samples)
#
# outputs
# significant_differences: the number of trials that resulted in a statistically different number of heads
def run_simulation(baseline_odds=0.01, test_odds_delta=0, alpha=0.05, beta=0.2, minimum_effect_size=0.005,
total_trials=100, samples_per_trial=None):
if samples_per_trial == None:
samples_per_trial = calculate_sample_size(baseline_odds, alpha, beta, minimum_effect_size)
# chi-squared inputs, degrees of freedom
# http://www.ling.upenn.edu/%7Eclight/chisquared.htm
# -----------------------------------
# | | test | control | TOTAL
# -----------------------------------
# | heads | 50 | 50 | 100
# -----------------------------------
# | tails | 50 | 50 | 100
# -----------------------------------
# | TOTAL | 100 | 100 | 200
#
# degrees of freedom: (rows - 1)(columns - 1) = (2 - 1)(2 - 1) = 1
degrees_of_freedom = 1
significant_differences = 0
for _ in range(total_trials):
control_heads_per_trial = 0
for _ in range(samples_per_trial):
if random.random() < baseline_odds:
control_heads_per_trial += 1
control_tails_per_trial = samples_per_trial - control_heads_per_trial
test_heads_per_trial = 0
for _ in range(samples_per_trial):
if random.random() < baseline_odds + test_odds_delta:
test_heads_per_trial += 1
test_tails_per_trial = samples_per_trial - test_heads_per_trial
total_number_of_samples = samples_per_trial * 2
expected_heads_percentage = (control_heads_per_trial + test_heads_per_trial) / (total_number_of_samples)
expected_tails_percentage = (control_tails_per_trial + test_tails_per_trial) / (total_number_of_samples)
expected_heads = expected_heads_percentage * samples_per_trial
expected_tails = expected_tails_percentage * samples_per_trial
X_2_control_heads = ((control_heads_per_trial - expected_heads) ** 2) / expected_heads
X_2_control_tails = ((control_tails_per_trial - expected_tails) ** 2) / expected_tails
X_2_test_heads = ((test_heads_per_trial - expected_heads) ** 2) / expected_heads
X_2_test_tails = ((test_tails_per_trial - expected_tails) ** 2) / expected_tails
X_2 = X_2_control_heads + X_2_control_tails + X_2_test_heads + X_2_test_tails
p = chi2.sf(X_2, degrees_of_freedom)
significant = p < alpha
if significant:
significant_differences += 1
return significant_differences
def generate_normal_distribution_ppf():
inverse_cdf_values = []
for x in range(0, 1000, 1):
inverse_cdf_values.append(norm.ppf(x / 1000))
print(inverse_cdf_values)
def main():
# number of trials to perform for each simulation; each
# trial draws 'samples_per_trial' samples (dynamically calculated based on A/B experiment parameters)
total_trials = 100
# number of times to run the simulation; each run contains total_trials number of trials
# multiple runs allow us to observe the variance in false positives/negatives across runs
# any individual run may have a lower or higher number of false positives/negatives than specified by
# alpha and beta, but the average of false positives/negatives across runs should correspond to the
# selected alpha and beta values
total_runs = 100
# A/B test parameters
baseline_odds = 0.5
test_odds_delta = 0.05
alpha = 0.05
beta = 0.2
minimum_effect_size = 0.05
samples_per_trial = calculate_sample_size(baseline_odds, alpha, beta, minimum_effect_size)
print("Samples per trial: %s" % samples_per_trial)
print("Trials per run: %s" % total_trials)
print("Total runs: %s" % total_runs)
# false positives exploration
if test_odds_delta == 0:
false_positives_simulation = {'data': [], 'bins': 100,
'figure': 0,
'xLabel': 'False positives',
'yLabel': 'Counts',
'title': 'A/B Experiments'}
for runs in range(total_runs):
significant = run_simulation(
baseline_odds=baseline_odds,
test_odds_delta=test_odds_delta,
alpha=alpha,
beta=beta,
minimum_effect_size=minimum_effect_size,
total_trials=total_trials,
samples_per_trial=samples_per_trial
)
false_positives_simulation['data'].append(significant)
average = sum(false_positives_simulation['data']) / len(false_positives_simulation['data'])
print("Mean false positives per run: %s" % average)
histogram(false_positives_simulation)
# false negatives exploration
# draw samples using the thresholds p+test_odds_delta and p-test_odds_delta because if p != 0.5,
# changing 'p' by a constant 'test_odds_delta' does not increase the odds of observing heads
# by the same relative amount as it decreases the odds of observing tails
#
# p=baseline odds of observing heads; delta=change to baseline odds
# relative chance of observing heads: (p+delta) / p
# relative chance of observing tails: (1-(p+delta)) / p
# (p+delta) / p == (1-(p+delta)) / p if and only if p=0.5
#
# for example:
# if p=0.5, and delta=0.1: (0.4+0.1)/0.5 == (1-(0.4+0.1))/0.5
# if p=0.49, and delta=0.1: (0.49+0.1)/0.49 != (1-(0.49+0.1))/0.49
#
# if we sample only from p+delta or only from p-delta, we'll get more or fewer false negatives than
# expected based on the calculated sample size
else:
false_negatives_simulation = {'data': [], 'bins': 100,
'figure': 1,
'xLabel': 'False negatives',
'yLabel': 'Counts',
'title': 'A/B Experiments'}
# baseline + delta
for runs in range(total_runs):
significant = run_simulation(
baseline_odds=baseline_odds,
test_odds_delta=test_odds_delta,
alpha=alpha,
beta=beta,
minimum_effect_size=minimum_effect_size,
total_trials=total_trials,
samples_per_trial=samples_per_trial
)
false_negatives_simulation['data'].append(total_trials - significant)
# baseline - delta
for runs in range(total_runs):
significant = run_simulation(
baseline_odds=baseline_odds,
test_odds_delta=-test_odds_delta,
alpha=alpha,
beta=beta,
minimum_effect_size=minimum_effect_size,
total_trials=total_trials,
samples_per_trial=samples_per_trial
)
false_negatives_simulation['data'].append(total_trials - significant)
average = sum(false_negatives_simulation['data']) / len(false_negatives_simulation['data'])
print("Mean false negatives per run: %s" % average)
histogram(false_negatives_simulation)
plt.show()
if __name__ == "__main__":
main()