Statistics¶
Various methods needed to evaluate experiment.
Source code in src/epstats/toolkit/statistics.py
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 |
|
false_positive_risk(null_hypothesis_rate, power, p_value)
staticmethod
¶
Computes false positive risk defined as:
where \(S\) is a statisically significant outcome, \(H_0\) is the null hypothesis, \(1 - \beta\) is the power of a test, and \(\pi\) is the global null hypothesis rate defined as the proportion of all tests in an experimentation program that have not improved or degraded the primary metric.
False positive risk \(P(H_0|S)\) is not the same as the false positive rate \(P(S|H_0) = \alpha\).
More information can be found in the paper: https://bit.ly/ABTestingIntuitionBusters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
null_hypothesis_rate |
float
|
global null hypothesis rate of the experimanation program |
required |
current_power |
power achieved in the test |
required | |
confidence_level |
confidence level of the test |
required |
Returns:
Type | Description |
---|---|
float
|
false positive risk |
Source code in src/epstats/toolkit/statistics.py
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 |
|
multiple_comparisons_correction(df, n_variants, metrics, confidence_level)
classmethod
¶
Holm-Bonferroni correction for multiple comparisons problem. It is applied when we have more than two variants, i.e. we have one control variant and at least two treatment variants.
It adjusts p-value and length of confidence interval - both to be more conservative. Complete manual
Algorithm:
For each metric, select (unadjusted) p-values and replace them with adjusted ones. Based on adjustment ratio, compute new (adjusted) confidence intervals and replace old (unadjusted) ones.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
dataframe as output of |
required |
n_variants |
int
|
number of variants in the experiment |
required |
metrics |
int
|
number of metrics of experiment |
required |
confidence_level |
float
|
desired confidence level at the end of the experiment, e.g. 0.95 |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
dataframe of the same format as input with adjusted p-values and confidence intervals. |
Source code in src/epstats/toolkit/statistics.py
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 |
|
obf_alpha_spending_function(confidence_level, total_length, actual_day)
classmethod
¶
O'Brien-Fleming alpha spending function. We adjust confidence level in time in experiment. Confidence level in this setting is a decreasing function of experiment time. See Sequential Analysis for details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
confidence_level |
int
|
required confidence level at the end of the test, e.g. 0.95 |
required |
total_length |
int
|
length of the test in days, e.g. 7, 14, 21 |
required |
actual_day |
int
|
actual days in the experiment period, must be between 1 and |
required |
Returns:
Type | Description |
---|---|
int
|
adjusted confidence level with respect to actual day of the experiment and total |
int
|
length of the experiment. |
Source code in src/epstats/toolkit/statistics.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
power_from_required_sample_size_per_variant(n_variants, sample_size_per_variant, required_sample_size_per_variant, required_power=DEFAULT_POWER, required_confidence_level=DEFAULT_CONFIDENCE_LEVEL)
staticmethod
¶
Computes power based on the ratio of sample_size_per_variant
and required_sample_size_per_variant
.
How does it work? Consider the formula for computing the sample size \(N\) for a given \(\alpha\) and \(1-\beta\):
We can define the required sample size \(N_R\) to reach 80% power as
The ratio \(\frac{N}{N_r}\) simplifies to
This means that the power can be computed as
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_variants |
int
|
number of variants in the experiment |
required |
sample_size_per_variant |
Union[int, float]
|
number of samples in one variant |
required |
required_sample_size_per_variant |
Union[int, float]
|
number of samples required to reach the
|
required |
required_confidence_level |
float
|
confidence level used to compute the
|
DEFAULT_CONFIDENCE_LEVEL
|
required_power |
float
|
power used to compute the |
DEFAULT_POWER
|
Returns:
Type | Description |
---|---|
float
|
power |
Source code in src/epstats/toolkit/statistics.py
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 |
|
required_sample_size_per_variant(n_variants, minimum_effect, mean, std, std_2=None, confidence_level=DEFAULT_CONFIDENCE_LEVEL, power=DEFAULT_POWER)
staticmethod
¶
Computes the sample size required to reach the defined confidence_level
and power
.
Uses the following formula:
where \(\Delta = \mathrm{MEI}\mu_1\). When std_2
is unknown,
we assume equal variance \(s_1^2 = s_2^2\):
For confidence_level = 0.95
and power = 0.8
:
$$
N = \frac{7.84 * 2s_1^2}{\Delta^2} = \frac{15.7s_1^2}{\Delta^2}
$$
The calculation is using Bonferroni correction when n_variants > 2
. The initial
\(\alpha\) defined by the confidence_level
parameter is adjusted to
where \(m\) is the number of treatment variants. This correction produces greater total sample size than Holm-Bonferroni correction because it assigns the most conservative \(\alpha^{*}\) to all variants.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_variants |
int
|
number of variants in the experiment |
required |
minimum_effect |
float
|
minimum (relative) effect that we find meaningful to detect |
required |
mean |
float
|
estimate of the current population mean, also known as rate in case of Bernoulli distribution |
required |
std |
float
|
estimate of the current population standard deviation |
required |
std_2 |
Optional[float]
|
estimate of the treatment population standard deviation |
None
|
confidence_level |
float
|
confidence level of the test |
DEFAULT_CONFIDENCE_LEVEL
|
power |
float
|
power of the test |
DEFAULT_POWER
|
Returns:
Type | Description |
---|---|
Union[int, float]
|
required sample size |
Source code in src/epstats/toolkit/statistics.py
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 |
|
required_sample_size_per_variant_bernoulli(n_variants, minimum_effect, mean, confidence_level=DEFAULT_CONFIDENCE_LEVEL, power=DEFAULT_POWER, **unused_kwargs)
classmethod
¶
Computes the sample size required to reach the defined confidence_level
and power
when the data follow Bernoulli distribution.
Uses Statistics.required_sample_size_per_variant
with std_2
defined as
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_variants |
int
|
number of variants in the experiment |
required |
minimum_effect |
float
|
minimum (relative) effect that we find meaningful to detect |
required |
mean |
float
|
estimate of the current population mean, also known as rate in case of Bernoulli distribution |
required |
confidence_level |
float
|
confidence level of the test |
DEFAULT_CONFIDENCE_LEVEL
|
power |
float
|
power of the test |
DEFAULT_POWER
|
Returns:
Type | Description |
---|---|
Union[int, float]
|
required sample size |
Source code in src/epstats/toolkit/statistics.py
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 |
|
ttest_evaluation(stats, control_variant)
classmethod
¶
Testing statistical significance of relative difference in means of treatment and control variant.
This is inspired by scipy.stats.ttest_ind_from_stats method that returns many more statistics than p-value and test statistic.
Statistics used:
- Welch's t-test
- Welch–Satterthwaite equation approximation of degrees of freedom.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stats |
array
|
array with dimensions (metrics, variants, stats) |
required |
control_variant |
str
|
string with the name of control variant |
required |
stats
array values:
metric_id
metric_name
exp_variant_id
count
mean
std
sum_value
sum_sqr_value
Returns:
Type | Description |
---|---|
DataFrame
|
dataframe containing statistics from the t-test |
Schema of returned dataframe:
metric_id
- metric id as inExperiment
definitionmetric_name
- metric name as inExperiment
definitionexp_variant_id
- variant idcount
- number of exposures, value of metric denominatormean
-sum_value
/count
std
- sample standard deviationsum_value
- value of goals, value of metric nominatorconfidence_level
- current confidence level used to calculatep_value
andconfidence_interval
diff
- relative diff between sample means of this and control varianttest_stat
- value of test statistic of the relative difference in meansp_value
- p-value of the test statistic under currentconfidence_level
confidence_interval
- confidence interval of thediff
under currentconfidence_level
standard_error
- standard error of thediff
degrees_of_freedom
- degrees of freedom of this variant mean
Source code in src/epstats/toolkit/statistics.py
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 |
|