#!/usr/bin/env python3 """High-precision variant of the budget pricing: use the flint-certified eigenvector at degree d, then Monte-Carlo the L2 mass inside the source's large-coordinate budget. Usage: cap_price_hp.py K EPS D [N] [OUTJSON] """ from __future__ import annotations import json import os import sys from fractions import Fraction as Fr import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) from cap_price import eval_F # noqa: E402 from certificate import ritz_vector # noqa: E402 A_SRC = Fr(2583, 10000) def run(k, eps, d, N=20000, seed=0, prec=512): e, c, lam = ritz_vector(k, eps, d, prec=prec) rng = np.random.default_rng(seed) E = rng.exponential(1.0, size=(N, k + 1)) pts = (1.0 + float(eps)) * E[:, :k] / E.sum(axis=1, keepdims=True) F = eval_F(pts, c, e.B, e.eps) F2 = F * F tot = F2.sum() delta = 3 / 250 B1 = 3 / 20 Bm = 4 / 25 def budget_ok(P, dc, b1, bm): m = P > dc s = (P * m).sum(axis=1) r = m.sum(axis=1) cap = np.where(r <= 2, b1, bm) return s <= cap, s, r res = {"k": k, "eps": str(eps), "d": d, "n": e.n, "lambda": lam, "N": N, "prec": prec} ok, s, r = budget_ok(pts * float(A_SRC), float(delta), float(B1), float(Bm)) res["t_mass_inside"] = float(F2[ok].sum() / tot) res["t_mean_sum"] = float(s.mean()) res["t_frac_ok"] = float(ok.mean()) oku, su, ru = budget_ok(pts, float(delta / A_SRC), float(B1 / A_SRC), float(Bm / A_SRC)) res["u_mass_inside"] = float(F2[oku].sum() / tot) res["u_mean_sum"] = float(su.mean()) res["u_frac_ok"] = float(oku.mean()) return res if __name__ == "__main__": k = int(sys.argv[1]); eps = Fr(sys.argv[2]); d = int(sys.argv[3]) N = int(sys.argv[4]) if len(sys.argv) > 4 else 20000 out = sys.argv[5] if len(sys.argv) > 5 else None r = run(k, eps, d, N=N) print(json.dumps(r, indent=2)) if out: with open(out, "w") as f: json.dump(r, f, indent=2)