SPB Git

spb/wp29_uqo

Public
3.4 KB · 79 lines python
1"""F5-conf-03: counterexample to proposition A and numerical check of B.2Run from workspace root: ~/wp29/venv/bin/python out/analysis.py3Reads only data/; writes out/results.json, out/counterexample.csv, out/exclusions.csv.4"""5import json, os6import numpy as np, pandas as pd7from scipy import stats89ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))10D, O = os.path.join(ROOT, "data"), os.path.join(ROOT, "out")1112props = pd.read_csv(os.path.join(D, "propositions.csv"))13design = pd.read_csv(os.path.join(D, "design.csv"))14par = dict(zip(design["parameter"], design["value"]))15seed, n = int(par["seed"]), int(par["n"])1617data_issues = []18if set(props["id"]) != {"A", "B"} or props["id"].duplicated().any():19    data_issues.append({"issue": "unexpected proposition ids", "handling": "reported"})2021rng = np.random.default_rng(seed)22# Counterexample construction (drawn first from the seeded generator):23# X ~ N(0,1), S ~ Rademacher independent of X, Y = S*X.24x = rng.standard_normal(n)25s = rng.choice(np.array([-1.0, 1.0]), size=n)26y = s * x27z = x + y2829def ks_fit(v):30    m, sd = v.mean(), v.std(ddof=1)31    return float(stats.kstest(v, "norm", args=(m, sd)).statistic)3233def ks_std(v):  # vs N(0,1) exactly (theoretical marginal)34    return float(stats.kstest(v, "norm").statistic)3536cv = {37    "construction": "X ~ N(0,1); S ~ Rademacher (P(S=1)=P(S=-1)=1/2) independent of X; Y = S*X. "38                    "Then Y ~ N(0,1) but X+Y = (1+S)X equals 0 w.p. 1/2 and 2X otherwise "39                    "(mixture of a point mass at 0 and N(0,4)), which is not normal.",40    "n": int(n), "seed": seed,41    "mean_x": float(x.mean()), "sd_x": float(x.std(ddof=1)),42    "mean_y": float(y.mean()), "sd_y": float(y.std(ddof=1)),43    "mean_sum": float(z.mean()), "sd_sum": float(z.std(ddof=1)),44    "ks_sum_vs_normal": ks_fit(z),45    # supplementary diagnostics46    "ks_x_vs_std_normal": ks_std(x), "ks_y_vs_std_normal": ks_std(y),47    "frac_sum_exactly_zero": float(np.mean(z == 0.0)),48    "ks_critical_5pct_approx": float(1.358 / np.sqrt(n)),49}50pd.DataFrame({"x": x, "y": y}).to_csv(os.path.join(O, "counterexample.csv"), index=False)5152# Baseline (proposition B): independent X, Y ~ N(0,1), drawn after the counterexample53xb = rng.standard_normal(n); yb = rng.standard_normal(n); zb = xb + yb54baseline = {"construction": "X, Y iid N(0,1), drawn from the same generator after the counterexample draws",55            "mean_sum": float(zb.mean()), "sd_sum": float(zb.std(ddof=1)),56            "ks_sum_vs_normal": ks_fit(zb)}5758res = {59    "task_id": "F5-conf-03",60    "statement_true": False,61    "counterexample_provided": True,62    "counterexample_values": cv,63    "statement_b_true": True,64    "feasibility": "complete",65    "feasibility_reason": None,66    "data_issues": data_issues,67    "n_input_rows": {"propositions.csv": int(len(props)), "design.csv": int(len(design))},68    "n_analysed": int(n),69    "random_seed": seed,70    "baseline_independent_sum": baseline,71}72with open(os.path.join(O, "results.json"), "w") as f:73    json.dump(res, f, indent=2)74pd.DataFrame([{"step": 0, "rule": "none (no filtering; simulated sample of size n)", "n_before": n,75               "n_after": n, "n_removed": 0, "rationale": "design-specified simulation, nothing excluded"}]76             ).to_csv(os.path.join(O, "exclusions.csv"), index=False)77print(json.dumps({k: res[k] for k in ["statement_true", "statement_b_true", "n_analysed"]}),78      "KS sum:", cv["ks_sum_vs_normal"], "baseline KS:", baseline["ks_sum_vs_normal"])79
Files · 152a55b8fa2830f50fa35e5aed99c0037f713345
Menu

Theme