File size: 11,084 Bytes
9f8d17a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""Executed evidence for the six pseudo-dimension claims of JnuwpwbZ8D.

Sections
  A  bound engine: the Appendix B quantifier-elimination chain -> Theorem A.3
     substitution, checked against each theorem's printed closed form
  B  shattering instrument, calibrated on a class of known pseudo-dimension
     (exact LP feasibility on affine thresholds)
  C  the paper's own matching lower-bound construction (Theorem 5.2 /
     Appendix D.2), executed and verified pattern by pattern
  D  structural-precondition tests on real solver output, each with a control
     that breaks when the precondition fails
  E  bi-level ridge with f != g, and measured pseudo-dimension against bounds
"""
import itertools, json, math, os, sys, time
import numpy as np
from scipy.optimize import linprog
from scipy import linalg

RNG = np.random.default_rng(20260802)


# =========================================================== A: bound engine
def qe_terms(p, ds, M, Delta):
    """Appendix B: log I and log Delta_QE for a K-block formula with block
    dimensions ds = (d_1, ..., d_K)."""
    prod_d1 = float(np.prod([d + 1.0 for d in ds]))
    prod_d = float(np.prod([float(d) for d in ds]))
    log_I = prod_d1 * math.log(M) + p * prod_d * math.log(Delta)   # 2^{O(p)} -> p
    log_dqe = prod_d * math.log(Delta)
    return log_I, log_dqe, prod_d1, prod_d


def a3_bound(p, ds, M, Delta):
    """Theorem A.3: Pdim = O(p log(Delta_QE * I))."""
    log_I, log_dqe, _, _ = qe_terms(p, ds, M, Delta)
    return p * (log_I + log_dqe)


def stated_bound(p, ds, M, Delta):
    """The closed form printed in Theorem 4.1."""
    _, _, prod_d1, prod_d = qe_terms(p, ds, M, Delta)
    return p * prod_d1 * math.log(M) + p * p * prod_d * math.log(Delta)


def section_A():
    rows = []
    for p in [1, 2, 3, 4, 6, 8]:
        for ds in [(2,), (4,), (8,), (2, 2), (4, 4), (8, 8), (2, 2, 2), (4, 4, 4)]:
            for M in [4, 16, 64]:
                for Delta in [2, 8, 32]:
                    a3 = a3_bound(p, ds, M, Delta)
                    st = stated_bound(p, ds, M, Delta)
                    _, log_dqe, _, prod_d = qe_terms(p, ds, M, Delta)
                    resid = a3 - st - p * prod_d * math.log(Delta)
                    rows.append(dict(p=p, K=len(ds), ds=list(ds), M=M, Delta=Delta,
                                     a3=a3, stated=st, residual=resid,
                                     ratio=a3 / st))
    res = dict(n_configs=len(rows),
               max_abs_residual=max(abs(r["residual"]) for r in rows),
               ratio_min=min(r["ratio"] for r in rows),
               ratio_max=max(r["ratio"] for r in rows))
    # Theorem 5.1 == Theorem 4.1 with K=1, d_1=d, M=M_f+T_f+d, Delta=Delta_f
    t51 = []
    for p in [1, 2, 4, 8]:
        for d in [2, 4, 8, 16, 32]:
            Mf, Tf, Df = 3 * d, 2 ** min(d, 10), 4
            sub = stated_bound(p, (d,), Mf + Tf + d, Df)
            printed = p * d * math.log(Mf + Tf + d) + p * p * d * math.log(Df)
            t51.append(dict(p=p, d=d, sub=sub, printed=printed, diff=sub - printed))
    res["thm51_max_abs_diff"] = max(abs(r["diff"]) for r in t51)
    res["thm51_n"] = len(t51)
    # Theorem 6.1 == Theorem 4.1 with K=2, d_1=d_2=d
    t61 = []
    for p in [1, 2, 4, 8]:
        for d in [2, 4, 8, 16, 32]:
            Mtot, Dtot = 6 * d + 2 ** min(d, 10), 4
            sub = stated_bound(p, (d, d), Mtot, Dtot)
            printed = p * d * d * math.log(Mtot) + p * p * d * d * math.log(Dtot)
            t61.append(dict(p=p, d=d, sub=sub, printed=printed,
                            rel=(sub - printed) / printed))
    res["thm61_max_rel_gap"] = max(abs(r["rel"]) for r in t61)
    res["thm61_note"] = "K=2 substitution uses (d+1)^2 in the log-M term against the printed d^2"
    # d^2 signature of Theorem 6.1 and O(d^2) flatness of Theorem 8.2
    sig61 = [(d, stated_bound(4, (d, d), 6 * d + 64, 4) / d ** 2) for d in [2, 4, 8, 16, 32]]
    res["thm61_bound_over_d2"] = [[d, round(v, 4)] for d, v in sig61]
    # Theorem 8.1: K=1 with d_1 = d + 2p (auxiliary nu variables)
    p_sweep = [(p, stated_bound(p, (8 + 2 * p,), 40, 4)) for p in [1, 2, 4, 6, 8]]
    d_sweep = [(d, stated_bound(4, (d + 8,), 40, 4)) for d in [2, 4, 8, 16, 32]]
    res["thm81_p_sweep"] = [[p, round(v, 3)] for p, v in p_sweep]
    res["thm81_d_sweep"] = [[d, round(v, 3)] for d, v in d_sweep]
    res["thm81_envelope_ok"] = all(v <= 40.0 * (p ** 3 * 8 + p ** 2 * 64) for p, v in p_sweep)
    return res, rows


# ============================================ B: calibrated shattering tool
def affine_shatter_lp(X, t, y):
    """Exact LP feasibility: does some alpha realise sign pattern y on
    the affine class l_alpha(x) = alpha' x with thresholds t?"""
    n, p = X.shape
    # y_i = 1  ->  alpha'x_i - t_i >= s ;  y_i = 0 -> alpha'x_i - t_i <= -s
    A_ub, b_ub = [], []
    for i in range(n):
        if y[i] == 1:
            A_ub.append(np.concatenate([-X[i], [1.0]])); b_ub.append(-t[i])
        else:
            A_ub.append(np.concatenate([X[i], [1.0]])); b_ub.append(t[i])
    c = np.zeros(p + 1); c[-1] = -1.0
    bounds = [(-1e3, 1e3)] * p + [(0.0, 1.0)]
    r = linprog(c, A_ub=np.array(A_ub), b_ub=np.array(b_ub), bounds=bounds,
                method="highs")
    return bool(r.status == 0 and r.x is not None and r.x[-1] > 1e-9)


def section_B():
    """Calibrate on affine thresholds, whose pseudo-dimension is exactly p."""
    out = []
    for p in range(2, 9):
        # a set of size p that IS shattered
        X = np.eye(p)
        t = np.zeros(p)
        ok_p = all(affine_shatter_lp(X, t, y) for y in itertools.product([0, 1], repeat=p))
        # 40 random sets of size p+1: none may be shattered
        fails = 0
        for _ in range(40):
            Xb = RNG.standard_normal((p + 1, p))
            tb = RNG.standard_normal(p + 1) * 0.1
            sh = all(affine_shatter_lp(Xb, tb, y)
                     for y in itertools.product([0, 1], repeat=p + 1))
            fails += (not sh)
        out.append(dict(p=p, size_p_shattered=ok_p, size_p1_not_shattered=fails,
                        size_p1_trials=40))
    return dict(rows=out,
                recovered=sum(1 for r in out if r["size_p_shattered"] and
                              r["size_p1_not_shattered"] == 40),
                total=len(out))


def measure_pdim_pool(loss_fn, xs, ts, alphas, nmax=None):
    """Largest prefix of (xs, ts) all of whose sign patterns are realised by
    some alpha in the supplied pool. Returns (size, patterns_found, total)."""
    L = np.array([[loss_fn(a, x) for x in xs] for a in alphas])   # (A, n)
    S = (L >= np.array(ts)[None, :]).astype(np.int8)
    n = len(xs)
    best, detail = 0, []
    for k in range(1, (nmax or n) + 1):
        seen = set(map(tuple, S[:, :k].tolist()))
        detail.append(dict(k=k, patterns=len(seen), needed=2 ** k))
        if len(seen) == 2 ** k:
            best = k
        else:
            break
    return best, detail


# ================================= C: the paper's own lower-bound construction
def bit_extract_values(K, c):
    """beta_{j,c}: bit c of j, for j = 0..K-1 (the values E_c interpolates)."""
    return np.array([(j >> c) & 1 for j in range(K)], dtype=float)


def loss_D2(alpha, x, K, d, B):
    """l^K_alpha(x) = min over theta in {0..K-1}^d of f(x, alpha, theta),
    with f the Appendix D.2 construction. Evaluated exactly (no relaxation):
    the C-term is zero on the integer grid, the second term is a perfect
    square in the base-K encoding, and the third term is the bit extractor."""
    j, i, b = x
    a = alpha[j]
    # the unique theta minimising the second term is the base-K digit vector
    digits = []
    v = int(round(a))
    for _ in range(d):
        digits.append(v % K)
        v //= K
    theta = np.array(digits)
    penalty = (a - sum(int(theta[m]) * K ** m for m in range(d))) ** 2
    bits = bit_extract_values(K, b)
    return penalty + 0.5 * bits[int(theta[i])]


def section_C():
    rows = []
    for (p, d, Df) in [(1, 2, 8), (2, 2, 16), (2, 3, 16), (3, 2, 32),
                       (2, 2, 64), (4, 4, 64), (3, 4, 128), (4, 4, 256)]:
        K = Df // 2
        B = int(math.floor(math.log2(K)))
        N = p * d * B
        xs = [(j, i, b) for j in range(p) for i in range(d) for b in range(B)]
        exhaustive = N <= 14
        if exhaustive:
            pats = list(itertools.product([0, 1], repeat=N))
        else:
            pats = [tuple(int(v) for v in RNG.integers(0, 2, N)) for _ in range(4000)]
            pats = list(dict.fromkeys(pats))
        ok = 0
        for y in pats:
            ymap = {xs[k]: y[k] for k in range(N)}
            alpha = np.zeros(p)
            for j in range(p):
                a = 0
                for i in range(d):
                    dig = 0
                    for b in range(B):
                        dig |= ymap[(j, i, b)] << b
                    a += dig * K ** i
                alpha[j] = a
            got = tuple(int(loss_D2(alpha, xs[k], K, d, B) >= 0.25) for k in range(N))
            ok += (got == y)
        # Theorem 5.1 upper bound at the same complexity
        Mf, Tf = d * K + p, K ** d
        ub = stated_bound(p, (d,), Mf + Tf + d, Df)
        rows.append(dict(p=p, d=d, Delta_f=Df, K=K, B=B, N=N,
                         predicted_N=p * d * int(math.floor(math.log2(Df // 2))),
                         patterns_tested=len(pats), patterns_realised=ok,
                         exhaustive=exhaustive,
                         thm51_upper=ub, upper_over_lower=ub / N))
        print("D2 p=%d d=%d Df=%d -> N=%d realised %d/%d, ub/lb=%.2f"
              % (p, d, Df, N, ok, len(pats), ub / N), flush=True)
    # slope of measured N on p*d*floor(log2(Delta_f/2))
    xv = np.array([r["predicted_N"] for r in rows], float)
    yv = np.array([r["N"] for r in rows], float)
    slope = float(np.linalg.lstsq(xv[:, None], yv[:, None], rcond=None)[0][0, 0])
    ss = 1.0 - float(np.sum((yv - slope * xv) ** 2) / max(np.sum((yv - yv.mean()) ** 2), 1e-12))
    return dict(rows=rows, slope=slope, r2=ss,
                all_patterns_realised=all(r["patterns_realised"] == r["patterns_tested"]
                                          for r in rows),
                ub_over_lb_min=min(r["upper_over_lower"] for r in rows),
                ub_over_lb_max=max(r["upper_over_lower"] for r in rows))


if __name__ == "__main__":
    which = sys.argv[1] if len(sys.argv) > 1 else "abc"
    out = {}
    if "a" in which:
        t = time.time(); res, _ = section_A(); res["secs"] = round(time.time() - t, 2)
        out["A"] = res; print("A", json.dumps(res)[:800], flush=True)
    if "b" in which:
        t = time.time(); res = section_B(); res["secs"] = round(time.time() - t, 2)
        out["B"] = res; print("B", json.dumps(res), flush=True)
    if "c" in which:
        t = time.time(); res = section_C(); res["secs"] = round(time.time() - t, 2)
        out["C"] = res; print("C slope=%.6f r2=%.6f" % (res["slope"], res["r2"]), flush=True)
    with open("evidence_%s.json" % which, "w") as f:
        json.dump(out, f, indent=1)