class CalibratedMax(MaxCore):
"""Maximum of fitted monotone cores after mapping each one through the CDF of its
scores on local benign traffic: ``max_i F_i(s_i)``.
A plain maximum with one threshold is dominated by the part with the highest benign
scores, and recalibrating the threshold silences the others. Each ``F_i`` is
non-decreasing, so the combination stays monotone (and certifiable). ``fit`` takes
benign flows only (local calibration); the parts are not refitted.
"""
local = True # fitted per test day on benign traffic of the other days
# Optional (set by the caller): exclude sub-cores that cannot reach this FPR on the
# local benign traffic, because more than that share of benign flows ties at their
# maximum score (a saturated model, as on CTU-13). Such a sub-core cannot separate
# anything at the target rate and only adds false alarms; METIS likewise drops a
# metric whose two references do not separate. The maximum over the remaining
# sub-cores is still monotone.
exclude_fpr: float | None = None
def fit(self, X, y=None):
self.ref_ = [np.sort(m.predict_proba(X[:, idx])[:, 1]) for idx, m in self.parts]
self.active_ = [True] * len(self.ref_)
if self.exclude_fpr is not None:
for i, ref in enumerate(self.ref_):
at_max = float((ref >= ref[-1]).mean())
self.active_[i] = at_max <= self.exclude_fpr
if not any(self.active_):
self.active_ = [True] * len(self.ref_)
return self
def part_scores(self, X):
"""Calibrated value of every part, (parts, n); inactive parts are -inf."""
u = []
active = getattr(self, "active_", [True] * len(self.ref_))
for (idx, m), ref, on in zip(self.parts, self.ref_, active):
if not on:
u.append(np.full(len(X), -np.inf))
continue
s = m.predict_proba(X[:, idx])[:, 1]
f = np.searchsorted(ref, s, side="right") / len(ref)
# Beyond the benign maximum keep ranking by the score instead of saturating.
u.append(np.where(s > ref[-1], 1.0 + (s - ref[-1]), f))
return np.vstack(u)
def score(self, X):
return self.part_scores(X).max(axis=0)
def predict_proba(self, X):
s = self.score(X)
p = s / (1.0 + s) # monotone map to [0, 1) for code that expects probabilities
return np.stack([1 - p, p], axis=1)