Skip to content
Edit this page View source of this page

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.

PyPI Python License CI Docs DOI

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,

\[ \min_x \; \sup_{q \,:\, D(q \,\|\, \mathrm{nominal}) \le \mathrm{radius}} \mathbb{E}_q[\ell(x, \xi)], \]

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.

Where each ambiguity set's adversary moves the probability mass as its budget grows Where each ambiguity set's adversary moves the probability mass as its budget grows
The worst-case distribution of every ambiguity set on the same twelve-scenario problem, read off autograd as the gradient of the worst-case expectation, as each ball grows to the radius at which it contains the point mass on the highest loss.
The seven ambiguity sets drawn on the probability simplex as their radius grows The seven ambiguity sets drawn on the probability simplex as their radius grows
The same sets on the probability simplex of a three-scenario problem. The shaded region is the ball around the nominal (grey dot), the trail is the path of the worst-case distribution, and the last panel shows the worst-case value.

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, Divergence and AmbiguitySet define what a new method subclasses. The module also holds device-resident convergence tracking and the working-dtype policy.

    optora.core

  • 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.

    optora.divergences

  • 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, PenalizedChiSquareDRO gives the penalized \(\chi^2\) objective, and transport_map an adversary that moves the inputs themselves (added in 0.5.0).

    optora.dro

  • Minimax solve


    MinimaxSolver combines 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.

    MinimaxSolver

  • 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.func transforms.

    optora.solvers

  • 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.

    optora.dro.calibration

  • 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.

    optora.dro.minibatch

  • Group DRO


    Train against the worst of several known groups, either exactly through an existing ambiguity set or online on minibatches with OnlineGroupDRO.

    optora.dro.group_dro

  • 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.

    Training guide

Why Optora

  • Robust by construction


    Optimize against the worst case over an ambiguity set instead of trusting a single empirical distribution.

    Formulations

  • 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.

    Benchmarks

  • 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 gradcheck at smooth points and finite differences at kinks. Each dtype has a documented accuracy, and the benchmark suite fails CI on a performance regression.

    Numerical behaviour

  • 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.

    Paper reproductions

Install

pip install optora

For local development, install the project in editable mode with the developer and documentation extras.

pip install -e ".[dev,docs]"

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

python -m tools.figures   # render the figures once (optional)
python -m tools.docs serve
python -m tools.figures
python -m tools.docs build

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.