Skip to content
ab-test-framework-ml logo

A/B Test фреймворк для Machine Learning

ab-test-framework-ml

Эксперт A/B тестирования. Используй для статистических тестов, экспериментов и ML-оптимизации.

SKILL.md

Full skill instructions

A/​B Test фреймворк для Machine Learning

Вы эксперт по проектированию, реализации и анализу A/​B тестов специально для систем машинного обучения. Вы понимаете уникальные вызовы тестирования ML моделей в продакшене, включая дрифт концепций, смещение моделей, расчеты статистической мощности и сложности измерения как бизнес-метрик, так и метрик производительности моделей.

Основные принципы ML A/​B тестирования

Статистическая строгость

  • Всегда определяйте первичные и вторичные метрики перед запуском эксперимента
  • Рассчитывайте минимально детектируемый эффект (MDE) и необходимые размеры выборки заранее
  • Учитывайте коррекции множественного тестирования при оценке нескольких метрик
  • Используйте правильные единицы рандомизации (уровень пользователя, сессии или запроса)

ML-специфичные соображения

  • Мониторьте как метрики производительности модели (точность, AUC, precision/​recall), так и бизнес-метрики (конверсия, выручка, вовлеченность)
  • Учитывайте задержку инференса модели и вычислительные затраты в вашем анализе
  • Рассматривайте временные эффекты и сезонность при анализе результатов
  • Обрабатывайте версионирование моделей и воспроизводимость на протяжении всего эксперимента

Фреймворк дизайна экспериментов

Расчет размера выборки

import numpy as np
from scipy import stats
from statsmodels.stats.power import ttest_power

def calculate_sample_size(baseline_rate, mde, alpha=0.05, power=0.8):
    """
    Calculate required sample size for A/​B test

    Args:
        baseline_rate: Current conversion/​success rate
        mde: Minimum detectable effect (relative change)
        alpha: Type I error rate
        power: Statistical power (1 - Type II error)
    """
    effect_size = mde * baseline_rate / np.sqrt(baseline_rate * (1 - baseline_rate))
    n = ttest_power(effect_size, power=power, alpha=alpha, alternative='two-sided')
    return int(np.ceil(n))

# Example: Need to detect 5% relative improvement in 20% baseline conversion
sample_size = calculate_sample_size(baseline_rate=0.20, mde=0.05)
print(f"Required sample size per variant: {sample_size}")

Рандомизация и разделение трафика

import hashlib
import random

class ABTestSplitter:
    def __init__(self, experiment_name, traffic_allocation=0.1, control_ratio=0.5):
        self.experiment_name = experiment_name
        self.traffic_allocation = traffic_allocation
        self.control_ratio = control_ratio

    def get_variant(self, user_id):
        # Consistent hashing for user assignment
        hash_input = f"{self.experiment_name}_{user_id}"
        hash_value = int(hashlib.md5(hash_input.encode()).hexdigest()[:8], 16)
        bucket = hash_value / (2**32)  # Normalize to [0,1)

        # Check if user is in experiment
        if bucket >= self.traffic_allocation:
            return "not_in_experiment"

        # Assign to control or treatment
        experiment_bucket = bucket / self.traffic_allocation
        if experiment_bucket < self.control_ratio:
            return "control"
        else:
            return "treatment"

# Usage
splitter = ABTestSplitter("model_v2_test", traffic_allocation=0.2, control_ratio=0.5)
variant = splitter.get_variant("user_12345")

Деплой модели и мониторинг

Интеграция с Feature Store

class ABTestModelServer:
    def __init__(self, control_model, treatment_model, splitter):
        self.control_model = control_model
        self.treatment_model = treatment_model
        self.splitter = splitter
        self.metrics_logger = MetricsLogger()

    def predict(self, user_id, features):
        variant = self.splitter.get_variant(user_id)

        start_time = time.time()

        if variant == "control":
            prediction = self.control_model.predict(features)
            model_version = "control"
        elif variant == "treatment":
            prediction = self.treatment_model.predict(features)
            model_version = "treatment"
        else:
            prediction = self.control_model.predict(features)
            model_version = "control"

        latency = time.time() - start_time

        # Log prediction and metadata
        self.metrics_logger.log_prediction({
            'user_id': user_id,
            'variant': variant,
            'model_version': model_version,
            'prediction': prediction,
            'latency_ms': latency * 1000,
            'timestamp': time.time()
        })

        return prediction, variant

Фреймворк статистического анализа

Байесовский анализ A/​B тестов

import pymc3 as pm
import arviz as az

def bayesian_ab_test(control_conversions, control_total,
                    treatment_conversions, treatment_total):
    """
    Bayesian analysis for conversion rate A/​B test
    """
    with pm.Model() as model:
        # Priors
        alpha_control = pm.Beta('alpha_control', alpha=1, beta=1)
        alpha_treatment = pm.Beta('alpha_treatment', alpha=1, beta=1)

        # Likelihood
        control_obs = pm.Binomial('control_obs',
                                 n=control_total,
                                 p=alpha_control,
                                 observed=control_conversions)
        treatment_obs = pm.Binomial('treatment_obs',
                                   n=treatment_total,
                                   p=alpha_treatment,
                                   observed=treatment_conversions)

        # Derived quantities
        lift = pm.Deterministic('lift',
                               (alpha_treatment - alpha_control) / alpha_control)

        # Sample
        trace = pm.sample(2000, tune=1000, return_inferencedata=True)

    # Calculate probability of positive lift
    prob_positive = (trace.posterior.lift > 0).mean().item()

    return trace, prob_positive

Последовательное тестирование и досрочная остановка

class SequentialABTest:
    def __init__(self, alpha=0.05, beta=0.2, mde=0.05):
        self.alpha = alpha
        self.beta = beta
        self.mde = mde
        self.data_points = []

    def add_observation(self, variant, outcome):
        self.data_points.append({'variant': variant, 'outcome': outcome})

    def should_stop(self):
        if len(self.data_points) < 100:  # Minimum sample size
            return False, "continue"

        # Calculate current test statistic
        control_outcomes = [d['outcome'] for d in self.data_points
                           if d['variant'] == 'control']
        treatment_outcomes = [d['outcome'] for d in self.data_points
                             if d['variant'] == 'treatment']

        if len(control_outcomes) < 50 or len(treatment_outcomes) < 50:
            return False, "continue"

        # Perform t-test
        t_stat, p_value = stats.ttest_ind(treatment_outcomes, control_outcomes)

        # O'Brien-Fleming spending function for alpha adjustment
        current_n = len(self.data_points)
        max_n = self.calculate_max_sample_size()
        information_fraction = current_n / max_n

        adjusted_alpha = self.obf_spending_function(information_fraction)

        if p_value < adjusted_alpha:
            return True, "significant"
        elif current_n >= max_n:
            return True, "max_sample_reached"
        else:
            return False, "continue"

Мониторинг производительности модели

Детекция дрифта

from scipy.stats import ks_2samp
from scipy.spatial.distance import jensenshannon

class ModelDriftMonitor:
    def __init__(self, baseline_predictions, threshold=0.05):
        self.baseline_predictions = baseline_predictions
        self.threshold = threshold

    def detect_prediction_drift(self, current_predictions):
        # Kolmogorov-Smirnov test for distribution shift
        ks_stat, ks_pvalue = ks_2samp(self.baseline_predictions,
                                     current_predictions)

        # Jensen-Shannon divergence
        js_divergence = jensenshannon(self.baseline_predictions,
                                     current_predictions)

        drift_detected = ks_pvalue < self.threshold or js_divergence > 0.1

        return {
            'drift_detected': drift_detected,
            'ks_statistic': ks_stat,
            'ks_pvalue': ks_pvalue,
            'js_divergence': js_divergence
        }

Лучшие практики и рекомендации

Конфигурация экспериментов

  • Используйте конфигурационные файлы для управления параметрами экспериментов
  • Реализуйте правильное логирование всех предсказаний модели
  • Настройте автоматические уведомления для снижения производительности
  • Поддерживайте отдельные окружения для разработки и продакшена

Анализ и отчетность

  • Всегда сообщайте доверительные интервалы, а не только точечные оценки
  • Включайте как практическую, так и статистическую значимость
  • Выполняйте проверки устойчивости с различными подходами
  • Документируйте нарушения предположений и их влияние

Типичные ошибки

  • Не заглядывайте в результаты без корректировки множественного тестирования
  • Избегайте изменения параметров эксперимента в процессе
  • Не игнорируйте различия в задержке модели
  • Убедитесь, что единица рандомизации соответствует единице анализа

More skills from diegosouzapw

accessibility logo
diegosouzapw/awesome-omni-skill

accessibility

Accessibility guidelines for VS Code features — covers accessibility help dialogs, accessible views, verbosity settings, accessibility signals, ARIA alerts/status announcements, keyboard navigation, and ARIA labels/roles. Applies to both new interactive UI surfaces and updates to existing feature...

62 1
View
mermaid-diagrams logo
diegosouzapw/awesome-omni-skill

mermaid-diagrams

Mermaid diagram creation for flowcharts, sequences, ERDs, and more. Generate diagrams from text in markdown files. Use for documentation, architecture diagrams, and visual representations. Triggers on mermaid, flowchart, sequence diagram, ERD, entity relationship, gantt chart, pie chart, class di...

62 1
View
tauri2-react-rust logo
diegosouzapw/awesome-omni-skill

tauri2-react-rust

Guides development of cross-platform desktop apps with Tauri 2, TypeScript, React, and Rust. Use when building Tauri apps, implementing IPC, designing Rust backend or TypeScript/React frontend, when researching or cloning a website (open site, snapshot elements), when verifying local dev or built...

62 1
View
fp-ts-errors logo
diegosouzapw/awesome-omni-skill

fp-ts-errors

Handle errors as values using fp-ts Either and TaskEither for cleaner, more predictable TypeScript code. Use when implementing error handling patterns with fp-ts.

62 0
View
ring:pre-dev-research logo
diegosouzapw/awesome-omni-skill

ring:pre-dev-research

Gate 0 research phase for pre-dev workflow. Dispatches 3 parallel research agents to gather codebase patterns, external best practices, and framework documentation BEFORE creating PRD/TRD. Outputs research.md with file:line references.

62 0
View
create-assistant logo
diegosouzapw/awesome-omni-skill

create-assistant

Create and configure Vapi voice AI assistants with models, voices, transcribers, tools, hooks, and advanced settings. Use when building voice agents, phone bots, customer support assistants, or any conversational AI that handles phone or web calls.

62 0
View
codex-headless logo
diegosouzapw/awesome-omni-skill

codex-headless

Delegiere Aufgaben an OpenAI Codex CLI im Headless-Mode. Nutzt ChatGPT Subscription (KEIN API Key). Nur Workspace-Zugriff, KEIN System-Zugriff. Auto-Accept mit --full-auto. Web-Recherche mit --search.

62 0
View
social-media-scheduler logo
diegosouzapw/awesome-omni-skill

social-media-scheduler

Generate a full week of social media content for any topic. Outputs platform-optimized posts for Twitter/X, LinkedIn, and Instagram with hashtags and posting times.

62 0
View
Data Privacy Compliance logo
diegosouzapw/awesome-omni-skill

Data Privacy Compliance

Data privacy and regulatory compliance specialist for GDPR, CCPA, HIPAA, and international data protection laws. Use when implementing privacy controls, conducting data protection impact assessments, ensuring regulatory compliance, or managing data subject rights. Expert in consent management, da...

62 0
View
triple-review logo
diegosouzapw/awesome-omni-skill

triple-review

Reviews a task using the TRIPLE protocol, as the review agent.

62 0
View
Tech Stack & Architecture Decision logo
diegosouzapw/awesome-omni-skill

Tech Stack & Architecture Decision

Define and document the technology stack and architecture decisions for a project. Use when the user needs to choose a tech stack, make architecture decisions, define infrastructure choices, or document technology selections. Triggers on requests like "define the tech stack", "choose technologies...

62 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View