optora.dro.mmd_dro¶
Maximum-mean-discrepancy-constrained ambiguity set (MMD DRO).
The worst case over a kernel ball,
is a second-order cone program with no one-dimensional structure. The nonnegativity of \(q\) couples every scenario through \(K\), so no scalar bracket pins the maximizer as it does for the tilted sets. It is solved by a primal-dual interior-point method on the cone \(\mathbb{R}^n_+ \times \mathcal{Q}^{n+1}\) with Nesterov-Todd scaling and Mehrotra's predictor-corrector steps (Nesterov and Todd 1997; Mehrotra 1992; Vandenberghe 2010), batched over the loss and radius. In practice it converges in a few tens of iterations.
MMDAmbiguitySet
¶
Bases: AmbiguitySet
Ambiguity set of distributions within a kernel MMD of the nominal (MMD DRO).
For a nominal \(p\) on a finite support \(\{x_1, \dots, x_n\}\) and a positive semidefinite kernel with Gram matrix \(K\), the set is the ball
of the maximum mean discrepancy (Staib and Jegelka 2019; Zhu et al. 2021), restricted to distributions on the support. Unlike a \(\varphi\)-divergence ball it contains distributions that move mass onto scenarios the nominal gives none, at a cost set by the kernel's geometry.
The worst case \(\sup_{q \in \mathcal{U}} \ell^\top q\) is solved exactly,
as the second-order cone program it is, by a batched primal-dual
interior-point method with Nesterov-Todd scaling (see the module
docstring), to a relative duality gap of tol on the loss standardized
to unit spread. In float64 the value is within about tol times the
loss spread of the true worst case. In float32 the accuracy is set by
the Gram matrix instead. Factoring it in float32 costs up to about its
condition number \(\kappa(K)\) in units of float32 epsilon, relative to
the loss spread, which for nearly coincident support points or a smooth
kernel can exceed the robust excess itself. Pass nominal, gram, and
loss in float64 when \(\kappa(K)\) is large or the radius small. The
gradient still reaches a float32 loss through the cast (see
docs/numerics.md). Each iteration factors one (n, n) matrix per
batch element, and a few tens of iterations suffice for supports into
the thousands. The closed-form bound
\(\ell^\top p + \epsilon \lVert \ell \rVert_{K^{-1}}\) of Staib and
Jegelka is not used. It drops the simplex constraints
\(\mathbf{1}^\top q = 1\) and \(q \ge 0\), so it can overstate the worst
case.
The worst case is reported as \(\sum_i q^\star_i \ell_i\) with \(q^\star\)
detached, so its gradient in loss is the maximizer (Danskin's
theorem). A zero-valued term adds its Hessian, the Jacobian
\(\partial q^\star / \partial \ell\) from the implicit function theorem on
the optimality conditions over the maximizer's support, so second
derivatives in loss are exact as well. The term is built only when
loss requires grad or the call runs inside a torch.func transform.
The derivatives with respect to nominal and a tensor radius are
first order, by the envelope theorem. The radius derivative is the
multiplier
of the radius constraint, which the solve resolves to about tol
divided by the radius, relative to the loss spread.
A radius at or beyond the kernel diameter of the simplex, \(\max_{i,j} \lVert e_i - e_j \rVert_K\), admits every distribution, so the worst case is the largest loss. The solve caps the radius at twice that diameter plus one, which leaves the value unchanged and keeps the program well scaled.
A radius of zero admits only the nominal when \(K\) is positive definite,
and the worst case is then the exact sum(nominal * loss). A singular
\(K\) (duplicate support points, or any kernel that does not separate
the support) does not. A distribution that differs from the nominal
along the null space of \(K\) has zero discrepancy, so the ball of radius
zero is a face of the simplex and the worst case can exceed the
nominal expectation. Which case applies is read off the eigenvalues at
construction, as \(\lambda_{\min} > n \varepsilon \lambda_{\max}\) with
\(\varepsilon\) the machine epsilon of the working dtype. For a singular
\(K\) a zero radius is solved at a small probe radius \(\delta\), equal to
\(\sqrt{\mathrm{tol}}\) times the radius cap below, and extrapolated along
the cone-dual multiplier, \(V(0) = V(\delta) - \delta\,\lambda(\delta)\).
This is exact up to the solve tolerance whenever \(V\) is linear on
\([0, \delta]\), as it is below the first change of the maximizer's
support. The same probe solve gives the radius derivative at zero.
Attributes:
| Name | Type | Description |
|---|---|---|
nominal |
Tensor
|
Reference distribution the ambiguity set is centered on. |
divergence |
MMDDivergence
|
|
radius |
float | Tensor
|
Nonnegative bound \(\epsilon\) on the maximum mean discrepancy, a float or a tensor of radii evaluated as one batch. |
kernel_factor |
Tensor
|
Factor \(L\) with \(L L^\top = K\), from an eigenvalue decomposition with negative rounding-level eigenvalues clipped, so a singular Gram matrix (duplicate support points) is valid. |
max_iter |
Maximum number of interior-point iterations. |
|
tol |
Tolerance on the relative duality gap, floored at 100 times the machine epsilon of the working dtype. |
|
check_interval |
Iterations between host reads of the convergence flag. An iteration factors a matrix per batch element, so a read every iteration is cheap by comparison. |
gram
property
¶
Gram matrix of the kernel, aliasing divergence.gram.
__init__(nominal, gram, radius, max_iter=50, tol=1e-10, check_interval=1, validate=False)
¶
Initialize the MMD ambiguity set.
Construction factors gram once by an eigenvalue decomposition and
reads off whether it is positive definite, which synchronizes with
the device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nominal
|
Tensor
|
Nominal distribution, a nonnegative tensor that sums to one along its last dimension. |
required |
gram
|
Tensor
|
Symmetric positive semidefinite Gram matrix of shape
|
required |
radius
|
float | Tensor
|
Nonnegative bound on the maximum mean discrepancy. A
tensor radius is broadcast against the batch shape of
|
required |
max_iter
|
int
|
Maximum number of interior-point iterations. |
50
|
tol
|
float
|
Tolerance on the relative duality gap. |
1e-10
|
check_interval
|
int
|
Iterations between host reads of the convergence flag. |
1
|
validate
|
bool
|
Whether to run the value checks that synchronize with
the device on |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
worst_case_expectation(loss)
¶
Compute the worst-case expected loss over the MMD ball.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss
|
Tensor
|
Per-scenario loss values of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A tensor of shape |
Tensor
|
It is the exact |
Tensor
|
and the Gram matrix is positive definite, and otherwise the |
Tensor
|
expectation |
Tensor
|
of |
Tensor
|
zero radius of a singular Gram matrix as the class docstring |
Tensor
|
describes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A boolean tensor that is |
Tensor
|
|