tea_tasting.multiplicity
#
Multiple hypothesis testing.
MultipleComparisonsResults
#
Bases: UserDict[Any, ExperimentResult]
, PrettyDictsMixin
Multiple comparisons result.
to_pandas()
#
to_pretty(keys=None, formatter=get_and_format_num)
#
Convert the object to a Pandas Dataframe with formatted values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys |
Sequence[str] | None
|
Keys to convert. If a key is not defined in the dictionary
it's assumed to be |
None
|
formatter |
Callable[[dict[str, Any], str], str]
|
Custom formatter function. It should accept a dictionary of metric result attributes and an attribute name, and return a formatted attribute value. |
get_and_format_num
|
Returns:
Type | Description |
---|---|
DataFrame
|
Pandas Dataframe with formatted values. |
Default formatting rules
- If a name starts with
"rel_"
or equals to"power"
consider it a percentage value. Round percentage values to 2 significant digits, multiply by100
and add"%"
. - Round other values to 3 significant values.
- If value is less than
0.001
, format it in exponential presentation. - If a name ends with
"_ci"
, consider it a confidence interval. Look up for attributes"{name}_lower"
and"{name}_upper"
, and format the interval as"[{lower_bound}, {lower_bound}]"
.
Source code in src/tea_tasting/utils.py
to_string(keys=None, formatter=get_and_format_num)
#
Convert the object to a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys |
Sequence[str] | None
|
Keys to convert. If a key is not defined in the dictionary
it's assumed to be |
None
|
formatter |
Callable[[dict[str, Any], str], str]
|
Custom formatter function. It should accept a dictionary of metric result attributes and an attribute name, and return a formatted attribute value. |
get_and_format_num
|
Returns:
Type | Description |
---|---|
str
|
A table with results rendered as string. |
Default formatting rules
- If a name starts with
"rel_"
or equals to"power"
consider it a percentage value. Round percentage values to 2 significant digits, multiply by100
and add"%"
. - Round other values to 3 significant values.
- If value is less than
0.001
, format it in exponential presentation. - If a name ends with
"_ci"
, consider it a confidence interval. Look up for attributes"{name}_lower"
and"{name}_upper"
, and format the interval as"[{lower_bound}, {lower_bound}]"
.
Source code in src/tea_tasting/utils.py
to_html(keys=None, formatter=get_and_format_num)
#
Convert the object to HTML.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys |
Sequence[str] | None
|
Keys to convert. If a key is not defined in the dictionary
it's assumed to be |
None
|
formatter |
Callable[[dict[str, Any], str], str]
|
Custom formatter function. It should accept a dictionary of metric result attributes and an attribute name, and return a formatted attribute value. |
get_and_format_num
|
Returns:
Type | Description |
---|---|
str
|
A table with results rendered as HTML. |
Default formatting rules
- If a name starts with
"rel_"
or equals to"power"
consider it a percentage value. Round percentage values to 2 significant digits, multiply by100
and add"%"
. - Round other values to 3 significant values.
- If value is less than
0.001
, format it in exponential presentation. - If a name ends with
"_ci"
, consider it a confidence interval. Look up for attributes"{name}_lower"
and"{name}_upper"
, and format the interval as"[{lower_bound}, {lower_bound}]"
.
Source code in src/tea_tasting/utils.py
to_dicts()
#
Convert the result to a sequence of dictionaries.
Source code in src/tea_tasting/multiplicity.py
adjust_fdr(experiment_results, metrics=None, *, alpha=None, arbitrary_dependence=True)
#
Adjust p-value and alpha to control the false discovery rate (FDR).
The number of hypotheses tested is the total number of metrics included in the comparison in all experiment results. For example, if there are 3 experiments with 2 metrics in each, the number of hypotheses is 6.
The function performs one of the following corrections, depending on parameters:
- Benjamini-Yekutieli procedure, assuming arbitrary dependence between
hypotheses (
arbitrary_dependence=True
). - Benjamini-Hochberg procedure, assuming non-negative correlation between
hypotheses (
arbitrary_dependence=False
).
The function adds the following attributes to the results
pvalue_adj
: The adjusted p-value, which should be compared with the unadjusted FDR (alpha
).alpha_adj
: The adjusted FDR, which should be compared with the unadjusted p-value (pvalue
).null_rejected
: A binary indicator (0
or1
) that shows whether the null hypothesis is rejected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiment_results |
ExperimentResult | Mapping[Any, ExperimentResult]
|
Experiment results. |
required |
metrics |
str | set[str] | Sequence[str] | None
|
Metrics included in the comparison.
If |
None
|
alpha |
float | None
|
Significance level. If |
None
|
arbitrary_dependence |
bool
|
If |
True
|
Returns:
Type | Description |
---|---|
MultipleComparisonsResults
|
The experiments results with adjusted p-values and alphas. |
Parameter defaults
Default for parameters alpha
can be changed using the config_context
and set_context
functions.
See the Global configuration
reference for details.
References
Examples:
import pandas as pd
import tea_tasting as tt
data = pd.concat((
tt.make_users_data(seed=42, orders_uplift=0.10, revenue_uplift=0.15),
tt.make_users_data(seed=21, orders_uplift=0.15, revenue_uplift=0.20)
.query("variant==1")
.assign(variant=2),
))
print(data)
#> user variant sessions orders revenue
#> 0 0 1 2 1 9.582790
#> 1 1 0 2 1 6.434079
#> 2 2 1 2 1 8.304958
#> 3 3 1 2 1 16.652705
#> 4 4 0 1 1 7.136917
#> ... ... ... ... ... ...
#> 3989 3989 2 4 4 34.931448
#> 3991 3991 2 1 0 0.000000
#> 3992 3992 2 3 3 27.964647
#> 3994 3994 2 2 1 17.217892
#> 3998 3998 2 3 0 0.000000
#>
#> [6046 rows x 5 columns]
experiment = tt.Experiment(
sessions_per_user=tt.Mean("sessions"),
orders_per_session=tt.RatioOfMeans("orders", "sessions"),
orders_per_user=tt.Mean("orders"),
revenue_per_user=tt.Mean("revenue"),
)
# Results without correction.
results = experiment.analyze(data, control=0, all_variants=True)
print(results)
#> variants metric control treatment rel_effect_size rel_effect_size_ci pvalue
#> (0, 1) sessions_per_user 2.00 1.98 -0.66% [-3.7%, 2.5%] 0.674
#> (0, 1) orders_per_session 0.266 0.289 8.8% [-0.89%, 19%] 0.0762
#> (0, 1) orders_per_user 0.530 0.573 8.0% [-2.0%, 19%] 0.118
#> (0, 1) revenue_per_user 5.24 5.99 14% [2.1%, 28%] 0.0212
#> (0, 2) sessions_per_user 2.00 2.02 0.98% [-2.1%, 4.1%] 0.532
#> (0, 2) orders_per_session 0.266 0.295 11% [1.2%, 22%] 0.0273
#> (0, 2) orders_per_user 0.530 0.594 12% [1.7%, 23%] 0.0213
#> (0, 2) revenue_per_user 5.24 6.25 19% [6.6%, 33%] 0.00218
# Success metrics.
metrics = {"orders_per_user", "revenue_per_user"}
# Benjamini-Yekutieli procedure,
# assuming arbitrary dependence between hypotheses.
adjusted_results_fdr = tt.adjust_fdr(results, metrics)
print(adjusted_results_fdr)
#> comparison metric control treatment rel_effect_size pvalue pvalue_adj
#> (0, 1) orders_per_user 0.530 0.573 8.0% 0.118 0.245
#> (0, 1) revenue_per_user 5.24 5.99 14% 0.0212 0.0592
#> (0, 2) orders_per_user 0.530 0.594 12% 0.0213 0.0592
#> (0, 2) revenue_per_user 5.24 6.25 19% 0.00218 0.0182
# The adjusted confidence level alpha.
print(adjusted_results_fdr.to_string(keys=(
"comparison",
"metric",
"control",
"treatment",
"rel_effect_size",
"pvalue",
"alpha_adj",
)))
#> comparison metric control treatment rel_effect_size pvalue alpha_adj
#> (0, 1) orders_per_user 0.530 0.573 8.0% 0.118 0.0240
#> (0, 1) revenue_per_user 5.24 5.99 14% 0.0212 0.0120
#> (0, 2) orders_per_user 0.530 0.594 12% 0.0213 0.0180
#> (0, 2) revenue_per_user 5.24 6.25 19% 0.00218 0.00600
# Benjamini-Hochberg procedure,
# assuming non-negative correlation between hypotheses.
print(tt.adjust_fdr(results, metrics, arbitrary_dependence=False))
#> comparison metric control treatment rel_effect_size pvalue pvalue_adj
#> (0, 1) orders_per_user 0.530 0.573 8.0% 0.118 0.118
#> (0, 1) revenue_per_user 5.24 5.99 14% 0.0212 0.0284
#> (0, 2) orders_per_user 0.530 0.594 12% 0.0213 0.0284
#> (0, 2) revenue_per_user 5.24 6.25 19% 0.00218 0.00873
Source code in src/tea_tasting/multiplicity.py
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 |
|
adjust_fwer(experiment_results, metrics=None, *, alpha=None, arbitrary_dependence=True, method='bonferroni')
#
Adjust p-value and alpha to control the family-wise error rate (FWER).
The number of hypotheses tested is the total number of metrics included in the comparison in all experiment results. For example, if there are 3 experiments with 2 metrics in each, the number of hypotheses is 6.
The function performs one of the following procedures, depending on parameters:
- Holm's step-down procedure, assuming arbitrary dependence between
hypotheses (
arbitrary_dependence=True
). - Hochberg's step-up procedure, assuming non-negative correlation between
hypotheses (
arbitrary_dependence=False
).
The function adds the following attributes to the results
pvalue_adj
: The adjusted p-value, which should be compared with the unadjusted FDR (alpha
).alpha_adj
: The adjusted FWER, which should be compared with the unadjusted p-value (pvalue
).null_rejected
: A binary indicator (0
or1
) that shows whether the null hypothesis is rejected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiment_results |
ExperimentResult | Mapping[Any, ExperimentResult]
|
Experiment results. |
required |
metrics |
str | set[str] | Sequence[str] | None
|
Metrics included in the comparison.
If |
None
|
alpha |
float | None
|
Significance level. If |
None
|
arbitrary_dependence |
bool
|
If |
True
|
method |
Literal['bonferroni', 'sidak']
|
Correction method, Bonferroni ( |
'bonferroni'
|
Returns:
Type | Description |
---|---|
MultipleComparisonsResults
|
The experiments results with adjusted p-values and alphas. |
Parameter defaults
Default for parameters alpha
can be changed using the config_context
and set_context
functions.
See the Global configuration
reference for details.
Examples:
import pandas as pd
import tea_tasting as tt
data = pd.concat((
tt.make_users_data(seed=42, orders_uplift=0.10, revenue_uplift=0.15),
tt.make_users_data(seed=21, orders_uplift=0.15, revenue_uplift=0.20)
.query("variant==1")
.assign(variant=2),
))
print(data)
#> user variant sessions orders revenue
#> 0 0 1 2 1 9.582790
#> 1 1 0 2 1 6.434079
#> 2 2 1 2 1 8.304958
#> 3 3 1 2 1 16.652705
#> 4 4 0 1 1 7.136917
#> ... ... ... ... ... ...
#> 3989 3989 2 4 4 34.931448
#> 3991 3991 2 1 0 0.000000
#> 3992 3992 2 3 3 27.964647
#> 3994 3994 2 2 1 17.217892
#> 3998 3998 2 3 0 0.000000
#>
#> [6046 rows x 5 columns]
experiment = tt.Experiment(
sessions_per_user=tt.Mean("sessions"),
orders_per_session=tt.RatioOfMeans("orders", "sessions"),
orders_per_user=tt.Mean("orders"),
revenue_per_user=tt.Mean("revenue"),
)
# Results without correction.
results = experiment.analyze(data, control=0, all_variants=True)
print(results)
#> variants metric control treatment rel_effect_size rel_effect_size_ci pvalue
#> (0, 1) sessions_per_user 2.00 1.98 -0.66% [-3.7%, 2.5%] 0.674
#> (0, 1) orders_per_session 0.266 0.289 8.8% [-0.89%, 19%] 0.0762
#> (0, 1) orders_per_user 0.530 0.573 8.0% [-2.0%, 19%] 0.118
#> (0, 1) revenue_per_user 5.24 5.99 14% [2.1%, 28%] 0.0212
#> (0, 2) sessions_per_user 2.00 2.02 0.98% [-2.1%, 4.1%] 0.532
#> (0, 2) orders_per_session 0.266 0.295 11% [1.2%, 22%] 0.0273
#> (0, 2) orders_per_user 0.530 0.594 12% [1.7%, 23%] 0.0213
#> (0, 2) revenue_per_user 5.24 6.25 19% [6.6%, 33%] 0.00218
# Success metrics.
metrics = {"orders_per_user", "revenue_per_user"}
# Holm's step-down procedure with Bonferroni correction,
# assuming arbitrary dependence between hypotheses.
adjusted_results_fwer = tt.adjust_fwer(results, metrics)
print(adjusted_results_fwer)
#> comparison metric control treatment rel_effect_size pvalue pvalue_adj
#> (0, 1) orders_per_user 0.530 0.573 8.0% 0.118 0.118
#> (0, 1) revenue_per_user 5.24 5.99 14% 0.0212 0.0635
#> (0, 2) orders_per_user 0.530 0.594 12% 0.0213 0.0635
#> (0, 2) revenue_per_user 5.24 6.25 19% 0.00218 0.00873
# The adjusted confidence level alpha.
print(adjusted_results_fwer.to_string(keys=(
"comparison",
"metric",
"control",
"treatment",
"rel_effect_size",
"pvalue",
"alpha_adj",
)))
#> comparison metric control treatment rel_effect_size pvalue alpha_adj
#> (0, 1) orders_per_user 0.530 0.573 8.0% 0.118 0.0167
#> (0, 1) revenue_per_user 5.24 5.99 14% 0.0212 0.0167
#> (0, 2) orders_per_user 0.530 0.594 12% 0.0213 0.0167
#> (0, 2) revenue_per_user 5.24 6.25 19% 0.00218 0.0125
# Hochberg's step-up procedure with Šidák correction,
# assuming non-negative correlation between hypotheses.
print(tt.adjust_fwer(
results,
metrics,
arbitrary_dependence=False,
method="sidak",
))
#> comparison metric control treatment rel_effect_size pvalue pvalue_adj
#> (0, 1) orders_per_user 0.530 0.573 8.0% 0.118 0.118
#> (0, 1) revenue_per_user 5.24 5.99 14% 0.0212 0.0422
#> (0, 2) orders_per_user 0.530 0.594 12% 0.0213 0.0422
#> (0, 2) revenue_per_user 5.24 6.25 19% 0.00218 0.00870
Source code in src/tea_tasting/multiplicity.py
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 |
|