cjd_flows.scores

Anomaly / OOD scores and detection metrics.

Convention: higher scores mean “more anomalous”, and detection metrics treat the out-of-distribution / anomalous samples as the positive class.

Scores (anomaly_scores()):

  • nll – −log p(x).

  • latent_norm – ‖f(x) − loc‖_p of the latent code (base.norm_of if the base provides it, else base.loc/base.p if present, else L2). For a CJD flow with a radially decreasing base this ranks samples exactly like nll.

  • typicality – −log p_R(r) with r the latent norm, if the base has a radial norm distribution (base.norm.log_prob); for StandardNormal the chi distribution is used. Otherwise a fitted TypicalityScorer gives |nll − mean_ref nll|.

  • hdr – F_R(r) (base.norm.cdf; chi CDF for StandardNormal). For radially decreasing bases this is the probability mass of the highest density region whose boundary passes through x (an HDR p-value is 1 − hdr).

Metrics: auroc(), aupr(), fpr_at_tpr() in pure torch.

class cjd_flows.scores.TypicalityScorer[source]

Bases: object

Reference-based typicality score |nll(x) − mean_ref nll|.

Usage:

ts = TypicalityScorer().fit(flow, x_train)
s = ts.score(flow, x_test)
__init__()[source]
cjd_flows.scores.anomaly_scores(flow, x, kinds=None, batch_size=1024, context=None, reference=None, device=None)[source]

Computes anomaly scores (higher = more anomalous) batch-wise.

Parameters:
  • flow (Module) – a Flow.

  • x (Union[Tensor, Dataset, DataLoader]) – tensor, dataset (x or (x, y) items) or DataLoader.

  • kinds (Optional[Sequence[str]]) – subset of ("nll", "latent_norm", "typicality", "hdr"). None computes every score that is available for this flow; explicitly requested but unavailable scores raise ValueError.

  • batch_size (int) – evaluation batch size.

  • context (Optional[Tensor]) – optional context for conditional flows (default: flow default).

  • reference (Optional[TypicalityScorer]) – fitted TypicalityScorer, used for typicality if the base has no radial norm distribution.

  • device (Union[str, device, None]) – evaluation device (default: the flow’s device).

Return type:

Dict[str, Tensor]

Returns:

{kind: (N,) tensor} on CPU.

cjd_flows.scores.aupr(scores_in, scores_out)[source]

Average precision with the out-of-distribution samples as positives (sklearn semantics).

Return type:

float

Parameters:
cjd_flows.scores.auroc(scores_in, scores_out)[source]

Area under the ROC curve, P(s_out > s_in) + ½ P(s_out = s_in) (out = positive).

Return type:

float

Parameters:
cjd_flows.scores.batched_apply(flow, data, fn, batch_size=1024, device=None)[source]

Applies fn(flow, x_batch) -> {name: (B,) tensor} batch-wise and concatenates (on CPU).

Return type:

Dict[str, Tensor]

Parameters:
cjd_flows.scores.detection_metrics(scores_in, scores_out, tpr=0.95)[source]

{"auroc", "aupr", "fpr@95"} (key uses round(100 * tpr)).

Return type:

Dict[str, float]

Parameters:
cjd_flows.scores.fpr_at_tpr(scores_in, scores_out, tpr=0.95)[source]

False-positive rate at the first threshold reaching true-positive rate tpr.

Return type:

float

Parameters:
cjd_flows.scores.latent_norm(base, z)[source]

Norm of latent codes w.r.t. the base: base.norm_of(z) or ‖z − loc‖_p (default L2).

Return type:

Tensor

Parameters:
cjd_flows.scores.one_class_protocol(classes, make_flow, get_data, fit_kwargs=None, kinds=('nll',), batch_size=1024, train_fn=None)[source]

One-class anomaly detection protocol (“train on one class, test vs. the rest”).

For every class c: train, test_in, test_out = get_data(c), a fresh flow make_flow(c) is trained with train_fn(flow, train, **fit_kwargs) (default cjd_flows.train.fit()) and scored on test_in (normal) and test_out (anomalous).

Return type:

Dict[Any, Dict[str, Dict[str, float]]]

Returns:

{c: {kind: {"auroc", "aupr", "fpr@95"}}}.

Parameters: