Robust regret against robust loss¶
Distributionally robust optimization protects a decision against every distribution in a ball around the sample. This protection makes the decision conservative. The worst case counts every unit of loss the adversary can produce, including loss that no decision could have avoided, and the robust decision moves away from the empirical one to guard against it. Fiechtner and Blanchet (2025) propose to count only the loss that a better decision would have avoided, which means minimizing the worst-case regret rather than the worst-case loss. They prove that, for a smooth loss with a unique empirical-risk minimizer, this leaves the empirical-risk decision unchanged to first order in the radius, while ordinary Wasserstein DRO typically moves it at first order.
This tutorial builds their distributionally robust regret optimization
(DRRO) on top of WassersteinAmbiguitySet and MinimaxSolver, measures
both claims, and compares the outcome with Wasserstein DRO on three losses,
one of which is the paper's own newsvendor.
Two kinds of regret¶
Write \(\ell(\theta, \xi)\) for the loss of a decision \(\theta\) when the uncertain quantity takes the value \(\xi\), and \(P\) for a distribution of \(\xi\). The regret of \(\theta\) under \(P\) is its expected loss in excess of the best decision for that distribution,
The benchmark \(\beta\) is chosen knowing \(P\) but before \(\xi\) is drawn, which the paper calls ex-ante regret. DRRO minimizes its worst case over a Wasserstein ball \(B_\delta\) around the empirical distribution \(\hat P\),
There is a simpler notion, ex-post regret, where the benchmark is chosen
after seeing \(\xi\). It is the per-scenario quantity
\(\ell(\theta, \xi) - \min_\beta \ell(\beta, \xi)\). The hindsight term does
not depend on \(\theta\), so this is the original loss shifted scenario by
scenario, and the shifted loss goes into MinimaxSolver unchanged. The
script tests both, and the difference between them turns out to matter.
Which regret the paper means
The theorems below are about ex-ante regret. Ex-post regret is included because it is the natural first thing to try, and because for two of the three losses it changes nothing. When every scenario can be fit exactly, the hindsight term is zero and ex-post DRRO coincides with Wasserstein DRO. The script asserts that.
Ex-ante regret as a worst-case expectation¶
The negative of the inner minimum over \(\beta\) is a supremum of linear functionals of \(P\), and the two suprema can be exchanged, so
The inner supremum is now the worst-case expectation of an ordinary
per-scenario loss, the difference \(\ell(\theta, \cdot) - \ell(\beta, \cdot)\)
for a fixed pair of decisions. This is what
WassersteinAmbiguitySet.worst_case_expectation computes, so the only new
ingredient is the outer maximization over the benchmark. The paper works
with the same representation (its Section 3 writes
\(R_\delta(\theta) = \sup_\beta F_\delta(\theta, \beta)\)), and this is why
no new solver is needed.
In the script the "wrapper" therefore takes two lines. It subtracts the loss of the benchmark from the loss of the decision and passes the difference to the ambiguity set. The outer maximization is the hard part. The paper shows that evaluating the regret exactly is NP-hard already for max-affine losses (its Theorem 5). Here the decision is a scalar, which keeps the maximization tractable.
The problems¶
Forty demands are drawn from a log-normal distribution and given equal weight. The script studies three losses.
| Loss | Form | Why it is here |
|---|---|---|
| quadratic | \((\theta - \xi)^2\) | The case of the paper's Theorem 4, stated there for type-\(p\) balls with \(p \ge 2\), in which the empirical-risk decision is DRRO-optimal at every radius. |
| LINEX (Varian 1975) | \(e^{a(\theta - \xi)} - a(\theta - \xi) - 1\) | A smooth, asymmetric loss outside the quadratic family, for Theorem 3. |
| newsvendor | \(b\theta - s\min(\theta, \xi)\) | The paper's own example. It is piecewise linear, so the smoothness assumption fails. |
The estimation losses use the ground cost \(|\xi - \xi'|\). Under it, and as long as both ends stay inside the support, the set of means the adversary can reach is the interval \([\bar\xi - \delta, \bar\xi + \delta]\), which is symmetric about the sample mean. This symmetry is what makes the quadratic result exact. The newsvendor uses \((\xi - \xi')^2\), which penalizes long moves more than proportionally, so the adversary prefers many short moves to one long one. With the absolute cost, the best moves of the newsvendor adversary shift mass among low-demand scenarios. These moves raise the worst-case loss at the largest possible rate without ever crossing the order quantity, and no decision changes until the radius is large. This behaviour is correct for a discrete support, but it hides the effect the comparison is meant to show.
Solving each policy¶
Every decision is a scalar and every objective is convex in it, so each
outer problem is solved by bisection on the sign of its derivative. After
34 halvings of the demand range the bracket is about \(10^{-10}\) wide, and
this accuracy holds at a kink as well. This matters because the DRRO
objective has a kink exactly at its minimum. The derivative of a
worst-case expectation is the expected derivative of the loss under the
worst-case distribution, and that distribution is the gradient of the worst
case with respect to the loss (Danskin's theorem), so torch.autograd
returns it together with the value.
The benchmark maximization starts from the best candidate on either side of \(\theta\) in a small grid and then alternates best responses. The adversary answers \(\beta\) with its worst-case distribution, and \(\beta\) answers with the empirical-risk minimizer under that distribution. Each round can only raise the regret. For the newsvendor the candidates are the scenarios themselves, which is exact for a piecewise-linear loss, and no rounds are needed. Every inner solution is compared with a dense benchmark grid, and the script asserts that the grid never exceeds it by more than \(10^{-9}\).
Wasserstein DRO and ex-post DRRO are ordinary MinimaxProblems, with the
loss or the shifted loss as loss_fn. The script solves them by the same
bisection for a like-for-like comparison and cross-checks the result
against MinimaxSolver at one radius. The two agree to
\(1.3 \times 10^{-2}\), which is the size of the oscillation that a fixed
step of \(10^{-2}\) leaves around the kink of a piecewise-linear objective.
Ex-ante DRRO is not a MinimaxProblem, because the supremum over the
benchmark sits outside the worst-case expectation rather than inside a
per-scenario loss.
The quadratic case stays at the empirical decision¶
For the quadratic loss, the regret under \(P\) is \((\theta - \mathbb{E}_P[\xi])^2\), which depends on \(P\) only through its mean. The robust regret is therefore the squared distance from \(\theta\) to the farther end of the reachable-mean interval, and its minimizer is the midpoint of that interval. With the absolute ground cost the interval is symmetric about the sample mean until one of its ends reaches the edge of the support, so the DRRO decision is the empirical-risk decision. The paper proves this result for strictly convex quadratic losses under type-\(p\) Wasserstein balls with \(p \ge 2\) on an unbounded support (its Theorem 4). Optora's ball reweights a fixed sample instead, and the conclusion follows here from the symmetric interval. The script finds that the two decisions agree to the last bit at every radius and that the robust regret equals \(\delta^2\), the squared half-width of the interval, to within \(10^{-11}\). Wasserstein DRO, on the same ball, moves by \(\delta\) to within \(10^{-10}\).
The animation shows why. The robust regret is the larger of two parabolas, one for the adversary that pushes the mean up and one for the adversary that pushes it down, and the two cross exactly at the sample mean. Raising the radius lifts the crossing point but does not move it sideways. The worst-case loss has no such symmetry. Its adversary moves mass toward whichever end of the support is farther from \(\theta\), and the minimizer drifts toward the middle of the support.
A smooth loss keeps only the second-order term¶
The LINEX loss is smooth and strictly convex but not quadratic, so Theorem 3 is the relevant result. The DRRO decision is \(\theta_0 + o(\delta)\) and its value is \(\kappa\delta^2 + o(\delta^2)\), whereas Wasserstein DRO typically moves the decision by \(O(\delta)\) and pays a price of the same order. The script fits log-log slopes on the five smallest radii, \(10^{-3}\) to \(10^{-2}\).
| Quantity | Wasserstein DRO | ex-ante DRRO |
|---|---|---|
| \(\lvert\theta_\delta - \theta_{\mathrm{ERM}}\rvert\) | slope \(0.98\) | slope \(2.52\) |
| value above the empirical risk | slope \(0.95\) (price of robustness) | slope \(1.93\) (robust regret) |
At \(\delta = 10^{-2}\) the DRO decision has moved by \(2.0 \times 10^{-2}\) and the DRRO decision by \(6.7 \times 10^{-4}\), thirty times less. The slopes at the two largest radii bend away from these values. There the pair of scenarios the adversary trades between changes, and the constant in front of \(\delta\) changes with it. The theorems are statements about the limit \(\delta \to 0\).
Which way each newsvendor policy moves¶
The paper introduces DRRO with a newsvendor and reports, at one radius, that DRO orders less than the empirical-risk policy while DRRO orders more. The same ordering appears here at every radius from \(10^{-2}\) on. DRO orders less and both regret policies order more, and from \(3 \times 10^{-2}\) on the ex-ante policy is the one that moves least. At \(\delta = 0.1\) the order quantities relative to the empirical-risk policy are \(-0.23\) (DRO), \(+0.13\) (ex-post) and \(+0.06\) (ex-ante), on an empirical order of \(1.22\).
The ex-post wrapper has a concrete meaning for this loss. Its hindsight term is \(-(s - b)\,\xi\), the profit of ordering exactly the demand, and subtracting it turns the profit form of the loss into the classical cost form \(b(\theta - \xi)_+ + (s - b)(\xi - \theta)_+\). The profit-form adversary gains from low demand whatever quantity was ordered, so DRO orders less. The cost-form adversary gains only from demand on the wrong side of the order, so it attacks both sides, and the decision moves the other way.
Two things differ from the smooth cases. First, the decisions do not move smoothly with the radius. With a piecewise-linear loss on a finite support the robust objectives are piecewise linear in \(\theta\), so their minimizers sit at kinks, and a kink can stay in place over a range of radii and then jump. Second, at small radii the ex-ante decision lies slightly below the empirical one before it settles above it. With a piecewise-linear loss the assumptions of Theorem 3 do not hold, and the paper notes that the regret policy "can become more or less aggressive as the radius grows, depending on the loss profile".
The figure¶
The figure is rendered from this script when the site is built, so it always shows the output of the current code.
What to take away¶
- Ex-ante robust regret is \(\sup_\beta\) of a worst-case expectation of a
loss difference, so
WassersteinAmbiguitySetevaluates it with a two-line wrapper and no new solver. The maximization over the benchmark is the expensive part. - Ex-post regret is a per-scenario shift of the loss and goes into
MinimaxSolveras is. For losses whose scenarios can be fit exactly it is the original problem, and for the newsvendor it is the cost form. - On a quadratic loss the DRRO decision is the empirical-risk decision at every radius, to the last bit. On a smooth loss it moves at second order where Wasserstein DRO moves at first order.
- On the paper's newsvendor the regret policies order more and DRO orders less, and the ex-ante policy stays closest to the empirical decision.
- Optora's Wasserstein ball reweights the sample rather than moving its points, so on a piecewise-linear loss the decisions jump between kinks as the radius grows. The theorems assume a smooth loss, so the newsvendor results are measurements rather than consequences of the theorems.
References¶
- Lukas-Benedikt Fiechtner and Jose Blanchet, "Wasserstein Distributionally Robust Regret Optimization", arXiv:2504.10796, 2025.
- Hal R. Varian, "A Bayesian Approach to Real Estate Assessment", in Studies in Bayesian Econometrics and Statistics in Honor of Leonard J. Savage, North-Holland, 1975.
- Peyman Mohajerin Esfahani and Daniel Kuhn, "Data-Driven Distributionally Robust Optimization Using the Wasserstein Metric", Mathematical Programming 171, 2018.
Source¶
r"""Distributionally robust regret against distributionally robust loss.
Wasserstein DRO guards against the worst distribution in a ball around the
sample, and this makes it conservative. The worst case counts every unit of
loss the adversary can create, including loss that no decision could have
avoided. Fiechtner and Blanchet (2025) replace the worst-case loss by the
worst-case regret. The regret of a decision theta under a distribution P is
its expected loss in excess of the best decision for that distribution,
R(theta, P) = E_P[l(theta, xi)] - min_beta E_P[l(beta, xi)],
and distributionally robust regret optimization (DRRO) minimizes
sup_P R(theta, P) over the Wasserstein ball. The benchmark beta is chosen
knowing P but before xi is drawn, which the paper calls ex-ante regret.
The negative of the inner minimum is a supremum of linear functionals of
P, so
sup_P R(theta, P) = sup_beta sup_P E_P[l(theta, xi) - l(beta, xi)],
and the inner supremum is a worst-case expectation of an ordinary
per-scenario loss, the difference l(theta, .) - l(beta, .). This is what
`WassersteinAmbiguitySet.worst_case_expectation` computes, so the only new
ingredient is the outer maximization over the benchmark.
This script measures the paper's two main results. With a smooth loss and
a unique empirical-risk minimizer theta_0, the DRRO decision is
theta_0 + o(delta) and the robust regret is kappa delta^2 + o(delta^2),
whereas the Wasserstein DRO decision typically moves by O(delta)
(Theorem 3). For a strictly convex quadratic loss and a type-p ball with
p >= 2, the empirical-risk decision is DRRO-optimal at every radius
(Theorem 4). Both are checked on a sample of forty log-normal demands, with
three losses.
* The quadratic loss (theta - xi)^2 is the Theorem 4 case. The regret under
P is (theta - E_P[xi])^2, and the robust regret has a closed form.
* The LINEX loss exp(a(theta - xi)) - a(theta - xi) - 1 (Varian 1975) is a
smooth, asymmetric loss outside the quadratic family, for Theorem 3.
* The newsvendor loss b theta - s min(theta, xi) of the paper's own example
is piecewise linear, so the smoothness assumption fails and the decisions
do not move smoothly with the radius.
The script also tests the simpler per-scenario (ex-post) regret,
l(theta, xi) - min_beta l(beta, xi), because it is a wrapper around the
loss and goes into `MinimaxSolver` unchanged. For the first two losses
every scenario can be fit exactly, the hindsight term is zero, and ex-post
DRRO coincides with Wasserstein DRO. For the newsvendor the hindsight term
is -(s - b) xi, and subtracting it turns the profit form of the loss into
the classical cost form b (theta - xi)_+ + (s - b)(xi - theta)_+.
The estimation losses use the ground cost |xi - xi'|, under which the
reachable means of the ball form the interval [mean - delta, mean + delta]
and the quadratic result is exact. Optora's ball reweights the fixed
sample, unlike the balls of the paper, and on this support the symmetric
interval gives the Theorem 4 conclusion directly. The newsvendor uses the
ground cost (xi - xi')^2, which penalizes long moves more than
proportionally. With the absolute cost the adversary's best moves shift
mass among low-demand scenarios without crossing the order quantity, and no
decision changes until the radius is large.
Every decision is scalar and every objective is convex in it, so each
outer problem is solved by bisection on the sign of its derivative. This
stays accurate to the final bracket width, about 1e-10 here, even at a
kink. The derivative of a worst-case expectation is the expected
derivative of the loss under the worst-case distribution, which autograd
returns. The benchmark maximization starts from the best grid point on
either side of theta and then alternates best responses. The adversary
answers beta with its worst-case distribution, and beta answers with the
empirical-risk minimizer under it. For the newsvendor the candidates are
the scenarios themselves, which is exact for a piecewise-linear loss. Each
inner solution is compared with a dense benchmark grid, and the
Wasserstein DRO and ex-post decisions are cross-checked against
`MinimaxSolver`.
References:
Lukas-Benedikt Fiechtner and Jose Blanchet, "Wasserstein
Distributionally Robust Regret Optimization", arXiv:2504.10796, 2025.
Hal R. Varian, "A Bayesian Approach to Real Estate Assessment", in
Studies in Bayesian Econometrics and Statistics in Honor of Leonard J.
Savage, North-Holland, 1975.
Peyman Mohajerin Esfahani and Daniel Kuhn, "Data-Driven
Distributionally Robust Optimization Using the Wasserstein Metric",
Mathematical Programming 171, 2018.
Run:
python tutorials/11_distributionally_robust_regret.py
"""
from collections.abc import Callable
from dataclasses import dataclass
import matplotlib.pyplot as plt
import torch
from _plotting import save_figure
from optora.dro import MinimaxProblem, MinimaxSolver, WassersteinAmbiguitySet
from optora.solvers import GradientDescent
SEED = 20261103
NUM_SCENARIOS = 40
LINEX_SHAPE = 1.5
ORDER_COST, SALE_PRICE = 1.0, 3.0
RATE_RADII = torch.logspace(-3.0, -1.5, 7, dtype=torch.float64)
# Slopes are read where the asymptotics apply. At the largest radii the pair
# of scenarios the Wasserstein adversary trades between changes, and the
# constant in front of the radius changes with it.
SLOPE_RADII = RATE_RADII <= 1e-2
NEWSVENDOR_RADII = torch.logspace(-4.0, -0.5, 8, dtype=torch.float64)
BENCHMARK_GRID = 9
BEST_RESPONSE_ROUNDS = 2
BISECTION_STEPS = 34
DENSE_GRID = 200
EXACT_TOLERANCE = 1e-9
CROSS_CHECK_RADIUS = 6
CROSS_CHECK_SOLVER = GradientDescent(
step_size=1e-2, max_iter=150, tol=1e-9, check_interval=1
)
Loss = Callable[[torch.Tensor, torch.Tensor], torch.Tensor]
def quadratic(theta: torch.Tensor, demand: torch.Tensor) -> torch.Tensor:
"""Squared error of a location estimate `theta` of shape `(..., 1)`."""
return (theta - demand) ** 2
def linex(theta: torch.Tensor, demand: torch.Tensor) -> torch.Tensor:
"""LINEX loss, exponential on one side and linear on the other."""
gap = LINEX_SHAPE * (theta - demand)
return torch.exp(gap) - gap - 1.0
def newsvendor(theta: torch.Tensor, demand: torch.Tensor) -> torch.Tensor:
"""Newsvendor loss in profit form, the order cost minus the revenue."""
return ORDER_COST * theta - SALE_PRICE * torch.minimum(theta, demand)
@dataclass(frozen=True)
class Problem:
"""One decision problem, given by a loss, its ground cost and its radii.
Attributes:
name: Label used in the output and the figure.
loss: Per-scenario loss of a decision of shape `(..., 1)` against
the demand vector, returning shape `(..., n)`.
cost_power: Exponent of `|xi - xi'|` in the ground cost.
radii: Wasserstein radii of the sweep.
benchmarks: Candidate benchmark decisions the regret maximization
starts from, or `None` for an even grid over the demand range.
refine: Whether to improve the best candidates by best-response
rounds. Off when the candidates already contain the maximizer,
as the scenarios do for a piecewise-linear loss.
"""
name: str
loss: Loss
cost_power: float
radii: torch.Tensor
benchmarks: torch.Tensor | None = None
refine: bool = True
@dataclass(frozen=True)
class Policies:
"""Decisions of the four policies at every radius, shape `(R,)` each."""
erm: torch.Tensor
wdro: torch.Tensor
ex_post: torch.Tensor
ex_ante: torch.Tensor
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 regret_loss_fn(
loss_fn: Callable[[torch.Tensor], torch.Tensor], hindsight: torch.Tensor
) -> Callable[[torch.Tensor], torch.Tensor]:
"""Wrap a per-scenario loss into its ex-post regret, for `MinimaxProblem`.
The hindsight term `min_beta l(beta, xi_i)` does not depend on the
decision, so the wrapper only shifts each scenario. What the shift
changes is where the adversary gains from moving mass.
"""
return lambda theta: loss_fn(theta) - hindsight
def argmin_by_bisection(
derivative: Callable[[torch.Tensor], torch.Tensor],
lower: torch.Tensor,
upper: torch.Tensor,
steps: int = BISECTION_STEPS,
) -> torch.Tensor:
"""Minimize convex functions by bisecting the sign of their derivative.
Args:
derivative: Maps a batch of points to a (sub)derivative of each
objective at that point, same shape.
lower: Lower ends of the brackets.
upper: Upper ends of the brackets, same shape as `lower`.
steps: Number of halvings. Forty of them shrink a unit bracket
below $10^{-12}$.
Returns:
The bracket midpoints after `steps` halvings.
"""
for _ in range(steps):
middle = 0.5 * (lower + upper)
positive = derivative(middle) > 0.0
upper = torch.where(positive, middle, upper)
lower = torch.where(positive, lower, middle)
return 0.5 * (lower + upper)
def derivative_of(
objective: Callable[[torch.Tensor], torch.Tensor],
) -> Callable[[torch.Tensor], torch.Tensor]:
"""Return the autograd derivative of a batched scalar objective."""
def derivative(theta: torch.Tensor) -> torch.Tensor:
theta = theta.detach().requires_grad_()
(gradient,) = torch.autograd.grad(objective(theta).sum(), theta)
return gradient
return derivative
def hindsight_losses(loss: Loss, demand: torch.Tensor) -> torch.Tensor:
"""Return `min_beta l(beta, xi_i)` for every scenario."""
margin = demand.max() - demand.min()
best = argmin_by_bisection(
derivative_of(lambda beta: torch.diagonal(loss(beta.unsqueeze(-1), demand))),
torch.full_like(demand, demand.min().item() - margin),
torch.full_like(demand, demand.max().item() + margin),
steps=60,
)
return torch.diagonal(loss(best.unsqueeze(-1), demand)).detach()
def best_response(
loss: Loss, demand: torch.Tensor, weights: torch.Tensor
) -> torch.Tensor:
"""Return the empirical-risk minimizer of `loss` under each distribution.
Args:
loss: Per-scenario loss.
demand: Scenarios, shape `(n,)`.
weights: Distributions over the scenarios, shape `(..., n)`.
Returns:
The minimizing decision per distribution, shape `(...)`.
"""
batch = weights.shape[:-1]
return argmin_by_bisection(
derivative_of(
lambda beta: (weights * loss(beta.unsqueeze(-1), demand)).sum(-1)
),
torch.full(batch, demand.min().item(), dtype=demand.dtype),
torch.full(batch, demand.max().item(), dtype=demand.dtype),
steps=32,
)
def worst_case(
ambiguity_set: WassersteinAmbiguitySet, loss: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]:
"""Return the worst-case expectation of `loss` and the maximizing distribution.
The maximizer is the gradient of the worst case with respect to the
loss (Danskin's theorem), so autograd returns it with the value.
"""
loss = loss.detach().requires_grad_()
value = ambiguity_set.worst_case_expectation(loss)
(adversary,) = torch.autograd.grad(value.sum(), loss)
return value.detach(), adversary
def robust_regret(
ambiguity_set: WassersteinAmbiguitySet,
loss: Loss,
demand: torch.Tensor,
theta: torch.Tensor,
benchmarks: torch.Tensor,
rounds: int = BEST_RESPONSE_ROUNDS,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
r"""Compute the ex-ante robust regret of `theta` at every radius.
Evaluates $\sup_\beta \sup_P \mathbb{E}_P[\ell(\theta) - \ell(\beta)]$.
The inner supremum is a worst-case expectation of the loss difference.
The outer one starts from the best candidate in `benchmarks` on either
side of `theta` and then runs `rounds` rounds of best responses. Each
round can only raise the value.
Args:
ambiguity_set: Wasserstein ball with one radius per entry of
`theta`, broadcast against a trailing benchmark dimension.
loss: Per-scenario loss.
demand: Scenarios, shape `(n,)`.
theta: Decisions, one per radius, shape `(R,)`.
benchmarks: Candidate benchmark decisions, shape `(K,)`.
rounds: Best-response rounds after the candidate scan.
Returns:
The robust regret, the maximizing benchmark and the worst-case
distribution, of shapes `(R,)`, `(R,)` and `(R, n)`.
"""
base = loss(theta[:, None, None], demand)
candidates = ambiguity_set.worst_case_expectation(
base - loss(benchmarks[None, :, None], demand)
)
below = torch.where(benchmarks <= theta[:, None], candidates, -torch.inf)
above = torch.where(benchmarks >= theta[:, None], candidates, -torch.inf)
beta = torch.stack([benchmarks[below.argmax(-1)], benchmarks[above.argmax(-1)]], -1)
for _ in range(rounds):
_, adversary = worst_case(
ambiguity_set, base - loss(beta.unsqueeze(-1), demand)
)
beta = best_response(loss, demand, adversary)
value, adversary = worst_case(
ambiguity_set, base - loss(beta.unsqueeze(-1), demand)
)
side = value.argmax(-1, keepdim=True)
return (
value.gather(-1, side).squeeze(-1),
beta.gather(-1, side).squeeze(-1),
adversary.gather(-2, side.unsqueeze(-1).expand(-1, 1, demand.numel())).squeeze(
-2
),
)
def solve_policies(
problem: Problem, nominal: torch.Tensor, demand: torch.Tensor
) -> tuple[Policies, WassersteinAmbiguitySet, torch.Tensor]:
"""Solve ERM, Wasserstein DRO, ex-post DRRO and ex-ante DRRO at every radius."""
cost = (demand[:, None] - demand[None, :]).abs() ** problem.cost_power
ambiguity_set = WassersteinAmbiguitySet(
nominal, cost, radius=problem.radii[:, None]
)
benchmarks = (
problem.benchmarks
if problem.benchmarks is not None
else torch.linspace(
demand.min().item(), demand.max().item(), BENCHMARK_GRID, dtype=demand.dtype
)
)
hindsight = hindsight_losses(problem.loss, demand)
rounds = BEST_RESPONSE_ROUNDS if problem.refine else 0
bracket = (
torch.full_like(problem.radii, demand.min().item()),
torch.full_like(problem.radii, demand.max().item()),
)
def loss_fn(theta: torch.Tensor) -> torch.Tensor:
return problem.loss(theta[:, None, None], demand)
def robust_loss(theta: torch.Tensor) -> torch.Tensor:
return ambiguity_set.worst_case_expectation(loss_fn(theta)).squeeze(-1)
def robust_ex_post(theta: torch.Tensor) -> torch.Tensor:
return ambiguity_set.worst_case_expectation(
regret_loss_fn(loss_fn, hindsight)(theta)
).squeeze(-1)
def ex_ante_derivative(theta: torch.Tensor) -> torch.Tensor:
_, _, adversary = robust_regret(
ambiguity_set, problem.loss, demand, theta, benchmarks, rounds
)
return derivative_of(
lambda point: (adversary * problem.loss(point.unsqueeze(-1), demand)).sum(
-1
)
)(theta)
policies = Policies(
erm=argmin_by_bisection(
derivative_of(lambda theta: (nominal * loss_fn(theta)).sum(-1).squeeze(-1)),
*bracket,
),
wdro=argmin_by_bisection(derivative_of(robust_loss), *bracket),
ex_post=argmin_by_bisection(derivative_of(robust_ex_post), *bracket),
ex_ante=argmin_by_bisection(ex_ante_derivative, *bracket),
)
return policies, ambiguity_set, benchmarks
def cross_check_minimax(
problem: Problem,
nominal: torch.Tensor,
demand: torch.Tensor,
hindsight: torch.Tensor,
policies: Policies,
) -> float:
"""Re-solve WDRO and ex-post DRRO with `MinimaxSolver` at one radius.
One outer step costs one inner Wasserstein solve, whose fixed overhead
dominates at this support size, so the check runs at a single radius
where the two policies have moved well away from the empirical-risk
decision. Both objectives are piecewise linear in the decision, so a
fixed step oscillates around the minimizer by about `step_size` times
the subgradient, and the returned gap should be read against that.
Returns:
The larger of the two differences from the bisection solutions.
"""
index = CROSS_CHECK_RADIUS
cost = (demand[:, None] - demand[None, :]).abs() ** problem.cost_power
ambiguity_set = WassersteinAmbiguitySet(
nominal, cost, radius=problem.radii[index].item()
)
def loss_fn(theta: torch.Tensor) -> torch.Tensor:
return problem.loss(theta.unsqueeze(-1), demand)
worst_gap = 0.0
for wrapped, reference in (
(loss_fn, policies.wdro[index]),
(regret_loss_fn(loss_fn, hindsight), policies.ex_post[index]),
):
result = MinimaxSolver(CROSS_CHECK_SOLVER).solve(
MinimaxProblem(ambiguity_set, wrapped, policies.erm[index].clone())
)
worst_gap = max(worst_gap, abs(result.point.item() - reference.item()))
return worst_gap
def log_slope(radii: torch.Tensor, values: torch.Tensor) -> float:
"""Least-squares slope of `log(values)` against `log(radii)`."""
x, y = torch.log(radii), torch.log(values)
return ((x - x.mean()) * (y - y.mean())).sum().item() / (
(x - x.mean()) ** 2
).sum().item()
def main() -> None:
nominal, demand = sample_demand()
problems = (
Problem("quadratic", quadratic, 1.0, RATE_RADII),
Problem("LINEX", linex, 1.0, RATE_RADII),
Problem(
"newsvendor",
newsvendor,
2.0,
NEWSVENDOR_RADII,
benchmarks=demand,
refine=False,
),
)
dense = torch.cat(
[
torch.linspace(
demand.min().item(), demand.max().item(), DENSE_GRID, dtype=demand.dtype
),
demand,
]
)
results: dict[str, tuple[Policies, torch.Tensor, torch.Tensor]] = {}
for problem in problems:
policies, ambiguity_set, benchmarks = solve_policies(problem, nominal, demand)
regret, _, _ = robust_regret(
ambiguity_set,
problem.loss,
demand,
policies.ex_ante,
benchmarks,
BEST_RESPONSE_ROUNDS if problem.refine else 0,
)
dense_regret = ambiguity_set.worst_case_expectation(
problem.loss(policies.ex_ante[:, None, None], demand)
- problem.loss(dense[None, :, None], demand)
).amax(-1)
inner_gap = (dense_regret - regret).max().item()
nominal_value = (nominal * problem.loss(policies.erm[:, None], demand)).sum(-1)
price = (
ambiguity_set.worst_case_expectation(
problem.loss(policies.wdro[:, None, None], demand)
).squeeze(-1)
- nominal_value
)
results[problem.name] = (policies, regret, price)
print(f"\n{problem.name}: empirical-risk decision {policies.erm[0].item():.6f}")
print(
" radius WDRO - ERM ex-post - ERM ex-ante - ERM"
" price of robustness robust regret"
)
for index, radius in enumerate(problem.radii.tolist()):
print(
f" {radius:8.2e} {policies.wdro[index] - policies.erm[index]:+12.3e}"
f" {policies.ex_post[index] - policies.erm[index]:+13.3e}"
f" {policies.ex_ante[index] - policies.erm[index]:+13.3e}"
f" {price[index]:19.3e} {regret[index]:13.3e}"
)
print(f" benchmark maximization vs dense grid: {inner_gap:.1e}")
assert inner_gap < EXACT_TOLERANCE
quadratic_policies, quadratic_regret, _ = results["quadratic"]
deviation = (quadratic_policies.ex_ante - quadratic_policies.erm).abs().max().item()
drift = (
(quadratic_policies.wdro - quadratic_policies.erm - RATE_RADII)
.abs()
.max()
.item()
)
curvature = (quadratic_regret - RATE_RADII**2).abs().max().item()
print(
f"\nquadratic: max |ex-ante DRRO - ERM| = {deviation:.1e}, "
f"max |WDRO - ERM - radius| = {drift:.1e}, "
f"max |regret - radius^2| = {curvature:.1e}"
)
assert max(deviation, drift, curvature) < EXACT_TOLERANCE
for name in ("quadratic", "LINEX"):
policies = results[name][0]
assert torch.allclose(policies.ex_post, policies.wdro, atol=1e-10), name
linex_policies, linex_regret, linex_price = results["LINEX"]
fitted = RATE_RADII[SLOPE_RADII]
slopes = {
"WDRO decision": log_slope(
fitted, (linex_policies.wdro - linex_policies.erm).abs()[SLOPE_RADII]
),
"ex-ante decision": log_slope(
fitted, (linex_policies.ex_ante - linex_policies.erm).abs()[SLOPE_RADII]
),
"price of robustness": log_slope(fitted, linex_price[SLOPE_RADII]),
"robust regret": log_slope(fitted, linex_regret[SLOPE_RADII]),
}
print(
"LINEX log-log slopes against the radius, radius <= 1e-2: "
+ ", ".join(f"{k} {v:.2f}" for k, v in slopes.items())
)
assert abs(slopes["WDRO decision"] - 1.0) < 0.1
assert abs(slopes["price of robustness"] - 1.0) < 0.1
assert slopes["ex-ante decision"] > 1.5
assert abs(slopes["robust regret"] - 2.0) < 0.15
assert (
(linex_policies.ex_ante - linex_policies.erm).abs()
< (linex_policies.wdro - linex_policies.erm).abs()
).all()
news_policies, _, _ = results["newsvendor"]
moves = {
name: (getattr(news_policies, name) - news_policies.erm)
for name in ("wdro", "ex_post", "ex_ante")
}
large = NEWSVENDOR_RADII >= 1e-2
direction = (
(moves["wdro"][large] < 0.0)
& (moves["ex_post"][large] > 0.0)
& (moves["ex_ante"][large] > 0.0)
)
larger = NEWSVENDOR_RADII >= 3e-2
least = (moves["ex_ante"].abs() < moves["ex_post"].abs())[larger] & (
moves["ex_post"].abs() < moves["wdro"].abs()
)[larger]
print(
"newsvendor: DRO orders less and both regret policies order more at every"
f" radius >= 1e-2: {bool(direction.all())}; ex-ante moves least and DRO"
f" most at every radius >= 3e-2: {bool(least.all())}"
)
assert bool(direction.all()) and bool(least.all())
gap = cross_check_minimax(
problems[2],
nominal,
demand,
hindsight_losses(newsvendor, demand),
news_policies,
)
print(
"newsvendor: MinimaxSolver vs bisection at radius "
f"{NEWSVENDOR_RADII[CROSS_CHECK_RADIUS].item():.1e}, "
f"max |difference| = {gap:.1e}"
)
assert gap < 2e-2
fig, (orders, decisions, values) = plt.subplots(1, 3, figsize=(14.5, 4.4))
radii = NEWSVENDOR_RADII.numpy()
orders.axhline(news_policies.erm[0].item(), color="0.5", linewidth=1.0, label="ERM")
for label, series, color, style in (
("Wasserstein DRO", news_policies.wdro, "C0", "-"),
("ex-post DRRO (cost form)", news_policies.ex_post, "C2", "--"),
("ex-ante DRRO", news_policies.ex_ante, "C3", "-"),
):
orders.step(
radii,
series.numpy(),
where="post",
color=color,
linestyle=style,
label=label,
)
orders.plot(radii, series.numpy(), "o", color=color, markersize=4)
orders.set_xscale("log")
orders.set_xlabel("Wasserstein radius $\\delta$ (squared ground cost)")
orders.set_ylabel("order quantity")
orders.set_title("newsvendor: DRO orders less, regret orders more")
orders.legend(loc="upper left", fontsize=7.5)
rate = RATE_RADII.numpy()
decisions.plot(
rate, rate, color="0.6", linewidth=1.0, linestyle=":", label="slope 1"
)
decisions.plot(
rate, rate**2, color="0.6", linewidth=1.0, linestyle="--", label="slope 2"
)
for name, color in (("quadratic", "C1"), ("LINEX", "C3")):
policies = results[name][0]
decisions.plot(
rate,
(policies.wdro - policies.erm).abs().numpy(),
"o-",
color=color,
markerfacecolor="none",
markeredgecolor=color,
label=f"{name}: WDRO",
)
if name == "LINEX":
decisions.plot(
rate,
(policies.ex_ante - policies.erm).abs().numpy(),
"s-",
color=color,
label=f"{name}: ex-ante DRRO",
)
decisions.text(
0.97,
0.03,
"quadratic ex-ante DRRO: exactly ERM\nat every radius (Theorem 4)",
transform=decisions.transAxes,
fontsize=8,
ha="right",
va="bottom",
)
decisions.set_xscale("log")
decisions.set_yscale("log")
decisions.set_xlabel("Wasserstein radius $\\delta$ (absolute ground cost)")
decisions.set_ylabel("$|\\theta_\\delta - \\theta_{\\mathrm{ERM}}|$")
decisions.set_title("the regret decision stays with ERM to first order")
decisions.legend(loc="upper left", fontsize=7.5)
values.plot(rate, rate, color="0.6", linewidth=1.0, linestyle=":", label="slope 1")
values.plot(
rate, rate**2, color="0.6", linewidth=1.0, linestyle="--", label="slope 2"
)
for name, color in (("quadratic", "C1"), ("LINEX", "C3")):
_, regret, price = results[name]
values.plot(
rate,
price.numpy(),
"o-",
color=color,
markerfacecolor="none",
markeredgecolor=color,
label=f"{name}: price of robustness",
)
values.plot(
rate, regret.numpy(), "s-", color=color, label=f"{name}: robust regret"
)
values.set_xscale("log")
values.set_yscale("log")
values.set_xlabel("Wasserstein radius $\\delta$ (absolute ground cost)")
values.set_ylabel("value")
values.set_title("worst-case loss grows like $\\delta$, regret like $\\delta^2$")
values.legend(loc="lower right", fontsize=7.5)
fig.tight_layout(w_pad=2.0)
save_figure(fig, "11_distributionally_robust_regret")
if __name__ == "__main__":
main()