Skip to content
Edit this page View source of this page

KL-DRO radius sweep

This tutorial isolates the ambiguity set from everything else. We fix a nominal distribution and a per-scenario loss, and we follow the worst-case expectation

\[ \sup_{q \,:\, D_{\mathrm{KL}}(q \,\|\, \mathrm{nominal}) \le \rho} \mathbb{E}_q[\mathrm{loss}] \]

as the radius \(\rho\) grows. There is no decision variable to optimize, so every change in the curve comes from the ambiguity set alone.

Why it is worth plotting

Both ends of the curve are known in closed form, which makes the sweep a useful correctness check.

Radius What the ball is Worst case
\(\rho = 0\) the single distribution \(\{\mathrm{nominal}\}\) \(\sum_i \mathrm{nominal}_i \,\mathrm{loss}_i\)
\(\rho \to \infty\) eventually contains the point mass on the worst scenario \(\max_i \mathrm{loss}_i\)

The script asserts the first identity to within \(10^{-12}\). When the radius is the float 0.0, Optora skips the solve and returns the nominal expectation, so it never has to handle a dual whose minimizer diverges as \(\rho \to 0\). The second limit is reached at a finite radius. Let \(P^\star\) be the nominal mass on the highest-loss scenarios. Once \(\rho\) reaches \(-\log P^\star\), every point of the tilt path is feasible and the maximizer is the point mass itself, so the curve meets the line at the maximum loss instead of approaching it asymptotically.

Between the two ends the curve is nondecreasing, because a larger ball contains a smaller one and its supremum cannot be smaller. The script checks this on the sampled radii as well.

Reading the output

There are three things to look at.

  1. The printed comparison between \(\mathbb{E}_{\mathrm{nominal}}[\mathrm{loss}]\) and worst_case_expectation(radius=0.0), which agree to within \(10^{-12}\).
  2. The monotonically nondecreasing in radius: True line.
  3. The figure, on a log-scaled radius axis. It starts flat near the nominal expectation, rises steeply over the middle decades of the radius, and levels off at \(\max_i \mathrm{loss}_i\).

Everything is float64 here

The tolerances in the script assume double precision. The tilt is bisected to the precision of the dtype, which is \(2^{-52}\) in float64 but only \(2^{-23}\) in float32. In single precision the rounding error on the flat part of the curve can be large enough to fail a tight check even though the value is correct to single precision.

The adversary as the radius grows

The static figure further down summarizes the sweep, and the animation shows where it comes from. At every radius it draws the worst-case distribution \(q^\star\) next to the nominal. The distribution is read off the gradient of the worst-case value with respect to the loss (Danskin's theorem). The exponential tilt moves mass gradually from the cheap scenarios to the expensive one. The right panel checks that the constraint is active, since \(D_{\mathrm{KL}}(q^\star \,\|\, p)\) equals \(\rho\) until the radius reaches \(-\log P^\star\). Beyond that radius the point mass is feasible and the divergence stays constant.

The KL worst-case distribution, the worst-case value and the divergence of the adversary as the radius grows The KL worst-case distribution, the worst-case value and the divergence of the adversary as the radius grows
The left panel shows the nominal and the worst-case distribution at the current radius. The middle panel shows the worst-case value on a log-scaled radius axis, with the nominal mean, the maximum loss and the saturation radius marked. The right panel shows the divergence of the adversary, which follows the diagonal while the constraint is active and stays flat once the ball contains the point mass.

The figure

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

Worst-case expectation of the KL ambiguity set against the radius Worst-case expectation of the KL ambiguity set against the radius
The worst case starts at the nominal expectation, rises along the log-scaled radius axis, and equals the maximum loss once the radius reaches $-\log P^\star$.

Source

tutorials/01_kl_dro_radius_sweep.py
"""KL-DRO worst-case expectation across a growing ambiguity radius.

The script isolates the core DRO primitive. For a fixed nominal
distribution and a fixed per-scenario loss, it measures how the
worst-case expected loss

    sup_{q: D_KL(q || nominal) <= radius} E_q[loss]

grows with the radius of the KL ball. Two closed-form limits bound the
curve, and the script checks both numerically.

- At radius 0 the ball collapses to `{nominal}`, so the worst case is
  exactly the nominal expectation `sum(nominal * loss)`.
- As the radius grows, the ball eventually contains the point mass on
  the highest-loss scenario, so the worst case saturates at
  `loss.max()`. This happens at a finite radius. From
  `radius = -log(nominal mass of that scenario)` on, the worst case is
  `loss.max()`. `KLAmbiguitySet` reaches it because it bisects a bounded
  parameterization of the tilt whose upper end is that point mass. A
  solve of the dual would instead have to approach its infimum as
  `eta -> 0`.

Run:
    python tutorials/01_kl_dro_radius_sweep.py
"""

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

from optora.dro import KLAmbiguitySet

NOMINAL = torch.tensor([0.4, 0.3, 0.2, 0.1], dtype=torch.float64)
LOSS = torch.tensor([0.5, 1.5, 3.0, 8.0], dtype=torch.float64)


def main() -> None:
    nominal_expectation = torch.sum(NOMINAL * LOSS).item()
    exact_zero_radius = KLAmbiguitySet(nominal=NOMINAL, radius=0.0)
    zero_radius_value = exact_zero_radius.worst_case_expectation(LOSS).item()
    print(f"E_nominal[loss]                    = {nominal_expectation:.6f}")
    print(f"worst_case_expectation(radius=0.0) = {zero_radius_value:.6f}")
    assert abs(nominal_expectation - zero_radius_value) < 1e-12

    radii = torch.logspace(-3, 1, steps=25, dtype=torch.float64)
    sweep = KLAmbiguitySet(nominal=NOMINAL, radius=radii)
    worst_case = sweep.worst_case_expectation(LOSS)
    worst_possible = LOSS.max().item()
    print(f"worst case at radius={radii[0].item():.4f}  -> {worst_case[0].item():.6f}")
    print(f"worst case at radius={radii[-1].item():.4f} -> {worst_case[-1].item():.6f}")
    print(f"loss.max() (saturation limit)      = {worst_possible:.6f}")

    increments = torch.diff(worst_case)
    is_nondecreasing = bool(torch.all(increments >= -1e-9))
    print(f"monotonically nondecreasing in radius: {is_nondecreasing}")

    fig, ax = plt.subplots(figsize=(6, 4))
    ax.plot(radii, worst_case, marker="o", label="worst-case E_q[loss]")
    ax.axhline(
        nominal_expectation, color="gray", linestyle="--", label="E_nominal[loss]"
    )
    ax.axhline(worst_possible, color="firebrick", linestyle=":", label="max_i loss_i")
    ax.set_xscale("log")
    ax.set_xlabel("KL ambiguity radius")
    ax.set_ylabel("worst-case expected loss")
    ax.set_title("KL-DRO worst-case expectation vs. ambiguity radius")
    ax.legend()
    fig.tight_layout()
    save_figure(fig, "01_kl_dro_radius_sweep")


if __name__ == "__main__":
    main()