Skip to content
Projects

About

A Python library for anomaly detection with statistical guarantees. Wraps any PyOD or scikit-learn detector, controls false alarms (FDR/FPR), and monitors streams for change points.

Science & Engineering

How these counts are measured

Measured star growth

in 24h
0
in 7 days
0
in 15 days
Collecting history
in 1 month
Collecting history

Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.

README.md

Original

nonconform

Turn anomaly scores into flags with a false-alarm rate you set.

PyPI version Supported Python versions Tests Code coverage Documentation License

Documentation · API Reference · Paper


A detector ranks points by how unusual they look but leaves the cutoff to you. The usual default, flagging the top 10% of scores, is a guess about how many anomalies there are. nonconform calibrates the scores of your existing detector against normal data, so the threshold follows from the error rate you can accept.

Works with     scikit-learn      PyOD      Custom AnomalyDetector protocol

The same Isolation Forest on seven benchmark datasets, with and without nonconform:

True anomalies and false alarms flagged on seven datasets: the detector alone flags about twice the real anomalies with 47 to 58 percent false; nonconform flags fewer, with 4 to 9 percent false

Isolation Forest from PyOD; "detector alone" uses its default predict() threshold, "+ nonconform" uses select(alpha=0.1). Mean of 20 random splits per benchmark dataset. The average false share stays under the 10% target on every dataset; where the detector barely separates anomalies (Mammography, Cardio), nonconform flags little rather than guess. The baseline trains on every row, nonconform holds some back for calibration. Reproduce with default_thresholds.py; the benchmark page adds a certified threshold and per-split results.

Installation

nonconform requires Python 3.11 or newer.

pip install nonconform
Optional extras
Extra Adds
pyod PyOD anomaly detectors
data oddball benchmark datasets and PyArrow support
fdr Streaming FDR procedures from online-fdr
probabilistic KDE-based probabilistic estimation and tuning
all Every optional feature

Quick start

Wrap a detector, fit it on normal data, and select anomalies at a target false discovery rate (FDR). The example loads a benchmark dataset, so it needs pip install "nonconform[data]":

from oddball import Dataset, load
from sklearn.ensemble import IsolationForest

from nonconform import ConformalDetector, Split

# Normal-only training data; a labelled test batch with 10% anomalies
x_train, x_test, y_test = load(Dataset.SHUTTLE, setup=True, seed=0)

alpha = 0.1  # target FDR: on average at most 10% of flags are false
detector = ConformalDetector(
    detector=IsolationForest(random_state=0),
    strategy=Split(n_calib=1_000),  # normal rows held back for calibration
    seed=0,
).fit(x_train)

flagged = detector.select(x_test, alpha=alpha)  # Boolean mask
hits = int(y_test[flagged].sum())
print(f"Flagged {flagged.sum()} of {len(x_test)}, {hits} true anomalies")
# Flagged 105 of 1000, 95 true anomalies

Any PyOD model, a recognized scikit-learn estimator, or a custom detector works in place of IsolationForest.

Set a score threshold

To flag no more than 5% of normal points, certify a raw-score threshold instead:

certificate = detector.fpr_bounds(confidence=0.95)
selected = certificate.select(detector.score_samples(x_test), target_fpr=0.05)
print(f"Certified threshold: {certificate.threshold_for(0.05)}")

With 95% probability over the calibration draw, the threshold flags at most 5% of future normal points. Calibration and future normal scores must be i.i.d.; targets below the certificate's band floor (about 3.85% for 1,000 calibration scores at 95% confidence) give an infinite threshold. See raw-score false-alarm control.

Monitor a stream

A fitted Split detector can start a monitor that alarms once data drifts from calibration.

Monitor a stream for change
import numpy as np
from sklearn.ensemble import IsolationForest

from nonconform import ConformalDetector, Split
from nonconform.martingales import AlarmConfig, SimpleJumperMartingale
from nonconform.monitoring import ExchangeabilityMonitor

rng = np.random.default_rng(42)
x_train = rng.normal(size=(1_000, 2))

detector = ConformalDetector(
    detector=IsolationForest(random_state=42),
    strategy=Split(n_calib=0.3),
    seed=42,
).fit(x_train)

alpha = 0.05
monitor = ExchangeabilityMonitor.from_split_detector(
    detector,
    martingale=SimpleJumperMartingale(
        alarm_config=AlarmConfig(restarted_ville_threshold=1 / alpha)
    ),
    seed=42,
)

# Stable observations followed by a distribution shift
x_stream = np.vstack([
    rng.normal(size=(50, 2)),
    rng.normal(loc=3.0, size=(50, 2)),
])

for x_t in x_stream:
    state = monitor.update(x_t)
    if "restarted_ville" in state.triggered_alarms:
        print(f"Change alarm at step {state.evidence_step}")
        break

Under the sequential validity assumptions, the restarted Ville alarm at 1 / alpha bounds the probability of ever raising a false alarm on one stream. See the sequential monitoring guide.

Choose a workflow

Goal Start with
Calibrate and select anomalies in a batch Split and select(...)
Set a per-inlier raw-score false-alarm target fpr_bounds(...) and FPR certificates
Aggregate evidence across random splits DerandomizedSplits and e-BH
Monitor a stream for change Exchangeability martingales
Reuse more data for fitting and calibration CrossValidation or JackknifeBootstrap
Account for covariate shift Weighted conformal inference
Calibrate groups with different normal behavior Group-conditional (Mondrian) p-values
Certify realized FDP across p-value thresholds fdp_bounds(...) and FDP certificates
Bring a custom or third-party detector Detector compatibility

Coding agents

An agent skill teaches coding agents to use nonconform without overclaiming. Install it in Claude Code (/plugin marketplace add OliverHennhoefer/nonconform, then /plugin install nonconform@nonconform) or Codex (codex plugin marketplace add OliverHennhoefer/nonconform, then codex plugin add nonconform@nonconform).

Statistical scope

[!IMPORTANT] Conformal inference needs remarkably little: no distributional model, and its guarantees hold in finite samples. The few assumptions it does make are worth knowing. Conformal p-values need calibration data and normal test points to be exchangeable; FDR claims need the assumptions of the chosen multiple-testing procedure; FPR certificates need i.i.d. scores from an independently fitted detector; weighted workflows need a credible covariate-shift model; Ville alarms cover one stream, while CUSUM and Shiryaev-Roberts thresholds need separate calibration.

nonconform calibrates detector scores; it cannot make an unsuitable detector or a mismatched calibration set valid. Handle spatial or temporal dependence before relying on exchangeability. Read the guides to FDR control and sequential monitoring before applying a guarantee to a new problem.

Citation

If you use nonconform in academic work, please cite the accompanying paper:

@inproceedings{Hennhoefer2026,
author = {Hennhöfer, Oliver and Kirsch, Maximilian and Preisach, Christine},
booktitle = {Proceedings of the Fifteenth Symposium on Conformal and Probabilistic Prediction with Applications},
pages = {613--632},
publisher = {PMLR},
title = {{Conformal Anomaly Detection in Python: Moving Beyond Heuristic Thresholds with nonconform}},
url = {https://proceedings.mlr.press/v329/hennhofer26a.html},
year = {2026}
}

Project

Read the documentation, browse the changelog, or report a problem in the issue tracker. Contributions are welcome; start with the contributing guide. nonconform is distributed under the BSD 3-Clause License.

Funding acknowledgement

Explore related projects

Loading…