Taxonomy
taxonomy
Standardized metrics taxonomy, calculation engines, and variance reduction routines.
This module houses the core representation of experimental measurements in xpyrment.
It implements a modular metrics hierarchy to support continuous (MeanMetric), binary
(ProportionMetric), and aggregate ratio (RatioMetric) metrics. All metrics support Welch's
t-test for hypothesis testing, and the continuous and ratio engines support integrated
CUPED (Controlled-comparison Using Pre-Existing Data) for variance reduction.
Mathematical Background
- Welch's t-test: Unlike Student's t-test, Welch's t-test does not assume equal variances between control and treatment groups. The test statistic is: $$ t = \frac{\bar{Y}_T - \bar{Y}_C}{\sqrt{\frac{s_C^2}{N_C} + \frac{s_T^2}{N_T}}} $$ And degrees of freedom \(\nu\) are approximated using the Welch-Satterthwaite equation: $$ \nu \approx \frac{\left(\frac{s_C^2}{N_C} + \frac{s_T^2}{N_T}\right)^2}{\frac{\left(s_C^2 / N_C\right)^2}{N_C - 1} + \frac{\left(s_T^2 / N_T\right)^2}{N_T - 1}} $$
-
CUPED (Controlled-comparison Using Pre-Existing Data): CUPED utilizes pre-experiment covariate data (\(X\)) to explain away pre-existing variance in the experiment period metric (\(Y\)), thereby increasing statistical power. $$ Y_{\text{CUPED}} = Y - \theta (X - E[X]) $$ where \(\theta\) is the optimal scaling factor computed as: $$ \theta = \frac{\text{Cov}(Y, X)}{\text{Var}(X)} $$ The variance of the CUPED-adjusted metric is: $$ \text{Var}(Y_{\text{CUPED}}) = \text{Var}(Y)(1 - \rho^2) $$ where \(\rho\) is the Pearson correlation coefficient between \(Y\) and \(X\).
-
Delta Method for Ratios: For a ratio metric \(R = U / V\) (e.g., clicks/impressions), the sample variance is approximated using a first-order Taylor expansion: $$ \text{Var}(R) \approx \frac{1}{\mu_V^2} \text{Var}(U) + \frac{\mu_U^2}{\mu_V^4} \text{Var}(V) - 2 \frac{\mu_U}{\mu_V^3} \text{Cov}(U, V) $$
| CLASS | DESCRIPTION |
|---|---|
BaseMetric |
Abstract base class representing a statistical metric in the experiment taxonomy. |
MeanMetric |
A metric representing a continuous or numeric value (e.g., average revenue, sessions). |
ProportionMetric |
A metric representing a binary/proportion rate (e.g., conversion rate, success rate). |
RatioMetric |
A metric calculated as the ratio: sum(numerator) / sum(denominator) (e.g., Click-Through-Rate). |
BaseMetric
Bases: ABC
Abstract base class representing a statistical metric in the experiment taxonomy.
All custom metrics must inherit from BaseMetric and implement the abstract .calculate()
method to return a standardized MetricResult dictionary.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
The unique descriptive name of the metric.
TYPE:
|
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Unique descriptive name of the metric.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
calculate |
Abstract method to compute statistics for control and treatment groups. |
Source code in src\xpyrment\metrics\taxonomy.py
calculate
abstractmethod
Abstract method to compute statistics for control and treatment groups.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
The experimental dataset.
TYPE:
|
treatment_col
|
Column name identifying experimental groups/arms.
TYPE:
|
control
|
The value in
TYPE:
|
treatment
|
The value in
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: A compliant |
Source code in src\xpyrment\metrics\taxonomy.py
MeanMetric
Bases: BaseMetric
A metric representing a continuous or numeric value (e.g., average revenue, sessions).
Supports optional pre-period CUPED (Controlled-comparison Using Pre-Existing Data) adjustment to explain away pre-existing variance and dramatically lower required sample sizes or runtimes.
| ATTRIBUTE | DESCRIPTION |
|---|---|
value_col |
The column in the DataFrame containing active experiment period values.
TYPE:
|
pre_period_col |
The column containing pre-experiment baseline values for CUPED.
TYPE:
|
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Unique descriptive name of the metric.
TYPE:
|
value_col
|
Column name containing experiment period values.
TYPE:
|
pre_period_col
|
Column name containing pre-experiment baseline values for CUPED. Defaults to None (no CUPED applied).
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
calculate |
Calculates descriptive and Welch's t-test statistics for the mean metric. |
Source code in src\xpyrment\metrics\taxonomy.py
calculate
calculate(
df: DataFrame,
treatment_col: str,
control: str,
treatment: str,
alpha: float = 0.05,
) -> Dict[str, Any]
Calculates descriptive and Welch's t-test statistics for the mean metric.
Drops missing values on the value column. If pre_period_col is provided,
performs joint missing drop and executes a standard linear CUPED adjustment:
| PARAMETER | DESCRIPTION |
|---|---|
df
|
The experimental dataset.
TYPE:
|
treatment_col
|
Column identifying treatment assignments.
TYPE:
|
control
|
Control arm identifier value in
TYPE:
|
treatment
|
Treatment arm identifier value in
TYPE:
|
alpha
|
Significance level for Welch's confidence intervals. Defaults to 0.05.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: A completed |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If either control or treatment group becomes empty after filtering. |
Source code in src\xpyrment\metrics\taxonomy.py
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 | |
ProportionMetric
Bases: MeanMetric
A metric representing a binary/proportion rate (e.g., conversion rate, success rate).
Inherits continuous logic from MeanMetric, as proportions can be modelled asymptotically
using normal approximations (Z-test/t-test) under the Central Limit Theorem.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Unique descriptive name of the metric.
TYPE:
|
value_col
|
Column name containing experiment period values.
TYPE:
|
pre_period_col
|
Column name containing pre-experiment baseline values for CUPED. Defaults to None (no CUPED applied).
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
calculate |
Calculates proportion conversion rates, differences, and statistical significance. |
Source code in src\xpyrment\metrics\taxonomy.py
calculate
calculate(
df: DataFrame,
treatment_col: str,
control: str,
treatment: str,
alpha: float = 0.05,
) -> Dict[str, Any]
Calculates proportion conversion rates, differences, and statistical significance.
Drops missing values, calculates means and variances of binary inputs, and delegates
to MeanMetric.calculate while overriding the metric type string to "Proportion".
| PARAMETER | DESCRIPTION |
|---|---|
df
|
The experimental dataset.
TYPE:
|
treatment_col
|
Column identifying treatment assignments.
TYPE:
|
control
|
Control arm identifier value.
TYPE:
|
treatment
|
Treatment arm identifier value.
TYPE:
|
alpha
|
Significance level. Defaults to 0.05.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Standardized results dict with "metric_type" set to "Proportion". |
Source code in src\xpyrment\metrics\taxonomy.py
RatioMetric
RatioMetric(
name: str,
numerator_col: str,
denominator_col: str,
pre_numerator_col: Optional[str] = None,
pre_denominator_col: Optional[str] = None,
)
Bases: BaseMetric
A metric calculated as the ratio: sum(numerator) / sum(denominator) (e.g., Click-Through-Rate).
Employs the Delta Method to approximate ratio-level variances and supports double-covariate ratio-level CUPED adjustments to independently reduce variance in numerator and denominator.
| ATTRIBUTE | DESCRIPTION |
|---|---|
numerator_col |
The column containing active period numerator values.
TYPE:
|
denominator_col |
The column containing active period denominator values (must be \(>0\)).
TYPE:
|
pre_numerator_col |
Column containing pre-experiment baseline numerator values.
TYPE:
|
pre_denominator_col |
Column containing pre-experiment baseline denominator values.
TYPE:
|
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Unique descriptive name.
TYPE:
|
numerator_col
|
Active numerator column name.
TYPE:
|
denominator_col
|
Active denominator column name.
TYPE:
|
pre_numerator_col
|
Pre-experiment numerator column. Defaults to None.
TYPE:
|
pre_denominator_col
|
Pre-experiment denominator column. Defaults to None.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
calculate |
Calculates ratio values, Delta-method variances, and statistical significance. |
Source code in src\xpyrment\metrics\taxonomy.py
calculate
calculate(
df: DataFrame,
treatment_col: str,
control: str,
treatment: str,
alpha: float = 0.05,
) -> Dict[str, Any]
Calculates ratio values, Delta-method variances, and statistical significance.
Cleans missing values and non-positive denominators. If double-covariates are present, separately fits linear CUPED adjustments to the numerator and denominator series:
The ratio variance is then estimated using the Delta Method formulation:
| PARAMETER | DESCRIPTION |
|---|---|
df
|
The experimental dataset.
TYPE:
|
treatment_col
|
Column identifying treatment assignments.
TYPE:
|
control
|
Control arm identifier value.
TYPE:
|
treatment
|
Treatment arm identifier value.
TYPE:
|
alpha
|
Significance level. Defaults to 0.05.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Completed |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If either control or treatment group becomes empty after filtering. |
Source code in src\xpyrment\metrics\taxonomy.py
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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | |