Skip to content
Edit this page View source of this page

optora.divergences.f_divergence

f-divergences (phi-divergences) between discrete probability distributions.

PhiDivergence

Bases: Divergence

General f-divergence generated by a caller-supplied convex function.

For discrete distributions represented as nonnegative tensors that sum to one along their last dimension, computes

\[ D_\phi(p \,\|\, q) = \sum_i q_i \, \phi\!\left(\frac{p_i}{q_i}\right) \]

for a convex generator \(\phi\) with \(\phi(1) = 0\). ChiSquareDivergence and TotalVariationDivergence are subclasses with a fixed generator. Any other convex generator can be passed here directly, or fixed in a subclass, to define further phi-divergence ambiguity sets with optora.dro.PhiAmbiguitySet.

Attributes:

Name Type Description
phi

Convex generator function with phi(1) = 0, applied elementwise to the density ratio p / q.

eps

Small positive constant used to clamp q away from zero before dividing, avoiding division by zero without branching.

__init__(phi, eps=1e-12)

Initialize the phi-divergence.

Parameters:

Name Type Description Default
phi Callable[[Tensor], Tensor]

Convex generator function with phi(1) = 0, applied elementwise to the density ratio p / q.

required
eps float

Small positive constant used to clamp q away from zero before dividing.

1e-12

Raises:

Type Description
ValueError

If eps is not positive.

forward(p, q)

Compute the phi-divergence of p from q.

Parameters:

Name Type Description Default
p Tensor

Candidate distribution of shape (..., n), nonnegative and summing to one along its last dimension.

required
q Tensor

Reference distribution of shape (..., n), broadcasting against p.

required

Returns:

Type Description
Tensor

\(D_\phi(p \,\|\, q)\) over the broadcast batch, of shape (...),

Tensor

clamped to be nonnegative to absorb floating-point error near

Tensor

zero, computed in optora.core.working_dtype(p, q) and returned

Tensor

in the promoted dtype of p and q.

ChiSquareDivergence

Bases: PhiDivergence

Chi-square divergence of a candidate distribution from a reference.

Computes

\[ D_{\chi^2}(p \,\|\, q) = \sum_i \frac{(p_i - q_i)^2}{q_i}, \]

the phi-divergence generated by \(\phi(t) = (t-1)^2\). optora.dro.phi_dro uses this divergence to define chi-square ambiguity sets.

__init__(eps=1e-12)

Initialize the chi-square divergence.

Parameters:

Name Type Description Default
eps float

Small positive constant used to clamp q away from zero before dividing.

1e-12

Raises:

Type Description
ValueError

If eps is not positive.

forward(p, q)

Compute the chi-square divergence of p from q.

Uses the closed form \((p-q)^2 / q\) instead of the generic \(q \, \phi(p/q)\) of PhiDivergence. The two are algebraically identical, but the generic form divides by q and multiplies by q again after squaring, which costs an extra elementwise operation. It also forms \(p/q - 1\) from a rounded ratio, which loses relative precision when \(p_i\) is close to \(q_i\), whereas the difference \(p_i - q_i\) is computed first here.

Parameters:

Name Type Description Default
p Tensor

Candidate distribution of shape (..., n), nonnegative and summing to one along its last dimension.

required
q Tensor

Reference distribution of shape (..., n), broadcasting against p.

required

Returns:

Type Description
Tensor

\(D_{\chi^2}(p \,\|\, q)\) over the broadcast batch, of shape

Tensor

(...), clamped to be nonnegative to absorb floating-point

Tensor

error near zero, computed in optora.core.working_dtype(p, q)

Tensor

and returned in the promoted dtype of p and q.

TotalVariationDivergence

Bases: PhiDivergence

Total variation divergence of a candidate distribution from a reference.

Computes

\[ D_{\mathrm{TV}}(p \,\|\, q) = \frac{1}{2} \sum_i |p_i - q_i|, \]

the phi-divergence generated by \(\phi(t) = \frac{|t-1|}{2}\). optora.dro.phi_dro uses this divergence to define total-variation ambiguity sets.

__init__(eps=1e-12)

Initialize the total variation divergence.

Parameters:

Name Type Description Default
eps float

Small positive constant used to clamp q away from zero before dividing.

1e-12

Raises:

Type Description
ValueError

If eps is not positive.

forward(p, q)

Compute the total variation divergence of p from q.

Uses the closed form \(\frac{1}{2}\sum |p-q|\) instead of the generic \(q \, \phi(p/q)\) of PhiDivergence. For \(q > 0\) the identity \(q \, |p/q - 1| = |p-q|\) cancels the factor q against the division, so the closed form needs neither the division nor its eps clamp.

Parameters:

Name Type Description Default
p Tensor

Candidate distribution of shape (..., n), nonnegative and summing to one along its last dimension.

required
q Tensor

Reference distribution of shape (..., n), broadcasting against p.

required

Returns:

Type Description
Tensor

\(D_{\mathrm{TV}}(p \,\|\, q)\) over the broadcast batch, of shape

Tensor

(...), clamped to be nonnegative to absorb floating-point

Tensor

error near zero, computed in optora.core.working_dtype(p, q)

Tensor

and returned in the promoted dtype of p and q.