Skip to content
Edit this page View source of this page

Recovering CVaR from a phi-divergence ball

Conditional value-at-risk is usually introduced as a risk measure, the average loss over the worst \(\alpha\)-fraction of outcomes. It is less often presented as a distributionally robust objective, although it is one. This tutorial takes the second view. It builds CVaR out of the general phi-divergence machinery in Optora, with no CVaR-specific code and no new solver, and then checks the result against CVaR computed directly from its definition.

The tutorial also tests a tempting substitute, a total-variation ball of radius \((1-\alpha)/\alpha\) in place of the CVaR set. This substitute gives the wrong value, and the way it fails leads to the correct relationship between CVaR and total variation.

CVaR as a worst case

Let \(p\) be a nominal distribution over \(n\) scenarios with losses \(\ell_1, \dots, \ell_n\). At tail probability \(\alpha \in (0, 1]\), CVaR is the mean of the upper \(\alpha\)-tail of the loss. It has two equivalent forms (Artzner et al. 1999; Rockafellar and Uryasev 2002),

\[ \mathrm{CVaR}_\alpha(\ell) = \sup_{q:\; q_i \,\le\, p_i/\alpha} \mathbb{E}_q[\ell] = \min_{\lambda \in \mathbb{R}} \; \lambda + \frac{1}{\alpha}\, \mathbb{E}_p\big[(\ell - \lambda)_+\big]. \]

The first form is a worst case. The adversary may reweight the scenarios, but no likelihood ratio \(q_i / p_i\) may exceed \(1/\alpha\). Its best response raises the ratio of every scenario in the upper tail to that cap and sets the rest to zero. The second form is a one-dimensional convex program whose minimizer is the value-at-risk.

The generator that turns a phi-ball into CVaR

A phi-divergence ball is \(\{q : D_\phi(q \,\|\, p) \le \rho\}\) with \(D_\phi(q \,\|\, p) = \sum_i p_i \, \phi(q_i / p_i)\). Ben-Tal et al. (2013) derived its dual,

\[ \sup_{q:\, D_\phi(q \,\|\, p) \le \rho} \mathbb{E}_q[\ell] = \inf_{\eta > 0,\, \lambda \in \mathbb{R}} \eta \rho + \lambda + \eta\, \mathbb{E}_p\!\left[ \phi^*\!\left(\frac{\ell - \lambda}{\eta}\right)\right], \]

which is the formula PhiAmbiguitySet solves. To recover CVaR, we choose the indicator generator

\[ \phi_\alpha(t) = \begin{cases} 0, & 0 \le t \le 1/\alpha, \\ +\infty, & \text{otherwise}. \end{cases} \]

The divergence \(D_{\phi_\alpha}(q \,\|\, p)\) is zero when every likelihood ratio stays below \(1/\alpha\) and infinite otherwise. The ball is therefore the CVaR set for every radius \(\rho \ge 0\). The conjugate of this generator is \(\phi_\alpha^*(s) = (s)_+ / \alpha\), which is positively homogeneous, so \(\eta\, \phi_\alpha^*((\ell - \lambda)/\eta) = (\ell - \lambda)_+/\alpha\). At \(\rho = 0\) the multiplier \(\eta\) cancels completely, and the dual becomes the Rockafellar-Uryasev formula above. The right panel of the figure at the end of this page shows this. The dual objective drawn at \(\eta = 1\) (lines) and \(\eta = 10\) (circles) is the same curve, and its minimum, marked with a diamond, lies at the CVaR level.

The conjugate is already in the library as cvar_conjugate, and the generator takes two lines. The script builds and solves the set as follows.

def cvar_generator(ratio, alpha):
    return torch.zeros_like(ratio).masked_fill(ratio > 1.0 / alpha, torch.inf)

ambiguity_set = PhiAmbiguitySet(
    nominal,
    divergence=PhiDivergence(functools.partial(cvar_generator, alpha=alpha)),
    radius=0.0,
    phi_conjugate=functools.partial(cvar_conjugate, alpha=alpha),
)
for step_size in (1e-2, 1e-3, 1e-4, 1e-5, 1e-6):
    ambiguity_set.dual_solver = GradientDescent(step_size, max_iter=1500, tol=1e-12)
    value = ambiguity_set.worst_case_expectation(loss.expand(len(alphas), -1))

Here alphas holds 25 levels between \(1\) and \(0.01\) and alpha = alphas.unsqueeze(-1), so every level is solved in one batched call. The next section explains the loop over step sizes.

A zero radius is solved, not short-circuited

Most ambiguity sets in Optora return the nominal expectation at the float radius 0.0 without solving anything. This is correct for a generator that vanishes only at \(t = 1\), because the ball of radius zero then contains only the nominal. The indicator generator vanishes on the whole interval \([0, 1/\alpha]\), so its ball of radius zero is the full CVaR set. PhiAmbiguitySet with a dual_solver therefore solves its dual at every radius, zero included. Only its exact tilt path, which needs a differentiable conjugate, keeps the shortcut.

Solving a dual that is not smooth

The Rockafellar-Uryasev objective is piecewise linear in \(\lambda\), with a kink at every scenario loss. Gradient descent with a fixed step size cannot settle exactly at a kink. It oscillates around the minimizer, and the error is proportional to the step. The script handles this in the standard way for subgradient methods. It shrinks the step in stages, from \(10^{-2}\) to \(10^{-6}\), and each stage starts from the point where the previous one stopped, because the set keeps that warm start between calls.

By weak duality, every feasible \((\eta, \lambda)\) gives a dual value at least as large as the primal optimum, so each stage also returns an upper bound on CVaR. The middle panel of the figure shows the relative gap closing from above, by about one order of magnitude per stage. After the last stage the largest relative error over all 25 levels is about \(2 \times 10^{-7}\), and the script checks that it stays below \(10^{-6}\).

Checking against the definition

The reference value is CVaR computed directly from its definition as a tail mean (Acerbi and Tasche 2002). We sort the scenarios by loss, take each one with its full nominal mass from the top until the cumulative mass reaches \(\alpha\), give the boundary scenario the remaining mass, and divide by \(\alpha\). The nominal is a uniform draw from the simplex over 400 log-normal losses. The boundary scenario is therefore split at almost every level, and an off-by-one error in the tail cannot be hidden by a round value of \(\alpha\).

The script compares three routes.

Route Code Agreement with the tail mean
Phi-divergence dual PhiAmbiguitySet + cvar_conjugate + GradientDescent about \(10^{-7}\) relative, from above
Closed form CVaRAmbiguitySet machine precision
Definition tail mean, written out in the script reference

It also reads the worst-case distribution \(q^\star\) from the closed form, as the gradient of the value with respect to the loss (Danskin's theorem). It then checks that no likelihood ratio exceeds \(1/\alpha\) and that the total-variation distance of \(q^\star\) from \(p\) is at most \(1 - \alpha\). The second bound is the subject of the next section.

The total-variation shortcut, and why it fails

The number \((1-\alpha)/\alpha\) does appear in the CVaR set. Rewriting the constraint \(q_i \le p_i/\alpha\) as

\[ \frac{q_i - p_i}{p_i} \le \frac{1 - \alpha}{\alpha} \]

shows that it bounds how far each likelihood ratio may rise above one. This is a pointwise constraint, a bound in the sup norm on \(q/p - 1\). Total variation, \(\tfrac{1}{2}\sum_i |q_i - p_i|\), limits a different quantity, namely the total (\(L^1\)) amount of mass that moves. Using one budget in place of the other gives the wrong set. For \(\alpha \le 1/2\) the budget \((1-\alpha)/\alpha\) is at least one, the total-variation ball covers the whole simplex, and its worst case is the largest loss. The script confirms this at every such level.

The correct link is an identity. A total-variation adversary with budget \(\varepsilon\) removes mass \(\varepsilon\) from the cheapest scenarios and places all of it on the single most expensive one. The mass that stays in place is the upper \((1-\varepsilon)\)-tail of \(p\), so

\[ \sup_{q:\, \mathrm{TV}(q, p) \le \varepsilon} \mathbb{E}_q[\ell] = \varepsilon \max_i \ell_i + (1 - \varepsilon)\, \mathrm{CVaR}_{1-\varepsilon}(\ell), \qquad 0 \le \varepsilon \le 1. \]

Setting \(\varepsilon = 1 - \alpha\) gives

\[ \sup_{q:\, \mathrm{TV}(q, p) \le 1-\alpha} \mathbb{E}_q[\ell] = \alpha\, \mathrm{CVaR}_\alpha(\ell) + (1 - \alpha) \max_i \ell_i . \]

The right radius is therefore \(1 - \alpha\), and even then the total-variation worst case is not CVaR but a mixture of CVaR and the maximum loss. The script verifies this identity to machine precision and then inverts it, recovering CVaR from TotalVariationAmbiguitySet as \(\big(\mathrm{TV}_{1-\alpha} - (1-\alpha)\max_i \ell_i\big) / \alpha\), with a relative error below \(10^{-13}\).

There is also a geometric reason why the radius \(1 - \alpha\) is natural. If \(q_i \le p_i / \alpha\), then wherever \(q_i \ge p_i\) we have \(q_i - p_i \le (1 - \alpha) q_i\), and summing over those scenarios gives \(\mathrm{TV}(q, p) \le 1 - \alpha\). The CVaR set always lies inside the total-variation ball of radius \(1 - \alpha\), so its worst case is never larger. By the identity above, the difference between the two worst cases is \((1 - \alpha)\big(\max_i \ell_i - \mathrm{CVaR}_\alpha(\ell)\big)\).

Watching the two sets

The first animation draws both sets on the probability simplex for three scenarios as \(\alpha\) decreases from \(1\) to \(0.1\). The filled region is the CVaR set, the solid outline is the total-variation ball of radius \(1 - \alpha\), and the dashed outline is the ball of radius \((1-\alpha)/\alpha\). The CVaR set never leaves the solid ball. The dashed ball grows much faster and covers the whole triangle once \(\alpha\) falls below about one half. From then on its worst case, the hollow diamond, stays at the vertex with the largest loss. In the right panel, the hollow circles are computed from the CVaR curve alone as \(\alpha\,\mathrm{CVaR}_\alpha + (1-\alpha)\max \ell\), and they fall exactly on the curve of the total-variation ball of radius \(1 - \alpha\).

The CVaR set inside the total-variation ball of radius one minus alpha on the probability simplex, as alpha decreases The CVaR set inside the total-variation ball of radius one minus alpha on the probability simplex, as alpha decreases
The left panel shows the CVaR set (filled) and the total-variation balls of radius $1-\alpha$ (solid) and $(1-\alpha)/\alpha$ (dashed), each with its worst case, over grey lines of equal expected loss. The right panel shows the three worst-case values over $\alpha$, and the hollow circles mark $\alpha\,\mathrm{CVaR}_\alpha + (1-\alpha)\max\ell$.

The second animation shows the same comparison as likelihood ratios on the twelve-scenario instance used across this site, for \(\alpha\) from \(1\) to \(0.05\). CVaR raises every scenario in the upper tail to the same ratio, \(1/\alpha\) (the dashed line), and sets the rest to zero. The total-variation adversary with radius \(1 - \alpha\) removes mass from the cheapest scenarios, leaves the others at ratio one, and places all the removed mass on the scenario with the largest loss. That scenario carries little nominal mass, so its ratio rises far above the CVaR cap. Both adversaries move at most \(1 - \alpha\) of mass, but they place it very differently.

Likelihood ratios of the CVaR and total-variation worst cases for each scenario as alpha decreases Likelihood ratios of the CVaR and total-variation worst cases for each scenario as alpha decreases
The likelihood ratio $q^\star_i / p_i$ of each scenario, sorted by loss, on a log scale. In the left panel CVaR caps every ratio at $1/\alpha$. In the right panel the total-variation ball of radius $1-\alpha$ puts its whole surplus on the worst scenario.

The figure

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

CVaR from three routes against the total-variation shortcut, the closing dual gap, and the dual objective at two values of eta CVaR from three routes against the total-variation shortcut, the closing dual gap, and the dual objective at two values of eta
The left panel shows that the tail mean, `CVaRAmbiguitySet` and the phi-divergence dual coincide, while both total-variation balls lie well above them. The middle panel shows the relative gap of the dual after each step-size stage. The right panel shows the dual objective in $\lambda$ for three levels at $\eta = 1$ (lines) and $\eta = 10$ (circles), and the diamond marks the minimum at the CVaR level.

What to take away

  • CVaR is the phi-divergence ball of the indicator generator \(\phi_\alpha\), and PhiAmbiguitySet with cvar_conjugate computes it with the general dual and no new solver.
  • The dual is piecewise linear, so a fixed step size only gets close. A short schedule of shrinking, warm-started steps reaches a relative error of about \(10^{-7}\), and every intermediate value is a valid upper bound.
  • A total-variation ball of radius \((1-\alpha)/\alpha\) is not the CVaR set. The radius \(1-\alpha\) is the right one, and the resulting worst case equals \(\alpha\,\mathrm{CVaR}_\alpha + (1-\alpha)\max\ell\), from which CVaR can be recovered exactly.
  • In practice, use CVaRAmbiguitySet. It computes the same value exactly in closed form, in one pass. This tutorial shows that the general machinery agrees with it.

References

  • Philippe Artzner, Freddy Delbaen, Jean-Marc Eber and David Heath, "Coherent Measures of Risk", Mathematical Finance 9(3), 1999.
  • Aharon Ben-Tal, Dick den Hertog, Anja De Waegenaere, Bertrand Melenberg and Gijs Rennen, "Robust Solutions of Optimization Problems Affected by Uncertain Probabilities", Management Science 59(2), 2013.
  • R. Tyrrell Rockafellar and Stanislav Uryasev, "Conditional Value-at-Risk for General Loss Distributions", Journal of Banking and Finance 26(7), 2002.
  • Carlo Acerbi and Dirk Tasche, "On the Coherence of Expected Shortfall", Journal of Banking and Finance 26(7), 2002.

Source

tutorials/10_cvar_from_phi_divergence.py
r"""Recovering CVaR from a phi-divergence ambiguity set.

Conditional value-at-risk at level alpha, the mean of the worst alpha-fraction
of outcomes, is itself a distributionally robust objective. It is the worst
expected loss over every distribution q whose likelihood ratio to the nominal
p never exceeds 1/alpha (Artzner et al. 1999; Rockafellar and Uryasev 2002).
That constraint is a phi-divergence ball (Ben-Tal et al. 2013) for the
indicator generator

    phi_alpha(t) = 0 for 0 <= t <= 1/alpha, and +inf otherwise,

whose convex conjugate is phi_alpha^*(s) = max(s, 0) / alpha. With that
conjugate in the general phi-DRO dual the multiplier eta cancels, which
leaves the Rockafellar-Uryasev formula

    CVaR_alpha(loss) = min over lam of  lam + E_p[(loss - lam)_+] / alpha.

No new solver is needed. This script builds the ball with the general
`PhiAmbiguitySet`, solves its dual with `GradientDescent`, and checks the
value against CVaR computed directly from its definition, the mean of the
upper alpha-tail. It checks the dedicated `CVaRAmbiguitySet` as well.

Two numerical details matter. First, the dual is piecewise linear in lam,
so a fixed step size only reaches the optimum to within a multiple of that
step. The script therefore shrinks the step in stages and lets each stage
warm-start from the last, which is a diminishing-step subgradient method.
By weak duality every stage returns an upper bound on CVaR, and the bound
closes from above. Second, the radius is zero, and the set still solves.
Most ambiguity sets return the nominal mean at radius 0.0 without solving,
which is right for a generator that vanishes only at t = 1. This generator
vanishes on all of [0, 1/alpha], so its ball of radius zero is the full
CVaR set, and for this reason `PhiAmbiguitySet` with a dual solver solves
at every radius.

The script also tests a tempting shortcut, a total-variation ball with
budget (1 - alpha)/alpha, and shows that it does not give CVaR. That
number bounds how far each likelihood ratio may rise above one, which is a
pointwise (sup-norm) constraint, while total variation limits the L1 mass
that moves. For alpha <= 1/2 the budget is at least one and the TV worst
case is the largest loss. The correct link is an identity. The TV worst
case at budget 1 - alpha equals alpha * CVaR_alpha + (1 - alpha) *
max(loss), because the TV adversary keeps the upper alpha-tail and moves
all remaining mass onto the single worst scenario. CVaR can therefore be
read off a TV ball, but only after that affine correction.

References:
    Philippe Artzner, Freddy Delbaen, Jean-Marc Eber and David Heath,
    "Coherent Measures of Risk", Mathematical Finance 9(3), 1999.
    Aharon Ben-Tal, Dick den Hertog, Anja De Waegenaere, Bertrand Melenberg
    and Gijs Rennen, "Robust Solutions of Optimization Problems Affected by
    Uncertain Probabilities", Management Science 59(2), 2013.
    R. Tyrrell Rockafellar and Stanislav Uryasev, "Conditional Value-at-Risk
    for General Loss Distributions", Journal of Banking and Finance 26(7),
    2002.
    Carlo Acerbi and Dirk Tasche, "On the Coherence of Expected Shortfall",
    Journal of Banking and Finance 26(7), 2002.

Run:
    python tutorials/10_cvar_from_phi_divergence.py
"""

import functools

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

from optora.divergences import PhiDivergence, TotalVariationDivergence
from optora.dro import (
    CVaRAmbiguitySet,
    PhiAmbiguitySet,
    TotalVariationAmbiguitySet,
    cvar_conjugate,
)
from optora.solvers import GradientDescent

SEED = 20261102
NUM_SCENARIOS = 400
ALPHAS = torch.logspace(0.0, -2.0, 25, dtype=torch.float64)
# Step sizes in the standardized units of `DualAmbiguitySet`. Each stage
# warm-starts from the previous optimum.
STEP_SCHEDULE = (1e-2, 1e-3, 1e-4, 1e-5, 1e-6)
STEPS_PER_STAGE = 1500
DUAL_TOLERANCE = 1e-6
EXACT_TOLERANCE = 1e-12
LANDSCAPE_ALPHAS = (0.5, 0.1, 0.02)
LANDSCAPE_ETAS = (1.0, 10.0)


def sample_problem() -> tuple[torch.Tensor, torch.Tensor]:
    """Return a weighted sample of heavy-tailed (log-normal) losses.

    The weights are a uniform draw from the simplex, so no scenario boundary
    lines up with a round value of alpha, and the boundary scenario is only
    partly in the tail at almost every level.
    """
    generator = torch.Generator().manual_seed(SEED)
    loss = torch.exp(
        torch.randn(NUM_SCENARIOS, generator=generator, dtype=torch.float64)
    )
    weights = -torch.log(
        torch.rand(NUM_SCENARIOS, generator=generator, dtype=torch.float64)
    )
    return weights / weights.sum(), loss


def tail_mean(
    nominal: torch.Tensor, loss: torch.Tensor, alphas: torch.Tensor
) -> torch.Tensor:
    """Compute CVaR from its definition, the mean of the upper alpha-tail.

    Scenarios are taken in decreasing order of loss with their full nominal
    mass until the cumulative mass reaches alpha, and the boundary scenario
    contributes the remainder (Acerbi and Tasche 2002).
    """
    order = torch.argsort(loss, descending=True)
    sorted_loss, sorted_mass = loss[order], nominal[order]
    mass_before = torch.cumsum(sorted_mass, dim=0) - sorted_mass
    taken = torch.clamp(
        torch.minimum(sorted_mass, alphas.unsqueeze(-1) - mass_before), min=0.0
    )
    return taken @ sorted_loss / alphas


def cvar_generator(ratio: torch.Tensor, alpha: torch.Tensor) -> torch.Tensor:
    """Return the indicator generator, zero on `[0, 1/alpha]` and `+inf` beyond."""
    return torch.zeros_like(ratio).masked_fill(ratio > 1.0 / alpha, torch.inf)


def phi_divergence_cvar(
    nominal: torch.Tensor, loss: torch.Tensor, alphas: torch.Tensor
) -> torch.Tensor:
    """Solve the CVaR phi-ball dual at every alpha, one stage per step size.

    Returns:
        The dual value after each stage, of shape `(stages, len(alphas))`.
    """
    alpha = alphas.unsqueeze(-1)
    ambiguity_set = PhiAmbiguitySet(
        nominal,
        divergence=PhiDivergence(functools.partial(cvar_generator, alpha=alpha)),
        radius=0.0,
        phi_conjugate=functools.partial(cvar_conjugate, alpha=alpha),
    )
    batched = loss.expand(alphas.shape[0], -1)
    stages = []
    for step_size in STEP_SCHEDULE:
        ambiguity_set.dual_solver = GradientDescent(
            step_size=step_size, max_iter=STEPS_PER_STAGE, tol=1e-12
        )
        stages.append(ambiguity_set.worst_case_expectation(batched).detach())
    return torch.stack(stages)


def cvar_maximizers(
    nominal: torch.Tensor, loss: torch.Tensor, alphas: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]:
    """Return CVaR from `CVaRAmbiguitySet` and its maximizers.

    By Danskin's theorem the gradient of the worst case with respect to the
    loss is the worst-case distribution itself.
    """
    batched = loss.expand(alphas.shape[0], -1).clone().requires_grad_()
    values = CVaRAmbiguitySet(nominal, alphas).worst_case_expectation(batched)
    (maximizers,) = torch.autograd.grad(values.sum(), batched)
    return values.detach(), maximizers


def total_variation_value(
    nominal: torch.Tensor, loss: torch.Tensor, radii: torch.Tensor
) -> torch.Tensor:
    """Return the total-variation worst case at every radius in `radii`."""
    ambiguity_set = TotalVariationAmbiguitySet(nominal, radius=radii)
    return ambiguity_set.worst_case_expectation(loss.expand(radii.shape[0], -1))


def dual_landscape(
    nominal: torch.Tensor,
    loss: torch.Tensor,
    lams: torch.Tensor,
    alpha: float,
    eta: float,
) -> torch.Tensor:
    """Evaluate the phi dual at radius zero over a grid of `lam` for fixed `eta`."""
    shift = (loss - lams.unsqueeze(-1)) / eta
    return lams + eta * cvar_conjugate(shift, alpha) @ nominal


def relative_gap(value: torch.Tensor, reference: torch.Tensor) -> torch.Tensor:
    """Return the elementwise relative gap `(value - reference) / reference`."""
    return (value - reference) / reference


def main() -> None:
    nominal, loss = sample_problem()
    max_loss = loss.max()
    direct = tail_mean(nominal, loss, ALPHAS)

    dual_stages = phi_divergence_cvar(nominal, loss, ALPHAS)
    dual_gaps = relative_gap(dual_stages, direct)
    closed_form, maximizers = cvar_maximizers(nominal, loss, ALPHAS)

    print("route                                   max relative gap to tail mean")
    for stage, step_size in enumerate(STEP_SCHEDULE):
        gap = dual_gaps[stage].abs().max().item()
        print(f"  PhiAmbiguitySet dual, step {step_size:.0e}       {gap:.2e}")
    closed_gap = relative_gap(closed_form, direct).abs().max().item()
    print(f"  CVaRAmbiguitySet closed form             {closed_gap:.2e}")
    assert dual_gaps.min().item() > -EXACT_TOLERANCE, "dual fell below CVaR"
    assert dual_gaps[-1].abs().max().item() < DUAL_TOLERANCE
    assert closed_gap < EXACT_TOLERANCE

    ratio_excess = (maximizers / nominal * ALPHAS.unsqueeze(-1)).amax(dim=-1) - 1.0
    tv_of_maximizer = TotalVariationDivergence()(maximizers, nominal)
    print(f"max_i q*_i / (p_i / alpha) - 1             {ratio_excess.max().item():.2e}")
    print(
        "max TV(q*, p) - (1 - alpha)               "
        f"{(tv_of_maximizer - (1.0 - ALPHAS)).max().item():.2e}"
    )
    assert ratio_excess.max().item() < EXACT_TOLERANCE
    assert (tv_of_maximizer <= 1.0 - ALPHAS + EXACT_TOLERANCE).all()

    tv_suggested = total_variation_value(nominal, loss, (1.0 - ALPHAS) / ALPHAS)
    tv_matched = total_variation_value(nominal, loss, 1.0 - ALPHAS)
    mixture = ALPHAS * direct + (1.0 - ALPHAS) * max_loss
    recovered = (tv_matched - (1.0 - ALPHAS) * max_loss) / ALPHAS
    saturated = ALPHAS <= 0.5
    print(
        "TV budget (1 - alpha)/alpha, max relative gap to CVaR       "
        f"{relative_gap(tv_suggested, direct).abs().max().item():.2e}"
    )
    print(
        "TV budget 1 - alpha vs alpha CVaR + (1 - alpha) max loss    "
        f"{relative_gap(tv_matched, mixture).abs().max().item():.2e}"
    )
    print(
        "CVaR recovered from the TV ball by the affine correction    "
        f"{relative_gap(recovered, direct).abs().max().item():.2e}"
    )
    assert torch.equal(tv_suggested[saturated], max_loss.expand(int(saturated.sum())))
    assert relative_gap(tv_matched, mixture).abs().max().item() < EXACT_TOLERANCE
    assert relative_gap(recovered, direct).abs().max().item() < 1e-10

    fig, (values_axes, gap_axes, dual_axes) = plt.subplots(
        1, 3, figsize=(14.5, 4.4), gridspec_kw={"width_ratios": [1.25, 1.0, 1.0]}
    )

    alphas = ALPHAS.numpy()
    values_axes.axhline(max_loss.item(), color="0.6", linewidth=1.0)
    values_axes.text(
        alphas[-1], max_loss.item(), "max loss ", va="bottom", ha="right", fontsize=8
    )
    values_axes.plot(
        alphas,
        tv_suggested.numpy(),
        "--",
        color="C2",
        label="TV, budget $(1-\\alpha)/\\alpha$",
    )
    values_axes.plot(
        alphas, tv_matched.numpy(), color="C2", label="TV, budget $1-\\alpha$"
    )
    values_axes.plot(
        alphas,
        direct.numpy(),
        color="C3",
        linewidth=3.0,
        label="tail mean (definition)",
    )
    values_axes.plot(
        alphas,
        closed_form.numpy(),
        ":",
        color="C0",
        linewidth=1.6,
        label="CVaRAmbiguitySet",
    )
    values_axes.plot(
        alphas,
        dual_stages[-1].numpy(),
        "o",
        color="C7",
        markerfacecolor="none",
        markeredgecolor="C7",
        markersize=7,
        label="PhiAmbiguitySet + cvar_conjugate",
    )
    values_axes.set_xscale("log")
    values_axes.invert_xaxis()
    values_axes.set_yscale("log")
    values_axes.set_xlabel("tail probability $\\alpha$ (log, decreasing)")
    values_axes.set_ylabel("worst-case expected loss")
    values_axes.set_title("three routes to CVaR, and the TV shortcut")
    values_axes.legend(loc="lower right", fontsize=7.5)

    for stage, step_size in enumerate(STEP_SCHEDULE):
        gap_axes.plot(
            alphas,
            dual_gaps[stage].clamp(min=1e-16).numpy(),
            "o-",
            color="C7",
            alpha=0.25 + 0.75 * stage / (len(STEP_SCHEDULE) - 1),
            markersize=3.5,
            markeredgewidth=0.0,
            linewidth=1.4,
            label=f"step {step_size:.0e}",
        )
    gap_axes.axhline(DUAL_TOLERANCE, color="0.6", linewidth=1.0, linestyle="--")
    gap_axes.set_xscale("log")
    gap_axes.invert_xaxis()
    gap_axes.set_yscale("log")
    gap_axes.set_xlabel("tail probability $\\alpha$ (log, decreasing)")
    gap_axes.set_ylabel("(dual value - CVaR) / CVaR")
    gap_axes.set_title("the dual bound closes from above")
    gap_axes.set_ylim(1e-17, 1.0)
    gap_axes.legend(loc="upper left", fontsize=7.5, ncol=3)

    lams = torch.linspace(
        0.0, float(torch.quantile(loss, 0.995)), 600, dtype=torch.float64
    )
    for index, alpha in enumerate(LANDSCAPE_ALPHAS):
        color = f"C{index * 2 + 1}"
        level = torch.tensor([alpha], dtype=torch.float64)
        cvar = tail_mean(nominal, loss, level).item()
        dual_axes.axhline(cvar, color=color, linewidth=0.8, alpha=0.6)
        small_eta, large_eta = (
            dual_landscape(nominal, loss, lams, alpha, eta) for eta in LANDSCAPE_ETAS
        )
        dual_axes.plot(
            lams.numpy(), small_eta.numpy(), color=color, label=f"$\\alpha$ = {alpha}"
        )
        dual_axes.plot(
            lams.numpy(),
            large_eta.numpy(),
            "o",
            color=color,
            markevery=40,
            markerfacecolor="none",
            markeredgecolor=color,
            markersize=6,
        )
        value_at_risk = lams[torch.argmin(small_eta)].item()
        dual_axes.plot([value_at_risk], [cvar], "D", color=color, markersize=7)
    dual_axes.set_xlabel("dual variable $\\lambda$")
    dual_axes.set_ylabel("dual objective")
    dual_axes.set_title(
        "$\\eta$ drops out: line $\\eta$ = 1, circles $\\eta$ = 10", fontsize=10.5
    )
    dual_axes.legend(loc="upper right", fontsize=7.5)

    fig.tight_layout(w_pad=2.0)
    save_figure(fig, "10_cvar_from_phi_divergence")


if __name__ == "__main__":
    main()