Files
OBLITERATUS/obliteratus/analysis/kernel_fixtures.py
Joseph MaglyandClaude Opus 5.5 9e65c2385e feat(analysis): batched null kernels and reduced-rank sketches
Add optimization O05: batched random-direction kernels that reproduce
the reference loops, plus validated low-rank sketches with an explicit
error budget. The reference loops stay the default.

- Batched gate, discovery and subspace nulls keep draw order, generator
  state, norm matching, pooling, orientation folding and percentile
  semantics; identical rows are projected once so exact ties survive.
  Declared tolerance 1e-12 with identical decisions; observed 0.0.
- select_rank keeps both residual variance and class-contrast loss
  within budget, measured exactly, and reports the variance-only rank;
  it returns inconclusive when no rank qualifies.
- The sketch gate null is approximate only when the spectrum test
  passes, an exact audit stays within 0.005 AUC and no threshold falls
  inside the band; otherwise it recomputes exactly. It is refused in
  enforce mode. The observed AUC is never approximated.
- A benchmark module reports wall time, peak memory, subspace angles,
  null errors and decision disagreement (timings host-specific).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:29:07 -05:00

74 lines
3.0 KiB
Python

"""Deterministic synthetic fixtures for the null-kernel and reduced-rank benchmark.
Each gate fixture returns ``(positive, controls, vector)`` in float64. The
``tail_signal`` fixture puts the class contrast on a low-variance direction
orthogonal to a dominant low-rank nuisance, so explained variance alone
selects a rank that discards the target signal.
"""
from __future__ import annotations
import torch
F64 = torch.float64
GATE_FIXTURES = ("low_rank_signal", "tail_signal", "ties", "flat", "low_rank_null")
def _low_rank(generator, n, d, rank, scale):
mixing = torch.linalg.qr(torch.randn(d, rank, generator=generator, dtype=F64))[0].T
return torch.randn(n, rank, generator=generator, dtype=F64) * scale @ mixing, mixing
def _oriented(positive, controls):
delta = positive.mean(0) - torch.cat(list(controls.values())).mean(0)
return delta / delta.norm().clamp_min(1e-12)
def gate_fixture(kind, *, n=96, d=128, seed=0):
"""Positive rows, two control sets and the oriented difference-of-means vector."""
if kind not in GATE_FIXTURES:
raise ValueError(f"Unknown gate fixture {kind!r}")
g = torch.Generator().manual_seed(seed)
total = 3 * n
if kind == "flat":
rows = torch.randn(total, d, generator=g, dtype=F64)
rows[:n, 0] += 0.8
elif kind == "ties":
prototypes = torch.randint(-3, 4, (12, d), generator=g).to(F64)
rows = prototypes[torch.randint(0, 12, (total,), generator=g)]
rows[:n] = prototypes[torch.randint(0, 6, (n,), generator=g)]
else:
rows, mixing = _low_rank(g, total, d, 4, torch.tensor([6.0, 5.0, 4.0, 3.0], dtype=F64))
rows += 1e-3 * torch.randn(total, d, generator=g, dtype=F64)
if kind == "low_rank_signal":
rows[:n] += 6.0 * mixing[3]
elif kind == "tail_signal":
tail = torch.randn(d, generator=g, dtype=F64)
tail -= mixing.T @ (mixing @ tail)
tail /= tail.norm()
rows += 0.3 * torch.randn(total, 1, generator=g, dtype=F64) * tail
rows[:n] += 1.0 * tail
positive = rows[:n]
controls = {"a": rows[n:2 * n], "b": rows[2 * n:]}
return positive, controls, _oriented(positive, controls)
def subspace_fixture(*, n=120, d=24, classes=4, rank=3, seed=1):
"""Multiclass rows with class centres in a random subspace and a supplied basis."""
g = torch.Generator().manual_seed(seed)
codes = torch.arange(n) % classes
centres = torch.randn(classes, d, generator=g, dtype=F64) * 1.5
x = centres[codes] + torch.randn(n, d, generator=g, dtype=F64)
basis = centres[1:rank + 1] - centres[0]
return x, codes, basis
def discovery_fixture(*, n=160, d=64, seed=2):
"""Binary rows with grouped samples (two same-label rows per group) and a planted channel."""
g = torch.Generator().manual_seed(seed)
y = (torch.arange(n) // 2) % 2 == 0
x = torch.randn(n, d, generator=g, dtype=F64)
x[y, 3] += 1.0
groups = [f"g{i // 2}" for i in range(n)]
return x, y, groups