Files
OBLITERATUS/tests/test_transfer_projection_oracles.py
Joseph MaglyandClaude Opus 5.5 6aaeb813d7 fix(analysis): exact Gaussian W2 and honest labels for transfer and projection search
Finish the measurement-validity audit for the two remaining geometry
modules.

wasserstein_transfer:
- The Gaussian W2 paired sorted eigenvalues and underestimated the
  distance for non-commuting covariances (0 for a 45-degree rotation of
  diag(100, 1)). Compute the exact closed form with a stable PSD square
  root, from each side's own mean and covariance.
- Mismatched widths gave a truncated value and failures gave 0; both are
  now None with a reason. Failed map inversion no longer falls back to
  the raw cross-covariance. Fidelity is exact Pearson correlation.
- The "transport map" is a ridge least-squares map, not a Gaussian Monge
  map. Viability, fidelity and recommendations are labelled heuristics
  and are None unless their inputs exist; refusal-removal estimates that
  measured nothing are unsupported and always None.

bayesian_kernel_projection:
- Relabel as seeded random search with local perturbation over a
  partial linear projection (no surrogate or kernel). The Pareto front
  could keep a dominated trial on reduction ties; fixed. With no signal
  or no input, best results are None with a reason instead of score 0.

Both modules now carry measurement evidence and catalog rows checked by
the registry test, and the app overview no longer claims a Monge map or
TPE optimization.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 12:22:50 -05:00

304 lines
16 KiB
Python

"""Analytic oracles for Gaussian W2 transfer statistics and the projection search."""
import math
import pytest
import torch
from obliteratus.analysis import wasserstein_transfer as wt
from obliteratus.analysis import wasserstein_transfer_evidence as ev
from obliteratus.analysis.bayesian_kernel_projection import (
BayesianKernelProjection,
ProjectionConfig,
TrialResult,
)
from obliteratus.analysis.gaussian_wasserstein import gaussian_w2_squared, psd_sqrt
pytestmark = pytest.mark.cpu
F64 = torch.float64
TOL = 1e-9 # declared relative tolerance for the float64 closed form
def rotation(theta):
c, s = math.cos(theta), math.sin(theta)
return torch.tensor([[c, -s], [s, c]], dtype=F64)
def legacy_sorted_eigen_w2(cov_a, cov_b):
"""The removed approximation: pair sorted eigenvalues, ignore eigenvectors."""
a = torch.linalg.eigvalsh(cov_a).clamp_min(0).sqrt()
b = torch.linalg.eigvalsh(cov_b).clamp_min(0).sqrt()
return math.sqrt(((a - b) ** 2).sum().item())
def spd(d, seed):
g = torch.Generator().manual_seed(seed)
a = torch.randn(d, d, generator=g, dtype=F64)
return a @ a.T + 0.5 * torch.eye(d, dtype=F64)
def test_commuting_covariances_match_closed_form():
a, b = torch.tensor([4., 9., 0.25, 2.], dtype=F64), torch.tensor([1., 16., 1., 2.], dtype=F64)
m_a, m_b = torch.tensor([1., 2., 3., 4.], dtype=F64), torch.tensor([0., 2., 5., 4.], dtype=F64)
q = torch.linalg.qr(torch.randn(4, 4, generator=torch.Generator().manual_seed(1), dtype=F64))[0]
expected = 1 + 4 + ((a.sqrt() - b.sqrt()) ** 2).sum().item()
# A shared rotation keeps the covariances commuting with aligned eigenvectors.
got = gaussian_w2_squared(m_a, m_b, q @ torch.diag(a) @ q.T, q @ torch.diag(b) @ q.T)
assert got == pytest.approx(expected, rel=TOL)
@pytest.mark.parametrize("theta", [math.pi / 7, math.pi / 4, math.pi / 2])
def test_noncommuting_2d_matches_trace_determinant_closed_form(theta):
s_a = torch.diag(torch.tensor([100., 1.], dtype=F64))
s_b = rotation(theta) @ torch.diag(torch.tensor([25., 4.], dtype=F64)) @ rotation(theta).T
# For 2x2 PSD M: tr sqrt(M) = sqrt(tr M + 2 sqrt(det M)), with M = S_a^1/2 S_b S_a^1/2.
cross = math.sqrt((s_a @ s_b).trace().item() + 2 * math.sqrt(torch.det(s_a).item() * torch.det(s_b).item()))
expected = (s_a.trace() + s_b.trace()).item() - 2 * cross
zero = torch.zeros(2, dtype=F64)
assert gaussian_w2_squared(zero, zero, s_a, s_b) == pytest.approx(expected, rel=TOL)
def test_regression_sorted_eigenvalue_pairing_underestimated_w2():
# Same spectrum rotated by 45 degrees: the old pairing reported 0.
s_a = torch.diag(torch.tensor([100., 1.], dtype=F64))
s_b = rotation(math.pi / 4) @ s_a @ rotation(math.pi / 4).T
zero = torch.zeros(2, dtype=F64)
exact = math.sqrt(202 - 2 * math.sqrt(5100.5 + 200))
assert legacy_sorted_eigen_w2(s_a, s_b) == pytest.approx(0, abs=1e-12)
assert wt.WassersteinRefusalTransfer()._compute_w2_gaussian(zero, zero, s_a, s_b) == pytest.approx(exact, rel=TOL)
assert exact == pytest.approx(7.5093897, rel=1e-7)
# Commuting covariances with swapped axes were also missed by the pairing.
s_c = torch.diag(torch.tensor([1., 100.], dtype=F64))
assert legacy_sorted_eigen_w2(s_a, s_c) == 0
assert math.sqrt(gaussian_w2_squared(zero, zero, s_a, s_c)) == pytest.approx(math.sqrt(162), rel=TOL)
@pytest.mark.parametrize("d", [3, 6])
def test_noncommuting_high_dimension_matches_monge_identity(d):
# For S_b = T S_a T with T symmetric PD, (S_a^1/2 S_b S_a^1/2)^1/2 = S_a^1/2 T S_a^1/2.
s_a, t = spd(d, d), spd(d, d + 10)
s_b = t @ s_a @ t
assert (s_a @ s_b - s_b @ s_a).norm() > 1e-3
expected = (s_a.trace() + s_b.trace() - 2 * (t @ s_a).trace()).item()
zero = torch.zeros(d, dtype=F64)
assert gaussian_w2_squared(zero, zero, s_a, s_b) == pytest.approx(expected, rel=1e-8)
def test_known_value_and_psd_sqrt():
s_a = torch.tensor([[2., 1.], [1., 2.]], dtype=F64) # eigenvalues 3 and 1
got = gaussian_w2_squared(torch.zeros(2, dtype=F64), torch.tensor([3., 4.], dtype=F64), s_a, torch.eye(2, dtype=F64))
assert got == pytest.approx(25 + 4 - 2 * math.sqrt(3), rel=TOL)
root = psd_sqrt(s_a - 1e-14 * torch.eye(2, dtype=F64))
torch.testing.assert_close(root @ root, s_a, atol=1e-12, rtol=0)
assert gaussian_w2_squared(torch.zeros(2), torch.zeros(2), s_a.float(), s_a.float()) == pytest.approx(0, abs=1e-6)
@pytest.mark.parametrize("args", [
(torch.zeros(2), torch.zeros(3), torch.eye(2), torch.eye(3)),
(torch.zeros(2), torch.zeros(2), torch.eye(2), torch.ones(2, 3)),
(torch.zeros(1, 2), torch.zeros(2), torch.eye(2), torch.eye(2)),
(torch.zeros(2), torch.zeros(2), torch.eye(2), torch.full((2, 2), float("nan"))),
])
def test_gaussian_w2_refuses_mismatched_or_nonfinite_inputs(args):
with pytest.raises(ValueError):
gaussian_w2_squared(*args)
assert wt.WassersteinRefusalTransfer()._compute_w2_gaussian(*args) is None
def paired(n=12, d=3, seed=0):
g = torch.Generator().manual_seed(seed)
src = torch.randn(n, d, generator=g, dtype=F64) @ spd(d, seed + 1)
tgt = src @ spd(d, seed + 2) + 0.1 * torch.randn(n, d, generator=g, dtype=F64) + 1.0
return src, tgt
def test_transport_plan_w2_uses_full_sample_moments():
src, tgt = paired(n=14)
tgt = torch.cat([tgt, tgt[:3] + 2]) # unequal row counts: W2 uses every row
plan = wt.WassersteinRefusalTransfer()._compute_transport_plan(src, tgt, "a", "b")
expected = gaussian_w2_squared(src.mean(0), tgt.mean(0), torch.cov(src.T), torch.cov(tgt.T))
assert plan.wasserstein_distance == pytest.approx(math.sqrt(expected), rel=TOL)
assert plan.transport_cost == pytest.approx(plan.wasserstein_distance * plan.condition_number)
assert all(e.status == "diagnostic" for e in plan.measurement_evidence.values())
def test_cross_width_transfer_keeps_direction_but_no_w2_or_recommendation():
src, _ = paired(d=3)
tgt = torch.cat([src, src[:, :1] ** 2], dim=1) + 0.01 * torch.arange(12.).unsqueeze(1)
result = wt.WassersteinRefusalTransfer().compute_transfer(
{0: src}, {0: tgt}, {0: torch.tensor([1., 0., 0.], dtype=F64)})
direction = result.transferred_directions[0]
assert direction.transferred_direction.shape == (4,)
assert direction.transfer_fidelity is not None and direction.wasserstein_bound is None
assert direction.measurement_evidence["wasserstein_bound"].unavailable_reason == ev.W2_WIDTH
assert result.wasserstein_distance is None and result.estimated_excess_refusal is None
for name in ("transfer_viability", "recommendation", "needs_refinement"):
assert getattr(result, name) is None
assert result.measurement_evidence[name].unavailable_reason == ev.NEEDS_BOTH
def test_direction_width_mismatch_and_bad_evidence_are_refused():
src, tgt = paired()
transfer = wt.WassersteinRefusalTransfer()
for direction in (torch.ones(2, dtype=F64), torch.ones(4, dtype=F64), torch.ones(1, 3, dtype=F64)):
with pytest.raises(ValueError):
transfer.compute_transfer({0: src}, {0: tgt}, {0: direction})
with pytest.raises(ValueError, match="finite"):
transfer.compute_transfer({0: src}, {0: tgt * float("nan")}, {0: torch.ones(3, dtype=F64)})
with pytest.raises(ValueError, match="transport map"):
transfer._transport_direction(torch.ones(2), torch.eye(3), src, tgt)
def test_fidelity_is_exact_clamped_pearson():
src, tgt = paired(n=20)
transfer = wt.WassersteinRefusalTransfer()
s, t = torch.tensor([1., 0., 0.], dtype=F64), torch.tensor([0., 1., 0.], dtype=F64)
x, y = (src @ s).abs(), (tgt @ t).abs()
pearson = torch.corrcoef(torch.stack([x, y]))[0, 1].item()
assert transfer._measure_fidelity(t, tgt, s, src) == pytest.approx(min(1, max(0, pearson)), rel=1e-12)
assert transfer._measure_fidelity(s, src, s, src) == pytest.approx(1.0, rel=1e-12)
assert transfer._measure_fidelity(-t, -tgt, s, -src) == pytest.approx(min(1, max(0, pearson)), rel=1e-12)
def test_unavailable_fidelity_is_none_and_blocks_the_recommendation():
src, _ = paired()
constant = torch.ones(12, 3, dtype=F64)
transfer = wt.WassersteinRefusalTransfer()
assert transfer._measure_fidelity(torch.ones(3, dtype=F64), constant, torch.ones(3, dtype=F64), src) is None
assert transfer._measure_fidelity(torch.ones(3), src[:1].float(), torch.ones(3), src[:1].float()) is None
result = transfer.compute_transfer({0: src}, {0: constant}, {0: torch.ones(3, dtype=F64)})
assert result.wasserstein_distance is not None # W2 to a point mass is still defined
assert result.mean_transfer_fidelity is None and result.min_transfer_fidelity is None
assert result.transfer_viability is result.recommendation is result.needs_refinement is None
assert result.measurement_evidence["mean_transfer_fidelity"].unavailable_reason == ev.NO_FIDELITY
def test_empty_transfer_reports_no_values_and_no_recommendation():
result = wt.WassersteinRefusalTransfer().compute_transfer({0: torch.ones(3, 2)}, {0: torch.ones(3, 2)}, {})
for name, evidence in result.measurement_evidence.items():
value = getattr(result, name)
assert value is None or value in ({}, [], 0, "source", "target"), name
if value is None:
assert evidence.unavailable_reason == ev.NO_PAIRS
def test_heuristics_are_labelled_and_unsupported_estimates_are_null():
src, tgt = paired(n=30)
result = wt.WassersteinRefusalTransfer().compute_transfer({0: src, 1: src}, {0: tgt, 1: tgt},
{0: torch.ones(3, dtype=F64), 1: torch.eye(3, dtype=F64)[0]})
heuristic = {"transfer_viability", "recommendation", "needs_refinement", "estimated_excess_refusal"}
for name in heuristic:
assert result.measurement_evidence[name].definition.startswith("Heuristic"), name
assert result.measurement_evidence[name].status == "diagnostic"
assert result.recommendation.startswith("Heuristic ")
assert result.estimated_vs_native_ratio is None
assert result.measurement_evidence["estimated_vs_native_ratio"].status == "unsupported"
for direction in result.transferred_directions:
assert direction.estimated_refusal_removal is None
assert direction.measurement_evidence["estimated_refusal_removal"].status == "unsupported"
for name in ("transfer_fidelity", "wasserstein_bound"):
assert direction.measurement_evidence[name].definition.startswith("Heuristic")
fids = [d.transfer_fidelity for d in result.transferred_directions]
assert result.mean_transfer_fidelity == pytest.approx(sum(fids) / 2)
assert result.min_transfer_fidelity == min(fids)
@pytest.mark.parametrize("fidelity,w2,expected", [
(0.9, 0.1, "excellent"), (0.7, 0.5, "good"), (0.5, 5.0, "marginal"), (0.1, 0.1, "poor")])
def test_viability_rule_and_text_follow_their_inputs(monkeypatch, fidelity, w2, expected):
transfer = wt.WassersteinRefusalTransfer()
monkeypatch.setattr(transfer, "_measure_fidelity", lambda *a: fidelity)
monkeypatch.setattr(transfer, "_compute_w2_gaussian", lambda *a: w2)
src, tgt = paired()
result = transfer.compute_transfer({0: src}, {0: tgt}, {0: torch.ones(3, dtype=F64)})
assert result.transfer_viability == expected
assert result.needs_refinement == (fidelity < 0.7 or expected in ("marginal", "poor"))
assert expected in result.recommendation
assert "unavailable" in transfer._generate_recommendation("marginal", 0.5, None, True)
def test_numerical_failures_become_null_with_reasons(monkeypatch):
src, tgt = paired()
transfer = wt.WassersteinRefusalTransfer()
def fail(*args):
raise RuntimeError("forced failure")
monkeypatch.setattr(wt, "gaussian_w2_squared", fail)
monkeypatch.setattr(torch.linalg, "svdvals", fail)
plan = transfer._compute_transport_plan(src, tgt, "a", "b")
assert plan.wasserstein_distance is plan.condition_number is plan.transport_cost is plan.is_viable is None
assert plan.measurement_evidence["wasserstein_distance"].unavailable_reason == ev.W2_FAILED
assert plan.measurement_evidence["condition_number"].unavailable_reason == ev.NO_KAPPA
single = transfer._compute_transport_plan(src[:1], tgt[:1], "a", "b")
assert single.measurement_evidence["wasserstein_distance"].unavailable_reason == ev.W2_ROWS
def test_zero_transport_map_has_no_condition_number():
src, _ = paired()
plan = wt.WassersteinRefusalTransfer()._compute_transport_plan(src, torch.zeros_like(src), "a", "b")
assert plan.condition_number is None and plan.is_viable is None
# ---------------------------------------------------------------- projection search
def no_signal():
rows = [torch.tensor([1., 2., 0.]), torch.tensor([3., 0., 1.])]
return {0: rows}, {0: [r.clone() for r in rows]}, {0: torch.eye(3)[:2]}
def test_basis_without_class_mean_separation_selects_nothing():
optimizer = BayesianKernelProjection(n_trials=5)
result = optimizer.optimize(*no_signal())
assert result.best_config is result.best_score is result.best_refusal_reduction is None
assert result.n_trials == 0 and result.all_trials == []
assert result.measurement_evidence["best_config"].status == "unavailable"
assert "no configuration is selected" in result.measurement_evidence["best_config"].unavailable_reason
stats = optimizer._precompute_stats(*no_signal(), [0])
trial = optimizer._evaluate_trial(ProjectionConfig((0, 0), {0: .5}, 1, 0., False), stats, [0], 0)
assert trial.refusal_reduction is None and trial.combined_score is None
assert trial.harmless_distortion == pytest.approx(1.25 / 7.5) # one basis row at strength .5; still measured
assert trial.measurement_evidence["combined_score"].status == "unavailable"
report = BayesianKernelProjection.format_report(result)
assert "not Bayesian optimization" in report and "unavailable" in report
def test_empty_projection_search_has_no_placeholder_config():
result = BayesianKernelProjection(n_trials=3).optimize({}, {}, {})
assert result.best_config is None
assert all(e.status == "unavailable" for name, e in result.measurement_evidence.items() if name != "n_trials")
def trial(reduction, distortion, idx):
return TrialResult(ProjectionConfig((0, 0), {0: 1.}, 1, 0., False), reduction, distortion, 0., idx)
def test_pareto_front_drops_trials_dominated_at_tied_reduction():
trials = [trial(1.0, 0.5, 0), trial(1.0, 0.2, 1), trial(0.5, 0.3, 2), trial(0.4, 0.1, 3), trial(0.4, 0.1, 4)]
front = BayesianKernelProjection(n_trials=1)._pareto_front(trials)
assert [t.trial_idx for t in front] == [1, 3] # trial 0 is dominated by trial 1
def test_search_outputs_match_independent_recomputation():
harmful = {ly: [torch.tensor([3., 2., 1.]) + ly, torch.tensor([3., 2., 2.])] for ly in range(3)}
harmless = {ly: [torch.tensor([1., 1., 2.]), torch.tensor([1., 1., 3.]) - ly] for ly in range(3)}
basis = {ly: torch.eye(3)[:2] for ly in range(3)}
result = BayesianKernelProjection(n_trials=25, seed=3).optimize(harmful, harmless, basis)
trials = result.all_trials
best = min(trials, key=lambda t: t.combined_score)
assert (result.best_config, result.best_score) == (best.config, best.combined_score)
dominated = {t.trial_idx for t in trials for u in trials
if u.refusal_reduction >= t.refusal_reduction and u.harmless_distortion <= t.harmless_distortion
and (u.refusal_reduction, u.harmless_distortion) != (t.refusal_reduction, t.harmless_distortion)}
front = {(t.refusal_reduction, t.harmless_distortion) for t in result.pareto_configs}
assert front == {(t.refusal_reduction, t.harmless_distortion) for t in trials if t.trial_idx not in dominated}
top = sorted(trials, key=lambda t: t.combined_score)[:10]
for ly, value in result.layer_importance.items():
assert value == pytest.approx(sum(t.config.per_layer_weights.get(ly, 0) > .3 for t in top) / 10)
assert result.measurement_evidence["layer_importance"].definition.startswith("Heuristic")
for t in trials:
assert t.combined_score == pytest.approx(.6 * (1 - t.refusal_reduction) + .4 * t.harmless_distortion)
assert set(t.measurement_evidence) == {"config", "refusal_reduction", "harmless_distortion",
"combined_score", "trial_idx"}