Skip to content
Edit this page View source of this page

optora.dro.kl_dro

KL-divergence-constrained ambiguity set (KL-DRO).

KLAmbiguitySet

Bases: TiltedAmbiguitySet

KL-divergence-constrained ambiguity set for KL-DRO.

Bounds every candidate distribution q by \(D_{\mathrm{KL}}(q \,\|\, \mathrm{nominal}) \le \mathrm{radius}\). The KKT conditions of

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

make the maximizer an exponential (Gibbs) tilt of the nominal (Hu and Hong 2013; Ben-Tal et al. 2013),

\[ q_\beta \propto \mathrm{nominal} \cdot e^{\beta\,\mathrm{loss}}, \qquad \beta = 1/\eta \ge 0, \]

whose divergence \(D_{\mathrm{KL}}(q_\beta \,\|\, \mathrm{nominal})\) rises monotonically in \(\beta\) from \(0\) to \(-\log P^\star\), the divergence of the nominal restricted to the highest-loss scenarios, where \(P^\star\) is the nominal mass those scenarios carry. The radius constraint is therefore tight at the optimum below that threshold and slack at or above it, and either way the tilt is pinned by one monotone scalar root solve rather than by an unconstrained minimization of the convex dual

\[ \inf_{\eta > 0} \; \eta \cdot \mathrm{radius} + \eta \log \mathbb{E}_{\mathrm{nominal}}\!\left[ e^{\mathrm{loss}/\eta}\right]. \]

See optora.core.dro_base.TiltedAmbiguitySet for the bracketed bisection that runs the root solve, and docs/formulations.md for the equivalence between the two. Unlike the Wasserstein and Sinkhorn sets, this formulation needs no ground cost and no optimal-transport solve.

Attributes:

Name Type Description
nominal Tensor

Reference distribution the ambiguity set is centered on.

divergence

KLDivergence instance measuring distance from nominal.

radius float | Tensor

Nonnegative bound on the KL divergence of any distribution inside the ambiguity set from nominal, either a float or a tensor of radii evaluated as one batch.

eps

Small positive constant passed through to the underlying KLDivergence, which clamps its arguments away from zero.

log_nominal Tensor

Elementwise logarithm of nominal, a constant of the tilt cached once rather than recomputed on every call. It is exactly \(-\infty\) on scenarios with zero nominal mass, so the tilt never moves mass off the nominal's support, where the KL divergence is infinite.

__init__(nominal, radius, eps=1e-12, validate=False)

Initialize the KL-DRO ambiguity set.

Parameters:

Name Type Description Default
nominal Tensor

Reference distribution the ambiguity set is centered on, a nonnegative tensor that sums to one along its last dimension.

required
radius float | Tensor

Nonnegative bound on the KL divergence of any distribution inside the ambiguity set from nominal. A tensor radius is broadcast against the batch shape of worst_case_expectation's loss, evaluating a sweep of radii in one solve.

required
eps float

Small positive constant passed through to the underlying KLDivergence, used by contains(...). The tilt itself takes the exact logarithm of nominal and needs no clamping.

1e-12
validate bool

Whether to check that nominal is nonnegative and sums to one. The check synchronizes with the device, so it is opt-in and off by default.

False

Raises:

Type Description
ValueError

If radius is a negative float, if eps is not positive, or if validate is set and nominal is not a valid probability distribution.

contains(candidate)

Check whether a candidate distribution lies inside the ambiguity set.

The answer is returned as a boolean tensor on candidate's device rather than as a Python bool, so membership can be used as a mask or composed with further tensor work without forcing a device-to-host synchronization. Call bool(...) on the result only where a host-side branch is genuinely needed.

Parameters:

Name Type Description Default
candidate Tensor

Candidate distribution with the same shape as nominal.

required

Returns:

Type Description
Tensor

A boolean tensor that is True where the divergence of

Tensor

candidate from nominal does not exceed radius.

worst_case_expectation(loss)

Compute the worst-case expected loss at the tilt pinned by radius.

Parameters:

Name Type Description Default
loss Tensor

Per-scenario loss values of shape (..., n), one trailing entry per element of nominal's support. Leading dimensions are a batch of independent loss vectors, bisected together in one vectorized solve.

required

Returns:

Type Description
Tensor

A tensor of shape (...) holding the worst-case expected loss.

Tensor

When radius is a zero float it is the exact expectation at

Tensor

the start of the tilt path (_center_expectation), which is

Tensor

sum(nominal * loss) by default. Otherwise it is the

Tensor

expectation of loss under the feasible tilted distribution

Tensor

the search returned, which equals the largest reachable loss

Tensor

once the radius saturates the set.

Tensor

The expectation is accumulated as a correction to max(loss)

Tensor

rather than directly. Every term of

Tensor

sum(q * (loss - max(loss))) is nonpositive, so the result can

Tensor

never round to above max(loss), and a loss with a small

Tensor

spread around a large offset keeps the precision of its spread

Tensor

instead of losing it to the offset.

Raises:

Type Description
ValueError

If loss's trailing dimension does not match nominal's support size, its batch shape does not broadcast against nominal and radius, or validate is set and loss is not finite.