Skip to content
Edit this page View source of this page

Sinkhorn relaxation of Wasserstein DRO

The worst-case expectation over a Wasserstein ball on a finite support is a linear program. Its value is exact and cheap to compute, and it is piecewise linear in the loss vector. Along a smooth path of decisions the robust objective therefore has kinks wherever the optimal transport plan changes. The kinks are not a rounding artifact, and they matter in practice, because the minimizer of a maximum of smooth functions is typically a point where two of them cross. An outer method with a gradient-norm stopping rule never stops there, and a fixed-step gradient method oscillates around the kink indefinitely.

Sinkhorn DRO (Wang, Gao and Xie 2025) replaces the Wasserstein distance by its entropic regularization. Its worst case is an exponential tilt, a smooth function of the loss whose first and second derivatives Optora computes exactly, and a single parameter \(\epsilon\) controls how far the ball is from the exact one. This tutorial puts SinkhornAmbiguitySet next to WassersteinAmbiguitySet on one problem and measures both sides of the trade-off. It measures how fast the relaxation converges to the exact solution as \(\epsilon \to 0\), and how its smoothness helps an outer solver.

The entropic ball as a subclass

Optora's ambiguity sets are subclasses of AmbiguitySet. The Wasserstein ball bisects its own piecewise-linear dual. The entropic ball is a TiltedAmbiguitySet, the same base the KL ball uses, because on a finite support the Sinkhorn ball is a mixture of KL balls with one shared budget (Wang, Gao and Xie, Lemma 3.11). Let \(Q_i \propto \nu\, e^{-c_{i\cdot}/\epsilon}\) be the row of the Gibbs kernel at support point \(i\), \(p\) the nominal weights, \(\rho\) the radius, and \(\bar\rho\) the budget in excess of the entropic bias (see the note below). Then

\[ \sup_{P:\, \mathcal{W}_\epsilon(\hat P, P) \le \rho} \mathbb{E}_P[\ell] = \sup_{\gamma_1, \ldots, \gamma_n} \Big\{ \sum_i p_i\, \mathbb{E}_{\gamma_i}[\ell] \;:\; \epsilon \sum_i p_i\, \mathrm{KL}(\gamma_i \,\|\, Q_i) \le \bar\rho \Big\}, \]

and every row's maximizer is the tilt \(\gamma_i \propto Q_i\, e^{\ell / (\lambda\epsilon)}\) at one shared temperature. The set therefore needs no solver of its own. The bracketed bisection on the tilt that KLAmbiguitySet uses pins the worst case exactly, with no host synchronization, and autograd differentiates through it. The formulation page has the full statement.

What radius means for the entropic ball

Entropic smoothing moves the centre of the ball. The smallest Sinkhorn distance any distribution can have from \(\hat P\) is not zero but \(-\epsilon \sum_i p_i \log \sum_k e^{-c_{ik}/\epsilon}\), attained at the kernel-smoothed nominal \(P^0 = p^\top Q\). The magnitude of this minimum is the entropic bias, and SinkhornAmbiguitySet.radius is the budget \(\bar\rho\) in excess of it. The script prints the bias next to every gap. The bias vanishes with \(\epsilon\) (it is \(2.9 \times 10^{-5}\) at \(\epsilon = 10^{-3}\) here), so the same number serves as the radius of both sets in the limit. For \(\epsilon\) between \(0.1\) and about \(0.006\) the bias is more than ten times larger than the value gap. A ball parameterized by the raw Sinkhorn distance would carry an offset of the size of the bias, which is at most \(\epsilon \log n\). The excess-budget convention removes this offset and makes the comparison below tight.

The problem

The problem uses the forty log-normal demands of the regret tutorial with equal weight, the ground cost \(|\xi - \xi'|\), the quadratic loss \((\theta - \xi)^2\) of a scalar location decision, and the radius \(0.1\). The loss is smooth in \(\theta\), so every kink in the Wasserstein objective comes from the ambiguity set and not from the loss.

Convergence as \(\epsilon \to 0\)

At the fixed decision \(\theta = 1\), the exact worst case is \(0.41675\). The script evaluates the Sinkhorn value at thirteen strengths from \(1\) down to \(10^{-3}\), and the table shows four of them.

\(\epsilon\) Sinkhorn value \(\lvert V_\epsilon - V_W \rvert\) entropic bias
\(1\) \(0.34832\) \(6.8 \times 10^{-2}\) \(3.25\)
\(10^{-1}\) \(0.42618\) \(9.4 \times 10^{-3}\) \(0.17\)
\(10^{-2}\) \(0.41670\) \(5.6 \times 10^{-5}\) \(4.3 \times 10^{-3}\)
\(10^{-3}\) \(0.41681\) \(5.3 \times 10^{-5}\) \(2.9 \times 10^{-5}\)

The gap is not monotone. Below \(\epsilon = 0.1\) it changes sign twice, once between \(0.03\) and \(0.02\) and once between \(0.01\) and \(0.006\), so a log plot of its absolute value dips at the crossings. It does obey a linear bound. For every \(\epsilon \le 0.1\) the script measures a gap of at most \(0.094\,\epsilon\) and asserts the bound \(0.15\,\epsilon\). A linear rate is consistent with the fact that, on a finite support, the entropic term of any transport plan is bounded, so the regularization perturbs the transport cost by \(O(\epsilon)\).

The gap also shrinks uniformly over the decisions. Over a grid of \(801\) decisions spanning the demand range, \(\sup_\theta |V_\epsilon - V_W|\) falls from \(0.13\) at \(\epsilon = 0.1\) to \(0.053\) at \(0.03\) and \(0.013\) at \(0.01\), and the mean derivative gap falls with it, from \(0.10\) to \(0.037\) to \(0.0097\).

The robust objective, its derivative and its minimizer under the Sinkhorn ball converging to the Wasserstein ones as the entropic strength vanishes The robust objective, its derivative and its minimizer under the Sinkhorn ball converging to the Wasserstein ones as the entropic strength vanishes
The animation uses the site's twenty-four-scenario instance and sweeps $\epsilon$ from $1$ to $10^{-3}$. The left panel shows the robust objective in $\theta$ under both balls, with their minimizers. The middle panel shows the derivative, a staircase for the Wasserstein ball and a smooth curve for the Sinkhorn ball that approaches it. The right panel shows the uniform value gap and the distance between the minimizers as $\epsilon$ falls.

Differentiable everywhere, measured

Between neighbouring grid points, a twice differentiable function changes its derivative by at most the step times its largest second derivative. The script evaluates both derivatives with autograd on the \(801\)-point grid (step \(2.25 \times 10^{-3}\)) and compares the largest increment with that bound.

objective largest increment of \(\mathrm{d}V/\mathrm{d}\theta\) step \(\times \max V''\) ratio
Wasserstein \(6.5 \times 10^{-2}\) \(4.5 \times 10^{-3}\) \(14.4\)
Sinkhorn, \(\epsilon = 0.1\) \(8.05 \times 10^{-3}\) \(8.05 \times 10^{-3}\) \(1.00\)
Sinkhorn, \(\epsilon = 0.03\) \(9.36 \times 10^{-3}\) \(9.36 \times 10^{-3}\) \(1.00\)
Sinkhorn, \(\epsilon = 0.01\) \(1.26 \times 10^{-2}\) \(1.26 \times 10^{-2}\) \(1.00\)

The Sinkhorn derivative attains the bound to three digits, as a smooth function does when its curvature peaks are wide compared with the grid step. The Wasserstein derivative exceeds the bound fourteenfold, because it jumps. Its autograd second derivative is \(2\) everywhere, which is the curvature of the loss under the active transport plan. The jumps have no finite second derivative, so autograd cannot report them. The Sinkhorn second derivative is finite, and its peaks lie near the Wasserstein kinks. The largest peak grows from \(3.6\) to \(5.6\) as \(\epsilon\) falls from \(0.1\) to \(0.01\), so the smooth curve sharpens toward the staircase.

The animation below sweeps the decision at a fixed \(\epsilon = 0.02\) and radius \(0.08\) on the twenty-four-scenario instance and shows where the jumps come from. The Wasserstein adversary is a vertex of the transport polytope. Between kinks it does not move at all, and when \(\theta\) crosses a kink it switches vertices and relocates a block of mass within a single frame. The Sinkhorn adversary is a tilt of the kernel rows. It moves a little in every frame, and it moves fastest where the Wasserstein adversary jumps.

The Wasserstein and Sinkhorn worst-case distributions and the derivatives they imply as the decision sweeps across the support The Wasserstein and Sinkhorn worst-case distributions and the derivatives they imply as the decision sweeps across the support
The left panel shows the nominal, the Sinkhorn adversary (filled) and the Wasserstein adversary (outlined) at the current decision (dashed line), with the total-variation mass each adversary moved since the previous frame. The right panel shows the derivative of the robust objective under each ball, with the current decision marked.

The minimizers

The exact robust decision is \(\theta_W = 1.2142\), and it sits on a kink. The derivative of the Wasserstein objective jumps from \(-2.1 \times 10^{-2}\) to \(+8.9 \times 10^{-4}\) across it, so the objective rises twenty-four times faster to the left of its minimizer than to the right.

\(\epsilon\) Sinkhorn minimizer \(\theta_\epsilon\) \(\lvert \theta_\epsilon - \theta_W \rvert\) \(V_W(\theta_\epsilon) - V_W(\theta_W)\)
\(10^{-1}\) \(1.1930\) \(2.1 \times 10^{-2}\) \(1.5 \times 10^{-3}\)
\(10^{-2}\) \(1.2177\) \(3.5 \times 10^{-3}\) \(1.5 \times 10^{-5}\)
\(10^{-3}\) \(1.2165\) \(2.3 \times 10^{-3}\) \(7.2 \times 10^{-6}\)

The last column is the number that matters when the relaxed decision is deployed against the exact objective. For every \(\epsilon \le 0.1\) the script measures at most \(0.022\,\epsilon\) and asserts the bound \(0.05\,\epsilon\). The middle column converges more slowly, and the flat right flank explains why. For \(\epsilon \le 10^{-2}\) the relaxed minimizer lies to the right of the kink, where a decision can move some distance at almost no cost in the exact objective. The location of the minimizer is ill-conditioned, but its value is not.

Effect of smoothness on the outer solve

MinimaxSolver wraps a GradientDescent with step \(0.2\), gradient tolerance \(10^{-8}\) and a budget of \(400\) steps around each ball, starting from \(\theta = 2\).

objective converged iterations final decision
Wasserstein no \(400\) (budget) \(1.21432\)
Sinkhorn, \(\epsilon = 0.1\) yes \(19\) \(1.19303\)
Sinkhorn, \(\epsilon = 0.03\) yes \(20\) \(1.20461\)
Sinkhorn, \(\epsilon = 0.01\) yes \(16\) \(1.21767\)

Unrolling sixty steps of the same iteration by hand shows what the Wasserstein solve is doing. Over the second half of the run its gradient magnitude stays between \(1.2 \times 10^{-3}\) and \(2.1 \times 10^{-2}\), close to the magnitudes of the two one-sided slopes at the kink. The Sinkhorn gradient at \(\epsilon = 0.01\) falls to \(10^{-14}\) by the last step. The Wasserstein iterate is accurate, within \(1.5 \times 10^{-4}\) of the exact minimizer, but no gradient test can confirm this to the solver. The relaxation is therefore useful in an outer loop. Its inner problem is smooth, so its gradient behaves as a line search or a stopping rule assumes, and the accuracy cost is linear in \(\epsilon\) and can be measured.

The figure

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

Gaps between the Sinkhorn and Wasserstein solutions over the entropic strength, the two derivatives near the minimizer, and the gradient magnitude of a fixed-step outer solve on each objective Gaps between the Sinkhorn and Wasserstein solutions over the entropic strength, the two derivatives near the minimizer, and the gradient magnitude of a fixed-step outer solve on each objective
The left panel shows the value gap, the minimizer distance and the exact suboptimality of the relaxed decision against $\epsilon$, with the entropic bias dotted. The middle panel shows the derivative of each objective around the Wasserstein minimizer (dashed). The right panel shows the gradient magnitude along sixty fixed steps on each objective, against the solver's tolerance.

What to take away

  • SinkhornAmbiguitySet is the smooth counterpart of WassersteinAmbiguitySet. On a finite support it is a budgeted mixture of KL balls, so it reuses the tilt machinery rather than a transport solver, and it works with MinimaxSolver unchanged.
  • With the excess-budget convention for its radius, the gap to the exact worst case is linear in \(\epsilon\) with a small constant. Here the measured constants are below \(0.1\) for the value and below \(0.03\) for the exact suboptimality of the relaxed decision.
  • The Wasserstein objective has derivative jumps, one of them at its own minimizer. The Sinkhorn objective is twice differentiable, and its curvature peaks sharpen toward those jumps as \(\epsilon\) falls.
  • A fixed-step gradient method with a gradient-norm stopping rule converges on the relaxation in about twenty steps and does not converge on the exact objective within \(400\) steps. The exact ball is better served by a method that tolerates kinks, such as the derivative-sign bisection the script uses as its reference.

References

  • Jie Wang, Rui Gao and Yao Xie, "Sinkhorn Distributionally Robust Optimization", Operations Research, 2025 (arXiv:2109.11926).
  • Marco Cuturi, "Sinkhorn Distances: Lightspeed Computation of Optimal Transport", Advances in Neural Information Processing Systems 26, 2013.
  • Peyman Mohajerin Esfahani and Daniel Kuhn, "Data-Driven Distributionally Robust Optimization Using the Wasserstein Metric", Mathematical Programming 171, 2018.

Source

tutorials/12_sinkhorn_relaxation_of_wasserstein_dro.py
r"""Entropic (Sinkhorn) relaxation of Wasserstein DRO.

The worst-case expectation over a Wasserstein ball on a finite support is
a linear program, and its value is a piecewise-linear function of the loss
vector. Along a smooth path of decisions the robust objective is therefore
piecewise smooth in the decision, with kinks wherever the optimal transport
plan changes. The kinks are not a numerical artifact. The minimizer of the
robust objective typically sits on one, because a maximum of smooth
functions is minimized where two of them cross. An outer method that
relies on a gradient-norm stopping test then never stops, and a fixed-step
gradient method oscillates around the kink indefinitely.

Sinkhorn DRO (Wang, Gao and Xie 2025) replaces the Wasserstein distance by
its entropic regularization with strength epsilon. On a finite support the
ball becomes a mixture of KL balls with one shared budget, one around each
row of the Gibbs kernel Q_ij proportional to exp(-c_ij / epsilon). Its
worst case is an exponential tilt, a smooth function of the loss whose
first and second derivatives are computed exactly. `SinkhornAmbiguitySet`
computes it with the same bracketed bisection as `KLAmbiguitySet`. This
script measures what the relaxation costs in accuracy and what it gains in
smoothness.

* As epsilon -> 0 the Sinkhorn worst case converges to the exact
  `WassersteinAmbiguitySet` value at the same radius, and so does the
  Sinkhorn minimizer. The script sweeps epsilon over three decades and
  records the gap at a fixed decision, the uniform gap over a grid of
  decisions, and the distance between the two minimizers.
* The Sinkhorn objective is differentiable everywhere. On a fine grid of
  decisions, the increments of its derivative between neighbouring points
  stay within the grid step times its largest second derivative, as the
  mean value theorem requires of a twice differentiable function. The
  Wasserstein derivative exceeds the bound its own curvature allows by an
  order of magnitude, because it jumps. The Sinkhorn second derivative is
  finite, with peaks that sharpen toward the Wasserstein kinks as epsilon
  falls.
* `MinimaxSolver` with a fixed-step `GradientDescent` converges on the
  Sinkhorn objective and reports it, while on the Wasserstein objective it
  exhausts its iteration budget without meeting the gradient tolerance.

One convention matters for the comparison. `SinkhornAmbiguitySet.radius` is
the transport budget in excess of the smallest Sinkhorn distance any
distribution can have from the nominal, so radius zero is the
kernel-smoothed nominal rather than the nominal itself. Under the counting
reference measure that smallest distance is minus the entropic bias,
epsilon * sum_i p_i log sum_k exp(-c_ik / epsilon), which is epsilon times
the expected log-partition of the kernel rows. The bias vanishes with
epsilon, so the same number serves as the radius of both sets in the
limit. The script prints it next to the gap.

The problem is the one of tutorial 11, namely forty log-normal demands with
equal weight, the ground cost |xi - xi'|, and the quadratic loss
(theta - xi)^2 of a scalar location decision. The loss is smooth in theta,
so every kink in the Wasserstein objective comes from the ambiguity set.

References:
    Jie Wang, Rui Gao and Yao Xie, "Sinkhorn Distributionally Robust
    Optimization", Operations Research, 2025 (arXiv:2109.11926).
    Marco Cuturi, "Sinkhorn Distances: Lightspeed Computation of Optimal
    Transport", NeurIPS, 2013.
    Peyman Mohajerin Esfahani and Daniel Kuhn, "Data-Driven
    Distributionally Robust Optimization Using the Wasserstein Metric",
    Mathematical Programming 171, 2018.

Run:
    python tutorials/12_sinkhorn_relaxation_of_wasserstein_dro.py
"""

from collections.abc import Callable

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

from optora.core import AmbiguitySet
from optora.dro import (
    MinimaxProblem,
    MinimaxSolver,
    SinkhornAmbiguitySet,
    WassersteinAmbiguitySet,
)
from optora.solvers import GradientDescent

SEED = 20261103
NUM_SCENARIOS = 40
RADIUS = 0.1
DECISION = 1.0
EPSILONS = torch.logspace(0.0, -3.0, 13, dtype=torch.float64)
# The value gap and the exact suboptimality of the relaxed decision are
# asserted linear in epsilon from here down, with these constants. Above it
# the kernel is so flat that the smoothed centre of the ball dominates.
LINEAR_FROM, VALUE_RATE, DECISION_RATE = 0.1, 0.15, 0.05
SMOOTH_EPSILONS = (0.1, 0.03, 0.01)
GRID_POINTS = 801
BISECTION_STEPS = 50
OUTER_STEP, OUTER_ITERATIONS, OUTER_TOLERANCE = 0.2, 400, 1e-8
TRAJECTORY_STEPS = 60
INITIAL_DECISION = 2.0


def quadratic(theta: torch.Tensor, demand: torch.Tensor) -> torch.Tensor:
    """Squared error of a location decision `theta` of shape `(..., 1)`."""
    return (theta - demand) ** 2


def sample_demand() -> tuple[torch.Tensor, torch.Tensor]:
    """Return a uniform nominal over a sorted log-normal demand sample."""
    generator = torch.Generator().manual_seed(SEED)
    demand = torch.exp(
        0.5 * torch.randn(NUM_SCENARIOS, generator=generator, dtype=torch.float64)
    )
    demand = torch.sort(demand).values
    return torch.full_like(demand, 1.0 / NUM_SCENARIOS), demand


def entropic_bias(nominal: torch.Tensor, cost: torch.Tensor, epsilon: float) -> float:
    r"""Return $\epsilon \sum_i p_i \log \sum_k e^{-c_{ik}/\epsilon}$.

    This is minus the smallest Sinkhorn distance from the nominal under the
    counting reference measure, the amount `SinkhornAmbiguitySet.radius`
    leaves out of its budget.
    """
    return epsilon * float(nominal @ torch.logsumexp(-cost / epsilon, dim=-1))


def derivatives(
    ambiguity_set: AmbiguitySet, theta: torch.Tensor, demand: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    """Return the robust objective and its first two derivatives at `theta`.

    Both derivatives come from autograd. The first is exact for either set
    by the envelope theorem. For the Sinkhorn set the second is exact as
    well. For the Wasserstein set autograd differentiates the active
    transport plan, so it reports the curvature of the loss and misses the
    jumps of the first derivative, which have no finite second derivative.

    Args:
        ambiguity_set: Wasserstein or Sinkhorn ball over the demands.
        theta: Decisions of shape `(G,)`.
        demand: Scenarios of shape `(n,)`.

    Returns:
        The value, first and second derivative, each of shape `(G,)`.
    """
    theta = theta.detach().requires_grad_()
    value = ambiguity_set.worst_case_expectation(quadratic(theta[:, None], demand))
    (first,) = torch.autograd.grad(value.sum(), theta, create_graph=True)
    (second,) = torch.autograd.grad(first.sum(), theta)
    return value.detach(), first.detach(), second


def robust_slope(
    ambiguity_set: AmbiguitySet, theta: torch.Tensor, demand: torch.Tensor
) -> torch.Tensor:
    """Return the derivative of the robust objective at `theta`, shape `(G,)`."""
    theta = theta.detach().requires_grad_()
    value = ambiguity_set.worst_case_expectation(quadratic(theta[:, None], demand))
    (first,) = torch.autograd.grad(value.sum(), theta)
    return first


def argmin_by_bisection(
    derivative: Callable[[torch.Tensor], torch.Tensor],
    lower: float,
    upper: float,
) -> float:
    """Minimize a convex function by bisecting the sign of its derivative.

    Fifty halvings of the demand range locate the minimizer to about
    1e-15, also where the derivative jumps, so the result serves as the
    reference minimizer of the nonsmooth objective.
    """
    low = torch.tensor(lower, dtype=torch.float64)
    high = torch.tensor(upper, dtype=torch.float64)
    for _ in range(BISECTION_STEPS):
        middle = 0.5 * (low + high)
        if derivative(middle[None])[0] > 0.0:
            high = middle
        else:
            low = middle
    return 0.5 * float(low + high)


def robust_minimizer(ambiguity_set: AmbiguitySet, demand: torch.Tensor) -> float:
    """Return the minimizer of the robust objective over the demand range."""
    return argmin_by_bisection(
        lambda theta: robust_slope(ambiguity_set, theta, demand),
        float(demand.min()),
        float(demand.max()),
    )


def gradient_trajectory(
    ambiguity_set: AmbiguitySet, demand: torch.Tensor
) -> torch.Tensor:
    """Unroll fixed-step gradient descent and return `|dV/dtheta|` per step."""
    theta = torch.tensor([INITIAL_DECISION], dtype=torch.float64)
    norms = []
    for _ in range(TRAJECTORY_STEPS):
        gradient = robust_slope(ambiguity_set, theta, demand)
        norms.append(gradient.abs())
        theta = theta - OUTER_STEP * gradient
    return torch.cat(norms)


def main() -> None:
    nominal, demand = sample_demand()
    cost = (demand[:, None] - demand[None, :]).abs()
    wasserstein = WassersteinAmbiguitySet(nominal, cost, RADIUS)
    sinkhorn = {
        epsilon: SinkhornAmbiguitySet(nominal, cost, epsilon, RADIUS)
        for epsilon in EPSILONS.tolist()
    }
    smooth = {
        epsilon: SinkhornAmbiguitySet(nominal, cost, epsilon, RADIUS)
        for epsilon in SMOOTH_EPSILONS
    }
    theta = torch.tensor([DECISION], dtype=torch.float64)
    loss = quadratic(theta, demand)

    # 1. The worst-case value at one decision, as epsilon vanishes.
    exact = float(wasserstein.worst_case_expectation(loss))
    values = torch.tensor(
        [float(ball.worst_case_expectation(loss)) for ball in sinkhorn.values()]
    )
    biases = torch.tensor(
        [entropic_bias(nominal, cost, epsilon) for epsilon in sinkhorn]
    )
    gaps = (values - exact).abs()
    print(
        f"radius {RADIUS}, decision theta = {DECISION}: "
        f"exact Wasserstein worst case {exact:.10f}"
    )
    print("  epsilon     Sinkhorn value   |gap|       entropic bias")
    for epsilon, value, gap, bias in zip(
        sinkhorn, values.tolist(), gaps.tolist(), biases.tolist(), strict=True
    ):
        print(f"  {epsilon:9.3e}  {value:14.10f}   {gap:9.3e}   {bias:9.3e}")
    linear = EPSILONS <= LINEAR_FROM
    print(
        f"  gap / epsilon <= {float((gaps / EPSILONS)[linear].max()):.3f} for"
        f" epsilon <= {LINEAR_FROM}; the gap changes sign twice on the way down"
    )
    assert torch.all(gaps[linear] <= VALUE_RATE * EPSILONS[linear])
    assert gaps[-1] < 1e-4 and biases[-1] < 1e-4

    # 2. Smoothness in the decision, on a grid over the demand range.
    grid = torch.linspace(
        float(demand.min()), float(demand.max()), GRID_POINTS, dtype=torch.float64
    )
    step = float(grid[1] - grid[0])
    curves: dict[str, tuple[torch.Tensor, torch.Tensor, torch.Tensor]] = {
        "wasserstein": derivatives(wasserstein, grid, demand)
    }
    for epsilon in SMOOTH_EPSILONS:
        curves[f"sinkhorn {epsilon}"] = derivatives(smooth[epsilon], grid, demand)
    exact_value, exact_slope, _ = curves["wasserstein"]
    print(
        f"\nderivative dV/dtheta on {GRID_POINTS} grid points (step {step:.2e})"
        " over the demand range"
    )
    print(
        "  objective        largest increment   step * max V''   ratio"
        "   sup |V - V_W|   mean |V' - V_W'|"
    )
    excess = {}
    for name, (value, slope, curvature) in curves.items():
        increment = float((slope[1:] - slope[:-1]).abs().max())
        bound = step * float(curvature.max())
        excess[name] = increment / bound
        print(
            f"  {name:16s} {increment:17.4e}   {bound:14.4e}   {excess[name]:5.2f}"
            f"   {float((value - exact_value).abs().max()):13.3e}"
            f"   {float((slope - exact_slope).abs().mean()):16.3e}"
        )
    # A C^2 function moves its derivative by at most the step times its
    # largest curvature between neighbouring grid points, and attains that
    # bound where the curvature peaks are wide against the step. The
    # Wasserstein derivative exceeds it by an order of magnitude because it
    # jumps.
    assert excess["wasserstein"] > 5.0
    assert all(excess[f"sinkhorn {epsilon}"] <= 1.05 for epsilon in SMOOTH_EPSILONS)
    uniform = [
        float((curves[f"sinkhorn {epsilon}"][0] - exact_value).abs().max())
        for epsilon in SMOOTH_EPSILONS
    ]
    slope_gaps = [
        float((curves[f"sinkhorn {epsilon}"][1] - exact_slope).abs().mean())
        for epsilon in SMOOTH_EPSILONS
    ]
    assert uniform == sorted(uniform, reverse=True)
    assert slope_gaps == sorted(slope_gaps, reverse=True)

    # 3. The minimizers, and the exact robust value of the relaxed decision.
    exact_minimizer = robust_minimizer(wasserstein, demand)
    minimizers = torch.tensor(
        [robust_minimizer(ball, demand) for ball in sinkhorn.values()]
    )
    exact_at = lambda point: float(  # noqa: E731
        wasserstein.worst_case_expectation(
            quadratic(torch.tensor([point], dtype=torch.float64), demand)
        )
    )
    suboptimality = torch.tensor(
        [exact_at(point) - exact_at(exact_minimizer) for point in minimizers.tolist()]
    )
    left, right = (
        float(robust_slope(wasserstein, torch.tensor([point]), demand))
        for point in (exact_minimizer - 1e-7, exact_minimizer + 1e-7)
    )
    print(
        f"\nWasserstein minimizer {exact_minimizer:.8f}; its derivative jumps"
        f" from {left:+.4e} to {right:+.4e} across the minimizer"
    )
    print("  epsilon     Sinkhorn minimizer   distance    exact suboptimality")
    for epsilon, point, gap in zip(
        sinkhorn,
        minimizers.tolist(),
        suboptimality.tolist(),
        strict=True,
    ):
        print(
            f"  {epsilon:9.3e}  {point:18.8f}   {abs(point - exact_minimizer):9.3e}"
            f"   {gap:9.3e}"
        )
    assert left < 0.0 < right
    distances = (minimizers - exact_minimizer).abs()
    print(
        f"  exact suboptimality / epsilon <="
        f" {float((suboptimality / EPSILONS)[linear].max()):.3f} for"
        f" epsilon <= {LINEAR_FROM}; the exact objective rises"
        f" {abs(left) / right:.0f} times faster to the left of its minimizer"
        " than to the right"
    )
    assert torch.all(suboptimality[linear] <= DECISION_RATE * EPSILONS[linear])
    assert distances[-1] < 5e-3 and suboptimality[-1] < 1e-5

    # 4. A gradient-based outer solve through MinimaxSolver.
    solver = GradientDescent(
        step_size=OUTER_STEP,
        max_iter=OUTER_ITERATIONS,
        tol=OUTER_TOLERANCE,
        check_interval=1,
    )
    print(
        f"\nMinimaxSolver with GradientDescent(step_size={OUTER_STEP},"
        f" tol={OUTER_TOLERANCE}) from theta = {INITIAL_DECISION}"
    )
    outcomes = {}
    for name, ball in (
        ("wasserstein", wasserstein),
        *((f"sinkhorn {epsilon}", ball) for epsilon, ball in smooth.items()),
    ):
        result = MinimaxSolver(solver).solve(
            MinimaxProblem(
                ball,
                lambda point: quadratic(point, demand),
                torch.tensor([INITIAL_DECISION], dtype=torch.float64),
            )
        )
        outcomes[name] = result
        print(
            f"  {name:16s} converged {str(result.converged):5s} after"
            f" {result.num_iterations:4d} iterations at"
            f" theta = {float(result.point):.8f}"
        )
    assert not outcomes["wasserstein"].converged
    assert all(outcomes[f"sinkhorn {epsilon}"].converged for epsilon in SMOOTH_EPSILONS)
    trajectories = {
        "wasserstein": gradient_trajectory(wasserstein, demand),
        **{
            f"sinkhorn {epsilon}": gradient_trajectory(ball, demand)
            for epsilon, ball in smooth.items()
        },
    }
    tail = trajectories["wasserstein"][TRAJECTORY_STEPS // 2 :]
    print(
        f"  unrolled {TRAJECTORY_STEPS} steps: Wasserstein |dV/dtheta| stays between"
        f" {float(tail.min()):.1e} and {float(tail.max()):.1e} over the second half,"
        f" Sinkhorn {SMOOTH_EPSILONS[-1]} ends at"
        f" {float(trajectories[f'sinkhorn {SMOOTH_EPSILONS[-1]}'][-1]):.1e}"
    )
    assert float(tail.min()) > 1e-4
    assert all(
        float(trajectories[f"sinkhorn {epsilon}"][-1]) < 1e-8
        for epsilon in SMOOTH_EPSILONS
    )

    fig, (limit, slopes, descent) = plt.subplots(1, 3, figsize=(14.5, 4.4))
    epsilons = EPSILONS.numpy()
    limit.plot(epsilons, gaps.numpy(), "o-", color="C5", label="$|V_\\epsilon - V_W|$")
    limit.plot(
        epsilons,
        distances.numpy(),
        "s-",
        color="C5",
        markerfacecolor="none",
        label="$|\\theta_\\epsilon - \\theta_W|$",
    )
    limit.plot(
        epsilons,
        suboptimality.numpy(),
        "d-",
        color="C4",
        label="$V_W(\\theta_\\epsilon) - V_W(\\theta_W)$",
    )
    limit.plot(
        epsilons, biases.numpy(), ":", color="0.6", linewidth=1.0, label="entropic bias"
    )
    limit.set_xscale("log")
    limit.set_yscale("log")
    limit.invert_xaxis()
    limit.set_xlabel("entropic strength $\\epsilon$")
    limit.set_ylabel("gap to the exact Wasserstein solution")
    limit.set_title("the relaxation converges as $\\epsilon \\to 0$")
    limit.legend(loc="upper right", fontsize=7.5)

    slopes.plot(
        grid.numpy(),
        exact_slope.numpy(),
        color="C4",
        linewidth=1.2,
        label="Wasserstein (jumps)",
    )
    for epsilon, alpha in zip(SMOOTH_EPSILONS, (0.4, 0.7, 1.0), strict=True):
        slopes.plot(
            grid.numpy(),
            curves[f"sinkhorn {epsilon}"][1].numpy(),
            color="C5",
            alpha=alpha,
            label=f"Sinkhorn $\\epsilon$ = {epsilon}",
        )
    slopes.axhline(0.0, color="0.6", linewidth=0.8)
    slopes.axvline(exact_minimizer, color="0.6", linewidth=0.8, linestyle="--")
    window = 0.35
    slopes.set_xlim(exact_minimizer - window, exact_minimizer + window)
    inside = (grid > exact_minimizer - window) & (grid < exact_minimizer + window)
    slopes.set_ylim(
        float(exact_slope[inside].min()) - 0.05, float(exact_slope[inside].max()) + 0.05
    )
    slopes.set_xlabel("decision $\\theta$")
    slopes.set_ylabel("$\\mathrm{d}V / \\mathrm{d}\\theta$")
    slopes.set_title("the Wasserstein derivative jumps, Sinkhorn's bends")
    slopes.legend(loc="upper left", fontsize=7.5)

    steps = range(1, TRAJECTORY_STEPS + 1)
    descent.plot(
        steps,
        trajectories["wasserstein"].numpy(),
        color="C4",
        label="Wasserstein",
    )
    for epsilon, alpha in zip(SMOOTH_EPSILONS, (0.4, 0.7, 1.0), strict=True):
        descent.plot(
            steps,
            trajectories[f"sinkhorn {epsilon}"].numpy(),
            color="C5",
            alpha=alpha,
            label=f"Sinkhorn $\\epsilon$ = {epsilon}",
        )
    descent.axhline(
        OUTER_TOLERANCE, color="0.6", linewidth=0.8, linestyle=":", label="tolerance"
    )
    descent.set_yscale("log")
    descent.set_xlabel("gradient step")
    descent.set_ylabel("$|\\mathrm{d}V / \\mathrm{d}\\theta|$ at the iterate")
    descent.set_title(f"fixed step {OUTER_STEP}: only the relaxation converges")
    descent.legend(loc="upper right", fontsize=7.5)

    fig.tight_layout(w_pad=2.0)
    save_figure(fig, "12_sinkhorn_relaxation_of_wasserstein_dro")


if __name__ == "__main__":
    main()