#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Verification of the PRICE LEMMA for two-family profile families. Lemma. Let {mu_w} (S rows) and {t_m} (T columns) be bounded index families with moment means U_j = , V_j = (U_1 = V_1 = 1), and let c_1, c_2, ... be scalars with c_1 != 0. Put A(v) = sum_{k>=1} c_k v^k u_k v_k^T, u_k = (mu_w^k)_w, v_k = (t_m^k)_m, and assume the tail is O(v^3), i.e. sum_{k>=3} |c_k| v^{k-2} ||u_k|| ||v_k|| is bounded as v -> 0. Then, as v -> 0, (i) ||A||_nuc / ||A||_F = 1 + (|c_2|/|c_1|) v sqrt(D_mu D_t) + O(v^2) (ii) ||A||_nuc - ||A||_F = |c_2| v^2 ||u_1|| ||v_1|| sqrt(D_mu D_t) + O(v^3) with the two families' normalised Cauchy-Schwarz deficits D_mu = (U_2 U_4 - U_3^2)/U_2^2, D_t = (V_2 V_4 - V_3^2)/V_2^2, so the excess above the Frobenius price is (|c_2|/|c_1|) v times the GEOMETRIC MEAN of the two dispersions. D >= 0 always, and D = 0 iff the family is constant (the family then has rank one and the Frobenius price is exact). What is tested here, all with exact singular values (numpy SVD, float64): T1 the law (i) on GENERIC families (mu_w = (w/S)^a, t_m = (m/T)^b) for several (a,b), several (S,T) and several coefficient profiles c_k -- not only the corpus's boxes; T2 the excess law (ii), including its ||u_1|| ||v_1|| scaling; T3 SEPARABILITY, the signature of a geometric mean: with the column family FIXED, (R-1)/(v sqrt(D_mu)) must be CONSTANT in the row family (and symmetrically); T4 the O(v^2) residual of (i) is bounded, i.e. the law is first order and not a fit; T5 the corpus's own profile (c_k = (2pi i)^k (1-rho^k)/k!, |c_2|/|c_1| = pi(1+rho)) reproduces the constant C = pi(1+rho) sqrt(D_mu D_t) that #905 verified against measurement. """ import cmath import math import sys import numpy as np sys.stdout.reconfigure(encoding="utf-8", errors="replace") def moments(xs, jmax=5): xs = np.asarray(xs, dtype=float) return [float(np.mean(xs ** j)) for j in range(jmax + 1)] def dispersion(U): """D = (U_2 U_4 - U_3^2)/U_2^2 >= 0, zero iff the family is constant.""" return (U[2] * U[4] - U[3] ** 2) / U[2] ** 2 def family(S, a): return (np.arange(1, S + 1) / S) ** a def coeffs_vaaler(rho, kmax=6): return [((2j * math.pi) ** k) * (1 - rho ** k) / math.factorial(k) for k in range(1, kmax + 1)] def build(S, T, a, b, v, cs, K=6): mu = family(S, a) t = family(T, b) A = np.zeros((S, T), dtype=complex) for k in range(1, min(K, len(cs)) + 1): A += cs[k - 1] * (v ** k) * np.outer(mu ** k, t ** k) return A, mu, t def ratio_and_excess(A): sv = np.linalg.svd(A, compute_uv=False) fro = math.sqrt(float(np.sum(sv ** 2))) return float(np.sum(sv)) / fro, float(np.sum(sv)) - fro def main(): rho = 2.0 cs = coeffs_vaaler(rho) cratio = abs(cs[1]) / abs(cs[0]) print("coefficient profile: c_k = (2 pi i)^k (1-rho^k)/k!, rho = %.1f, |c_2|/|c_1| = %.10f " "(= pi(1+rho) = %.10f)" % (rho, cratio, math.pi * (1 + rho))) assert abs(cratio - math.pi * (1 + rho)) < 1e-9 * cratio print() print("=" * 100) print("T1/T4. the law (i) on GENERIC families: mu_w = (w/S)^a, t_m = (m/T)^b") print("=" * 100) print("%5s %5s %5s %4s %10s %14s %14s %12s %12s" % ("S", "T", "a", "b", "v", "sqrt(D_mu D_t)", "(R-1)/v pred", "(R-1)/v meas", "resid/v^2")) worst = 0.0 for S, T in ((64, 48), (200, 150), (37, 91)): for a, b in ((1.0, 1.0), (0.5, 2.0), (2.5, 0.7), (1.3, 1.3)): mu = family(S, a) t = family(T, b) U = moments(mu) V = moments(t) Dm, Dt = dispersion(U), dispersion(V) gm = math.sqrt(Dm * Dt) for v in (1 / 200, 1 / 60, 1 / 25): A, _, _ = build(S, T, a, b, v, cs) R, exc = ratio_and_excess(A) pred = cratio * gm meas = (R - 1) / v resid = (R - 1 - cratio * gm * v) / v ** 2 worst = max(worst, abs(pred - meas) / max(pred, 1e-300)) print("%5d %5d %5.2f %4.1f %10.5f %14.8f %14.8f %14.8f %12.5f" % (S, T, a, b, v, gm, pred, meas, resid)) print("worst relative deviation of (R-1)/v from pi(1+rho) sqrt(D_mu D_t): %.5f" % worst) print() print("=" * 100) print("T2. the excess law (ii): ||A||_nuc - ||A||_F = |c_2| v^2 ||u_1|| ||v_1|| sqrt(D_mu D_t)") print("=" * 100) S, T, a, b = 200, 150, 1.3, 1.3 mu, t = family(S, a), family(T, b) U, V = moments(mu), moments(t) gm = math.sqrt(dispersion(U) * dispersion(V)) n1 = math.sqrt(S * U[2]) * math.sqrt(T * V[2]) print("%10s %18s %18s %12s" % ("v", "predicted excess", "measured excess", "ratio")) for v in (1 / 400, 1 / 100, 1 / 40, 1 / 16): A, _, _ = build(S, T, a, b, v, cs) _, exc = ratio_and_excess(A) pred = abs(cs[1]) * v ** 2 * n1 * gm print("%10.6f %18.8f %18.8f %12.6f" % (v, pred, exc, exc / pred)) print() print("=" * 100) print("T3. SEPARABILITY -- the signature of a geometric mean. Column family FIXED, row varies.") print("=" * 100) S, T, b = 128, 96, 1.1 V = moments(family(T, b)) Dt = dispersion(V) print("%6s %16s %18s %18s %14s" % ("a", "D_mu", "(R-1)/v", "(R-1)/(v*sqrt(D_mu))", "expected")) vals = [] for a in (0.3, 0.7, 1.0, 1.6, 2.2, 3.0): U = moments(family(S, a)) Dm = dispersion(U) v = 1 / 50 A, _, _ = build(S, T, a, b, v, cs) R, _ = ratio_and_excess(A) sep = (R - 1) / (v * math.sqrt(Dm)) vals.append(sep) print("%6.2f %16.10f %18.8f %18.10f %14.10f" % (a, Dm, (R - 1) / v, sep, cratio * math.sqrt(Dt))) spread = (max(vals) - min(vals)) / (sum(vals) / len(vals)) print("relative spread of the separated quantity over six row families: %.5f" % spread) print() print("=" * 100) print("T5. the corpus's own boxes (S = T = 512, a = b = 1, rho = 2) reproduce #905's C") print("=" * 100) S = T = 512 U = moments(family(S, 1.0)) V = moments(family(T, 1.0)) gm = math.sqrt(dispersion(U) * dispersion(V)) for rho in (1.25, 1.5, 2.0, 2.5): c = coeffs_vaaler(rho) pred = abs(c[1]) / abs(c[0]) * gm v = 1 / 100 A, _, _ = build(S, T, 1.0, 1.0, v, c) R, _ = ratio_and_excess(A) print("rho = %4.2f: C_pred = %.6f (R-1)/v measured = %.6f ratio = %.6f" % (rho, pred, (R - 1) / v, ((R - 1) / v) / pred)) return 0 if __name__ == "__main__": raise SystemExit(main())