Skip to content
Edit this page View source of this page

Robust decisions across geometries

Once we decide to optimize against a worst case, we still have to choose which worst case. This tutorial takes one small decision problem and solves it four times, changing only the shape of the ambiguity set,

\[ \min_x \; \sup_{q \in \mathcal{Q}(\mathrm{nominal}, \rho)} \mathbb{E}_q\!\left[(\mathrm{outcomes} - x)^2\right] \]

with \(\mathcal{Q}\) ranging over the KL, \(\chi^2\), total-variation, and Wasserstein balls. The outcomes are [1, 2, 3, 10] under a uniform nominal. Three of them are ordinary values and one is an outlier, so the experiment shows how strongly each geometry weights the 10.

What the comparison shows

At \(\rho = 0\) every ball collapses to \(\{\mathrm{nominal}\}\), so all four formulations return the same answer. This is the empirical-risk decision, which for squared error is the nominal mean, \(4.0\). The common starting point makes the four curves comparable.

As \(\rho\) grows, the three \(\varphi\)-divergence decisions move toward the outlier at different rates. The radii are not on a common scale, because each divergence measures distance in its own units, but the shape of each curve is a property of the divergence rather than of the solver.

  • KL moves fastest at small radii. Its worst-case distribution is an exponential tilt of the nominal that keeps every scenario at positive mass below the saturation radius. For small \(\rho\) the worst-case value grows as \(\mathbb{E}[\cdot] + \sqrt{2\rho \operatorname{Var}[\cdot]} + O(\rho)\), an expansion that tests/dro/test_kl_dro.py and tests/dro/test_equivalences.py check.
  • \(\chi^2\) follows the same pattern with a smaller constant at the same radius. In the interior regime, where no scenario is truncated to zero mass, its worst case equals the variance-penalty form \(\mathbb{E}[\cdot] + \sqrt{\rho \operatorname{Var}[\cdot]}\) (Duchi and Namkoong, 2021)1. Its maximizer is a truncated linear tilt, so a scenario can reach zero mass at a finite radius.
  • Total variation starts slowest. Its worst case grows piecewise linearly in \(\rho\), because it moves a block of mass from the cheapest scenario to the most expensive one. Its exact minimizer overtakes the other two between \(\rho = 0.1\) and \(\rho = 0.2\) and then stays at \(5.5\), the midpoint of \(1\) and \(10\). There the two outcomes tie for the largest loss and the robust objective has a kink. The fixed-step solver of the script does not settle exactly at a kink within its 120 iterations, so its printed values near \(5.5\) are approximate.
  • Wasserstein is the only geometry that depends on the distances between outcomes rather than only on their probabilities. It is given the squared-distance cost matrix, so moving mass from \(1\) to \(10\) costs \(81\), while moving it from \(2\) to \(1\) costs \(1\). Its decision moves the other way. Near \(x = 4\) the cheapest way to raise the loss is to shift mass from \(2\) toward \(1\), and from \(3\) as well at larger radii, so the decision drifts toward the low cluster and away from the outlier.

The last item is the main difference between the two families. A \(\varphi\)-divergence depends only on the probabilities of the scenarios and not on how far apart they are, whereas the Wasserstein distance depends on both.

A note on cost

Every point on every curve is a nested solve. The outer MinimaxSolver evaluates an objective that runs the ambiguity set's own inner solve at every iteration, so the work multiplies, and the outer iteration budget at the top of the script is cut well below the default. The inner solves need no budget. Total variation has a closed form, and the KL, \(\chi^2\) and Wasserstein sets are solved by a bisection with a fixed number of steps set by the dtype, so each inner solve has a fixed cost.

The outer gradient flows correctly through worst_case_expectation even though the worst-case distribution is computed under detach. This follows from Danskin's theorem. The optimal value is the support function of the ambiguity set evaluated at the loss, so its gradient with respect to the loss is the maximizing distribution, and only the final weighted sum of the losses needs to stay attached to the graph.

Four landscapes, one radius

The decisions in the static figure are minimizers, and the animation shows the objectives they minimize. The left panel draws \(\sup_q \mathbb{E}_q[(\xi - x)^2]\) as a function of \(x\) for every geometry at the current radius, with the nominal loss dashed and each minimizer marked. The middle panel tracks the minimizers as the radius grows, and the right panel shows the adversary of each ball at its own decision. The minimizers come from a bisection on the derivative of the robust objective, which gives the exact point that the fixed-step solver of the script approaches.

The right panel also explains the direction of the Wasserstein decision. Its adversary moves mass toward \(1\), which is the cheapest transport under the squared cost. The three \(\varphi\)-divergence adversaries do not depend on distance and move mass onto the \(10\). As their decisions move toward the outlier, the loss at \(1\) grows as well, so they also move mass onto the \(1\). At the total-variation decision \(5.5\) the outcomes \(1\) and \(10\) have equal loss.

The robust objective, its minimizer and its adversary under four ambiguity-set geometries as the radius grows The robust objective, its minimizer and its adversary under four ambiguity-set geometries as the radius grows
The left panel shows the robust objective in $x$ for each geometry, with the nominal loss dashed and the minimizers marked. The middle panel shows the decisions against the radius. The right panel shows the worst-case distribution of each ball at its own decision, drawn over the nominal.

The figure

The figure is rendered from this script when the site is built, so it always shows the output of the current code.

Robust decision against the radius under four ambiguity-set geometries Robust decision against the radius under four ambiguity-set geometries
Every geometry starts from the empirical-risk decision at radius zero. The divergence determines where the decision goes and how fast, so the $\varphi$-divergence decisions move toward the outlier and the Wasserstein decision moves the other way.

Source

tutorials/03_robust_decision_across_ambiguity_sets.py
"""Comparing the robust decision across all four ambiguity-set geometries.

The problem is to choose a single scalar decision `x` that minimizes the
worst-case expected squared error against four observed outcomes,

    min_x sup_{q in ambiguity_set(nominal, radius)} E_q[(outcomes - x) ** 2]

At `radius = 0` every ambiguity set collapses to `{nominal}`, so every
formulation recovers the same empirical-risk decision, the
nominal-weighted mean of `outcomes`. As `radius` grows, the robust
decision `x*` of each formulation moves toward the outcomes onto which
the adversary shifts probability mass, and the divergence that defines
the ambiguity set decides which outcomes those are.

The KL, chi-square and total-variation balls depend only on the
probabilities of the scenarios, so their adversaries put more mass on
the outlier 10 and their decisions rise from 4.0 toward it. The
Wasserstein ball also depends on the distances between outcomes. Under
the squared cost, moving mass onto 10 costs at least 49 per unit, while
moving mass from 2 and 3 down to 1 raises the loss near `x = 4` the
most per unit of transport cost. Its decision therefore falls toward
the low cluster and away from the outlier.

Run:
    python tutorials/03_robust_decision_across_ambiguity_sets.py
"""

from collections.abc import Callable

import matplotlib.pyplot as plt
import torch
from _plotting import save_figure

from optora.dro import (
    ChiSquareAmbiguitySet,
    KLAmbiguitySet,
    MinimaxProblem,
    MinimaxSolver,
    TotalVariationAmbiguitySet,
    WassersteinAmbiguitySet,
)
from optora.solvers import GradientDescent

OUTCOMES = torch.tensor([1.0, 2.0, 3.0, 10.0], dtype=torch.float64)
NOMINAL = torch.full_like(OUTCOMES, 1.0 / OUTCOMES.numel())
COST = (OUTCOMES.unsqueeze(0) - OUTCOMES.unsqueeze(1)) ** 2

# Every outer step runs a full inner worst-case solve, so nested solves are
# expensive in eager PyTorch. The outer iteration budget below is therefore
# cut well below the solver's default. The tutorial shows qualitative
# behavior and is not a tight-tolerance convergence test.
# The outer solver uses `check_interval=1` because one of its steps costs a
# full inner worst-case solve, which is far more than the synchronization a
# convergence check costs.
OUTER_SOLVER = GradientDescent(step_size=0.01, max_iter=120, tol=1e-7, check_interval=1)


def loss_fn(x: torch.Tensor) -> torch.Tensor:
    return (OUTCOMES - x) ** 2


def robust_decision(ambiguity_set: object, radius: float) -> float:
    problem = MinimaxProblem(
        ambiguity_set=ambiguity_set,  # type: ignore[arg-type]
        loss_fn=loss_fn,
        initial_point=torch.tensor(OUTCOMES.mean().item(), dtype=torch.float64),
    )
    result = MinimaxSolver(solver=OUTER_SOLVER).solve(problem)
    return result.point.item()


def main() -> None:
    erm_decision = OUTCOMES.mean().item()
    print(
        f"empirical-risk decision (every formulation at radius=0): {erm_decision:.4f}"
    )

    families: dict[str, Callable[[float], object]] = {
        "KL": lambda radius: KLAmbiguitySet(nominal=NOMINAL, radius=radius),
        "chi-square": lambda radius: ChiSquareAmbiguitySet(
            nominal=NOMINAL, radius=radius
        ),
        "total variation": lambda radius: TotalVariationAmbiguitySet(
            nominal=NOMINAL, radius=radius
        ),
        "Wasserstein": lambda radius: WassersteinAmbiguitySet(
            nominal=NOMINAL,
            cost=COST,
            radius=radius,
        ),
    }
    radii = [0.0, 0.02, 0.05, 0.1, 0.2, 0.4]

    fig, ax = plt.subplots(figsize=(7, 4))
    for name, build_ambiguity_set in families.items():
        decisions = [
            robust_decision(build_ambiguity_set(radius), radius) for radius in radii
        ]
        print(f"{name:>16}: " + " -> ".join(f"{value:.3f}" for value in decisions))
        ax.plot(radii, decisions, marker="o", label=name)

    ax.axhline(
        erm_decision, color="gray", linestyle="--", label="empirical risk (radius=0)"
    )
    ax.set_xlabel("ambiguity radius")
    ax.set_ylabel("robust decision x*")
    ax.set_title("robust decision vs. radius, across ambiguity-set geometries")
    ax.legend()
    fig.tight_layout()
    save_figure(fig, "03_robust_decision_across_ambiguity_sets")


if __name__ == "__main__":
    main()

  1. John C. Duchi and Hongseok Namkoong, "Learning Models with Uniform Performance via Distributionally Robust Optimization", Annals of Statistics 49(3), 1378-1406 (2021). arXiv:1810.08750. ↩