tea_tasting.metrics.resampling
#
Metrics analyzed using resampling methods.
Bootstrap(columns, statistic, *, alternative=None, confidence_level=None, n_resamples=None, method='bca', batch=None, random_state=None)
#
Bases: MetricBaseGranular[BootstrapResult]
Metric for analysis of a statistic using bootstrap resampling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
str | Sequence[str]
|
Names of the columns to be used in the analysis. |
required |
statistic |
Callable[..., NDArray[number[Any]]]
|
Statistic. It must be a vectorized callable
that accepts a NumPy array as the first argument and returns
the resulting statistic.
It must also accept a keyword argument |
required |
alternative |
Literal['two-sided', 'greater', 'less'] | None
|
Alternative hypothesis. |
None
|
confidence_level |
float | None
|
Confidence level for the confidence interval. |
None
|
n_resamples |
int | None
|
The number of resamples performed to form the bootstrap distribution of the statistic. |
None
|
method |
Literal['percentile', 'basic', 'bca']
|
Whether to return the "percentile" bootstrap confidence
interval ( |
'bca'
|
batch |
int | None
|
The number of resamples to process in each vectorized call
to statistic. Memory usage is O( |
None
|
random_state |
int | Generator | SeedSequence | None
|
Pseudorandom number generator state used to generate resamples. |
None
|
Multiple columns
If columns
is a sequence of strings, then the sample passed
to the statistic callable contains an extra dimension in the first axis.
See examples below.
Parameter defaults
Defaults for parameters alternative
, confidence_level
,
and n_resamples
can be changed using the
config_context
and set_context
functions.
See the Global configuration
reference for details.
Examples:
import numpy as np
import tea_tasting as tt
experiment = tt.Experiment(
orders_per_user=tt.Bootstrap("orders", np.mean, random_state=42),
)
data = tt.make_users_data(seed=42)
result = experiment.analyze(data)
print(result)
#> metric control treatment rel_effect_size rel_effect_size_ci pvalue
#> orders_per_user 0.530 0.573 8.0% [-1.8%, 19%] -
With multiple columns:
def ratio_of_means(sample, axis):
means = np.mean(sample, axis=axis)
return means[0] / means[1]
experiment = tt.Experiment(
orders_per_session=tt.Bootstrap(
("orders", "sessions"),
ratio_of_means,
random_state=42,
),
)
data = tt.make_users_data(seed=42)
result = experiment.analyze(data)
print(result)
#> metric control treatment rel_effect_size rel_effect_size_ci pvalue
#> orders_per_session 0.266 0.289 8.8% [-0.61%, 20%] -
Source code in src/tea_tasting/metrics/resampling.py
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 |
|
cols: Sequence[str]
property
#
Columns to be fetched for a metric analysis.
analyze(data, control, treatment, variant=None)
#
Analyze a metric in an experiment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | Table | dict[Any, DataFrame]
|
Experimental data. |
required |
control |
Any
|
Control variant. |
required |
treatment |
Any
|
Treatment variant. |
required |
variant |
str | None
|
Variant column name. |
None
|
Returns:
Type | Description |
---|---|
R
|
Analysis result. |
Source code in src/tea_tasting/metrics/base.py
analyze_dataframes(control, treatment)
#
Analyze metric in an experiment using granular data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
control |
DataFrame
|
Control data. |
required |
treatment |
DataFrame
|
Treatment data. |
required |
Returns:
Type | Description |
---|---|
BootstrapResult
|
Analysis result. |
Source code in src/tea_tasting/metrics/resampling.py
BootstrapResult
#
Bases: NamedTuple
Result of the analysis using bootstrap resampling.
Attributes:
Name | Type | Description |
---|---|---|
control |
float
|
Control statistic value. |
treatment |
float
|
Treatment statistic value. |
effect_size |
float
|
Absolute effect size. Difference between the two statistic values. |
effect_size_ci_lower |
float
|
Lower bound of the absolute effect size confidence interval. |
effect_size_ci_upper |
float
|
Upper bound of the absolute effect size confidence interval. |
rel_effect_size |
float
|
Relative effect size. Difference between the two statistic values, divided by the control statistic value. |
rel_effect_size_ci_lower |
float
|
Lower bound of the relative effect size confidence interval. |
rel_effect_size_ci_upper |
float
|
Upper bound of the relative effect size confidence interval. |
Quantile(column, q=0.5, *, alternative=None, confidence_level=None, n_resamples=None, method='basic', batch=None, random_state=None)
#
Bases: Bootstrap
Metric for the analysis of quantiles using bootstrap resampling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
column |
str
|
Name of the column for the quantiles to compute. |
required |
q |
float
|
Probability for the quantiles to compute. |
0.5
|
alternative |
Literal['two-sided', 'greater', 'less'] | None
|
Alternative hypothesis. |
None
|
confidence_level |
float | None
|
Confidence level for the confidence interval. |
None
|
n_resamples |
int | None
|
The number of resamples performed to form the bootstrap distribution of the statistic. |
None
|
method |
Literal['percentile', 'basic', 'bca']
|
Whether to return the "percentile" bootstrap confidence
interval ( |
'basic'
|
batch |
int | None
|
The number of resamples to process in each vectorized call
to statistic. Memory usage is O( |
None
|
random_state |
int | Generator | SeedSequence | None
|
Pseudorandom number generator state used to generate resamples. |
None
|
Parameter defaults
Defaults for parameters alternative
, confidence_level
,
and n_resamples
can be changed using the
config_context
and set_context
functions.
See the Global configuration
reference for details.
Default method
Default method is "basic" which is different from default
method "bca" in Bootstrap
. The "bca" confidence intervals cannot
be calculated when the bootstrap distribution is degenerate
(e.g. all elements are identical). This is often the case for the
quantile metrics.
Examples:
import tea_tasting as tt
experiment = tt.Experiment(
revenue_per_user_p80=tt.Quantile("revenue", 0.8, random_state=42),
)
data = tt.make_users_data(seed=42)
result = experiment.analyze(data)
print(result)
#> metric control treatment rel_effect_size rel_effect_size_ci pvalue
#> revenue_per_user_p80 10.6 11.6 9.1% [-1.3%, 21%] -
Source code in src/tea_tasting/metrics/resampling.py
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 |
|
cols: Sequence[str]
property
#
Columns to be fetched for a metric analysis.
analyze(data, control, treatment, variant=None)
#
Analyze a metric in an experiment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DataFrame | Table | dict[Any, DataFrame]
|
Experimental data. |
required |
control |
Any
|
Control variant. |
required |
treatment |
Any
|
Treatment variant. |
required |
variant |
str | None
|
Variant column name. |
None
|
Returns:
Type | Description |
---|---|
R
|
Analysis result. |
Source code in src/tea_tasting/metrics/base.py
analyze_dataframes(control, treatment)
#
Analyze metric in an experiment using granular data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
control |
DataFrame
|
Control data. |
required |
treatment |
DataFrame
|
Treatment data. |
required |
Returns:
Type | Description |
---|---|
BootstrapResult
|
Analysis result. |