"""Regenerates out/results.json and out/counterexample.csv from data/ (deterministic).""" 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")) issues = [] if set(props["id"]) != {"A", "B"} or len(props) != 2: issues.append({"issue": "propositions.csv does not contain exactly ids A and B", "handling": "used ids present"}) dv = dict(zip(design["parameter"], design["value"])) seed, n = int(dv["seed"]), int(dv["n"]) # Counterexample to A: X ~ N(0,1), S independent Rademacher (+/-1 w.p. 1/2), Y = S*X. # Y ~ N(0,1) by symmetry of N(0,1); X+Y = (1+S)X equals 0 w.p. 1/2 and 2X otherwise. rng = np.random.default_rng(seed) x = rng.standard_normal(n) s = rng.choice(np.array([-1.0, 1.0]), size=n) y = s * x pd.DataFrame({"x": x, "y": y}).to_csv(os.path.join(O, "counterexample.csv"), index=False) z = x + y ks = float(stats.kstest(z, "norm", args=(z.mean(), z.std(ddof=1))).statistic) # auxiliary checks (reported in report, not required keys) aux = { "ks_x_vs_N01": float(stats.kstest(x, "norm").statistic), "ks_y_vs_N01": float(stats.kstest(y, "norm").statistic), "frac_sum_exactly_zero": float(np.mean(z == 0.0)), "corr_xy": float(np.corrcoef(x, y)[0, 1]), } res = { "task_id": "F5-conf-03", "statement_true": False, "counterexample_provided": True, "counterexample_values": { "construction": "X ~ N(0,1); S independent of X with P(S=1)=P(S=-1)=1/2; Y = S*X. " "Then Y ~ N(0,1) and X+Y = (1+S)X has an atom of mass 1/2 at 0 (and equals 2X otherwise), so it is not normal.", "n": 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, }, "statement_b_true": True, "feasibility": "complete", "feasibility_reason": None, "data_issues": issues, "n_input_rows": {"propositions.csv": int(len(props)), "design.csv": int(len(design))}, "n_analysed": n, "random_seed": seed, "auxiliary_checks": aux, } with open(os.path.join(O, "results.json"), "w") as f: json.dump(res, f, indent=2) print(json.dumps(res, indent=2))