Optora¶

A composable PyTorch library for distributionally robust optimization. Its ambiguity sets compute differentiable worst-case expectations on any PyTorch device, and its minimax solver trains a decision against them.
Latest release: v0.4.0
Overview¶
Empirical risk minimization fits the training distribution as it is. Distributionally robust optimization instead minimizes the expected loss under the worst distribution in a neighbourhood of the training distribution,
where \(D\) is a divergence between probability distributions. Optora splits
this problem into three composable classes. A Divergence is \(D\). An
AmbiguitySet combines a divergence with a nominal distribution and a
radius and solves the inner supremum. A Solver minimizes over the
decision \(x\). The worst-case expectation is an ordinary differentiable
PyTorch objective, so it can be used in any nn.Module and torch.optim
training loop. The derivations are in
Mathematical formulations.
In action¶
The first animation shows seven ambiguity sets with the same nominal distribution (grey) and the same loss. In every frame each radius is the same fraction of that set's saturation radius, and each geometry moves the probability mass to different scenarios.
Every figure on this site is rendered from the library by
python -m tools.figures at build time, in both colour schemes. No figure
is drawn by hand, and no rendered figure is committed to the repository.
Core components¶
-
Core contracts
The abstract base classes
Solver,DivergenceandAmbiguitySetdefine what a new method subclasses. The module also holds device-resident convergence tracking and the working-dtype policy. -
Divergences
The module implements the Kullback-Leibler, general \(\varphi\), \(\chi^2\) and total variation divergences, the max divergence, the Sinkhorn and entropic transport divergences, and the maximum mean discrepancy.
-
Ambiguity sets
The module provides KL, \(\varphi\), \(\chi^2\), total variation, CVaR, Wasserstein, Sinkhorn and MMD balls on a finite support. Each one computes its worst case by a bracketed tilt, a closed form, a bisection or an interior-point cone program. For training,
PenalizedChiSquareDROgives the penalized \(\chi^2\) objective, andtransport_mapan adversary that moves the inputs themselves (added in 0.5.0). -
Minimax solve
MinimaxSolvercombines any ambiguity set with an outer solver over the decision variable. By the envelope theorem the inner supremum is differentiable without unrolling the inner solve. -
Solvers
The module provides fixed-step gradient descent and a primal-dual saddle-point solver. Both evaluate their stopping rule on the device and compose with
torch.functransforms. -
Radius calibration
These functions turn a confidence level and a sample size into a KL, \(\chi^2\), general \(\varphi\) or Wasserstein radius. The robust value is then an asymptotically exact upper confidence bound on the true risk.
-
Minibatch estimators
Two estimators target the population worst case from minibatches. The randomized multilevel Monte Carlo estimator removes the bias of the plug-in estimate up to its largest batch size, and the stochastic \(\varphi\)-dual has an unbiased minibatch mean.
-
Group DRO
Train against the worst of several known groups, either exactly through an existing ambiguity set or online on minibatches with
OnlineGroupDRO. -
Training
The worst case is a PyTorch loss with exact first and second derivatives with respect to the per-sample loss, the nominal and the radius.
Why Optora¶
-
Robust by construction
Optimize against the worst case over an ambiguity set instead of trusting a single empirical distribution.
-
Exact inner solves
The default inner solves have no tolerance or step size to tune. Each ambiguity set reaches the precision of its working dtype, up to the exceptions documented on Numerical behaviour, and the test suite checks it against independent linear-program, cone-program, closed-form or dual references.
-
GPU-first
Every routine is vectorized PyTorch and batched over leading dimensions. The KL, \(\chi^2\), total variation, CVaR, Wasserstein and Sinkhorn sets perform no host synchronization, and all tensor state moves with one
.to(device)call. -
Verified numerics
The tests check property-based invariants, and they run
gradcheckat smooth points and finite differences at kinks. Each dtype has a documented accuracy, and the benchmark suite fails CI on a performance regression. -
Checked against published papers
A separate repository reruns experiments from published DRO papers with the released package. Every claim is compared with a tolerance fixed before the run, and a claim that falls outside it is reported with its cause.
Install¶
For local development, install the project in editable mode with the developer and documentation extras.
Quick start¶
The example below finds the decision \(x\) that minimizes the worst-case squared error over a KL ball of distributions on four observed outcomes.
import torch
from optora import GradientDescent, KLAmbiguitySet, MinimaxProblem, MinimaxSolver
nominal = torch.tensor([0.25, 0.25, 0.25, 0.25])
outcomes = torch.tensor([1.0, 2.0, 3.0, 10.0])
problem = MinimaxProblem(
ambiguity_set=KLAmbiguitySet(nominal=nominal, radius=0.1),
loss_fn=lambda x: (outcomes - x) ** 2,
initial_point=torch.tensor(0.0),
)
outer_solver = GradientDescent(step_size=0.01, max_iter=200, tol=1e-6)
robust_decision = MinimaxSolver(solver=outer_solver).solve(problem).point
To train an nn.Module instead, call worst_case_expectation on the
per-sample loss and step any torch.optim optimizer, as the
training guide shows. The Examples
show each component in a short script, the
Tutorials combine them in longer experiments, and
the paper reproductions rerun published
results, with one
Colab notebook
that holds every report.
Imports¶
Every main class is re-exported from the package root, so the two forms below name the same object.
from optora import KLAmbiguitySet, MinimaxSolver
from optora.dro import KLAmbiguitySet, MinimaxSolver
The root exports only classes, namely the Solver, Divergence and
AmbiguitySet contracts, their implementations, the minibatch and group
estimators, and the problem and result types. Convergence diagnostics stay
in optora.core, and helper functions such as the radius calibrations stay
in the subpackage that defines them. There are no root-level workflow
functions, so an algorithm runs through Solver.solve(problem).
Build documentation¶
The API reference and the figures are generated
tools.docs discovers every public module under optora/, writes one page
per module, and rewrites the API navigation. tools.figures renders every
chart, animation and benchmark on the site from the library itself, in
both colour schemes. Never edit the generated pages by hand, and never
commit a rendered figure.
Versioning¶
Optora is alpha software and follows Semantic Versioning. During 0.x a
minor release may change the public API and a patch release does not.
API stability defines what counts as public.
Citing¶
If you use Optora in your research, please cite it. Every release is archived
on Zenodo under the concept DOI
10.5281/zenodo.23850922, which
always resolves to the latest version. The metadata is in
CITATION.cff,
which GitHub's "Cite this repository" button turns into APA or BibTeX.
Contributing¶
The development workflow, coding style and pull request checklist are in
CONTRIBUTING.md.
If Optora is useful to you, a star on
GitHub
helps other researchers find it.