r"""extrapolate-kstar-1943.py -- how far past k = 10 does the new step's curve have to go? THE PROBLEM. The certificate at a step is N_k = 0 at k = K*+1, and K* is exactly what the run must reach. At 19#->43# the engine's measured cells stop at k = 10 because the cost grows like 2^k per shape; K* is known to be >= 16 by monotonicity (adding prime 43 to the base-19 enterers only kills more slots, and the served document measures K*(19#->41#) = 16), so a certificate needs k >= 17 and the run is priced in hours at best. THE MODEL, and its calibration. N_k decays geometrically with a ratio that itself drifts slowly: fit log N_k = a + b k + c k^2 on the first ten cells and read the first k with N_k < 1. The point of this script is NOT the model -- it is that the model is CALIBRATED on the three frozen steps, where the true K* is known and where the same ten-cell window is available. The bias it makes there is the error bar it gets here. A model that cannot predict a known K* from the same amount of data is not evidence about an unknown one. """ import json import math import os HERE = os.path.dirname(os.path.abspath(__file__)) FROZEN = { # 19#->37# is NOT frozen: it is the served period-free certificate this run reproduced # independently (K* = 13, curve measured here). It is a calibration point of a different # kind -- the same base 19 as the step being estimated, and the only one of the four that # is not a full period walk -- so it is reported both in and out of the calibration. "19#->37#": (13, [71775574200, 14726572962, 2741100756, 507878268, 92290916, 15643066, 2562766, 419752, 72170, 13384, 2254, 324, 34, 0]), "13#->29#": (10, [105221160, 29114520, 6942634, 1470674, 285422, 52048, 9456, 1712, 270, 36, 0]), "13#->31#": (17, [3691273410, 1208132640, 357506328, 98048374, 25570726, 6426266, 1601992, 403958, 103170, 25738, 5568, 1180, 296, 80, 18, 6, 2, 0]), "17#->31#": (13, [2524470300, 607806150, 128515868, 25793636, 5037414, 929548, 167144, 29148, 4894, 710, 138, 22, 6, 0]), } def fit_quad(curve): """Least squares on (k, log N_k) for the supplied cells; returns (a, b, c).""" ks = [k for k in range(1, len(curve) + 1) if curve[k - 1] > 0] ys = [math.log(curve[k - 1]) for k in ks] n = len(ks) # normal equations for the 3-parameter quadratic in k A = [[sum(k ** (i + j) for k in ks) for j in range(3)] for i in range(3)] B = [sum(y * k ** i for k, y in zip(ks, ys)) for i in range(3)] for i in range(3): # gaussian elimination, 3x3 p = max(range(i, 3), key=lambda r: abs(A[r][i])) A[i], A[p] = A[p], A[i] B[i], B[p] = B[p], B[i] for r in range(i + 1, 3): f = A[r][i] / A[i][i] for cc in range(i, 3): A[r][cc] -= f * A[i][cc] B[r] -= f * B[i] x = [0.0, 0.0, 0.0] for i in (2, 1, 0): x[i] = (B[i] - sum(A[i][j] * x[j] for j in range(i + 1, 3))) / A[i][i] return x def first_zero(curve, cap=200): """First k with N_k < 1 under the fitted model, as a float crossing.""" a, b, c = fit_quad(curve) lo, hi = float(len(curve)), float(cap) def f(k): return a + b * k + c * k * k # f increases in k for the fitted curvature; walk outward k = len(curve) while k <= cap and f(k) < 0: k += 1 if k > cap: return None k0, k1 = k - 1, k if f(k1) - f(k0) == 0: return float(k1) return k0 + (0.0 - f(k0)) / (f(k1) - f(k0)) def main(): out = {"model": "log N_k quadratic in k, fit on the first ten cells, zero at N_k < 1", "calibration": {}, "new_step": None} print("CALIBRATION (fit on the first ten cells only, true K* known):") for name, (kstar, curve) in FROZEN.items(): pred = first_zero(curve[:10]) out["calibration"][name] = {"true_K*": kstar, "predicted_from_10_cells": pred, "bias": None if pred is None else round(pred - kstar, 2)} print(" %-10s true K*=%2d predicted from 10 cells = %s bias %s" % (name, kstar, "none" if pred is None else "%.2f" % pred, out["calibration"][name]["bias"])) newp = os.path.join(HERE, "curve-kmax10.json") j = json.load(open(newp, encoding="utf-8")) curve = j["Nk_summed"] pred = first_zero(curve) biases = [v["bias"] for v in out["calibration"].values() if v["bias"] is not None] mean_bias = sum(biases) / len(biases) if biases else None base19 = [v["bias"] for k, v in out["calibration"].items() if k.startswith("19#") and v["bias"] is not None] out["calibration_base19_only"] = base19 if base19: print(" base-19 calibration point bias: %s (the same base as the estimated step)" % ", ".join("%+.2f" % b for b in base19)) out["new_step"] = {"measured_kmax": 10, "N1": curve[0], "N_k": curve, "predicted_first_zero": None if pred is None else round(pred, 2), "Kstar_predicted": None if pred is None else round(pred - 1, 2), "calibration_mean_bias": None if mean_bias is None else round(mean_bias, 2), "Kstar_bias_corrected": None if pred is None or mean_bias is None else round(pred - 1 - mean_bias, 2)} print("\nNEW STEP 19#->43#, measured cells k = 1..10:") print(" predicted first zero (N_k < 1) at k = %.2f -> K* ~ %.1f" % (pred, pred - 1)) if mean_bias is not None: print(" mean bias on the calibration steps: %+.2f -> bias-corrected K* ~ %.1f" % (mean_bias, pred - 1 - mean_bias)) if base19: print(" using the base-19 point alone: bias %+.2f -> K* ~ %.1f" % (base19[0], pred - 1 - base19[0])) print(" PROVEN lower bound from monotonicity in Q: K* >= 16 (served K*(19#->41#) = 16)") # what the certificate would cost at that predicted k # PRICE, from MEASURED rates of the fast 64-bit path (per shape, one process): # 13#->29# |Q|=4 kmax=11 D=1485 0.34 ms (frozen step 0) # 19#->37# |Q|=4 kmax=14 D=378675 1.58 ms (this run, 567 s for the whole step) # 19#->43# |Q|=6 kmax=10 D=378675 0.504 ms (this run, 191 s for the whole step) # Two growth models bracket the extrapolation from k=14 to k=22 at fixed |Q|=4: # OBSERVED 1.66 per k (k=11 -> k=14 measured 0.34 -> 1.58); under-states the # asymptotic 2x per k, so it is a LOWER bound on the price # ELEMENTS |Q|*k*2^(k-1) work, i.e. 2.18 per k at k=14 (UPPER bound) k14 = 1.58 scale_q = 6.0 / 4.0 print("\nPRICE at 19#->43# (|Q|=6), anchored on the measured 1.58 ms/shape at |Q|=4, k=14:") for kk in (14, 16, 17, 18, 20, 22): lo = k14 * scale_q * 1.66 ** (kk - 14) hi = k14 * scale_q * ((kk * (1 << kk)) / (14.0 * (1 << 14))) print(" k=%-3d %.1f-%.1f ms/shape -> %.1f-%.1f h single process" % (kk, lo, hi, lo * 378675 / 3.6e6, hi * 378675 / 3.6e6)) out["price_hours_single_process"] = { str(kk): [round(k14 * scale_q * 1.66 ** (kk - 14) * 378675 / 3.6e6, 1), round(k14 * scale_q * ((kk * (1 << kk)) / (14.0 * (1 << 14))) * 378675 / 3.6e6, 1)] for kk in (14, 16, 17, 18, 20, 22)} with open(os.path.join(HERE, "extrapolate-kstar-1943.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(out, fh, indent=1) return 0 if __name__ == "__main__": raise SystemExit(main())