File size: 5,110 Bytes
9552b49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Constructive checks of jointly optimized archive and repair geometry.

Analytic curves and finite algebraic/numerical checks only; no measured
hardware or dataset performance is represented by these values.
"""
import json
import sys
from pathlib import Path

import numpy as np
from scipy.linalg import block_diag

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
from oqaivaei.continuation import balanced_real_projector

TOL = 2e-10


def omitted_basis(q, r):
    projection = balanced_real_projector(q, r)
    eigenvalues, vectors = np.linalg.eigh(projection)
    return vectors[:, eigenvalues > 0.5]


def main():
    rng = np.random.default_rng(20261020)
    records = []
    assertions = 0
    residuals = []
    for q in range(2, 25):
        for r in range(1, q + 1):
            B = 3.0
            U = omitted_basis(q, r)
            assert U.shape == (q, r)
            assert np.allclose(U.T @ U, np.eye(r), atol=TOL)
            assertions += 2
            norms = np.einsum("ij,ij->i", U, U)
            shared_primal = float(r * norms.max() / B)
            shared_formula = r * r / (q * B)
            announced_primal = float(norms.max() / B)
            announced_formula = r / (q * B)
            for actual, expected in [(shared_primal, shared_formula),
                                     (announced_primal, announced_formula)]:
                residuals.append(abs(actual - expected))
                assert abs(actual - expected) < TOL
                assertions += 1
            # Arbitrary dual mixtures cannot exceed the exhibited certificate.
            max_random_dual = 0.0
            for _ in range(8):
                pi = rng.dirichlet(np.ones(q))
                G = U.T @ (pi[:, None] * U)
                score = float(np.sqrt(np.maximum(0, np.linalg.eigvalsh(G))).sum() ** 2 / B)
                max_random_dual = max(max_random_dual, score)
                assert score <= shared_formula + TOL
                assertions += 1
            # Check the actual unbiased signal reconstruction for a random state.
            h = rng.normal(size=q)
            residual = U.T @ h
            retained = h - U @ residual
            S = np.sqrt(B / r) * np.eye(r)
            y = S @ residual
            decoded = retained + np.sqrt(r / B) * U @ y
            assert np.allclose(decoded, h, atol=TOL)
            assertions += 1
            records.append({"q": q, "p": q - r, "r": r, "B": B,
                            "shared_optimum": shared_formula,
                            "announced_optimum": announced_formula,
                            "coordinate_storage_shared": r / B,
                            "shared_constructed": shared_primal,
                            "max_random_dual": max_random_dual})
    blocks = []
    for q in [4, 8, 12, 16, 24]:
        for r in range(1, q + 1):
            for k in range(1, r + 1):
                if q % k or r % k:
                    continue
                s, d = q // k, r // k
                U = block_diag(*[omitted_basis(s, d) for _ in range(k)])
                assert U.shape == (q, r)
                assert np.allclose(U.T @ U, np.eye(r), atol=TOL)
                # Each active cell uses all gain B=1 in d dimensions.
                costs = []
                for g in range(k):
                    rows = U[g * s:(g + 1) * s]
                    costs.append(float(d * np.sum(rows * rows, axis=1).max()))
                formula = r * r / (k * q)
                assert abs(max(costs) - formula) < TOL
                residuals.append(abs(max(costs) - formula))
                assertions += 3
                blocks.append({"q": q, "r": r, "k": k, "B": 1,
                               "optimum": formula, "constructed": max(costs)})
    # Curve used in a worked chapter example: q64, p32, B8.
    announcements = []
    for bits in range(7):
        k = 2 ** bits
        r, q, B = 32, 64, 8
        v = max(r / q, r * r / (min(k, r) * q)) / B
        announcements.append({"q": q, "p": q - r, "B": B,
                              "advance_bits": bits, "cells": k,
                              "optimal_variance": v,
                              "rho": 0.8, "stationary_mse_bound": v / 0.36})
    result = {"status": "PASS", "seed": 20261020,
              "scope": "analytic constructions; floating-point finite checks",
              "checks": assertions, "subspace_cases": len(records),
              "block_cases": len(blocks), "tolerance": TOL,
              "max_abs_equality_residual": max(residuals),
              "memory_frontier": records, "divisible_blocks": blocks,
              "announcement_example": announcements}
    destination = ROOT / "results" / "codesign_results.json"
    destination.parent.mkdir(exist_ok=True)
    destination.write_text(json.dumps(result, indent=2) + "\n", encoding="utf-8")
    print(json.dumps({key: result[key] for key in
                     ["status", "checks", "subspace_cases", "block_cases",
                      "max_abs_equality_residual", "tolerance"]}, indent=2))


if __name__ == "__main__":
    main()