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‖_pof the latent code (base.norm_ofif the base provides it, elsebase.loc/base.pif present, else L2). For a CJD flow with a radially decreasing base this ranks samples exactly likenll.typicality–−log p_R(r)withrthe latent norm, if the base has a radialnormdistribution (base.norm.log_prob); forStandardNormalthe chi distribution is used. Otherwise a fittedTypicalityScorergives|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 throughx(an HDR p-value is1 − hdr).
Metrics: auroc(), aupr(), fpr_at_tpr() in pure torch.
- class cjd_flows.scores.TypicalityScorer[source]¶
Bases:
objectReference-based typicality score
|nll(x) − mean_ref nll|.Usage:
ts = TypicalityScorer().fit(flow, x_train) s = ts.score(flow, x_test)
- 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:
x (
Union[Tensor,Dataset,DataLoader]) – tensor, dataset (xor(x, y)items) or DataLoader.kinds (
Optional[Sequence[str]]) – subset of("nll", "latent_norm", "typicality", "hdr").Nonecomputes every score that is available for this flow; explicitly requested but unavailable scores raiseValueError.batch_size (
int) – evaluation batch size.context (
Optional[Tensor]) – optional context for conditional flows (default: flow default).reference (
Optional[TypicalityScorer]) – fittedTypicalityScorer, used fortypicalityif the base has no radial norm distribution.device (
Union[str,device,None]) – evaluation device (default: the flow’s device).
- Return type:
- 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).
- 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).
- 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).
- cjd_flows.scores.detection_metrics(scores_in, scores_out, tpr=0.95)[source]¶
{"auroc", "aupr", "fpr@95"}(key usesround(100 * tpr)).
- 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.
- 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).
- 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 flowmake_flow(c)is trained withtrain_fn(flow, train, **fit_kwargs)(defaultcjd_flows.train.fit()) and scored ontest_in(normal) andtest_out(anomalous).- Return type:
- Returns:
{c: {kind: {"auroc", "aupr", "fpr@95"}}}.- Parameters:
get_data (Callable[[Any], Tuple[Tensor | Dataset | DataLoader, Tensor | Dataset | DataLoader, Tensor | Dataset | DataLoader]])
batch_size (int)