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
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 |
|
eps |
Small positive constant used to clamp |
__init__(phi, eps=1e-12)
¶
Initialize the phi-divergence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phi
|
Callable[[Tensor], Tensor]
|
Convex generator function with |
required |
eps
|
float
|
Small positive constant used to clamp |
1e-12
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
forward(p, q)
¶
Compute the phi-divergence of p from q.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
Tensor
|
Candidate distribution of shape |
required |
q
|
Tensor
|
Reference distribution of shape |
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 |
Tensor
|
in the promoted dtype of |
ChiSquareDivergence
¶
Bases: PhiDivergence
Chi-square divergence of a candidate distribution from a reference.
Computes
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 |
1e-12
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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 |
required |
q
|
Tensor
|
Reference distribution of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
\(D_{\chi^2}(p \,\|\, q)\) over the broadcast batch, of shape |
Tensor
|
|
Tensor
|
error near zero, computed in |
Tensor
|
and returned in the promoted dtype of |
TotalVariationDivergence
¶
Bases: PhiDivergence
Total variation divergence of a candidate distribution from a reference.
Computes
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 |
1e-12
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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 |
required |
q
|
Tensor
|
Reference distribution of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
\(D_{\mathrm{TV}}(p \,\|\, q)\) over the broadcast batch, of shape |
Tensor
|
|
Tensor
|
error near zero, computed in |
Tensor
|
and returned in the promoted dtype of |