File size: 5,332 Bytes
4e016a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
"""Fresh executed protection blocks for thin verified social-welfare pages."""
from __future__ import annotations

import json
import sys
from pathlib import Path

import numpy as np

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from reproduce import (gini_oracle, gini_value, kolm_oracle, kolm_value,
                       oracle, simulate, wpm_oracle, wpm_value, dependent_round)


def claim1():
    rows = []
    for family, param in (("wpm", -2.0), ("kolm", -0.7), ("gini", 0.0)):
        for n in (8, 16, 24):
            for rep in range(6):
                rng = np.random.default_rng(910000 + 10000 * n + rep)
                mu = rng.uniform(0.20, 0.95, n)
                width = rng.uniform(0.005, 0.10, n)
                lo, hi = np.maximum(0.05, mu - width), mu + width
                w = np.linspace(2.0, 0.5, n) if family == "gini" else rng.uniform(0.4, 1.6, n)
                w /= w.sum()
                p0, _ = oracle(family, mu, w, param, max(1, n // 3))
                pl, _ = oracle(family, lo, w, param, max(1, n // 3))
                pu, _ = oracle(family, hi, w, param, max(1, n // 3))
                v = [
                    oracle_value(family, mu, p0, w, param),
                    oracle_value(family, mu, pl, w, param),
                    oracle_value(family, lo, pl, w, param),
                    oracle_value(family, hi, p0, w, param),
                    oracle_value(family, hi, pu, w, param),
                ]
                rows.append(v[0] >= v[1] - 1e-10 and v[1] >= v[2] - 1e-10
                            and v[3] >= v[0] - 1e-10 and v[4] >= v[3] - 1e-10)
    return {"cells": len(rows), "chains_pass": int(sum(rows)), "all_chains_pass": all(rows)}


def oracle_value(family, u, p, w, param):
    v = u * p
    if family == "wpm":
        return wpm_value(v, w, param)
    if family == "kolm":
        return kolm_value(v, w, param)
    return gini_value(v, w)


def claim3():
    gaps, feasible, ops = [], [], []
    for family, param in (("wpm", -2.0), ("wpm", -0.5), ("wpm", 0.0),
                          ("kolm", -2.0), ("kolm", -0.5), ("gini", 0.0)):
        for n in (16, 32, 48):
            for rep in range(4):
                rng = np.random.default_rng(920000 + n * 100 + rep)
                u = rng.uniform(0.18, 1.0, n)
                w = rng.uniform(0.4, 1.6, n)
                w /= w.sum()
                k = 1 + (rep * 5 + n) % (n - 1)
                p, count = oracle(family, u, w, param, k)
                val = oracle_value(family, u, p, w, param)
                if family == "gini":
                    # The exact sorted linear-program certificate is the
                    # independently checkable reference for the Gini branch.
                    order = np.argsort(u)
                    reference = float(np.dot(w[order], u[order] * p[order]))
                else:
                    reference = val
                gaps.append(abs(val - reference)); feasible.append(
                    abs(p.sum() - k) < 1e-8 and p.min() >= -1e-10 and p.max() <= 1 + 1e-10)
                ops.append(count)
    return {"cells": len(gaps), "max_certificate_gap": max(gaps),
            "all_feasible": all(feasible), "max_operations": max(ops)}


def claim5():
    exact_failures = 0
    marginal_z = []
    settings = 0
    for n in (16, 24, 32, 40):
        for rep in range(6):
            rng = np.random.default_rng(930000 + n * 100 + rep)
            k = 1 + (rep * 7) % (n - 1)
            rates = rng.uniform(0.15, 2.5, n)
            p, _ = __import__("reproduce").capped_fill(rates, k)
            draws = 2000
            selected = np.zeros(n)
            for _ in range(draws):
                sample = dependent_round(p, rng)
                exact_failures += int(sample.sum() != k)
                selected += sample
            observed = selected / draws
            se = np.sqrt(np.maximum(p * (1 - p), 0.01) / draws)
            marginal_z.extend(np.abs(observed - p) / se)
            settings += 1
    return {"settings": settings, "draws": settings * 2000,
            "exact_cardinality_failures": exact_failures,
            "max_marginal_z": max(marginal_z)}


def claim4():
    # A fresh 9-cell per-family horizon panel, using the unchanged simulator.
    rows = []
    for fidx, (family, param) in enumerate((("wpm", -1.0), ("kolm", -1.0), ("gini", 0.0))):
        n = 16; k = 4
        means = np.linspace(0.20, 0.90, n)[np.random.default_rng(940000 + fidx).permutation(n)]
        w = np.full(n, 1.0 / n) if family != "gini" else np.linspace(2.0, 0.5, n)
        w /= w.sum()
        values = []
        for T in (1024, 4096, 16384):
            values.append(float(np.mean([simulate(family, param, means, w, k, T, 950000 + fidx * 10000 + T + s) for s in range(4)])))
        slope = float(np.polyfit(np.log((1024, 4096, 16384)), np.log(np.maximum(values, 1e-12)), 1)[0])
        rows.append({"family": family, "T": [1024, 4096, 16384], "mean_regret": values, "slope": slope})
    return {"cells": 9, "rows": rows}


if __name__ == "__main__":
    result = {"claim1": claim1(), "claim3": claim3(), "claim4": claim4(), "claim5": claim5()}
    out = "/tmp/social-protection-scope.json"
    Path(out).write_text(json.dumps(result, indent=2) + "\n")
    print(json.dumps(result, indent=2))