mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +01:00
Rank-k extraction stores a basis, but the gate validates only its
primary direction, so an informative first vector plus unrelated
dimensions could pass as a whole subspace.
Add a standalone, versioned subspace report (not wired into the gate):
spectrum and conditioning, PCA-threshold and entropy effective rank,
bootstrap principal-angle stability, held-out multivariate prediction
per rank, rank-matched random-subspace nulls, and joint/interaction
diagnostics for direction pairs. The verdict reports the supported rank
("supported_rank: s of k"), counting only consecutive ranks whose
held-out increment beats both their null and a minimum increment. The
report compares the primary-vector, PCA-threshold, entropy-rank and
prediction arms side by side.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
251 lines
11 KiB
Python
251 lines
11 KiB
Python
"""Synthetic CPU outcome tests for the full-subspace report (issue #436)."""
|
|
|
|
import json
|
|
import math
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from obliteratus.analysis import subspace_stats as st
|
|
from obliteratus.analysis.subspace_report import SCHEMA, SCHEMA_VERSION, build_subspace_report
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
F64 = torch.float64
|
|
|
|
|
|
def _gen(seed=1):
|
|
return torch.Generator().manual_seed(seed)
|
|
|
|
|
|
def _noise(g, n, d, scale=1.0):
|
|
return torch.randn(n, d, generator=g, dtype=F64) * scale
|
|
|
|
|
|
def _axes(d, k):
|
|
return torch.eye(d, dtype=F64)[:k]
|
|
|
|
|
|
def _tetrahedron(g):
|
|
"""Four classes at regular-simplex vertices: a known rank-3 signal plus noise."""
|
|
vertices = torch.tensor([[1, 1, 1], [1, -1, -1], [-1, 1, -1], [-1, -1, 1]], dtype=F64) * 1.5
|
|
labels = torch.arange(4).repeat_interleave(60)
|
|
x = _noise(g, 240, 10)
|
|
x[:, :3] += vertices[labels]
|
|
return x, labels
|
|
|
|
|
|
def _one_informative(g):
|
|
"""Four ordered classes along axis 0 only; axes 1 and 2 are unrelated."""
|
|
labels = torch.arange(4).repeat_interleave(60)
|
|
x = _noise(g, 240, 10)
|
|
x[:, 0] += torch.tensor([-4.5, -1.5, 1.5, 4.5], dtype=F64)[labels]
|
|
return x, labels
|
|
|
|
|
|
def _json_roundtrip(report):
|
|
assert json.loads(json.dumps(report, allow_nan=False)) == report
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def tetra():
|
|
x, labels = _tetrahedron(_gen())
|
|
return x, labels, build_subspace_report(x, labels, _axes(10, 3))
|
|
|
|
|
|
def test_known_rank_k_signal_is_supported_at_rank_k_with_positive_increments(tetra):
|
|
_, _, report = tetra
|
|
_json_roundtrip(report)
|
|
assert report["schema"] == SCHEMA and report["schema_version"] == SCHEMA_VERSION
|
|
verdict = report["verdict"]
|
|
assert verdict["status"] == "pass" and verdict["supported_rank"] == 3
|
|
assert verdict["summary"] == "supported_rank: 3 of 3"
|
|
increments = [row["increment"] for row in report["per_rank"]]
|
|
assert len(increments) == 3 and all(value > 0.05 for value in increments)
|
|
assert all(row["passes"] for row in report["per_rank"])
|
|
assert report["subspace_null"]["observed_quantile"] == 1.0
|
|
assert len(report["subspace_null"]["scores"]) == report["parameters"]["n_null"] == 50
|
|
assert report["measurement_evidence"]["verdict"]["evidence"] == "synthetic-tested"
|
|
assert report["measurement_evidence"]["entropy_effective_rank"]["evidence"] == "descriptive"
|
|
assert report["stability"]["refit_rank"] == 3
|
|
assert max(report["stability"]["refit_vs_supplied_angles_deg"]) < 30
|
|
|
|
|
|
def test_rotation_within_identical_span_preserves_span_level_conclusions(tetra):
|
|
x, labels, report = tetra
|
|
rotation, _ = torch.linalg.qr(_noise(_gen(7), 3, 3))
|
|
rotated = build_subspace_report(
|
|
x, labels, rotation @ _axes(10, 3), reference_basis=_axes(10, 3),
|
|
)
|
|
assert max(rotated["reference_principal_angles_deg"]) < 1e-5
|
|
assert rotated["verdict"] == report["verdict"]
|
|
for a, b in zip(rotated["per_rank"], report["per_rank"]):
|
|
assert a["heldout_score"] == pytest.approx(b["heldout_score"], abs=1e-9)
|
|
assert rotated["spectrum"] == pytest.approx(report["spectrum"])
|
|
assert rotated["stability"]["refit_vs_supplied_angles_deg"] == pytest.approx(
|
|
report["stability"]["refit_vs_supplied_angles_deg"], abs=1e-6)
|
|
|
|
|
|
def test_one_informative_direction_plus_unrelated_rows_is_not_a_whole_subspace_pass():
|
|
x, labels = _one_informative(_gen())
|
|
report = build_subspace_report(x, labels, _axes(10, 3))
|
|
assert report["max_linear_rank"] == 3 # the cap is not what limits support
|
|
assert report["verdict"]["status"] == "partial"
|
|
assert report["verdict"]["supported_rank"] == 1
|
|
assert report["verdict"]["summary"] == "supported_rank: 1 of 3"
|
|
first, *rest = report["per_rank"]
|
|
assert first["increment"] > 0.5 and first["passes"]
|
|
assert all(abs(row["increment"]) < 0.02 and not row["passes"] for row in rest)
|
|
# The gate-equivalent primary arm looks strong on its own.
|
|
assert report["arms"]["primary_vector"]["heldout_score"] > 0.8
|
|
|
|
|
|
def test_binary_gate_scenario_reports_primary_auc_and_rank_cap():
|
|
g = _gen(3)
|
|
labels = torch.arange(300) % 2
|
|
x = _noise(g, 300, 10)
|
|
x[:, 0] += 2.0 * labels
|
|
report = build_subspace_report(x, labels, _axes(10, 3), basis_fit_on_evaluation_data=False)
|
|
primary = report["arms"]["primary_vector"]
|
|
assert primary["in_sample_auc"] > 0.85 and primary["heldout_auc"] > 0.85
|
|
assert report["verdict"]["supported_rank"] == 1 and report["verdict"]["status"] == "partial"
|
|
assert any(q.startswith("label_rank_cap") for q in report["verdict"]["qualifiers"])
|
|
assert "basis_fit_data_not_declared_disjoint" not in report["verdict"]["qualifiers"]
|
|
|
|
|
|
def test_low_variance_signal_direction_is_kept_by_prediction_not_by_explained_variance():
|
|
g = _gen(4)
|
|
labels = torch.arange(4).repeat_interleave(60)
|
|
x = _noise(g, 240, 8)
|
|
x[:, 0] = _noise(g, 240, 1, 5.0)[:, 0] + 6.0 * (2 * (labels % 2) - 1)
|
|
x[:, 1] = _noise(g, 240, 1, 0.1)[:, 0] + 0.3 * (2 * (labels // 2) - 1)
|
|
report = build_subspace_report(x, labels, _axes(8, 2))
|
|
assert report["explained_variance_ratio"][-1] < 0.01
|
|
assert report["pca_threshold_rank"] == 1
|
|
assert report["verdict"]["supported_rank"] == 2
|
|
full = report["per_rank"][-1]["heldout_score"]
|
|
assert report["arms"]["pca_threshold"]["heldout_score"] < full - 0.3
|
|
assert report["per_rank"][1]["increment"] > 0.2
|
|
|
|
|
|
def test_circular_feature_is_reported_as_one_joint_two_dimensional_unit():
|
|
g = _gen(5)
|
|
labels = torch.arange(12).repeat_interleave(30) # twelve "months" on a circle
|
|
theta = labels.double() * (2 * math.pi / 12)
|
|
x = _noise(g, 360, 6, 0.25)
|
|
x[:, 0] += 3 * theta.cos()
|
|
x[:, 1] += 3 * theta.sin()
|
|
report = build_subspace_report(x, labels, _axes(6, 2))
|
|
(pair,) = report["pairs"]
|
|
assert pair["joint"] and pair["pair_score"] > 0.85
|
|
assert pair["pair_score"] - max(pair["single_scores"]) > 0.3
|
|
assert report["units"] == [[0, 1]]
|
|
assert report["verdict"]["supported_rank"] == 2
|
|
|
|
|
|
def test_orthogonal_interacting_directions_are_not_reported_as_independent():
|
|
g = _gen(6)
|
|
x = _noise(g, 400, 6)
|
|
labels = ((x[:, 0] * x[:, 1]) > 0).long() # XOR-like, non-additive
|
|
report = build_subspace_report(x, labels, _axes(6, 2))
|
|
(pair,) = report["pairs"]
|
|
assert max(pair["single_scores"]) < 0.6 and pair["pair_score"] < 0.6
|
|
assert pair["interaction"] and pair["interaction_gain"] > 0.15
|
|
assert report["independence"] == "interaction_detected"
|
|
assert report["units"] == [[0, 1]]
|
|
assert any(q.startswith("nonadditive_interaction") for q in report["verdict"]["qualifiers"])
|
|
assert report["verdict"]["supported_rank"] == 0
|
|
|
|
|
|
def test_shuffled_labels_have_no_supported_rank(tetra):
|
|
x, labels, _ = tetra
|
|
shuffled = labels[torch.randperm(labels.numel(), generator=_gen(9))]
|
|
report = build_subspace_report(x, shuffled, _axes(10, 3))
|
|
assert report["verdict"]["supported_rank"] == 0 and report["verdict"]["status"] == "none"
|
|
assert report["subspace_null"]["observed_quantile"] < 0.99
|
|
|
|
|
|
def test_deterministic_given_seed_and_grouped_resampling():
|
|
x, labels = _one_informative(_gen(11))
|
|
groups = [f"g{i // 4}" for i in range(labels.numel())]
|
|
kwargs = dict(groups=groups, seed=3, n_bootstrap=8, n_null=10)
|
|
first = build_subspace_report(x, labels, _axes(10, 3), **kwargs)
|
|
second = build_subspace_report(x, labels, _axes(10, 3), **kwargs)
|
|
assert first == second
|
|
assert first["parameters"]["grouped"] and first["stability"]["group_resampling"]
|
|
assert first["verdict"]["supported_rank"] == 1
|
|
|
|
|
|
def test_full_width_basis_marks_nulls_unavailable_and_custom_refit_is_used():
|
|
x, labels = _one_informative(_gen(12))
|
|
x = x[:, :3]
|
|
calls = []
|
|
|
|
def refit(data, codes):
|
|
calls.append(data.shape)
|
|
return _axes(3, 1)
|
|
|
|
report = build_subspace_report(x, labels, _axes(3, 3), refit=refit, n_bootstrap=2)
|
|
assert report["verdict"]["status"] == "unavailable"
|
|
assert report["verdict"]["supported_rank"] is None and report["subspace_null"] is None
|
|
evidence = report["measurement_evidence"]["per_rank"]
|
|
assert evidence["status"] == "unavailable" and evidence["unavailable_reason"]
|
|
assert report["parameters"]["refit"] == "custom" and len(calls) == 1 + 5 + 2
|
|
assert report["stability"]["bootstrap_median_angles_deg"] == [0.0]
|
|
_json_roundtrip(report)
|
|
|
|
|
|
def test_failed_refits_mark_stability_unavailable():
|
|
x, labels = _one_informative(_gen(13))
|
|
|
|
def refit(data, codes):
|
|
if data.shape[0] == x.shape[0] and torch.equal(data, x):
|
|
return _axes(10, 1)
|
|
if len(calls) >= 5:
|
|
raise ValueError("synthetic refit failure")
|
|
calls.append(1)
|
|
return _axes(10, 1)
|
|
|
|
calls = []
|
|
report = build_subspace_report(x, labels, _axes(10, 1), refit=refit, n_bootstrap=3, n_null=5)
|
|
assert report["stability"]["bootstrap_failures"] == 3
|
|
assert report["measurement_evidence"]["stability"]["status"] == "unavailable"
|
|
assert report["measurement_evidence"]["pairs"]["status"] == "unavailable"
|
|
assert report["independence"] == "not_assessed"
|
|
|
|
|
|
@pytest.mark.parametrize("change, message", [
|
|
(dict(activations=torch.zeros(0, 3)), "activations"),
|
|
(dict(activations=torch.full((8, 3), float("nan"))), "activations"),
|
|
(dict(basis=torch.ones(1, 4)), "basis must"),
|
|
(dict(basis=torch.ones(2, 3)), "linearly dependent"),
|
|
(dict(labels=[0] * 7 + [1]), "two classes"),
|
|
(dict(labels=[0, 1]), "one entry per"),
|
|
(dict(folds=1), "folds"),
|
|
(dict(n_null=501), "n_null"),
|
|
(dict(null_quantile=1.0), "null_quantile"),
|
|
(dict(groups=["a"] * 8), "distinct group"),
|
|
])
|
|
def test_invalid_inputs_raise(change, message):
|
|
args = dict(activations=_noise(_gen(), 8, 3), labels=[0, 1] * 4, basis=_axes(3, 1),
|
|
folds=2)
|
|
args.update(change)
|
|
with pytest.raises(ValueError, match=message):
|
|
build_subspace_report(args.pop("activations"), args.pop("labels"), args.pop("basis"),
|
|
**args)
|
|
|
|
|
|
def test_stat_helpers_on_analytic_inputs():
|
|
a = torch.tensor([[1.0, 0, 0], [0, 1.0, 0]], dtype=F64)
|
|
tilted = torch.tensor([[1.0, 0, 0], [0, math.cos(0.3), math.sin(0.3)]], dtype=F64)
|
|
assert st.principal_angles(a, tilted) == pytest.approx([0.0, math.degrees(0.3)], abs=1e-6)
|
|
assert st.principal_angles(a[0], a[1]) == pytest.approx([90.0])
|
|
singular = torch.tensor([3.0, 1.0, 0.0], dtype=F64)
|
|
assert st.pca_threshold_rank(singular, 0.9) == 1
|
|
assert st.pca_threshold_rank(singular, 1.0) == 2
|
|
assert st.pca_threshold_rank(torch.zeros(2, dtype=F64), 0.9) == 0
|
|
assert st.entropy_effective_rank(torch.ones(4, dtype=F64)) == pytest.approx(4.0)
|
|
assert st.entropy_effective_rank(torch.zeros(3, dtype=F64)) is None
|
|
assert st.null_quantile(1.0, [0.0, 1.0, 2.0]) == pytest.approx(0.5)
|
|
with pytest.raises(ValueError, match="class-contrast"):
|
|
st.fisher_basis(torch.zeros(6, 3, dtype=F64), torch.tensor([0, 0, 0, 1, 1, 1]), 1)
|