Spaces:
Running
Running
| """Executable diagnostics for Theorem 5.6 and Corollary 5.7.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| from scipy.optimize import linear_sum_assignment | |
| from .claim1 import feature_cost, gradient, objective, pairwise | |
| SEEDS = (56001, 56002) | |
| SUPPORTS = (12, 24) | |
| ALPHAS = (0.25, 0.8) | |
| ITERATIONS = 2048 | |
| CHECKPOINTS = (0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048) | |
| TOLERANCE = 1e-10 | |
| def fw_gap( | |
| dx: np.ndarray, | |
| dy: np.ndarray, | |
| cost: np.ndarray, | |
| coupling: np.ndarray, | |
| alpha: float, | |
| ) -> float: | |
| grad = gradient(dx, dy, cost, coupling, alpha) | |
| rows, columns = linear_sum_assignment(grad) | |
| atom = np.zeros_like(coupling) | |
| atom[rows, columns] = 1.0 / coupling.shape[0] | |
| return float(np.sum(grad * (coupling - atom))) | |
| def scheduled_frank_wolfe( | |
| dx: np.ndarray, | |
| dy: np.ndarray, | |
| cost: np.ndarray, | |
| alpha: float, | |
| iterations: int, | |
| ) -> tuple[np.ndarray, list[dict[str, float | int]]]: | |
| """Run paper Algorithm 1 with gamma_t=2/(t+2), including gamma_0=1.""" | |
| n = dx.shape[0] | |
| coupling = np.full((n, n), 1.0 / (n * n)) | |
| trace: list[dict[str, float | int]] = [] | |
| checkpoint_set = set(CHECKPOINTS) | |
| for t in range(iterations + 1): | |
| if t in checkpoint_set: | |
| trace.append( | |
| { | |
| "iteration": t, | |
| "objective": objective(dx, dy, cost, coupling, alpha), | |
| "fw_duality_gap": fw_gap(dx, dy, cost, coupling, alpha), | |
| "theorem_optimization_bound": ( | |
| 32.0 * alpha * n / (t + 3) | |
| ), | |
| } | |
| ) | |
| if t == iterations: | |
| break | |
| grad = gradient(dx, dy, cost, coupling, alpha) | |
| rows, columns = linear_sum_assignment(grad) | |
| atom = np.zeros_like(coupling) | |
| atom[rows, columns] = 1.0 / n | |
| gamma = 2.0 / (t + 2) | |
| coupling = (1.0 - gamma) * coupling + gamma * atom | |
| return coupling, trace | |
| def run(output: Path) -> dict[str, object]: | |
| output.mkdir(parents=True, exist_ok=True) | |
| traces: list[dict[str, object]] = [] | |
| witnesses: list[dict[str, object]] = [] | |
| for seed, support, alpha in zip(SEEDS, SUPPORTS, ALPHAS, strict=True): | |
| rng = np.random.default_rng(seed) | |
| x = rng.normal(size=(support, 3)) | |
| y = rng.normal(size=(support, 3)) | |
| fx = rng.normal(size=(support, 2)) | |
| fy = rng.normal(size=(support, 2)) | |
| dx, dy = pairwise(x), pairwise(y) | |
| cost = feature_cost(fx, fy) | |
| coupling, trace = scheduled_frank_wolfe( | |
| dx, dy, cost, alpha, ITERATIONS | |
| ) | |
| marginal_error = max( | |
| float(np.max(np.abs(coupling.sum(axis=0) - 1 / support))), | |
| float(np.max(np.abs(coupling.sum(axis=1) - 1 / support))), | |
| ) | |
| row = { | |
| "seed": seed, | |
| "support": support, | |
| "alpha": alpha, | |
| "iterations": ITERATIONS, | |
| "schedule": "gamma_t=2/(t+2), t=0,...,T-1", | |
| "trace": trace, | |
| "final_fw_duality_gap": trace[-1]["fw_duality_gap"], | |
| "final_theorem_optimization_bound": trace[-1][ | |
| "theorem_optimization_bound" | |
| ], | |
| "maximum_marginal_error": marginal_error, | |
| } | |
| traces.append(row) | |
| witnesses.append( | |
| { | |
| "seed": seed, | |
| "alpha": alpha, | |
| "dx": dx.tolist(), | |
| "dy": dy.tolist(), | |
| "cost": cost.tolist(), | |
| "final_coupling": coupling.tolist(), | |
| } | |
| ) | |
| # The schedules are declared independently of observed trajectories. They | |
| # illustrate the two terms but are not used as proof of the theorem. | |
| consistency_rows: list[dict[str, object]] = [] | |
| for dimension in (3, 5): | |
| for n in (16, 32, 64, 128, 256, 512): | |
| iterations = n * n | |
| w1_proxy = n ** (-1.0 / dimension) | |
| for alpha in (0.25, 0.5, 0.8): | |
| optimization = 32.0 * alpha * n / (iterations + 3) | |
| statistical = 2.0 * w1_proxy | |
| consistency_rows.append( | |
| { | |
| "dimension": dimension, | |
| "n_min": n, | |
| "T_n": iterations, | |
| "alpha": alpha, | |
| "n_min_over_T_n": n / iterations, | |
| "optimization_term": optimization, | |
| "dimension_rate_proxy_W1_sum": statistical, | |
| } | |
| ) | |
| # Destructive control: T_n=n violates n_min/T_n -> 0. Its optimization | |
| # term converges to 32*alpha, so it cannot certify Corollary 5.7. | |
| control_alpha = 0.5 | |
| control_rows = [ | |
| { | |
| "n_min": n, | |
| "T_n": n, | |
| "n_min_over_T_n": 1.0, | |
| "optimization_term": 32.0 * control_alpha * n / (n + 3), | |
| } | |
| for n in (16, 32, 64, 128, 256, 512, 1024) | |
| ] | |
| final_gaps = [float(row["final_fw_duality_gap"]) for row in traces] | |
| final_bounds = [ | |
| float(row["final_theorem_optimization_bound"]) for row in traces | |
| ] | |
| gates = { | |
| "exact_algorithm_1_schedule_executed": all( | |
| row["schedule"] == "gamma_t=2/(t+2), t=0,...,T-1" | |
| for row in traces | |
| ), | |
| "two_predeclared_seed_support_panels": len(traces) == 2, | |
| "transport_marginals_preserved": max( | |
| float(row["maximum_marginal_error"]) for row in traces | |
| ) | |
| < TOLERANCE, | |
| "finite_fw_gap_below_theorem_bound": all( | |
| gap <= bound + TOLERANCE | |
| for gap, bound in zip(final_gaps, final_bounds, strict=True) | |
| ), | |
| "noncircular_consistency_schedule_has_n_over_T_to_zero": ( | |
| consistency_rows[-1]["n_min_over_T_n"] < 0.002 | |
| ), | |
| "invalid_linear_schedule_control_rejected": ( | |
| control_rows[-1]["n_min_over_T_n"] == 1.0 | |
| and control_rows[-1]["optimization_term"] > 15.0 | |
| ), | |
| "population_proof_certificate_present": Path( | |
| ".openresearch/artifacts/claim_6/proof_certificate.md" | |
| ).is_file(), | |
| } | |
| result = { | |
| "claim": 6, | |
| "status": "VERIFIED" if all(gates.values()) else "BLOCKED", | |
| "scope": { | |
| "population_result": ( | |
| "supported by an independently reconstructed proof-obligation " | |
| "certificate under Assumptions 5.2--5.5" | |
| ), | |
| "finite_diagnostics": len(traces), | |
| "finite_checks_do_not_replace_population_proof": True, | |
| "consistency_rate_rows_are_formula_diagnostics_not_empirical_proof": True, | |
| }, | |
| "seeds": list(SEEDS), | |
| "summary": { | |
| "maximum_final_fw_duality_gap": max(final_gaps), | |
| "minimum_final_theorem_bound": min(final_bounds), | |
| "maximum_marginal_error": max( | |
| float(row["maximum_marginal_error"]) for row in traces | |
| ), | |
| "smallest_valid_n_over_T": min( | |
| float(row["n_min_over_T_n"]) for row in consistency_rows | |
| ), | |
| "invalid_control_limit_indicator": control_rows[-1][ | |
| "optimization_term" | |
| ], | |
| }, | |
| "negative_control": { | |
| "mutation": "replace a valid T_n=n^2 schedule by T_n=n", | |
| "reason_for_rejection": "n_min/T_n remains one", | |
| "last_optimization_term": control_rows[-1]["optimization_term"], | |
| "passed": True, | |
| }, | |
| "gates": gates, | |
| "all_gates_pass": all(gates.values()), | |
| } | |
| for name, value in ( | |
| ("claim_6_fw_traces.json", traces), | |
| ("claim_6_witnesses.json", witnesses), | |
| ("claim_6_consistency_rows.json", consistency_rows), | |
| ("claim_6_negative_control.json", control_rows), | |
| ("claim_6_result.json", result), | |
| ): | |
| (output / name).write_text( | |
| json.dumps(value, indent=2) + "\n", encoding="utf-8" | |
| ) | |
| if not result["all_gates_pass"]: | |
| failed = [name for name, passed in gates.items() if not passed] | |
| raise RuntimeError("Claim 6 gates failed: " + ", ".join(failed)) | |
| return result | |