"""F5-conf-02: Monte Carlo bias of OLS AR(1) coefficient vs Kendall/Marriott-Pope approximation. Run from workspace root: ~/wp29/venv/bin/python out/analysis.py """ import json, os import numpy as np import pandas as pd d = pd.read_csv("data/design.csv") p = dict(zip(d["parameter"], d["value"])) n = int(p["n"]); rho = float(p["rho"]); sigma = float(p["sigma"]) R = int(p["n_replications"]); seed = int(p["seed"]) issues = [] assert str(p["intercept"]).strip().lower() == "yes" assert str(p["initial_condition"]).strip().lower() == "stationary" if not (abs(rho) < 1 and n > 2 and R > 1 and sigma > 0): issues.append({"issue": "invalid design value", "handling": "none"}) rng = np.random.default_rng(seed) # Draw order: y_1 for all replications, then innovations eps_2..eps_n (R x (n-1)). y = np.empty((R, n)) y[:, 0] = rng.normal(0.0, sigma / np.sqrt(1 - rho**2), size=R) eps = rng.normal(0.0, sigma, size=(R, n - 1)) for t in range(1, n): y[:, t] = rho * y[:, t - 1] + eps[:, t - 1] # c = 0 x = y[:, :-1]; z = y[:, 1:] # regressor y_{t-1}, response y_t, t=2..n xd = x - x.mean(axis=1, keepdims=True) zd = z - z.mean(axis=1, keepdims=True) rho_hat = (xd * zd).sum(1) / (xd**2).sum(1) # OLS slope with intercept # sanity check vs. explicit lstsq on first replication X0 = np.column_stack([np.ones(n - 1), x[0]]) assert np.isclose(np.linalg.lstsq(X0, z[0], rcond=None)[0][1], rho_hat[0]) ok = np.isfinite(rho_hat) rh = rho_hat[ok] mean = float(rh.mean()); sd = float(rh.std(ddof=1)); se = sd / np.sqrt(rh.size) bias_an = -(1 + 3 * rho) / n bias_mc = mean - rho # For reference: same formula using the effective regression sample size n-1 bias_an_nm1 = -(1 + 3 * rho) / (n - 1) res = { "task_id": "F5-conf-02", "bias_analytical": bias_an, "bias_mc": bias_mc, "bias_mc_se": se, "rho_hat_mean": mean, "rho_hat_sd_mc": sd, "discrepancy_se_units": (bias_mc - bias_an) / se, "se_asymptotic": float(np.sqrt((1 - rho**2) / n)), "n_replications": R, "feasibility": "complete", "feasibility_reason": None, "data_issues": issues, "n_input_rows": {"design.csv": int(len(d))}, "n_analysed": int(rh.size), "random_seed": seed, # supplementary (not required) "bias_analytical_n_minus_1": bias_an_nm1, "discrepancy_se_units_n_minus_1": (bias_mc - bias_an_nm1) / se, "rho_hat_median": float(np.median(rh)), "share_rho_hat_below_rho": float((rh < rho).mean()), "relative_discrepancy": (bias_mc - bias_an) / abs(bias_an), } os.makedirs("out", exist_ok=True) with open("out/results.json", "w") as f: json.dump(res, f, indent=2) print(json.dumps(res, indent=2))