"""F5-conf-03: counterexample to proposition A and numerical check of B. Run from workspace root: ~/wp29/venv/bin/python out/analysis.py Reads only data/; writes out/results.json, out/counterexample.csv, out/exclusions.csv. """ import json, os import numpy as np, pandas as pd from scipy import stats ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) D, O = os.path.join(ROOT, "data"), os.path.join(ROOT, "out") props = pd.read_csv(os.path.join(D, "propositions.csv")) design = pd.read_csv(os.path.join(D, "design.csv")) par = dict(zip(design["parameter"], design["value"])) seed, n = int(par["seed"]), int(par["n"]) data_issues = [] if set(props["id"]) != {"A", "B"} or props["id"].duplicated().any(): data_issues.append({"issue": "unexpected proposition ids", "handling": "reported"}) rng = np.random.default_rng(seed) # Counterexample construction (drawn first from the seeded generator): # X ~ N(0,1), S ~ Rademacher independent of X, Y = S*X. x = rng.standard_normal(n) s = rng.choice(np.array([-1.0, 1.0]), size=n) y = s * x z = x + y def ks_fit(v): m, sd = v.mean(), v.std(ddof=1) return float(stats.kstest(v, "norm", args=(m, sd)).statistic) def ks_std(v): # vs N(0,1) exactly (theoretical marginal) return float(stats.kstest(v, "norm").statistic) cv = { "construction": "X ~ N(0,1); S ~ Rademacher (P(S=1)=P(S=-1)=1/2) independent of X; Y = S*X. " "Then Y ~ N(0,1) but X+Y = (1+S)X equals 0 w.p. 1/2 and 2X otherwise " "(mixture of a point mass at 0 and N(0,4)), which is not normal.", "n": int(n), "seed": seed, "mean_x": float(x.mean()), "sd_x": float(x.std(ddof=1)), "mean_y": float(y.mean()), "sd_y": float(y.std(ddof=1)), "mean_sum": float(z.mean()), "sd_sum": float(z.std(ddof=1)), "ks_sum_vs_normal": ks_fit(z), # supplementary diagnostics "ks_x_vs_std_normal": ks_std(x), "ks_y_vs_std_normal": ks_std(y), "frac_sum_exactly_zero": float(np.mean(z == 0.0)), "ks_critical_5pct_approx": float(1.358 / np.sqrt(n)), } pd.DataFrame({"x": x, "y": y}).to_csv(os.path.join(O, "counterexample.csv"), index=False) # Baseline (proposition B): independent X, Y ~ N(0,1), drawn after the counterexample xb = rng.standard_normal(n); yb = rng.standard_normal(n); zb = xb + yb baseline = {"construction": "X, Y iid N(0,1), drawn from the same generator after the counterexample draws", "mean_sum": float(zb.mean()), "sd_sum": float(zb.std(ddof=1)), "ks_sum_vs_normal": ks_fit(zb)} res = { "task_id": "F5-conf-03", "statement_true": False, "counterexample_provided": True, "counterexample_values": cv, "statement_b_true": True, "feasibility": "complete", "feasibility_reason": None, "data_issues": data_issues, "n_input_rows": {"propositions.csv": int(len(props)), "design.csv": int(len(design))}, "n_analysed": int(n), "random_seed": seed, "baseline_independent_sum": baseline, } with open(os.path.join(O, "results.json"), "w") as f: json.dump(res, f, indent=2) pd.DataFrame([{"step": 0, "rule": "none (no filtering; simulated sample of size n)", "n_before": n, "n_after": n, "n_removed": 0, "rationale": "design-specified simulation, nothing excluded"}] ).to_csv(os.path.join(O, "exclusions.csv"), index=False) print(json.dumps({k: res[k] for k in ["statement_true", "statement_b_true", "n_analysed"]}), "KS sum:", cv["ks_sum_vs_normal"], "baseline KS:", baseline["ks_sum_vs_normal"])