Skip to content
Edit this page View source of this page

optora.dro.calibration

Ambiguity radii calibrated as confidence levels from finite-sample theory.

An ambiguity set centered on the empirical distribution \(\hat P_n\) of \(n\) i.i.d. observations is a confidence region for the data-generating distribution once its radius is a quantile of the right limit law. The robust value \(\sup_{q \in \mathcal{U}} \mathbb{E}_q[\ell]\) is then an asymptotically exact upper confidence bound on the true risk \(\mathbb{E}_{P_0}[\ell]\), and \([-\sup_{q} \mathbb{E}_q[-\ell], \sup_{q} \mathbb{E}_q[\ell]]\) an asymptotically exact confidence interval for it.

  • Phi-divergence balls (Duchi, Glynn, and Namkoong 2021; Duchi and Namkoong 2021). For a generator smooth at one, \(n\) times the profile divergence of the true risk converges to \(\frac{\varphi''(1)}{2} \chi^2_1\), so the radius is

$$ \rho_n = \frac{\varphi''(1)}{2} \cdot \frac{\chi^2_{1,\,1-\alpha}}{n}. $$

  • Wasserstein balls with quadratic cost (Blanchet, Kang, and Murthy 2019). For the cost \(c(x, y) = \lVert x - y \rVert_2^2\) on the input space, \(n\) times the robust Wasserstein profile of the true risk converges to \(\frac{\mathrm{Var}(\ell)}{\mathbb{E} \lVert \nabla_x \ell \rVert_2^2} \chi^2_1\), so the radius is the plug-in

$$ \delta_n = \frac{\widehat{\mathrm{Var}}(\ell)} {\overline{\lVert \nabla_x \ell \rVert_2^2}} \cdot \frac{\chi^2_{1,\,1-\alpha}}{n}. $$

Both radii shrink like \(1/n\), so the robust value exceeds the empirical risk by a correction of order \(\sqrt{\mathrm{Var}(\ell) / n}\) rather than by a dimension-dependent one.

A two-sided confidence \(1 - \alpha\) uses \(\chi^2_{1,\,1-\alpha} = z_{1-\alpha/2}^2\). A one-sided upper bound at level \(1 - \alpha\) uses \(z_{1-\alpha}^2\) instead, since each side of the two-sided interval fails with probability \(\alpha / 2\).

A phi-divergence ball can instead be calibrated to cover a \(d\)-dimensional parameter simultaneously, such as every component of a vector risk or the minimizer of a risk over \(d\) parameters, which replaces \(\chi^2_1\) by \(\chi^2_d\) (Duchi, Glynn, and Namkoong 2021). Every quantile is computed on the host in double precision, from the standard library's normal quantile for one degree of freedom and from series for the two \(\chi^2_d\) tails otherwise, so no quantile computation touches a device.

phi_divergence_radius(num_samples, confidence, curvature, one_sided=False, degrees_of_freedom=1)

Return the phi-divergence radius that makes the ball a confidence region.

Sets \(\rho_n = \frac{\varphi''(1)}{2} \chi^2_{d,\,1-\alpha} / n\) (Duchi, Glynn, and Namkoong 2021). With the default \(d = 1\) the ball is a confidence region for a scalar risk. With \(d > 1\) it covers a \(d\)-dimensional parameter simultaneously, at the price of a larger radius. The divergence is the one optora computes, \(D_\varphi(q \,\|\, p) = \sum_i p_i \varphi(q_i / p_i)\), so the curvature is that of its generator, \(1\) for KL (\(\varphi(t) = t \log t - t + 1\)) and \(2\) for chi-square (\(\varphi(t) = (t - 1)^2\)). Papers that normalize \(\varphi''(1) = 2\) or \(\varphi(t) = \frac{1}{2}(t - 1)^2\) state radii that differ from these by a constant factor. Total variation is not smooth at one and has no such calibration.

Parameters:

Name Type Description Default
num_samples int

Number \(n\) of i.i.d. observations the empirical nominal is built from, at least one.

required
confidence float

Target coverage \(1 - \alpha\) of the interval, or of the upper bound alone when one_sided is set.

required
curvature float

Second derivative \(\varphi''(1)\) of the generator at one, positive and finite.

required
one_sided bool

Whether confidence is the coverage of the upper confidence bound \(\sup_q \mathbb{E}_q[\ell]\) alone rather than of the two-sided interval. Only defined for one degree of freedom.

False
degrees_of_freedom int

Dimension \(d \ge 1\) of the parameter the confidence region covers.

1

Returns:

Type Description
float

The calibrated radius, a Python float.

Raises:

Type Description
ValueError

If num_samples is less than one, confidence is out of range, curvature is not positive and finite, degrees_of_freedom is not a positive integer, or one_sided is set with more than one degree of freedom.

kl_radius(num_samples, confidence, one_sided=False, degrees_of_freedom=1)

Return the calibrated radius of a KLAmbiguitySet.

The radius is \(\chi^2_{d,\,1-\alpha} / (2n)\).

Parameters:

Name Type Description Default
num_samples int

Number of i.i.d. observations the empirical nominal is built from.

required
confidence float

Target coverage of the interval, or of the upper bound alone when one_sided is set.

required
one_sided bool

Whether confidence is the coverage of the upper bound alone.

False
degrees_of_freedom int

Dimension of the parameter the confidence region covers.

1

Returns:

Type Description
float

phi_divergence_radius with the KL curvature \(\varphi''(1) = 1\).

Raises:

Type Description
ValueError

If num_samples, confidence, or degrees_of_freedom is out of range.

chi_square_radius(num_samples, confidence, one_sided=False, degrees_of_freedom=1)

Return the calibrated radius of a ChiSquareAmbiguitySet.

The radius is \(\chi^2_{d,\,1-\alpha} / n\). In the interior regime the chi-square worst case is \(\bar\ell + \sqrt{\rho\, s_n^2}\), so at \(d = 1\) this radius reproduces the normal interval \(\bar\ell \pm z_{1-\alpha/2}\, s_n / \sqrt{n}\) exactly.

Parameters:

Name Type Description Default
num_samples int

Number of i.i.d. observations the empirical nominal is built from.

required
confidence float

Target coverage of the interval, or of the upper bound alone when one_sided is set.

required
one_sided bool

Whether confidence is the coverage of the upper bound alone.

False
degrees_of_freedom int

Dimension of the parameter the confidence region covers.

1

Returns:

Type Description
float

phi_divergence_radius with the chi-square curvature

float

\(\varphi''(1) = 2\).

Raises:

Type Description
ValueError

If num_samples, confidence, or degrees_of_freedom is out of range.

wasserstein_radius(loss, input_gradient, confidence, one_sided=False)

Return the robust-Wasserstein-profile radius for the quadratic transport cost.

Sets \(\delta_n = \chi^2_{1,\,1-\alpha}\, \widehat{\mathrm{Var}}(\ell) / (n\, \overline{\lVert \nabla_x \ell \rVert_2^2})\), the plug-in quantile of the limit law of the robust Wasserstein profile of the true risk (Blanchet, Kang, and Murthy 2019), with the plug-in (biased) variance. To first order the worst case over that ball is \(\bar\ell + \sqrt{\delta_n\, \overline{\lVert \nabla_x \ell \rVert_2^2}} = \bar\ell + z_{1-\alpha/2}\, s_n / \sqrt{n}\), the same normal interval the calibrated phi-divergence balls give (Gao, Chen, and Kleywegt 2024).

The calibration is for transport over the whole input space with cost \(\lVert x - y \rVert_2^2\). A WassersteinAmbiguitySet whose support is the observations alone can only move mass among them, so its worst case under this radius is no larger, and its coverage no higher, than the calibrated one.

Parameters:

Name Type Description Default
loss Tensor

Per-observation losses of shape (..., n). Leading dimensions are independent samples, calibrated in one call.

required
input_gradient Tensor

Gradient of each observation's loss with respect to that observation's input, of shape (..., n, d).

required
confidence float

Target coverage of the interval, or of the upper bound alone when one_sided is set.

required
one_sided bool

Whether confidence is the coverage of the upper bound alone.

False

Returns:

Type Description
Tensor

The calibrated radius of shape (...), in the dtype and on the

Tensor

device of loss. It is 0 where the losses are constant and inf

Tensor

where they vary but every input gradient vanishes, since no

Tensor

transport of the inputs then moves the loss.

Raises:

Type Description
ValueError

If confidence is out of range, loss is zero-dimensional, or input_gradient's shape is not loss's followed by a feature dimension.