Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

137 lines
4.6 KiB
Python

from dataclasses import dataclass
from enum import Enum
from typing import Any, Optional
from ..shared.enums import DifferenceType
from ..shared.statistics import AnyStatistic, StatisticError
from .tests import SequentialTwoSidedTTest, StatisticalTest, TestResult, TwoSidedTTest
class TestType(Enum):
"""Available test types."""
TWO_SIDED = "two_sided"
DEFAULT_SEQUENTIAL_TUNING_PARAMETER: float = SequentialTwoSidedTTest.DEFAULT_TUNING_PARAMETER
@dataclass
class FrequentistConfig:
"""Configuration for frequentist testing."""
alpha: float = 0.05
difference_type: DifferenceType = DifferenceType.RELATIVE
test_type: TestType = TestType.TWO_SIDED
sequential_testing_enabled: bool = False
sequential_tuning_parameter: float = DEFAULT_SEQUENTIAL_TUNING_PARAMETER
def __post_init__(self):
"""Validate configuration parameters."""
if not (0 < self.alpha < 1):
raise StatisticError("Alpha must be between 0 and 1")
if self.sequential_testing_enabled and self.sequential_tuning_parameter <= 0:
raise StatisticError("Sequential tuning parameter must be positive")
class FrequentistMethod:
"""
Main class for frequentist A/B testing.
This class provides a high-level interface for running statistical tests
on A/B experiment data using various test types and configurations.
Example:
# Basic usage
method = FrequentistMethod()
result = method.run_test(treatment_stat, control_stat)
# With custom configuration
config = FrequentistConfig(
alpha=0.01,
difference_type=DifferenceType.ABSOLUTE,
test_type=TestType.TWO_SIDED
)
method = FrequentistMethod(config)
result = method.run_test(treatment_stat, control_stat)
"""
def __init__(self, config: Optional[FrequentistConfig] = None):
"""
Initialize FrequentistMethod with configuration.
Args:
config: Configuration object (uses defaults if None)
"""
self.config = config or FrequentistConfig()
def run_test(self, treatment_stat: AnyStatistic, control_stat: AnyStatistic, **kwargs) -> TestResult:
"""
Run statistical test comparing treatment vs control.
Args:
treatment_stat: Treatment group statistic
control_stat: Control group statistic
**kwargs: Additional parameters (overrides config values)
Returns:
TestResult with all statistical outputs
Raises:
StatisticError: If inputs are invalid or test fails
"""
if self.config.test_type == TestType.TWO_SIDED:
if self.config.sequential_testing_enabled:
test: StatisticalTest = SequentialTwoSidedTTest(
alpha=self.config.alpha,
sequential_tuning_parameter=self.config.sequential_tuning_parameter,
)
else:
test = TwoSidedTTest(self.config.alpha)
else:
raise StatisticError(f"Unknown test type: {self.config.test_type}")
try:
return test.run_test(
treatment_stat=treatment_stat,
control_stat=control_stat,
difference_type=self.config.difference_type,
**kwargs,
)
except Exception as e:
raise StatisticError(f"Test execution failed: {str(e)}") from e
def get_summary(self, result: TestResult) -> dict[str, Any]:
"""
Get human-readable summary of test result.
Args:
result: TestResult object
Returns:
Dict with summary information
"""
summary: dict[str, Any] = {
"test_type": result.test_type,
"point_estimate": result.point_estimate,
"confidence_interval": result.confidence_interval,
"p_value": result.p_value,
"is_significant": result.is_significant,
"alpha": result.alpha,
"degrees_of_freedom": result.degrees_of_freedom,
}
# Add interpretation
if self.config.difference_type == DifferenceType.RELATIVE:
summary["interpretation"] = {
"effect_size": f"{result.point_estimate:.1%}",
"effect_direction": "positive" if result.point_estimate > 0 else "negative",
}
elif self.config.difference_type == DifferenceType.ABSOLUTE:
summary["interpretation"] = {
"effect_size": result.point_estimate,
"effect_direction": "positive" if result.point_estimate > 0 else "negative",
}
return summary