-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
281 lines (210 loc) · 9.81 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>A/B Test Simulator</title>
</head>
<body>
<h1>A/B Test Simulator (see console logs)</h1>
<span id="result"></span>
<script type="text/javascript">
var control_conversion = [1,5];
var test_conversion = [0.3,4];
//var test_conversion = [0.8,4.8];
var P_VALUE_TRESHHOLD = 0.05;
// document.getElementById("result").innerHTML = document.getElementById("result").innerHTML + "1";
// https://stackoverflow.com/questions/25582882/javascript-math-random-normal-distribution-gaussian-bell-curve
function get_random_number_with_normal_distrubution(min, max, skew) {
var u = 0, v = 0;
while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
while(v === 0) v = Math.random();
let num = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
num = num / 10.0 + 0.5; // Translate to 0 -> 1
if (num > 1 || num < 0) num = get_random_number_with_normal_distrubution(min, max, skew); // resample between 0 and 1 if out of range
num = Math.pow(num, skew); // Skew
num *= max - min; // Stretch to fill range
num += min; // offset to min
return num;
}
function is_control() {
if (Math.random() <= 0.5) {
return true;
} else {
return false;
}
}
function is_conversion_for_visitor (min,max) {
var random_conversion_rate_for_this_visitor = get_random_number_with_normal_distrubution(min,max,1);
if (Math.random() <= random_conversion_rate_for_this_visitor/100 ) {
return true; // conversion happened
} else {
return false; //no conversion
}
}
function return_p_value (control_hits, control_conversions,test_hits,test_conversions) {
//https://codereview.stackexchange.com/questions/86242/calculate-ab-significance-with-control-and-treatment-objects
//https://vwo.com/ab-split-test-significance-calculator/
function cr(t) {
return t.conversions / t.hits;
}
function calcZScore(c, t) {
var z = cr(t) - cr(c);
var s = (cr(t) * (1 - cr(t))) / t.hits + (cr(c) * (1 - cr(c))) / c.hits;
return z/Math.sqrt(s);
}
function cumNorDist(x) {
var b1 = 0.319381530;
var b2 = -0.356563782;
var b3 = 1.781477937;
var b4 = -1.821255978;
var b5 = 1.330274429;
var p = 0.2316419;
var c = 0.39894228;
if (x >= 0.0) {
t = 1.0 / ( 1.0 + p * x );
return (1.0 - c * Math.exp(-x * x / 2.0) * t *
( t * ( t * ( t * ( t * b5 + b4 ) + b3 ) + b2 ) + b1 ));
} else {
t = 1.0 / ( 1.0 - p * x );
return ( c * Math.exp(-x * x / 2.0) * t *
( t * ( t * ( t * ( t * b5 + b4 ) + b3 ) + b2 ) + b1 ));
}
}
var c = {};
c['hits'] = control_hits;
c['conversions'] = control_conversions;
var t = {};
t['hits'] = test_hits;
t['conversions'] = test_conversions;
return 1 - ( cumNorDist(calcZScore(c,t)) );
}
function if_stats_significant(control_hits, control_conversions,test_hits,test_conversions) {
if (
(return_p_value(control_hits, control_conversions,test_hits,test_conversions) <= P_VALUE_TRESHHOLD)
||
(return_p_value(test_hits, test_conversions,control_hits,control_conversions) <= P_VALUE_TRESHHOLD)
) {
return true;
} else {
return false;
}
}
function run_one_simulation(check_at_hits) {
var control_segment_hits = 0;
var control_segment_conversion_hits = 0;
var test_segment_hits = 0;
var test_segment_conversion_hits = 0;
var current_hits = 0;
var result_limited_time = {};
var result_unlimited = {};
var result = {};
while (true) {
if ( is_control() ) {
control_segment_hits = control_segment_hits+ 1;
if (is_conversion_for_visitor(control_conversion[0], control_conversion[1])) {
control_segment_conversion_hits = control_segment_conversion_hits +1;
}
} else {
test_segment_hits = test_segment_hits + 1;
if (is_conversion_for_visitor(test_conversion[0], test_conversion[1])) {
test_segment_conversion_hits = test_segment_conversion_hits +1;
}
}
current_hits = control_segment_hits+test_segment_hits;
if (current_hits == check_at_hits) {
result_limited_time['control_segment_hits'] = control_segment_hits;
result_limited_time['control_segment_conversion_hits'] = control_segment_conversion_hits;
result_limited_time['test_segment_hits'] = test_segment_hits;
result_limited_time['test_segment_conversion_hits'] = test_segment_conversion_hits;
result_limited_time['control_segment_conversion_rate'] = (control_segment_conversion_hits / control_segment_hits).toFixed(3);
result_limited_time['test_segment_conversion_rate'] = (test_segment_conversion_hits / test_segment_hits).toFixed(3);
if (if_stats_significant(control_segment_hits, control_segment_conversion_hits,test_segment_hits,test_segment_conversion_hits)) {
result_limited_time['is_result_significant'] = true;
} else {
result_limited_time['is_result_significant'] = false;
}
if (result_limited_time['control_segment_conversion_rate'] > result_limited_time['test_segment_conversion_rate']) {
result_limited_time['is_experiment_error'] = false;
} else {
result_limited_time['is_experiment_error'] = true;
}
if (if_stats_significant(control_segment_hits, control_segment_conversion_hits,test_segment_hits,test_segment_conversion_hits)) {
break; // test is over, no need to continue
}
} // current hits = hits
if (Object.keys(result_unlimited).length == 0 && if_stats_significant(control_segment_hits, control_segment_conversion_hits,test_segment_hits,test_segment_conversion_hits)) {
result_unlimited['control_segment_hits'] = control_segment_hits;
result_unlimited['control_segment_conversion_hits'] = control_segment_conversion_hits;
result_unlimited['test_segment_hits'] = test_segment_hits;
result_unlimited['test_segment_conversion_hits'] = test_segment_conversion_hits;
result_unlimited['control_segment_conversion_rate'] = (control_segment_conversion_hits / control_segment_hits).toFixed(3);
result_unlimited['test_segment_conversion_rate'] = (test_segment_conversion_hits / test_segment_hits).toFixed(3);
result_unlimited['is_result_significant'] = true;
if (result_unlimited['control_segment_conversion_rate'] > result_unlimited['test_segment_conversion_rate']) {
result_unlimited['is_experiment_error'] = false;
} else {
result_unlimited['is_experiment_error'] = true;
}
if (current_hits<check_at_hits) {
result_unlimited['is_early'] = true;
result_unlimited['stopped_at_hits'] = current_hits;
} else {
result_unlimited['is_early'] = false;
result_unlimited['stopped_at_hits'] = current_hits;
}
} // if result_unlimited empty & is significant current result
if (current_hits>check_at_hits && Object.keys(result_unlimited).length > 0) break;
if (current_hits > 1000000) break;
} // while
//log_results(i,control_segment_hits, control_segment_conversion_hits,test_segment_hits,test_segment_conversion_hits);
result['result_unlimited'] = result_unlimited;
result['result_limited_time'] = result_limited_time;
return result;
}
function run_many_simulations(number_of_simulations,check_at_hits) {
var overall_result = {};
overall_result['result_limited_time_significant_error'] = 0;
overall_result['result_limited_time_significant_noerror'] = 0;
overall_result['result_limited_time_notsignificant'] = 0;
overall_result['result_unlimited_time_significant_error'] = 0;
overall_result['result_unlimited_time_significant_noerror'] = 0;
overall_result['result_unlimited_time_significant_error_is_early'] = 0;
overall_result['result_unlimited_time_significant_error_is_late'] = 0;
overall_result['result_unlimited_time_significant_noerror_is_early'] = 0;
overall_result['result_unlimited_time_significant_noerror_is_late'] = 0;
var simulation_result = {};
for (var i = 0; i < number_of_simulations; i++) {
simulation_result = run_one_simulation(check_at_hits);
if (simulation_result['result_limited_time']['is_result_significant'] == true) {
if (simulation_result['result_limited_time']['is_experiment_error'] == true) {
overall_result['result_limited_time_significant_error'] = overall_result['result_limited_time_significant_error'] + 1;
} else {
overall_result['result_limited_time_significant_noerror'] = overall_result['result_limited_time_significant_noerror'] + 1;
}//simulation_result['result_limited_time']['is_experiment_error'] == true
} else {
overall_result['result_limited_time_notsignificant'] = overall_result['result_limited_time_notsignificant'] +1;
} // simulation_result['result_limited_time']['is_result_significant'] == true
if (simulation_result['result_unlimited']['is_result_significant'] == true) {
if (simulation_result['result_unlimited']['is_experiment_error'] == true) {
overall_result['result_unlimited_time_significant_error'] = overall_result['result_unlimited_time_significant_error'] + 1;
if (simulation_result['result_unlimited']['is_early'] == true) {
overall_result['result_unlimited_time_significant_error_is_early'] = overall_result['result_unlimited_time_significant_error_is_early'] + 1;
} else {
overall_result['result_unlimited_time_significant_error_is_late'] = overall_result['result_unlimited_time_significant_error_is_late'] + 1;
}
} else {
overall_result['result_unlimited_time_significant_noerror'] = overall_result['result_unlimited_time_significant_noerror'] + 1;
if (simulation_result['result_unlimited']['is_early'] == true) {
overall_result['result_unlimited_time_significant_noerror_is_early'] = overall_result['result_unlimited_time_significant_noerror_is_early'] + 1;
} else {
overall_result['result_unlimited_time_significant_noerror_is_late'] = overall_result['result_unlimited_time_significant_noerror_is_late'] + 1;
}
}
}
} //end one simulation
return overall_result;
}
console.log(run_many_simulations(10000,26000));
// https://www.optimizely.com/sample-size-calculator/
</script>
</body>
</html>