robustmargin
Robustness margins for AI safety evaluation: how much stress an AI system takes before it fails, with a confidence interval.
robustmargin is a robustness calculation engine. AI systems are usually evaluated by a success rate under nominal conditions; a safety case needs a different number: how far conditions can degrade before the system starts to fail, and how sure we are of that point. robustmargin estimates that robustness margin, with a confidence interval, from stress tests at increasing severity, scored pass/fail or on a continuous scale.
It is model-agnostic and framework-free: it sees only severities and outcomes (success counts or real-valued scores), and needs no GPU and no ML framework. That makes it usable wherever robustness and safety carry weight:
- AI safety research: measure how much perturbation a model or policy tolerates, and whether a training intervention moved that margin.
- Physical AI: learned policies evaluated under graded sensing noise, latency or disturbances.
- Autonomous and safety-critical systems: margins reported with honest uncertainty, and censored bounds instead of invented numbers when the data cannot support an estimate.
Status: latest release
0.3.0. Dose-response fits for pass/fail outcomes that fall or rise with severity and for real-valued scores (Gaussian least squares), thresholds with profile-likelihood or delta confidence intervals, censoring, ratios of two thresholds with Fieller or log-delta intervals, fit diagnostics, and the grid break-point rule. Clustered and paired inference, model-free fits and design helpers are planned for0.4. While the version is below 1.0, a minor release may change the public API.
What it does
You test a system at increasing levels of some stress (the severity), such as input noise, a perturbation, a disturbance or a distribution shift, and count successes at each level (or record a score for each run). robustmargin fits a dose-response curve to those outcomes and reports the severity at which performance crosses a level you choose, with a confidence interval. When the data cannot support a number, it says so: a threshold beyond the tested range is censored and reported as a bound, and a failed fit carries a status flag and no estimate.
The statistics are dose-response threshold estimation, the method toxicology and psychophysics use to find the dose at which a response crosses a given level. That is why the second worked example below uses a classic toxicology dataset: the code is the same whatever the system under test.
For control engineers, the threshold is an empirical analogue of the probabilistic robustness margin of randomized robust control (Barmish, Lagoa & Tempo, ACC 1997; Chen, Zhou & Aravena, SIAM J. Control Optim. 2008). That margin is the largest radius at which a known model still succeeds with probability 1 − ε, averaged over every perturbation up to that radius. robustmargin needs no model: it estimates the corresponding point from success counts at held severities, with a confidence interval.
How it fits into an evaluation stack
robustmargin does not run models. Your policy or model evaluator does, and a thin adapter turns its results into robustmargin's input:
- The evaluator applies a graded degradation (input noise, a perturbation, a disturbance, a distribution shift) at a set of severity levels.
- It records the outcome of each run: pass/fail, or a real-valued score.
- The adapter hands robustmargin the severities and the success counts or scores; robustmargin returns the threshold, its confidence interval, its censoring status and fit diagnostics.
Because the interface is only severities and outcomes, the same adapter pattern works for any
evaluator. A typical use is comparing checkpoints, for example before and after RL
post-training: ratio_interval gives a confidence interval for the ratio of their margins, so
"the new checkpoint is more robust" becomes a claim with an interval attached.
Install
Requires Python 3.11 or later.
pip install robustmargin
Runtime dependencies are numpy, scipy, and statsmodels, with loose minimum versions.
Worked example: robustness before and after RL post-training
Two checkpoints of the same policy, before and after RL post-training, are evaluated on a task at increasing levels of Gaussian input noise: 200 runs per level, each scored pass or fail. The counts are illustrative. The question a safety case asks is not "what is the success rate?" but "how much noise can each checkpoint take before success falls below 90%, and did post-training move that point?"
from robustmargin import (
Axis,
Censoring,
Definition,
Observations,
Status,
fit_dose_response,
ratio_interval,
threshold,
)
axis = Axis(name="input_noise", unit="std", scale="log")
noise = [0.0, 0.05, 0.1, 0.2, 0.4, 0.8]
runs = [200] * len(noise)
passed = {
"before_rl": [196, 193, 181, 142, 61, 9],
"after_rl": [197, 196, 192, 177, 119, 38],
}
margins = {}
for name, successes in passed.items():
obs = Observations.from_counts(
axis,
severity=noise,
successes=successes,
trials=runs,
outcome="task_success",
direction="decreasing",
)
# Clean-input success is below 100%, so the upper asymptote is estimated, not fixed at 1.
fit = fit_dose_response(obs, model="binomial", link="probit", upper="estimate", lower=0.0)
assert fit.status is Status.OK
margins[name] = threshold(
fit, definition=Definition.absolute(0.9), interval_method="profile", level=0.95
)
# The noise level at which success falls to 90%, with a 95% profile-likelihood interval.
before, after = margins["before_rl"], margins["after_rl"]
assert before.censoring is Censoring.NONE and after.censoring is Censoring.NONE
assert 0.096 < before.lo < before.value < before.hi < 0.130 # about 0.11
assert 0.159 < after.lo < after.value < after.hi < 0.217 # about 0.19
# Did RL post-training move the margin? The ratio of the two margins, with its own interval.
ratio = ratio_interval(after, before, method="log_delta", dependence="independent")
assert 1.35 < ratio.lo < ratio.estimate < ratio.hi < 2.06 # about 1.67x, interval excludes 1
When the data cannot support a number, robustmargin does not invent one. A checkpoint that never drops below the criterion within the tested range gets a censored bound, not an estimate:
from robustmargin import (
Axis,
Censoring,
Definition,
Observations,
fit_dose_response,
threshold,
)
obs = Observations.from_counts(
Axis(name="input_noise", unit="std", scale="log"),
severity=[0.0, 0.05, 0.1, 0.2, 0.4, 0.8],
successes=[200, 200, 199, 200, 199, 198],
trials=[200] * 6,
outcome="task_success",
direction="decreasing",
)
fit = fit_dose_response(obs, model="binomial", link="probit", upper="estimate", lower=0.0)
t = threshold(fit, definition=Definition.absolute(0.9), interval_method="profile", level=0.95)
# "The margin exceeds 0.8" (exact one-sided bound), never "the margin is 0.8".
assert t.censoring is Censoring.RIGHT
assert t.value is None and t.lo == 0.8
Worked example: a classic dose-response dataset
Finney's (1971) toxicity data: groups of insects exposed to increasing doses, with the number affected in each group. The outcome here is survival, which decreases with dose. The dose-0 group is an untreated control.
from robustmargin import (
Axis,
Censoring,
Definition,
Observations,
Status,
fit_dose_response,
threshold,
)
dose = [0.0, 2.6, 3.8, 5.1, 7.7, 10.2]
affected = [0, 6, 16, 24, 42, 44]
total = [49, 50, 48, 46, 49, 50]
obs = Observations.from_counts(
Axis(name="dose", unit="mg", scale="log"),
severity=dose,
successes=[n - a for n, a in zip(total, affected, strict=True)],
trials=total,
outcome="survival",
direction="decreasing",
)
# The link and the asymptotes are chosen before looking at the data; nothing is auto-selected.
fit = fit_dose_response(obs, model="binomial", link="probit", upper=1.0, lower=0.0)
assert fit.status is Status.OK
# The dose at which survival falls to 50%, with a 95% profile-likelihood interval.
t50 = threshold(fit, definition=Definition.absolute(0.5), interval_method="profile", level=0.95)
assert t50.censoring is Censoring.NONE
assert abs(t50.value - 4.85) < 0.01
assert 4.3 < t50.lo < t50.value < t50.hi < 5.4
# A successful fit carries goodness-of-fit diagnostics, and every fit, failed or not, carries a
# monotonicity check on the raw counts. Both are reported and never acted on: nothing is
# rescaled, and the link you chose is never swapped for a better-scoring one.
diag = fit.diagnostics
assert diag.df == 3 # five dosed groups, two estimated parameters
assert diag.dispersion < 1.5 # no sign of overdispersion
assert set(diag.link_aic) == {"probit", "logit", "cloglog"}
assert not fit.monotonicity.any_flagged # survival never rises between adjacent doses
Comparing two curves
Two curves from the selenium data: survival against concentration for two forms of a
compound. baseline_fraction(0.5) asks where survival falls to half of what the control group
achieves, and the upper asymptote is estimated rather than fixed at 1. The second form shows
what the diagnostics are for.
from robustmargin import (
Axis,
Definition,
Observations,
fit_dose_response,
ratio_interval,
threshold,
)
conc = [0.0, 100.0, 200.0, 300.0, 400.0, 500.0]
curves = {
"form_a": ([151, 146, 116, 159, 150, 140], [3, 40, 31, 85, 102, 112]),
"form_b": ([141, 153, 142, 139, 154, 155], [2, 30, 59, 82, 62, 85]),
}
thresholds = {}
fits = {}
for name, (total, dead) in curves.items():
obs = Observations.from_counts(
Axis(name="concentration", unit="ug/L", scale="log"),
severity=conc,
successes=[n - d for n, d in zip(total, dead, strict=True)],
trials=total,
outcome="survival",
direction="decreasing",
)
fits[name] = fit_dose_response(
obs, model="binomial", link="probit", upper="estimate", lower=0.0
)
thresholds[name] = threshold(
fits[name],
definition=Definition.baseline_fraction(0.5),
interval_method="profile",
level=0.95,
)
ratio = ratio_interval(
thresholds["form_a"], thresholds["form_b"], method="fieller", dependence="independent"
)
assert 0.5 < ratio.lo < ratio.estimate < ratio.hi < 0.9 # form_a fails at a lower concentration
# form_b is where diagnostics earn their place: survival rises from 300 to 400, more than chance
# allows, and the groups vary far more than a binomial model expects. Both are reported on the
# fit. The reversal is also repeated as a warning on every threshold and ratio built from it.
# Neither changes a number.
form_b = fits["form_b"]
flagged = [(p.severity_low, p.severity_high) for p in form_b.monotonicity.pairs if p.flagged]
assert flagged == [(300.0, 400.0)]
assert form_b.diagnostics.dispersion > 1.5
assert any(w.startswith("OVERDISPERSED: ") for w in form_b.diagnostics.warnings)
assert any(w.startswith("NON_MONOTONE_DATA: ") for w in ratio.warnings)
Treat an overdispersed fit's intervals as too narrow. Clustered inference, which addresses this,
is planned for 0.4.
An outcome that rises with severity
Often what you count gets worse as severity grows: events per trial, not successes. Declare it
with direction="increasing", and successes counts the outcome that rises. The curve runs from
lower, the rate at zero severity, up to upper. An increasing fit is the decreasing fit of the
complementary outcome (trials - successes) mapped back, so every status, interval and censoring
rule is the one the decreasing case uses, and every number and message is on the scale of the
outcome you counted.
Definition.baseline_multiple(k) asks where the rate reaches k times its value at zero
severity. It is defined on log axes only, and threshold raises ValueError on a linear axis. A
multiple of zero is not a threshold, so lower must be estimated (or fixed above zero): a fixed
lower=0.0 over a control that has events is reported as CONTROL_INCOMPATIBLE, and
baseline_multiple raises for a fixed lower of zero. It also raises ValueError on any failed
fit (SEPARATION or NOT_CONVERGED), because a multiple of the control's observed rate would be
an uncovered target; baseline_fraction(p) raises on a failed fit for the same reason. Use
Definition.absolute(a) then, or a converged refit (for example with lower fixed above zero),
and catch the ValueError per job in a sweep.
from robustmargin import (
Axis,
Definition,
Observations,
Status,
fit_dose_response,
threshold,
)
severity = [0.0, 0.5, 1.0, 2.0, 4.0, 8.0, 16.0]
events = [20, 22, 35, 90, 220, 340, 380]
obs = Observations.from_counts(
Axis(name="severity", unit="unit", scale="log"),
severity=severity,
successes=events,
trials=[400] * len(severity),
outcome="event",
direction="increasing",
)
fit = fit_dose_response(obs, model="binomial", link="probit", upper=1.0, lower="estimate")
assert fit.status is Status.OK
assert 0.03 < fit.params["lower"].value < 0.07 # about 5% of trials have an event at severity 0
# The severity at which the event rate doubles, with a 95% profile-likelihood interval.
doubling = threshold(
fit, definition=Definition.baseline_multiple(2.0), interval_method="profile", level=0.95
)
assert doubling.status is Status.OK
assert 0.7 < doubling.lo < doubling.value < doubling.hi < 1.5
assert abs(fit.predict(doubling.value).estimate - 2 * fit.params["lower"].value) < 1e-6
Definition.absolute(a) and Definition.relative(p) mean the same in both directions, with the
crossing read upward. Definition.baseline_fraction(p) belongs to a decreasing outcome, and
threshold raises if you pair it with an increasing fit (and baseline_multiple with a
decreasing one). For an increasing outcome, RIGHT reads "still passes at the highest level
tested": the rate is confidently below the target there.
A continuous response
When each run gives a real-valued score instead of a pass or fail, build
ContinuousObservations and fit with model="gaussian". The curve is the same, now with
independent normal errors fitted by least squares, and link still names the shape of the
transition ("logit", "probit" and "cloglog" are R drc's LL.4, LN.4 and W1.4).
ContinuousObservations.from_observations takes one response per observation and from_summaries takes
a mean, a sample SD and a count per level; both describe the same data and give the same fit.
upper and lower are in the units of the response, so a threshold target such as
Definition.absolute(0.7) is too. Intervals are calibrated on the fit's residual degrees of
freedom: the delta interval uses a Student-t quantile and the profile uses the F distribution on
the change in the residual sum of squares. A continuous response has no exact bounds, so a
threshold is OK, UNREACHABLE (the target lies outside the fitted range of the response) or
NOT_CONVERGED, and a value beyond the tested range carries an EXTRAPOLATED: warning. A fit that
cannot be identified (a transition wider than the tested range, a step between two levels, no
residual degrees of freedom, a response that does not move the way direction says) is
NOT_CONVERGED with the reason in warnings and no number.
from robustmargin import (
Axis,
ContinuousObservations,
Definition,
Status,
fit_dose_response,
threshold,
)
severity = [x for x in (0.0, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0) for _ in range(3)]
score = [
0.95, 0.96, 0.94, 0.87, 0.88, 0.87, 0.80, 0.84, 0.78, 0.59, 0.63, 0.62,
0.39, 0.36, 0.39, 0.27, 0.20, 0.23, 0.13, 0.14, 0.13,
] # fmt: skip
obs = ContinuousObservations.from_observations(
Axis(name="severity", unit="unit", scale="log"),
severity=severity,
value=score,
outcome="score",
direction="decreasing",
)
fit = fit_dose_response(obs, model="gaussian", link="logit", upper="estimate", lower="estimate")
assert fit.status is Status.OK
assert fit.residual.df == 17 # 21 observations, 4 estimated parameters
# The severity at which the score has fallen to 0.7, with a 95% profile interval.
margin = threshold(fit, definition=Definition.absolute(0.7), interval_method="profile", level=0.95)
assert margin.status is Status.OK
assert 0.6 < margin.lo < margin.value < margin.hi < 0.9
assert abs(fit.predict(margin.value).estimate - 0.7) < 1e-6
A ratio of two continuous thresholds uses a Student-t quantile on Welch-Satterthwaite degrees of
freedom, and ratio_interval raises ValueError for a count threshold against a continuous one.
Grid break-point rule
grid_break_point finds the smallest tested severity at which observed performance falls
strictly below a criterion. It reports an observed grid statistic, not an estimated
threshold. The reported value is always one of the severities you tested. It is never
interpolated, and it has no confidence interval.
from robustmargin import Censoring, grid_break_point
severity = [0.0, 0.5, 1.0, 2.0, 4.0]
performance = [1.0, 0.98, 0.96, 0.91, 0.40] # e.g. success rate at each level
result = grid_break_point(severity, performance, criterion=0.95)
assert result.value == 2.0 # first tested level strictly below 0.95
assert result.max_tested == 4.0
assert result.censoring is Censoring.NONE
When no tested level fails, there is no value. The result is right-censored and records how far testing went, so it reads as "held up to 2.0", never as "cannot fail":
from robustmargin import Censoring, grid_break_point
held = grid_break_point([0.0, 1.0, 2.0], [1.0, 0.99, 0.97], criterion=0.95)
assert held.value is None
assert held.max_tested == 2.0
assert held.censoring is Censoring.RIGHT
On a signed axis, pass signed=True. The rule then breaks on magnitude, on whichever side fails
first:
from robustmargin import grid_break_point
signed = grid_break_point(
[-0.2, -0.1, 0.0, 0.1, 0.2],
[0.05, 0.77, 0.99, 0.33, 0.0],
criterion=0.95,
signed=True,
)
assert signed.value == 0.1
assert signed.max_tested == 0.2
Some points about the rule:
- A level sitting exactly on the criterion passes. Only
performance < criterionfails. - Monotonicity is neither assumed nor checked, and nothing is pooled or fitted.
- Invalid input raises
ValueErrorrather than returning a placeholder. That covers empty or mismatched arrays, non-finite values, and negative severities withoutsigned=True.
Results and JSON
Every result is a frozen dataclass carrying a schema_version and a provenance mapping.
provenance records where the inputs came from; robustmargin stores it and never interprets it.
GridBreakPointis whatgrid_break_pointreturns.Fit,ThresholdandRatioare whatfit_dose_response,thresholdandratio_intervalreturn. They check their own consistency when constructed. For example, a failed fit cannot carry parameters, and a censored threshold cannot carry a point estimate.
Results are collected in a Scorecard and written to JSON with robustmargin.report:
import json
from robustmargin import Scorecard, grid_break_point
from robustmargin.report import from_dict, load_schema, to_dict
held = grid_break_point([0.0, 1.0, 2.0], [1.0, 0.99, 0.97], criterion=0.95)
card = Scorecard(results=(held,), provenance={"run": "example"})
text = json.dumps(to_dict(card), allow_nan=False)
assert from_dict(json.loads(text)) == card
assert load_schema()["$schema"] == "https://json-schema.org/draft/2020-12/schema"
The JSON follows schema/scorecard-v2.json, a JSON Schema shipped inside the package, and
schema/scorecard-v1.json is kept so that cards written by 0.1.1 and 0.2.0 still read.
Schema "2" adds increasing outcomes and baseline_multiple. It covers binary outcomes only,
and reading is strict: a card with fields this version does not know is rejected, not silently
trimmed. To read a stored card, use a robustmargin at least as new as the
one that wrote it. The card's library_version field records which version that was.
For your own tests, robustmargin.testing provides fake_fit, fake_threshold and fake_ratio.
They return schema-valid results in every status and censoring state, and each is labelled as a
fake rather than an estimate.
Development
pip install -e ".[dev]"
ruff check . && ruff format --check .
mypy
pytest # enforces 90% branch coverage
CI runs the tests on Python 3.11, 3.12, and 3.13 against current dependencies. It also runs them
on Python 3.11 against the oldest supported versions, pinned in
ci/constraints-min.txt.
Changes are recorded in CHANGELOG.md.
License
Apache-2.0. See LICENSE.
Metadata
Release files for robustmargin 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| robustmargin-0.3.0.tar.gz | 193.9 kB | With provenance | Details for robustmargin-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| robustmargin-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for robustmargin-0.3.0-py3-none-any.whl |
Total release size: 391.0 kB
Release files / robustmargin-0.3.0.tar.gz
| Download URL | robustmargin-0.3.0.tar.gz |
|---|---|
| Size | 193.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a578f490f027c402c81d664e23740c132feb0b0d03383413ef6091950fcb6225
|
|
BLAKE2b-256 checksum How to use checksums |
45f848ee042bb2b6f8f2a1185653d26df02a63a1821f728672f796fa81ecb433
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.
Transparency logRelease files / robustmargin-0.3.0-py3-none-any.whl
| Download URL | robustmargin-0.3.0-py3-none-any.whl |
|---|---|
| Size | 197.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bd128ee09827ec8f87ec2868a4d4f932de5d2e795183eefdb34000dedcbb5f48
|
|
BLAKE2b-256 checksum How to use checksums |
1322b5d2866c1bb936986073ab7f6f3cf3c712c999f56a687b819fa0cd9bbaac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.
Transparency logSecurity for robustmargin 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for robustmargin-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.