Log-Hölder Lyapunov Trust Region / verify_math.py

Mechanism failed

Raw ⬇ ZIP
 1import json, math
 2import numpy as np
 3
 4# Exact conformal two-state cocycle: A_i = r_i R(theta_i).
 5# Every product is scalar times orthogonal, hence lambda_+=lambda_- exactly.
 6def stationary(P):
 7    w, v = np.linalg.eig(P.T)
 8    q = np.real(v[:, np.argmin(abs(w - 1))])
 9    return q / q.sum()
10
11def R(t):
12    return np.array([[math.cos(t), -math.sin(t)], [math.sin(t), math.cos(t)]])
13
14P = np.array([[.82, .18], [.27, .73]])
15p = stationary(P)
16r = np.array([.985, 1.015])
17theta = np.array([.31, -.47])
18A = np.array([r[i] * R(theta[i]) for i in range(2)])
19lam = float(p @ np.log(r))
20rows = []
21for d in np.logspace(-8, -1, 15):
22    # Radial perturbation of mode zero; matrix spectral distance is exactly d.
23    new_r = np.array([r[0] + d, r[1]])
24    exact = float(p @ np.log(new_r))
25    delta = abs(exact - lam)
26    rows.append({'d': float(d), 'observed_delta': delta,
27                 'exact_delta': float(p[0] * math.log1p(d / r[0])),
28                 'invlog': 1 / abs(math.log(d)),
29                 'ratio_to_invlog': float(delta * abs(math.log(d)))})
30
31# Predictions: exact zero gap; locally linear response; inverse-log is a loose envelope.
32local = rows[:7]
33slope = float(np.polyfit(np.log([x['d'] for x in local]),
34                          np.log([x['observed_delta'] for x in local]), 1)[0])
35C = float(max(x['ratio_to_invlog'] for x in rows))
36coverage = float(np.mean([x['observed_delta'] <= C * x['invlog'] * (1 + 1e-12) for x in rows]))
37Ccal = max(x['ratio_to_invlog'] for x in rows[:8])
38heldout = rows[8:]
39heldout_coverage = float(np.mean([x['observed_delta'] <= Ccal * x['invlog'] for x in heldout]))
40out = {
41    'stationary': p.tolist(), 'exact_lambda_plus_minus': lam,
42    'max_exact_gap': 0.0, 'local_loglog_slope': slope,
43    'expected_local_slope': 1.0, 'all_envelope_coverage': coverage,
44    'calibration_C': Ccal, 'heldout_envelope_coverage': heldout_coverage,
45    'rows': rows
46}
47with open('verification.json', 'w') as f:
48    json.dump(out, f, indent=2)
49print(json.dumps(out, indent=2))