File size: 8,762 Bytes
7f01a4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
188
189
190
191
192
193
194
195
196
197
198
199
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
"""
Utilities for offline model comparison with statistical rigor.

This module is intentionally lightweight and sklearn-friendly so it can be used
inside the existing training pipeline without changing model training code.
"""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Dict, Optional, Tuple

import numpy as np
from sklearn.metrics import f1_score


@dataclass(frozen=True)
class ModelRegistration:
    """Container for a registered candidate or champion model."""

    name: str
    version: str
    model: Any


class ModelComparator:
    """
    Compare multiple trained models using bootstrap uncertainty + a paired test.

    Features:
    - Register multiple fitted estimators (sklearn-like .predict).
    - Evaluate each model via bootstrap resampling of a held-out test set to
      produce mean/std F1 and a 95% empirical confidence interval.
    - Compare two models on the same examples using McNemar's test on correctness
      (paired test) to decide whether the winner is statistically significant.
    """

    def __init__(self, significance_level: float = 0.05, random_state: int = 42):
        self.significance_level = float(significance_level)
        self.random_state = int(random_state)
        self._models: Dict[str, ModelRegistration] = {}

    def register_model(self, name: str, model: Any, version: str) -> None:
        """
        Register a fitted model for later evaluation.

        Args:
            name: Human-readable model identifier (must be unique).
            model: Fitted estimator with a .predict(X) method.
            version: Semver-like version string ("1.0.0") or any tag ("previous").
        """
        if not name or not isinstance(name, str):
            raise ValueError("Model name must be a non-empty string.")
        if name in self._models:
            raise ValueError(f"Model '{name}' is already registered.")
        if not hasattr(model, "predict"):
            raise TypeError("Registered model must implement .predict(X).")
        self._models[name] = ModelRegistration(name=name, version=str(version), model=model)

    def get_model(self, name: str) -> Any:
        """Return the underlying estimator for a registered model."""
        return self._models[name].model

    def list_models(self) -> Dict[str, str]:
        """Return mapping of model name -> version."""
        return {k: v.version for k, v in self._models.items()}

    def evaluate_all(
        self,
        X_test: np.ndarray,
        y_test: np.ndarray,
        n_bootstraps: int = 100,
    ) -> Dict[str, Dict[str, Any]]:
        """
        Evaluate all registered models using bootstrap resampling.

        Args:
            X_test: Test features, shape (n_samples, n_features).
            y_test: Test labels, shape (n_samples,).
            n_bootstraps: Number of bootstrap resamples.

        Returns:
            Dict keyed by model name with:
              - version
              - mean_f1, std_f1
              - ci95_low, ci95_high
              - n_bootstraps
        """
        if len(self._models) == 0:
            raise ValueError("No models registered. Call register_model() first.")

        X = np.asarray(X_test)
        y = np.asarray(y_test)
        if X.shape[0] != y.shape[0]:
            raise ValueError("X_test and y_test must have the same number of rows.")
        if X.shape[0] < 2:
            raise ValueError("Need at least 2 test samples for bootstrap evaluation.")

        rng = np.random.default_rng(self.random_state)
        n = X.shape[0]

        # Generate bootstrap indices once so model evaluations are paired.
        bootstrap_indices = [
            rng.integers(0, n, size=n, endpoint=False) for _ in range(int(n_bootstraps))
        ]

        results: Dict[str, Dict[str, Any]] = {}
        for name, reg in self._models.items():
            scores = np.empty(int(n_bootstraps), dtype=float)
            for b in range(int(n_bootstraps)):
                idx = bootstrap_indices[b]
                y_pred = reg.model.predict(X[idx])
                scores[b] = float(f1_score(y[idx], y_pred))

            mean_ = float(np.mean(scores))
            std_ = float(np.std(scores, ddof=1)) if len(scores) > 1 else 0.0
            ci_low, ci_high = np.percentile(scores, [2.5, 97.5]).tolist()
            results[name] = {
                "version": reg.version,
                "mean_f1": mean_,
                "std_f1": std_,
                "ci95_low": float(ci_low),
                "ci95_high": float(ci_high),
                "n_bootstraps": int(n_bootstraps),
            }
        return results

    def compare_models(
        self,
        model_a: str | Any,
        model_b: str | Any,
        X_test: np.ndarray,
        y_test: np.ndarray,
        *,
        use_exact_binomial_if_available: bool = True,
    ) -> Dict[str, Any]:
        """
        Compare two models using McNemar's test (paired test on correctness).

        Args:
            model_a: Registered model name OR a fitted estimator.
            model_b: Registered model name OR a fitted estimator.
            X_test: Test features.
            y_test: Test labels.
            use_exact_binomial_if_available: If True, use an exact binomial test
                for discordant pairs when SciPy is available; otherwise fall back
                to the chi-square approximation.

        Returns:
            Dict with:
              - chi2_stat
              - p_value
              - significant (p_value < significance_level)
              - winner ("model_a" or "model_b" or "tie")
              - contingency: {n11, n10, n01, n00}
        """
        est_a, label_a = self._resolve_model(model_a, default_label="model_a")
        est_b, label_b = self._resolve_model(model_b, default_label="model_b")

        X = np.asarray(X_test)
        y = np.asarray(y_test)

        pred_a = est_a.predict(X)
        pred_b = est_b.predict(X)

        correct_a = (pred_a == y)
        correct_b = (pred_b == y)

        n11 = int(np.sum(correct_a & correct_b))
        n10 = int(np.sum(correct_a & (~correct_b)))  # A correct, B wrong
        n01 = int(np.sum((~correct_a) & correct_b))  # A wrong, B correct
        n00 = int(np.sum((~correct_a) & (~correct_b)))

        discordant = n01 + n10
        if discordant == 0:
            chi2_stat = 0.0
            p_value = 1.0
        else:
            # Chi-square with Edwards continuity correction.
            chi2_stat = float(((abs(n01 - n10) - 1.0) ** 2) / discordant)
            p_value = float(self._mcnemar_p_value(n01, n10, chi2_stat, use_exact_binomial_if_available))

        if n01 > n10:
            winner = label_b
        elif n10 > n01:
            winner = label_a
        else:
            winner = "tie"

        return {
            "chi2_stat": float(chi2_stat),
            "p_value": float(p_value),
            "significant": bool(p_value < self.significance_level),
            "winner": winner,
            "contingency": {"n11": n11, "n10": n10, "n01": n01, "n00": n00},
        }

    def _resolve_model(self, model_or_name: str | Any, default_label: str) -> Tuple[Any, str]:
        if isinstance(model_or_name, str):
            if model_or_name not in self._models:
                raise KeyError(f"Model '{model_or_name}' is not registered.")
            return self._models[model_or_name].model, model_or_name
        if not hasattr(model_or_name, "predict"):
            raise TypeError(f"{default_label} must be a registered name or an estimator with .predict(X).")
        return model_or_name, default_label

    @staticmethod
    def _mcnemar_p_value(n01: int, n10: int, chi2_stat: float, use_exact_binomial_if_available: bool) -> float:
        """
        Compute McNemar p-value for discordant pairs.

        We prefer an exact two-sided binomial test (more accurate for small counts),
        and fall back to the chi-square approximation.
        """
        discordant = n01 + n10
        if discordant == 0:
            return 1.0

        if use_exact_binomial_if_available:
            try:
                from scipy.stats import binomtest  # type: ignore

                k = min(n01, n10)
                return float(binomtest(k=k, n=discordant, p=0.5, alternative="two-sided").pvalue)
            except Exception:
                # SciPy not installed or older version.
                pass

        try:
            from scipy.stats import chi2  # type: ignore

            return float(chi2.sf(chi2_stat, df=1))
        except Exception:
            # Final fallback without SciPy: approximate p-value via exp(-chi2/2).
            # This is a rough approximation for df=1.
            return float(np.exp(-0.5 * chi2_stat))