river icon indicating copy to clipboard operation
river copied to clipboard

check_roc_auc passes for a detector that returns a constant score

Open mateenali66 opened this issue 3 months ago • 2 comments

check_roc_auc is the detection-quality check that runs for every AnomalyDetector via check_estimator, and it asserts roc_auc_score(labels, scores) >= 0.5. A detector that returns the same score for every sample gets exactly 0.5, so it passes.

from river import datasets
from river.base import AnomalyDetector
from river.checks.anomaly import check_roc_auc

class Constant(AnomalyDetector):
    def learn_one(self, x):
        pass
    def score_one(self, x):
        return 0.5

check_roc_auc(Constant(), datasets.CreditCard().take(1000))  # passes

Two things compound here.

The bar is inclusive and the degenerate case lands exactly on it. sklearn returns 0.5 for all-tied scores, so the check cannot tell "rank anomalies above normal points" (its docstring) from "ignores the input".

The fixture is CreditCard().take(1000), which holds 2 positive labels. With 2 positives the check has almost no power against a random scorer either: uniform random scores pass on 108 of 200 seeds (river 0.26.1, numpy default_rng, seeds 0 to 199). CreditCard's prevalence is 0.173%, and its first 20 positives happen to sit inside the first 6,821 rows, so take(7000) is enough to give the assertion some power.

Two changes would close this:

  1. Make the bound strict. A constant scorer lands on exactly 0.5, so > 0.5 already excludes the degenerate case. On its own that stays brittle with 2 positives, which is why the fixture matters more.
  2. Enlarge the fixture so it holds enough positives. Runtime is not a concern, 6,821 rows run in well under a second per detector, and the memory check already streams 10,000.

One caveat to be upfront about. On the first 6,821 rows a bare OneClassSVM() scores AUC 0.16 while HalfSpaceTrees scores 0.85, so the longer fixture surfaces a real below-chance case rather than staying green. That is arguably the check doing its job, but it means the change is not a pure test tweak. This also does not collide with #1970, which rewrites the file but keeps both the >= 0.5 bound and the 1,000-row fixture.

This is the anomaly-module version of the baseline point I raised on #1914. I benchmark anomaly detectors on OpenTelemetry infrastructure data (writeup: https://doi.org/10.1109/ACCESS.2026.3705430) and the recurring result is that detectors which never beat a data-ignoring baseline still look fine on evaluations without one.

Happy to open a PR if this direction works for you.

mateenali66 avatar Aug 31 '26 04:08 mateenali66

Hi @mateenali66 , I'd like to work on this issue as my first contribution to River. The proposed changes to make the ROC-AUC bound strict and increase the CreditCard fixture size make sense to me. Would it be okay if I open a PR implementing this? Thanks!

ashish-4169 avatar Sep 21 '26 17:09 ashish-4169

Hi — I would like to pick this up. My plan is the strict > 0.5 bound proposed in the issue. I measured the fixture enlargement idea (take(1000) -> take(7000)) and decided against it: at larger fixtures OneClassSVM gets an AUC of only 0.41 at take(2000) and 0.16 at take(7000), so it would fail its own check, and LOF check time blows up quadratically, with no extra power against random scorers. I will keep the existing fixture and add a regression test for the constant-score case. PR follows shortly.

Vinayak19112003 avatar Sep 21 '26 19:09 Vinayak19112003