omnibias.curvature.information
Scoped parameter-curvature formulas, Fisher and Gauss–Newton constructions, natural steps, and sharpness utilities.
import omnibias.curvature.informationModule contract & mathematical conventions
Pack-parameter Fisher metric (theory 04-01).
Public leftover surface. Distinct from
omnibias.curvature.glm_fisher.fisher_information_metric.
Shared formulas do not imply universal floating-point identity. Verify dtype, shape, derivative-order support, and the stated numerical assumptions for the path you use.
DEGENERACY_ABS_FLOOR
omnibias.curvature.information.DEGENERACY_ABS_FLOORSource ↗DEGENERACY_ABS_FLOOR = 0.01Absolute Fisher floor used only by the *damped* natural step when the collapse report says the coordinate is near-degenerate. Distinct from the tighter pinv cutoff (linear algebra) and from the founding delta.
Regression example · two bias near collapse is degenerate
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_two_bias_near_collapse_is_degenerate() -> None:
report = collapse_degeneracy(two_bias_family(), np.array([1e-4]))
assert report.exponent == pytest.approx(2.0, abs=0.05)
assert report.eigenvalue < 1e-9
assert report.recommendation == "reparameterize by order"
assert report.damping == DEGENERACY_ABS_FLOORpython -m pytest packages/omnibias-curvature/tests/test_information.py -k test_two_bias_near_collapse_is_degenerate -qRead the implementation
DEGENERACY_ABS_FLOOR = 1e-2DegeneracyReport
omnibias.curvature.information.DegeneracyReportSource ↗class DegeneracyReportDataclass constructor fields; inherited fields, if any, are declared by the base class. A field(...) expression describes a default factory or field options, not a literal argument default.
Collapse-direction diagnosis for a pack-parameter metric.
damping is the extra Tikhonov used by
damped_natural_step. The pseudo-inverse
(fisher_pinv) uses the tighter PINV_RCOND_RULE.
Parameters
| Name | Type & default | Contract |
|---|---|---|
eigenvaluedataclass field | floatRequired | See the operation contract and implementation below. |
exponentdataclass field | floatRequired | See the operation contract and implementation below. |
condition_numberdataclass field | floatRequired | See the operation contract and implementation below. |
recommendationdataclass field | strRequired | See the operation contract and implementation below. |
dampingdataclass field | floatRequired | See the operation contract and implementation below. |
pathdataclass field | strDefault: PATH_CLOSED | See the operation contract and implementation below. |
directiondataclass field | strDefault: 'spread' | See the operation contract and implementation below. |
Fields & methods
eigenvaluefield
eigenvalue: floatDeclared float field on DegeneracyReport. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗exponentfield
exponent: floatDeclared float field on DegeneracyReport. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗condition_numberfield
condition_number: floatDeclared float field on DegeneracyReport. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗recommendationfield
recommendation: strDeclared str field on DegeneracyReport. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗dampingfield
damping: floatDeclared float field on DegeneracyReport. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗pathfield
path: str = PATH_CLOSEDDeclared str field on DegeneracyReport. Default expression: PATH_CLOSED.
directionfield
direction: str = 'spread'Declared str field on DegeneracyReport. Default expression: 'spread'.
Read the implementation
class DegeneracyReport:
"""Collapse-direction diagnosis for a pack-parameter metric.
``damping`` is the extra Tikhonov used by
:func:`damped_natural_step`. The pseudo-inverse
(:func:`fisher_pinv`) uses the tighter :data:`PINV_RCOND_RULE`.
"""
eigenvalue: float
exponent: float
condition_number: float
recommendation: str
damping: float
path: str = PATH_CLOSED
direction: str = "spread"FisherEvaluation
omnibias.curvature.information.FisherEvaluationSource ↗class FisherEvaluationDataclass constructor fields; inherited fields, if any, are declared by the base class. A field(...) expression describes a default factory or field options, not a literal argument default.
Metric matrix plus the path that produced the expectation.
Parameters
| Name | Type & default | Contract |
|---|---|---|
metricdataclass field | FloatArrayRequired | See the operation contract and implementation below. |
pathdataclass field | strRequired | See the operation contract and implementation below. |
Fields & methods
metricfield
metric: FloatArrayDeclared FloatArray field on FisherEvaluation. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗pathfield
path: strDeclared str field on FisherEvaluation. A value must be provided when constructing the configuration, unless an inherited constructor supplies it.
View source ↗Read the implementation
class FisherEvaluation:
"""Metric matrix plus the path that produced the expectation."""
metric: FloatArray
path: strNotADensityError
omnibias.curvature.information.NotADensityErrorSource ↗class NotADensityError(ValueError)Fisher was asked for a pack that is not a probability density.
Regression example · k ge 3 finite difference is not a density
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_k_ge_3_finite_difference_is_not_a_density() -> None:
family = finite_difference_family(n_biases=3)
with pytest.raises(NotADensityError, match="inapplicable"):
fisher_metric(family, np.array([0.2]))python -m pytest packages/omnibias-curvature/tests/test_information.py -k test_k_ge_3_finite_difference_is_not_a_density -qRegression example · non sigmoid activation is refused
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_non_sigmoid_activation_is_refused() -> None:
with pytest.raises(NotADensityError, match="logistic"):
from omnibias.curvature.information import PackFamily
PackFamily(kind="logistic_location", activation="tanh")python -m pytest packages/omnibias-curvature/tests/test_information.py -k test_non_sigmoid_activation_is_refused -qRead the implementation
class NotADensityError(ValueError):
"""Fisher was asked for a pack that is not a probability density."""PATH_CLOSED
omnibias.curvature.information.PATH_CLOSEDSource ↗PATH_CLOSED = 'closed_form_integrand'This exported constant is defined by the expression above. Its exact value and annotation come from the pinned source; use it with the module contract.
Read the implementation
PATH_CLOSED = "closed_form_integrand"PATH_MC
omnibias.curvature.information.PATH_MCSource ↗PATH_MC = 'monte_carlo'This exported constant is defined by the expression above. Its exact value and annotation come from the pinned source; use it with the module contract.
Read the implementation
PATH_MC = "monte_carlo"PINV_RCOND_RULE
omnibias.curvature.information.PINV_RCOND_RULESource ↗PINV_RCOND_RULE = 'max(n_params) * eps(dtype), matching numpy.linalg.pinv'numpy.linalg.pinv rule: max(n, m) * eps(dtype). Documented threshold.
Regression example · pinv uses numpy rcond rule
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_pinv_uses_numpy_rcond_rule() -> None:
g = np.diag([1.0, 1e-20])
rc = pinv_rcond(g)
assert rc == pytest.approx(2.0 * np.finfo(g.dtype).eps)
pin = fisher_pinv(g)
assert pin[0, 0] == pytest.approx(1.0)
assert pin[1, 1] == pytest.approx(0.0)
assert "numpy.linalg.pinv" in PINV_RCOND_RULEpython -m pytest packages/omnibias-curvature/tests/test_information.py -k test_pinv_uses_numpy_rcond_rule -qRead the implementation
PINV_RCOND_RULE = "max(n_params) * eps(dtype), matching numpy.linalg.pinv"PackFamily
omnibias.curvature.information.PackFamilySource ↗class PackFamilyDataclass constructor fields; inherited fields, if any, are declared by the base class. A field(...) expression describes a default factory or field options, not a literal argument default.
A parametric family of 1-D logistic pack densities.
Parameters
kind two_bias_logistic (theta = (delta,)), two_bias_located (theta = (mu, delta)), logistic_location (theta = (mu,), unit logistic), logistic_mixture (two components; see unpack), or finite_difference (refused for n_components >= 3).
n_components Mixture size or finite-difference bias count.
activation Must be sigmoid: only the logistic mixture is a density with a closed-form score in this leftover close-out.
Parameters
| Name | Type & default | Contract |
|---|---|---|
kinddataclass field | strRequired | two_bias_logistic (theta = (delta,)), two_bias_located (theta = (mu, delta)), logistic_location (theta = (mu,), unit logistic), logistic_mixture (two components; see unpack), or finite_difference (refused for n_components >= 3). |
n_componentsdataclass field | intDefault: 1 | Mixture size or finite-difference bias count. |
activationdataclass field | strDefault: 'sigmoid' | Must be sigmoid: only the logistic mixture is a density with a closed-form score in this leftover close-out. |
Executed regression · distinguishability wald count
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_distinguishability_wald_count() -> None:
family = logistic_location_family()
n = distinguishability_samples(family, np.array([0.0]), np.array([0.6]))
assert n == 66python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_distinguishability_wald_count" -qRegression example · non sigmoid activation is refused
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_non_sigmoid_activation_is_refused() -> None:
with pytest.raises(NotADensityError, match="logistic"):
from omnibias.curvature.information import PackFamily
PackFamily(kind="logistic_location", activation="tanh")python -m pytest packages/omnibias-curvature/tests/test_information.py -k test_non_sigmoid_activation_is_refused -qFields & methods
kindfield
kind: strtwo_bias_logistic (theta = (delta,)), two_bias_located (theta = (mu, delta)), logistic_location (theta = (mu,), unit logistic), logistic_mixture (two components; see unpack), or finite_difference (refused for n_components >= 3).
activationfield
activation: str = 'sigmoid'Must be sigmoid: only the logistic mixture is a density with a closed-form score in this leftover close-out.
__post_init__method
__post_init__(self) -> NoneSee the class contract above and the exact method implementation below.
Returns: None
Validation & failure cases
- When
self.kind not in _KINDS, raisesValueError(f'unknown PackFamily.kind {self.kind!r}'). - When
self.activation != 'sigmoid', raisesNotADensityError(f'pack Fisher is implemented for logistic/sigmoid densities only, got {self.activation!r}'). - When
self.n_components < 1, raisesValueError(f'n_components must be >= 1, got {self.n_components}'). - When
self.kind == 'logistic_mixture' and self.n_components != 2, raisesValueError(f'logistic_mixture is the two-component D8 suite; got n_components={self.n_components}').
Executed regression · distinguishability wald count
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_distinguishability_wald_count() -> None:
family = logistic_location_family()
n = distinguishability_samples(family, np.array([0.0]), np.array([0.6]))
assert n == 66python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_distinguishability_wald_count" -qMethod implementation
def __post_init__(self) -> None:
if self.kind not in _KINDS:
raise ValueError(f"unknown PackFamily.kind {self.kind!r}")
if self.activation != "sigmoid":
raise NotADensityError(
"pack Fisher is implemented for logistic/sigmoid densities "
f"only, got {self.activation!r}"
)
if self.n_components < 1:
raise ValueError(f"n_components must be >= 1, got {self.n_components}")
if self.kind == "logistic_mixture" and self.n_components != 2:
raise ValueError(
"logistic_mixture is the two-component D8 suite; "
f"got n_components={self.n_components}"
)n_paramsproperty
n_params(self) -> intSee the class contract above and the exact method implementation below.
Returns: int
Executed regression · distinguishability wald count
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_distinguishability_wald_count() -> None:
family = logistic_location_family()
n = distinguishability_samples(family, np.array([0.0]), np.array([0.6]))
assert n == 66python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_distinguishability_wald_count" -qMethod implementation
def n_params(self) -> int:
if self.kind in {"two_bias_logistic", "logistic_location", "finite_difference"}:
return 1
if self.kind == "two_bias_located":
return 2
if self.kind == "logistic_mixture":
return 5
raise ValueError(f"n_params undefined for kind {self.kind!r}")unpackmethod
unpack(self, theta: Any) -> tuple[np.ndarray, np.ndarray, np.ndarray]Return (locations, scales, weights) for mixture-like kinds.
Parameters
| Name | Type & default | Contract |
|---|---|---|
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
Returns: tuple[np.ndarray, np.ndarray, np.ndarray]
Executed regression · metric is symmetric pspd
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_metric_is_symmetric_pspd() -> None:
family = logistic_mixture_family()
for theta in randomized_mixture_suite(n=4, seed=1):
g = fisher_metric(family, theta, nodes=64)
assert np.max(np.abs(g - g.T)) <= 1e-14
eig = np.linalg.eigvalsh(0.5 * (g + g.T))
assert float(np.min(eig)) >= 1e-3python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_metric_is_symmetric_pspd" -qMethod implementation
def unpack(
self, theta: Any
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Return ``(locations, scales, weights)`` for mixture-like kinds."""
t = _theta(self, theta)
if self.kind == "logistic_location":
return np.array([t[0]]), np.array([1.0]), np.array([1.0])
if self.kind == "logistic_mixture":
mu1, phi1, mu2, phi2, psi = (float(v) for v in t)
c1 = 1.0 / (1.0 + math.exp(-psi))
return (
np.array([mu1, mu2]),
np.array([math.exp(phi1), math.exp(phi2)]),
np.array([c1, 1.0 - c1]),
)
if self.kind == "two_bias_logistic":
return np.array([0.0]), np.array([1.0]), np.array([1.0])
if self.kind == "two_bias_located":
return np.array([t[0]]), np.array([1.0]), np.array([1.0])
raise NotADensityError(_finite_difference_msg(self))Read the implementation
class PackFamily:
"""A parametric family of 1-D logistic pack densities.
Parameters
----------
kind
``two_bias_logistic`` (``theta = (delta,)``),
``two_bias_located`` (``theta = (mu, delta)``),
``logistic_location`` (``theta = (mu,)``, unit logistic),
``logistic_mixture`` (two components; see :meth:`unpack`),
or ``finite_difference`` (refused for ``n_components >= 3``).
n_components
Mixture size or finite-difference bias count.
activation
Must be ``sigmoid``: only the logistic mixture is a density
with a closed-form score in this leftover close-out.
"""
kind: str
n_components: int = 1
activation: str = "sigmoid"
def __post_init__(self) -> None:
if self.kind not in _KINDS:
raise ValueError(f"unknown PackFamily.kind {self.kind!r}")
if self.activation != "sigmoid":
raise NotADensityError(
"pack Fisher is implemented for logistic/sigmoid densities "
f"only, got {self.activation!r}"
)
if self.n_components < 1:
raise ValueError(f"n_components must be >= 1, got {self.n_components}")
if self.kind == "logistic_mixture" and self.n_components != 2:
raise ValueError(
"logistic_mixture is the two-component D8 suite; "
f"got n_components={self.n_components}"
)
@property
def n_params(self) -> int:
if self.kind in {"two_bias_logistic", "logistic_location", "finite_difference"}:
return 1
if self.kind == "two_bias_located":
return 2
if self.kind == "logistic_mixture":
return 5
raise ValueError(f"n_params undefined for kind {self.kind!r}")
def unpack(
self, theta: Any
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Return ``(locations, scales, weights)`` for mixture-like kinds."""
t = _theta(self, theta)
if self.kind == "logistic_location":
return np.array([t[0]]), np.array([1.0]), np.array([1.0])
if self.kind == "logistic_mixture":
mu1, phi1, mu2, phi2, psi = (float(v) for v in t)
c1 = 1.0 / (1.0 + math.exp(-psi))
return (
np.array([mu1, mu2]),
np.array([math.exp(phi1), math.exp(phi2)]),
np.array([c1, 1.0 - c1]),
)
if self.kind == "two_bias_logistic":
return np.array([0.0]), np.array([1.0]), np.array([1.0])
if self.kind == "two_bias_located":
return np.array([t[0]]), np.array([1.0]), np.array([1.0])
raise NotADensityError(_finite_difference_msg(self))collapse_degeneracy
omnibias.curvature.information.collapse_degeneracySource ↗collapse_degeneracy(family: PackFamily, theta: Any, *, direction: str='spread') -> DegeneracyReportEigenvalue, local exponent, and damping along the collapse chart.
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
directionkeyword-only | strDefault: 'spread' | See the operation contract and implementation below. |
DegeneracyReportExplicit validation & failure cases
- When
direction != 'spread', raisesValueError(f"only direction='spread' is implemented, got {direction!r}").
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · two bias near collapse is degenerate
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_two_bias_near_collapse_is_degenerate() -> None:
report = collapse_degeneracy(two_bias_family(), np.array([1e-4]))
assert report.exponent == pytest.approx(2.0, abs=0.05)
assert report.eigenvalue < 1e-9
assert report.recommendation == "reparameterize by order"
assert report.damping == DEGENERACY_ABS_FLOORpython -m pytest "packages/omnibias-curvature/tests/test_information.py::test_two_bias_near_collapse_is_degenerate" -qRead the implementation
def collapse_degeneracy(
family: PackFamily,
theta: Any,
*,
direction: str = "spread",
) -> DegeneracyReport:
"""Eigenvalue, local exponent, and damping along the collapse chart."""
if direction != "spread":
raise ValueError(f"only direction='spread' is implemented, got {direction!r}")
_require_density(family, theta)
g = fisher_metric(family, theta)
eig = np.linalg.eigvalsh(_sym(g))
lam_min = float(np.min(eig))
lam_max = float(np.max(np.abs(eig)))
cond = (
float("inf")
if lam_min <= 0.0
else float(lam_max / max(lam_min, 1e-300))
)
exponent = _spread_exponent(family, theta)
near = bool(lam_min < DEGENERACY_ABS_FLOOR or exponent > 1.5)
if near:
recommendation = "reparameterize by order"
damping = max(pinv_rcond(g) * max(lam_max, 1e-300), DEGENERACY_ABS_FLOOR)
else:
recommendation = "spread coordinate is identifiable"
damping = pinv_rcond(g) * max(lam_max, 1e-300)
return DegeneracyReport(
eigenvalue=lam_min,
exponent=exponent,
condition_number=cond,
recommendation=recommendation,
damping=float(damping),
path=PATH_CLOSED,
direction=direction,
)damped_natural_step
omnibias.curvature.information.damped_natural_stepSource ↗damped_natural_step(family: PackFamily, theta: Any, data: Any, *, lr: float=1.0, report: DegeneracyReport | None=None) -> FloatArrayNatural step with eigenvalues floored by DegeneracyReport.
Only the collapse direction is damped. Well-conditioned coordinates
keep their Fisher scaling. This is the opposite of adding
damping * I to every direction.
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
datapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
lrkeyword-only | floatDefault: 1.0 | See the operation contract and implementation below. |
reportkeyword-only | DegeneracyReport | NoneDefault: None | See the operation contract and implementation below. |
FloatArrayRead the implementation
def damped_natural_step(
family: PackFamily,
theta: Any,
data: Any,
*,
lr: float = 1.0,
report: DegeneracyReport | None = None,
) -> FloatArray:
"""Natural step with eigenvalues floored by :class:`DegeneracyReport`.
Only the collapse direction is damped. Well-conditioned coordinates
keep their Fisher scaling. This is the opposite of adding
``damping * I`` to every direction.
"""
t = _theta(family, theta)
g = _sym(fisher_metric(family, theta))
grad = nll_gradient(family, theta, data)
used = report if report is not None else collapse_degeneracy(family, theta)
eig, vecs = np.linalg.eigh(g)
floored = np.maximum(eig, float(used.damping))
step = vecs @ ((vecs.T @ grad) / floored)
return _project_theta(family, t - float(lr) * step)distinguishability_samples
omnibias.curvature.information.distinguishability_samplesSource ↗distinguishability_samples(family: PackFamily, theta_a: Any, theta_b: Any, *, power: float=0.8, alpha: float=0.05, steps: int=64) -> intWald sample count from the Fisher distance.
N = (z_{1-alpha/2} + z_{power})^2 / d_F^2. Default
alpha=0.05, power=0.8 gives the usual 7.849 / d_F^2.
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
theta_apositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
theta_bpositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
powerkeyword-only | floatDefault: 0.8 | See the operation contract and implementation below. |
alphakeyword-only | floatDefault: 0.05 | See the operation contract and implementation below. |
stepskeyword-only | intDefault: 64 | See the operation contract and implementation below. |
intExplicit validation & failure cases
- When
not 0.0 < float(power) < 1.0, raisesValueError(f'power must be in (0, 1), got {power}'). - When
not 0.0 < float(alpha) < 1.0, raisesValueError(f'alpha must be in (0, 1), got {alpha}'). - When
dist <= 0.0, raisesValueError('Fisher distance is zero; the settings are identical').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · distinguishability wald count
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_distinguishability_wald_count() -> None:
family = logistic_location_family()
n = distinguishability_samples(family, np.array([0.0]), np.array([0.6]))
assert n == 66python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_distinguishability_wald_count" -qRead the implementation
def distinguishability_samples(
family: PackFamily,
theta_a: Any,
theta_b: Any,
*,
power: float = 0.8,
alpha: float = 0.05,
steps: int = 64,
) -> int:
"""Wald sample count from the Fisher distance.
``N = (z_{1-alpha/2} + z_{power})^2 / d_F^2``. Default
``alpha=0.05``, ``power=0.8`` gives the usual ``7.849 / d_F^2``.
"""
if not 0.0 < float(power) < 1.0:
raise ValueError(f"power must be in (0, 1), got {power}")
if not 0.0 < float(alpha) < 1.0:
raise ValueError(f"alpha must be in (0, 1), got {alpha}")
dist = fisher_distance(family, theta_a, theta_b, steps=steps)
if dist <= 0.0:
raise ValueError("Fisher distance is zero; the settings are identical")
z_a = _normal_ppf(1.0 - float(alpha) / 2.0)
z_p = _normal_ppf(float(power))
return max(1, int(math.ceil((z_a + z_p) ** 2 / (dist * dist))))empirical_distinguishability_n
omnibias.curvature.information.empirical_distinguishability_nSource ↗empirical_distinguishability_n(family: PackFamily, theta_a: Any, theta_b: Any, *, power: float=0.8, alpha: float=0.05, n_trials: int=80, seed: int=0, n_min: int=8, n_max: int=512) -> intSmallest n whose Neyman–Pearson test reaches power.
Simple H0: theta_a vs H1: theta_b. Threshold is the
1 - alpha quantile of the log-likelihood ratio under H0.
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
theta_apositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
theta_bpositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
powerkeyword-only | floatDefault: 0.8 | See the operation contract and implementation below. |
alphakeyword-only | floatDefault: 0.05 | See the operation contract and implementation below. |
n_trialskeyword-only | intDefault: 80 | See the operation contract and implementation below. |
seedkeyword-only | intDefault: 0 | See the operation contract and implementation below. |
n_minkeyword-only | intDefault: 8 | See the operation contract and implementation below. |
n_maxkeyword-only | intDefault: 512 | See the operation contract and implementation below. |
intRead the implementation
def empirical_distinguishability_n(
family: PackFamily,
theta_a: Any,
theta_b: Any,
*,
power: float = 0.8,
alpha: float = 0.05,
n_trials: int = 80,
seed: int = 0,
n_min: int = 8,
n_max: int = 512,
) -> int:
"""Smallest ``n`` whose Neyman–Pearson test reaches ``power``.
Simple ``H0: theta_a`` vs ``H1: theta_b``. Threshold is the
``1 - alpha`` quantile of the log-likelihood ratio under ``H0``.
"""
lo, hi = int(n_min), int(n_max)
best = hi
while lo <= hi:
mid = (lo + hi) // 2
pw = _likelihood_ratio_power(
family,
theta_a,
theta_b,
n=mid,
n_trials=int(n_trials),
seed=int(seed),
alpha=float(alpha),
)
if pw >= float(power):
best = mid
hi = mid - 1
else:
lo = mid + 1
return int(best)evaluate_fisher
omnibias.curvature.information.evaluate_fisherSource ↗evaluate_fisher(family: PackFamily, theta: Any, *, closed_form: bool=True, nodes: int=96, n_mc: int=200000, seed: int=0) -> FisherEvaluationAssemble G(theta). Closed-form integrand plus quadrature by default.
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
closed_formkeyword-only | boolDefault: True | See the operation contract and implementation below. |
nodeskeyword-only | intDefault: 96 | See the operation contract and implementation below. |
n_mckeyword-only | intDefault: 200000 | See the operation contract and implementation below. |
seedkeyword-only | intDefault: 0 | See the operation contract and implementation below. |
FisherEvaluationExecuted regression · distinguishability wald count
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_distinguishability_wald_count() -> None:
family = logistic_location_family()
n = distinguishability_samples(family, np.array([0.0]), np.array([0.6]))
assert n == 66python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_distinguishability_wald_count" -qRead the implementation
def evaluate_fisher(
family: PackFamily,
theta: Any,
*,
closed_form: bool = True,
nodes: int = 96,
n_mc: int = 200_000,
seed: int = 0,
) -> FisherEvaluation:
"""Assemble ``G(theta)``. Closed-form integrand plus quadrature by default."""
_require_density(family, theta)
if closed_form:
return FisherEvaluation(
metric=_quadrature_metric(family, theta, nodes=nodes),
path=PATH_CLOSED,
)
metric, _se = _monte_carlo_metric(family, theta, n=n_mc, seed=seed)
return FisherEvaluation(metric=metric, path=PATH_MC)finite_difference_family
omnibias.curvature.information.finite_difference_familySource ↗finite_difference_family(*, n_biases: int) -> PackFamilyCentral finite-difference pack. Fisher applies only for n_biases == 2.
Parameters
| Name | Type & default | Contract |
|---|---|---|
n_biaseskeyword-only | intRequired | See the operation contract and implementation below. |
PackFamilyExecuted regression · k ge 3 finite difference is not a density
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_k_ge_3_finite_difference_is_not_a_density() -> None:
family = finite_difference_family(n_biases=3)
with pytest.raises(NotADensityError, match="inapplicable"):
fisher_metric(family, np.array([0.2]))python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_k_ge_3_finite_difference_is_not_a_density" -qRead the implementation
def finite_difference_family(*, n_biases: int) -> PackFamily:
"""Central finite-difference pack. Fisher applies only for ``n_biases == 2``."""
return PackFamily(kind="finite_difference", n_components=int(n_biases))fisher_delta_delta
omnibias.curvature.information.fisher_delta_deltaSource ↗fisher_delta_delta(delta: float, *, nodes: int=200) -> floatG_{delta,delta} for the two-bias logistic pack.
Parameters
| Name | Type & default | Contract |
|---|---|---|
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
nodeskeyword-only | intDefault: 200 | See the operation contract and implementation below. |
floatExecuted regression · two bias prefactor matches g2
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_two_bias_prefactor_matches_g2() -> None:
delta = 1e-4
g = fisher_delta_delta(delta, nodes=200)
expected = (1.0 / 720.0) * delta * delta
assert abs(g - expected) / expected <= 1e-6python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_two_bias_prefactor_matches_g2" -qRead the implementation
def fisher_delta_delta(delta: float, *, nodes: int = 200) -> float:
"""``G_{delta,delta}`` for the two-bias logistic pack."""
metric = fisher_metric(two_bias_family(), np.array([float(delta)]), nodes=nodes)
return float(metric[0, 0])fisher_distance
omnibias.curvature.information.fisher_distanceSource ↗fisher_distance(family: PackFamily, theta_a: Any, theta_b: Any, *, steps: int=64) -> floatFisher length of the chart-straight path.
Equals the geodesic in one parameter. In several parameters it is the length of the linear interpolant (an upper bound on the geodesic). The returned matrix can be used directly by downstream geometry code.
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
theta_apositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
theta_bpositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
stepskeyword-only | intDefault: 64 | See the operation contract and implementation below. |
floatExplicit validation & failure cases
- When
steps < 1, raisesValueError(f'steps must be >= 1, got {steps}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · distinguishability wald count
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_distinguishability_wald_count() -> None:
family = logistic_location_family()
n = distinguishability_samples(family, np.array([0.0]), np.array([0.6]))
assert n == 66python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_distinguishability_wald_count" -qRegression example · location distance is euclidean over sqrt i
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_location_distance_is_euclidean_over_sqrt_i() -> None:
family = logistic_location_family()
dist = fisher_distance(family, np.array([0.0]), np.array([math.sqrt(3.0)]))
assert dist == pytest.approx(1.0, rel=1e-12)python -m pytest packages/omnibias-curvature/tests/test_information.py -k test_location_distance_is_euclidean_over_sqrt_i -qRead the implementation
def fisher_distance(
family: PackFamily,
theta_a: Any,
theta_b: Any,
*,
steps: int = 64,
) -> float:
"""Fisher length of the chart-straight path.
Equals the geodesic in one parameter. In several parameters it is
the length of the linear interpolant (an upper bound on the
geodesic). The returned matrix can be used directly by downstream geometry code.
"""
if steps < 1:
raise ValueError(f"steps must be >= 1, got {steps}")
a = _theta(family, theta_a)
b = _theta(family, theta_b)
length = 0.0
delta = (b - a) / float(steps)
for i in range(steps):
mid = a + (i + 0.5) * delta
g = fisher_metric(family, mid)
quad = float(delta @ g @ delta)
length += math.sqrt(max(quad, 0.0))
return lengthfisher_metric
omnibias.curvature.information.fisher_metricSource ↗fisher_metric(family: PackFamily, theta: Any, *, closed_form: bool=True, nodes: int=96, n_mc: int=200000, seed: int=0) -> FloatArrayClosed form where the family's moments are; else Monte Carlo.
Records the path on the returned evaluation via
evaluate_fisher. This leftover uses quadrature of a
closed-form integrand (not a sampling-free moment formula).
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
closed_formkeyword-only | boolDefault: True | See the operation contract and implementation below. |
nodeskeyword-only | intDefault: 96 | See the operation contract and implementation below. |
n_mckeyword-only | intDefault: 200000 | See the operation contract and implementation below. |
seedkeyword-only | intDefault: 0 | See the operation contract and implementation below. |
FloatArrayExecuted regression · distinguishability wald count
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_distinguishability_wald_count() -> None:
family = logistic_location_family()
n = distinguishability_samples(family, np.array([0.0]), np.array([0.6]))
assert n == 66python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_distinguishability_wald_count" -qRegression example · logistic location fisher is one third
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_logistic_location_fisher_is_one_third() -> None:
g = fisher_metric(logistic_location_family(), np.array([0.3]), nodes=96)
assert float(g[0, 0]) == pytest.approx(1.0 / 3.0, rel=1e-12)python -m pytest packages/omnibias-curvature/tests/test_information.py -k test_logistic_location_fisher_is_one_third -qRead the implementation
def fisher_metric(
family: PackFamily,
theta: Any,
*,
closed_form: bool = True,
nodes: int = 96,
n_mc: int = 200_000,
seed: int = 0,
) -> FloatArray:
"""Closed form where the family's moments are; else Monte Carlo.
Records the path on the returned evaluation via
:func:`evaluate_fisher`. This leftover uses quadrature of a
closed-form integrand (not a sampling-free moment formula).
"""
return evaluate_fisher(
family,
theta,
closed_form=closed_form,
nodes=nodes,
n_mc=n_mc,
seed=seed,
).metricfisher_metric_mc
omnibias.curvature.information.fisher_metric_mcSource ↗fisher_metric_mc(family: PackFamily, theta: Any, *, n: int, seed: int) -> tuple[FloatArray, FloatArray]Monte Carlo Fisher and per-entry standard errors.
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
nkeyword-only | intRequired | See the operation contract and implementation below. |
seedkeyword-only | intRequired | See the operation contract and implementation below. |
tuple[FloatArray, FloatArray]Executed regression · mixture mc agrees with quadrature
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_mixture_mc_agrees_with_quadrature() -> None:
family = logistic_mixture_family()
theta = randomized_mixture_suite(n=1, seed=2)[0]
closed = fisher_metric(family, theta, nodes=64)
mc, se = fisher_metric_mc(family, theta, n=40_000, seed=0)
sig = np.abs(closed - mc) / np.maximum(se, 1e-18)
assert float(np.max(sig)) <= 3.0python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_mixture_mc_agrees_with_quadrature" -qRead the implementation
def fisher_metric_mc(
family: PackFamily,
theta: Any,
*,
n: int,
seed: int,
) -> tuple[FloatArray, FloatArray]:
"""Monte Carlo Fisher and per-entry standard errors."""
_require_density(family, theta)
return _monte_carlo_metric(family, theta, n=int(n), seed=int(seed))fisher_pinv
omnibias.curvature.information.fisher_pinvSource ↗fisher_pinv(metric: Any) -> FloatArraySymmetric eigen-pseudoinverse at pinv_rcond.
Parameters
| Name | Type & default | Contract |
|---|---|---|
metricpositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
FloatArrayExecuted regression · pinv uses numpy rcond rule
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_pinv_uses_numpy_rcond_rule() -> None:
g = np.diag([1.0, 1e-20])
rc = pinv_rcond(g)
assert rc == pytest.approx(2.0 * np.finfo(g.dtype).eps)
pin = fisher_pinv(g)
assert pin[0, 0] == pytest.approx(1.0)
assert pin[1, 1] == pytest.approx(0.0)
assert "numpy.linalg.pinv" in PINV_RCOND_RULEpython -m pytest "packages/omnibias-curvature/tests/test_information.py::test_pinv_uses_numpy_rcond_rule" -qRead the implementation
def fisher_pinv(metric: Any) -> FloatArray:
"""Symmetric eigen-pseudoinverse at :func:`pinv_rcond`."""
g = _sym(metric)
eig, vecs = np.linalg.eigh(g)
cutoff = pinv_rcond(g) * float(np.max(np.abs(eig)))
inv = np.where(np.abs(eig) > cutoff, 1.0 / eig, 0.0)
return np.asarray((vecs * inv) @ vecs.T, dtype=np.float64)honesty_payload
omnibias.curvature.information.honesty_payloadSource ↗honesty_payload() -> dict[str, object]The module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.
dict[str, object]Executed regression · worked example and honesty
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_and_honesty() -> None:
ex = worked_example()
assert abs(float(ex["g_over_delta2"]) - 1.0 / 720.0) / (1.0 / 720.0) < 1e-6
assert ex["recommendation"] == "reparameterize by order"
payload = honesty_payload()
assert payload["bias_collapse"] is True
assert payload["temperature_collapse"] is False
assert payload["k_ge_3_fisher"] == "inapplicable_not_a_density"
assert payload["theorem_prover_verified"] is Falsepython -m pytest "packages/omnibias-curvature/tests/test_information.py::test_worked_example_and_honesty" -qRead the implementation
def honesty_payload() -> dict[str, object]:
return {
"bias_collapse": True,
"temperature_collapse": False,
"k_ge_3_fisher": "inapplicable_not_a_density",
"scalar_glm_fisher_is_a_different_object": True,
"theorem_prover_verified": False,
"mathlib_verified": False,
"pinv_rcond_rule": PINV_RCOND_RULE,
}log_density
omnibias.curvature.information.log_densitySource ↗log_density(family: PackFamily, theta: Any, data: Any) -> FloatArrayPer-observation log p_theta(x).
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
datapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
FloatArrayRead the implementation
def log_density(family: PackFamily, theta: Any, data: Any) -> FloatArray:
"""Per-observation ``log p_theta(x)``."""
_require_density(family, theta)
xs = np.asarray(data, dtype=float).reshape(-1)
p, _jac = _density_and_jac(family, theta, xs)
return np.asarray(np.log(np.maximum(p, 1e-300)), dtype=np.float64)logistic_location_family
omnibias.curvature.information.logistic_location_familySource ↗logistic_location_family() -> PackFamilyUnit logistic location family; Fisher is exactly 1/3.
PackFamilyExecuted regression · distinguishability wald count
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_distinguishability_wald_count() -> None:
family = logistic_location_family()
n = distinguishability_samples(family, np.array([0.0]), np.array([0.6]))
assert n == 66python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_distinguishability_wald_count" -qRegression example · logistic location fisher is one third
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_logistic_location_fisher_is_one_third() -> None:
g = fisher_metric(logistic_location_family(), np.array([0.3]), nodes=96)
assert float(g[0, 0]) == pytest.approx(1.0 / 3.0, rel=1e-12)python -m pytest packages/omnibias-curvature/tests/test_information.py -k test_logistic_location_fisher_is_one_third -qRead the implementation
def logistic_location_family() -> PackFamily:
"""Unit logistic location family; Fisher is exactly ``1/3``."""
return PackFamily(kind="logistic_location", n_components=1)logistic_mixture_family
omnibias.curvature.information.logistic_mixture_familySource ↗logistic_mixture_family() -> PackFamilyTwo-component logistic mixture (the G1 / D8 suite).
PackFamilyExecuted regression · metric is symmetric pspd
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_metric_is_symmetric_pspd() -> None:
family = logistic_mixture_family()
for theta in randomized_mixture_suite(n=4, seed=1):
g = fisher_metric(family, theta, nodes=64)
assert np.max(np.abs(g - g.T)) <= 1e-14
eig = np.linalg.eigvalsh(0.5 * (g + g.T))
assert float(np.min(eig)) >= 1e-3python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_metric_is_symmetric_pspd" -qRegression example · mixture mc agrees with quadrature
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_mixture_mc_agrees_with_quadrature() -> None:
family = logistic_mixture_family()
theta = randomized_mixture_suite(n=1, seed=2)[0]
closed = fisher_metric(family, theta, nodes=64)
mc, se = fisher_metric_mc(family, theta, n=40_000, seed=0)
sig = np.abs(closed - mc) / np.maximum(se, 1e-18)
assert float(np.max(sig)) <= 3.0python -m pytest packages/omnibias-curvature/tests/test_information.py -k test_mixture_mc_agrees_with_quadrature -qRead the implementation
def logistic_mixture_family() -> PackFamily:
"""Two-component logistic mixture (the G1 / D8 suite)."""
return PackFamily(kind="logistic_mixture", n_components=2)mean_nll
omnibias.curvature.information.mean_nllSource ↗mean_nll(family: PackFamily, theta: Any, data: Any) -> floatMean negative log-density of i.i.d. observations.
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
datapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
floatRead the implementation
def mean_nll(family: PackFamily, theta: Any, data: Any) -> float:
"""Mean negative log-density of i.i.d. observations."""
_require_density(family, theta)
xs = np.asarray(data, dtype=float).reshape(-1)
p, _jac = _density_and_jac(family, theta, xs)
return float(-np.mean(np.log(np.maximum(p, 1e-300))))nll_gradient
omnibias.curvature.information.nll_gradientSource ↗nll_gradient(family: PackFamily, theta: Any, data: Any) -> FloatArrayMean -score of i.i.d. observations (NLL gradient).
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
datapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
FloatArrayRead the implementation
def nll_gradient(family: PackFamily, theta: Any, data: Any) -> FloatArray:
"""Mean ``-score`` of i.i.d. observations (NLL gradient)."""
_require_density(family, theta)
xs = np.asarray(data, dtype=float).reshape(-1)
_p, jac = _density_and_jac(family, theta, xs)
score = jac / np.maximum(_p[:, None], 1e-300)
return np.asarray(-np.mean(score, axis=0), dtype=np.float64)pinv_rcond
omnibias.curvature.information.pinv_rcondSource ↗pinv_rcond(metric: Any) -> floatDocumented pseudo-inverse cutoff: max(shape) * eps(dtype).
Parameters
| Name | Type & default | Contract |
|---|---|---|
metricpositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
floatExecuted regression · pinv uses numpy rcond rule
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_pinv_uses_numpy_rcond_rule() -> None:
g = np.diag([1.0, 1e-20])
rc = pinv_rcond(g)
assert rc == pytest.approx(2.0 * np.finfo(g.dtype).eps)
pin = fisher_pinv(g)
assert pin[0, 0] == pytest.approx(1.0)
assert pin[1, 1] == pytest.approx(0.0)
assert "numpy.linalg.pinv" in PINV_RCOND_RULEpython -m pytest "packages/omnibias-curvature/tests/test_information.py::test_pinv_uses_numpy_rcond_rule" -qRead the implementation
def pinv_rcond(metric: Any) -> float:
"""Documented pseudo-inverse cutoff: ``max(shape) * eps(dtype)``."""
arr = np.asarray(metric)
return float(max(arr.shape)) * float(np.finfo(arr.dtype).eps)randomized_mixture_suite
omnibias.curvature.information.randomized_mixture_suiteSource ↗randomized_mixture_suite(*, n: int=8, seed: int=0, min_eig: float=0.001) -> list[FloatArray]Well-separated two-component mixtures for G1 / G3.
Draws are rejected until the closed-form metric is positive definite
away from collapse (lambda_min >= min_eig).
Parameters
| Name | Type & default | Contract |
|---|---|---|
nkeyword-only | intDefault: 8 | See the operation contract and implementation below. |
seedkeyword-only | intDefault: 0 | See the operation contract and implementation below. |
min_eigkeyword-only | floatDefault: 0.001 | See the operation contract and implementation below. |
list[FloatArray]Explicit validation & failure cases
- When
attempts > 200 * int(n), raisesRuntimeError('could not draw a well-separated mixture suite').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · metric is symmetric pspd
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_metric_is_symmetric_pspd() -> None:
family = logistic_mixture_family()
for theta in randomized_mixture_suite(n=4, seed=1):
g = fisher_metric(family, theta, nodes=64)
assert np.max(np.abs(g - g.T)) <= 1e-14
eig = np.linalg.eigvalsh(0.5 * (g + g.T))
assert float(np.min(eig)) >= 1e-3python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_metric_is_symmetric_pspd" -qRegression example · mixture mc agrees with quadrature
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_mixture_mc_agrees_with_quadrature() -> None:
family = logistic_mixture_family()
theta = randomized_mixture_suite(n=1, seed=2)[0]
closed = fisher_metric(family, theta, nodes=64)
mc, se = fisher_metric_mc(family, theta, n=40_000, seed=0)
sig = np.abs(closed - mc) / np.maximum(se, 1e-18)
assert float(np.max(sig)) <= 3.0python -m pytest packages/omnibias-curvature/tests/test_information.py -k test_mixture_mc_agrees_with_quadrature -qRead the implementation
def randomized_mixture_suite(
*,
n: int = 8,
seed: int = 0,
min_eig: float = 1e-3,
) -> list[FloatArray]:
"""Well-separated two-component mixtures for G1 / G3.
Draws are rejected until the closed-form metric is positive definite
away from collapse (``lambda_min >= min_eig``).
"""
rng = np.random.default_rng(int(seed))
family = logistic_mixture_family()
out: list[FloatArray] = []
attempts = 0
while len(out) < int(n):
attempts += 1
if attempts > 200 * int(n):
raise RuntimeError("could not draw a well-separated mixture suite")
mu1 = float(rng.uniform(-2.2, -0.8))
mu2 = float(rng.uniform(0.8, 2.2))
phi1 = float(rng.uniform(-0.15, 0.35))
phi2 = float(rng.uniform(-0.15, 0.35))
psi = float(rng.uniform(-0.5, 0.5))
theta = np.array([mu1, phi1, mu2, phi2, psi], dtype=np.float64)
eig = np.linalg.eigvalsh(fisher_metric(family, theta, nodes=64))
if float(np.min(eig)) >= float(min_eig):
out.append(theta)
return outsample_family
omnibias.curvature.information.sample_familySource ↗sample_family(family: PackFamily, theta: Any, n: int, *, seed: int) -> FloatArrayExact inverse-CDF samples from a logistic pack mixture / two-bias pack.
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
npositional-or-keyword | intRequired | See the operation contract and implementation below. |
seedkeyword-only | intRequired | See the operation contract and implementation below. |
FloatArrayExecuted regression · mixture mc agrees with quadrature
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_mixture_mc_agrees_with_quadrature() -> None:
family = logistic_mixture_family()
theta = randomized_mixture_suite(n=1, seed=2)[0]
closed = fisher_metric(family, theta, nodes=64)
mc, se = fisher_metric_mc(family, theta, n=40_000, seed=0)
sig = np.abs(closed - mc) / np.maximum(se, 1e-18)
assert float(np.max(sig)) <= 3.0python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_mixture_mc_agrees_with_quadrature" -qRead the implementation
def sample_family(
family: PackFamily,
theta: Any,
n: int,
*,
seed: int,
) -> FloatArray:
"""Exact inverse-CDF samples from a logistic pack mixture / two-bias pack."""
_require_density(family, theta)
rng = np.random.default_rng(int(seed))
if family.kind in {"two_bias_logistic", "two_bias_located"}:
mu, delta = _two_bias_mu_delta(family, theta)
u1 = np.clip(rng.random(int(n)), 1e-16, 1.0 - 1e-16)
u2 = rng.random(int(n))
return np.asarray(
mu + np.log(u1 / (1.0 - u1)) + float(delta) * (u2 - 0.5),
dtype=np.float64,
)
locations, scales, weights = family.unpack(theta)
choice = rng.choice(len(weights), size=int(n), p=weights)
u = np.clip(rng.random(int(n)), 1e-16, 1.0 - 1e-16)
return np.asarray(
locations[choice] + np.log(u / (1.0 - u)) / scales[choice],
dtype=np.float64,
)two_bias_A
omnibias.curvature.information.two_bias_ASource ↗two_bias_A(delta: float) -> floatA = (delta/2) cosh(delta/2) - sinh(delta/2), series-stable.
Parameters
| Name | Type & default | Contract |
|---|---|---|
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
floatExecuted regression · two bias prefactor matches g2
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_two_bias_prefactor_matches_g2() -> None:
delta = 1e-4
g = fisher_delta_delta(delta, nodes=200)
expected = (1.0 / 720.0) * delta * delta
assert abs(g - expected) / expected <= 1e-6python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_two_bias_prefactor_matches_g2" -qRead the implementation
def two_bias_A(delta: float) -> float:
"""``A = (delta/2) cosh(delta/2) - sinh(delta/2)``, series-stable."""
h = 0.5 * float(delta)
if h < 0.5:
tot = 0.0
for k in range(1, 25):
tot += h ** (2 * k + 1) * (
1.0 / math.factorial(2 * k) - 1.0 / math.factorial(2 * k + 1)
)
return tot
return h * math.cosh(h) - math.sinh(h)two_bias_ddelta_u
omnibias.curvature.information.two_bias_ddelta_uSource ↗two_bias_ddelta_u(u: Any, delta: float) -> AnyCancellation-free d p_delta / d delta in u = exp(-x).
Parameters
| Name | Type & default | Contract |
|---|---|---|
upositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
AnyExplicit validation & failure cases
- When
d <= 0.0, raisesValueError(f'delta must be positive, got {delta}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · two bias prefactor matches g2
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_two_bias_prefactor_matches_g2() -> None:
delta = 1e-4
g = fisher_delta_delta(delta, nodes=200)
expected = (1.0 / 720.0) * delta * delta
assert abs(g - expected) / expected <= 1e-6python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_two_bias_prefactor_matches_g2" -qRead the implementation
def two_bias_ddelta_u(u: Any, delta: float) -> Any:
"""Cancellation-free ``d p_delta / d delta`` in ``u = exp(-x)``."""
d = float(delta)
if d <= 0.0:
raise ValueError(f"delta must be positive, got {delta}")
uu = np.asarray(u, dtype=float)
s = math.sinh(0.5 * d)
c = math.cosh(0.5 * d)
denom = 1.0 + 2.0 * uu * c + uu * uu
a = two_bias_A(d)
return 2.0 * uu * (denom * a - d * uu * s * s) / (d * d * denom * denom)two_bias_density_naive
omnibias.curvature.information.two_bias_density_naiveSource ↗two_bias_density_naive(x: Any, delta: float) -> AnyDefinition form (sigma(x+d/2) - sigma(x-d/2)) / d.
Parameters
| Name | Type & default | Contract |
|---|---|---|
xpositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
AnyExecuted regression · stable two bias matches definition
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_stable_two_bias_matches_definition() -> None:
xs = np.linspace(-3.0, 3.0, 81)
u = np.exp(-xs)
assert np.max(np.abs(two_bias_density_u(u, 0.4) - two_bias_density_naive(xs, 0.4))) < 1e-12python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_stable_two_bias_matches_definition" -qRead the implementation
def two_bias_density_naive(x: Any, delta: float) -> Any:
"""Definition form ``(sigma(x+d/2) - sigma(x-d/2)) / d``."""
d = float(delta)
xx = np.asarray(x, dtype=float)
sp = 1.0 / (1.0 + np.exp(-(xx + 0.5 * d)))
sm = 1.0 / (1.0 + np.exp(-(xx - 0.5 * d)))
return (sp - sm) / dtwo_bias_density_u
omnibias.curvature.information.two_bias_density_uSource ↗two_bias_density_u(u: Any, delta: float) -> AnyCancellation-free p_delta in u = exp(-x).
Parameters
| Name | Type & default | Contract |
|---|---|---|
upositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
AnyExplicit validation & failure cases
- When
d <= 0.0, raisesValueError(f'delta must be positive, got {delta}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · two bias prefactor matches g2
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_two_bias_prefactor_matches_g2() -> None:
delta = 1e-4
g = fisher_delta_delta(delta, nodes=200)
expected = (1.0 / 720.0) * delta * delta
assert abs(g - expected) / expected <= 1e-6python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_two_bias_prefactor_matches_g2" -qRegression example · stable two bias matches definition
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_stable_two_bias_matches_definition() -> None:
xs = np.linspace(-3.0, 3.0, 81)
u = np.exp(-xs)
assert np.max(np.abs(two_bias_density_u(u, 0.4) - two_bias_density_naive(xs, 0.4))) < 1e-12python -m pytest packages/omnibias-curvature/tests/test_information.py -k test_stable_two_bias_matches_definition -qRead the implementation
def two_bias_density_u(u: Any, delta: float) -> Any:
"""Cancellation-free ``p_delta`` in ``u = exp(-x)``."""
d = float(delta)
if d <= 0.0:
raise ValueError(f"delta must be positive, got {delta}")
uu = np.asarray(u, dtype=float)
s = math.sinh(0.5 * d)
c = math.cosh(0.5 * d)
denom = 1.0 + 2.0 * uu * c + uu * uu
return 2.0 * uu * s / (d * denom)two_bias_dx_u
omnibias.curvature.information.two_bias_dx_uSource ↗two_bias_dx_u(u: Any, delta: float) -> AnyCancellation-free d p_delta / d x in u = exp(-x).
Parameters
| Name | Type & default | Contract |
|---|---|---|
upositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
AnyExplicit validation & failure cases
- When
d <= 0.0, raisesValueError(f'delta must be positive, got {delta}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · located pack translation
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_located_pack_translation() -> None:
family = two_bias_located_family()
g0 = fisher_metric(family, np.array([0.0, 0.8]), nodes=96)
g1 = fisher_metric(family, np.array([1.3, 0.8]), nodes=96)
assert g0 == pytest.approx(g1, rel=1e-9, abs=1e-12)python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_located_pack_translation" -qRead the implementation
def two_bias_dx_u(u: Any, delta: float) -> Any:
"""Cancellation-free ``d p_delta / d x`` in ``u = exp(-x)``."""
d = float(delta)
if d <= 0.0:
raise ValueError(f"delta must be positive, got {delta}")
uu = np.asarray(u, dtype=float)
s = math.sinh(0.5 * d)
c = math.cosh(0.5 * d)
denom = 1.0 + 2.0 * uu * c + uu * uu
return -2.0 * s * uu * (1.0 - uu * uu) / (d * denom * denom)two_bias_family
omnibias.curvature.information.two_bias_familySource ↗two_bias_family() -> PackFamilyOne-parameter two-bias logistic pack p_delta.
PackFamilyExecuted regression · two bias prefactor matches g2
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_two_bias_prefactor_matches_g2() -> None:
delta = 1e-4
g = fisher_delta_delta(delta, nodes=200)
expected = (1.0 / 720.0) * delta * delta
assert abs(g - expected) / expected <= 1e-6python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_two_bias_prefactor_matches_g2" -qRegression example · two bias near collapse is degenerate
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_two_bias_near_collapse_is_degenerate() -> None:
report = collapse_degeneracy(two_bias_family(), np.array([1e-4]))
assert report.exponent == pytest.approx(2.0, abs=0.05)
assert report.eigenvalue < 1e-9
assert report.recommendation == "reparameterize by order"
assert report.damping == DEGENERACY_ABS_FLOORpython -m pytest packages/omnibias-curvature/tests/test_information.py -k test_two_bias_near_collapse_is_degenerate -qRead the implementation
def two_bias_family() -> PackFamily:
"""One-parameter two-bias logistic pack ``p_delta``."""
return PackFamily(kind="two_bias_logistic", n_components=2)two_bias_located_family
omnibias.curvature.information.two_bias_located_familySource ↗two_bias_located_family() -> PackFamilyLocation-plus-spread two-bias pack p(x; mu, delta).
PackFamilyExecuted regression · located pack translation
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_located_pack_translation() -> None:
family = two_bias_located_family()
g0 = fisher_metric(family, np.array([0.0, 0.8]), nodes=96)
g1 = fisher_metric(family, np.array([1.3, 0.8]), nodes=96)
assert g0 == pytest.approx(g1, rel=1e-9, abs=1e-12)python -m pytest "packages/omnibias-curvature/tests/test_information.py::test_located_pack_translation" -qRegression example · distinguishability wald count
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_distinguishability_wald_count() -> None:
family = logistic_location_family()
n = distinguishability_samples(family, np.array([0.0]), np.array([0.6]))
assert n == 66python -m pytest packages/omnibias-curvature/tests/test_information.py -k test_distinguishability_wald_count -qRead the implementation
def two_bias_located_family() -> PackFamily:
"""Location-plus-spread two-bias pack ``p(x; mu, delta)``."""
return PackFamily(kind="two_bias_located", n_components=2)undamped_natural_step
omnibias.curvature.information.undamped_natural_stepSource ↗undamped_natural_step(family: PackFamily, theta: Any, data: Any, *, lr: float=1.0) -> FloatArraytheta - lr * G^{-1} grad with the tight pinv cutoff only.
Parameters
| Name | Type & default | Contract |
|---|---|---|
familypositional-or-keyword | PackFamilyRequired | See the operation contract and implementation below. |
thetapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
datapositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
lrkeyword-only | floatDefault: 1.0 | See the operation contract and implementation below. |
FloatArrayRead the implementation
def undamped_natural_step(
family: PackFamily,
theta: Any,
data: Any,
*,
lr: float = 1.0,
) -> FloatArray:
"""``theta - lr * G^{-1} grad`` with the tight pinv cutoff only."""
g = fisher_metric(family, theta)
grad = nll_gradient(family, theta, data)
step = fisher_pinv(g) @ grad
return _project_theta(family, _theta(family, theta) - float(lr) * step)worked_example
omnibias.curvature.information.worked_exampleSource ↗worked_example() -> dict[str, object]Tiny deterministic example for docs snippets.
dict[str, object]Executed regression · worked example and honesty
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_and_honesty() -> None:
ex = worked_example()
assert abs(float(ex["g_over_delta2"]) - 1.0 / 720.0) / (1.0 / 720.0) < 1e-6
assert ex["recommendation"] == "reparameterize by order"
payload = honesty_payload()
assert payload["bias_collapse"] is True
assert payload["temperature_collapse"] is False
assert payload["k_ge_3_fisher"] == "inapplicable_not_a_density"
assert payload["theorem_prover_verified"] is Falsepython -m pytest "packages/omnibias-curvature/tests/test_information.py::test_worked_example_and_honesty" -qRead the implementation
def worked_example() -> dict[str, object]:
"""Tiny deterministic example for docs snippets."""
family = two_bias_family()
delta = 1e-4
g = fisher_delta_delta(delta, nodes=96)
report = collapse_degeneracy(family, np.array([0.1]))
loc = logistic_location_family()
dist = fisher_distance(loc, np.array([0.0]), np.array([math.sqrt(3.0)]))
return {
"g_over_delta2": g / (delta * delta),
"predicted_prefactor": 1.0 / 720.0,
"recommendation": report.recommendation,
"unit_location_distance": dist,
}