Experiment
experiment
Central orchestrator for experiment setup, configuration, and phase management.
This module houses the Experiment class, which acts as the core controller and lifecycle
coordinator for both industrial A/B testing and classical Design of Experiments. It encapsulates
the underlying pandas DataFrame, tracks added metrics, and enforces phase-gating rules using
the ExperimentState state machine.
| CLASS | DESCRIPTION |
|---|---|
Experiment |
The central orchestration class for setting up, configuring, and executing experiments. |
Experiment
Experiment(
data: DataFrame,
treatment_col: str,
id_col: Optional[str] = None,
covariates: Optional[List[str]] = None,
)
The central orchestration class for setting up, configuring, and executing experiments.
The Experiment class binds the experimental dataset, defines treatment structures, maps
the metric taxonomy, and strictly enforces state transitions across the execution lifecycle.
Through the state-machine rules, it ensures that all calculations are performed sequentially
and reproducibly, eliminating retrospective tampering or incorrect state usage.
| ATTRIBUTE | DESCRIPTION |
|---|---|
data |
A copy of the input DataFrame containing assignments and telemetry.
TYPE:
|
treatment_col |
The column in
TYPE:
|
id_col |
The column in
TYPE:
|
metrics |
List of metrics registered for statistical calculation.
TYPE:
|
state |
The current lifecycle phase of the experiment.
TYPE:
|
State Gating Mechanism
Execution functions across downstream submodules verify that the experiment is in the
appropriate state before proceeding. For example, running power analysis transitions the
state from CREATED to PLANNED. Running randomization moves from PLANNED to DESIGNED.
Analyzing results requires a transition to ANALYZED.
Examples:
Example
>>> import pandas as pd
>>> from xpyrment import Experiment
>>> from xpyrment.metrics.taxonomy import MeanMetric
>>> df = pd.DataFrame({"user_id": [1, 2, 3], "group": ["control", "treatment", "control"], "revenue": [10.5, 12.0, 9.5]})
>>> exp = Experiment(df, treatment_col="group", id_col="user_id")
>>> exp.state
<ExperimentState.CREATED: 'CREATED'>
>>> metric = MeanMetric("Revenue Metric", value_col="revenue")
>>> exp.add_metrics(metric)
>>> exp.state
<ExperimentState.PLANNED: 'PLANNED'>
Copies the input DataFrame to guarantee immutability of the source dataset during internal state transitions and potential data transformations (e.g., CUPED alignment or log scaling).
| PARAMETER | DESCRIPTION |
|---|---|
data
|
The source DataFrame containing unit-level data.
TYPE:
|
treatment_col
|
Name of the column designating experimental groups/arms.
TYPE:
|
id_col
|
Name of the column containing unique identifiers for each experimental unit. Required for certain operations like sequential analysis and user assignments.
TYPE:
|
covariates
|
List of baseline covariates for balance checking or adjustments.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
| METHOD | DESCRIPTION |
|---|---|
transition_to |
Enforces transition logic to guarantee the phase-gated execution flow. |
add_metrics |
Adds statistical metrics to the experiment configuration. |
add_covariates |
Adds baseline covariates to the experiment configuration. |
register_metric |
Conveniently registers a metric and appends it to the configuration. |
Source code in src\xpyrment\core\experiment.py
transition_to
transition_to(target_state: ExperimentState) -> None
Enforces transition logic to guarantee the phase-gated execution flow.
Uses the ordinal indices of ExperimentState members to verify that the transition is
monotonically increasing (forward-only).
Mathematical/Logical Representation: Let \(S\) be the ordered tuple of states: $$ S = (\text{CREATED}, \text{PLANNED}, \text{DESIGNED}, \text{RUNNING}, \text{ANALYZED}, \text{REPORTED}) $$ A state transition from state \(s_1\) to state \(s_2\) is valid if and only if: $$ \text{Index}(s_2) \ge \text{Index}(s_1) $$ with a special exemption permitting \(s_1 = \text{ANALYZED} \rightarrow s_2 = \text{ANALYZED}\) to support re-running statistical engines on the locked design data.
| PARAMETER | DESCRIPTION |
|---|---|
target_state
|
The state the experiment is attempting to transition into.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
PhaseOrderError
|
If a backwards state transition is attempted, or if transition is otherwise unauthorized. |
Source code in src\xpyrment\core\experiment.py
add_metrics
add_metrics(
metrics: Union[BaseMetric, List[BaseMetric]],
) -> Experiment
Adds statistical metrics to the experiment configuration.
Successfully registering a metric moves the experiment from CREATED to PLANNED state, representing
that the evaluation criteria have been defined prior to running designs, validations, or analyses.
| PARAMETER | DESCRIPTION |
|---|---|
metrics
|
A single metric object or a list of metrics
(inheriting from
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Experiment
|
The experiment instance itself (for fluent API chaining).
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
PhaseOrderError
|
If the experiment has already progressed past the |
Source code in src\xpyrment\core\experiment.py
add_covariates
add_covariates(names: Union[str, List[str]]) -> Experiment
Adds baseline covariates to the experiment configuration.
| PARAMETER | DESCRIPTION |
|---|---|
names
|
A single covariate column name or a list of names.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Experiment
|
The experiment instance itself (for fluent API chaining).
TYPE:
|
Source code in src\xpyrment\core\experiment.py
register_metric
register_metric(
name: str,
metric_type: str = "mean",
value_col: Optional[str] = None,
covariate: Optional[str] = None,
numerator_col: Optional[str] = None,
denominator_col: Optional[str] = None,
pre_numerator_col: Optional[str] = None,
pre_denominator_col: Optional[str] = None,
) -> Experiment
Conveniently registers a metric and appends it to the configuration.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Unique descriptive name of the metric.
TYPE:
|
metric_type
|
Type of metric. Options: "mean", "proportion", "ratio". Defaults to "mean".
TYPE:
|
value_col
|
Column name containing experiment period values. Defaults to the metric name.
TYPE:
|
covariate
|
Pre-period covariate column name for CUPED. Defaults to None.
TYPE:
|
numerator_col
|
Column containing numerator values for RatioMetric.
TYPE:
|
denominator_col
|
Column containing denominator values for RatioMetric.
TYPE:
|
pre_numerator_col
|
Pre-period numerator column for RatioMetric CUPED.
TYPE:
|
pre_denominator_col
|
Pre-period denominator column for RatioMetric CUPED.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Experiment
|
The experiment instance itself (for fluent API chaining).
TYPE:
|