"""Replication script for F2-smoke-01. Run from workspace root.""" import json, os import numpy as np, pandas as pd, statsmodels.api as sm from scipy import stats d = pd.read_csv("data/panel.csv") n_in = len(d) issues = [] na = d[["firm_id", "x", "y"]].isna().any(axis=1) if na.any(): issues.append({"issue": f"{int(na.sum())} rows with missing values", "handling": "dropped (complete-case)"}) d = d[~na] dup = d.duplicated() if dup.any(): issues.append({"issue": f"{int(dup.sum())} exact duplicate rows", "handling": "retained (cannot distinguish from genuine repeats); flagged"}) fin = np.isfinite(d[["x", "y"]]).all(axis=1) if not fin.all(): issues.append({"issue": f"{int((~fin).sum())} non-finite values", "handling": "dropped"}) d = d[fin] X = sm.add_constant(d["x"].to_numpy()) y = d["y"].to_numpy() ols = sm.OLS(y, X).fit() cl = sm.OLS(y, X).fit(cov_type="cluster", cov_kwds={"groups": d["firm_id"].to_numpy(), "use_correction": True}) G = int(d["firm_id"].nunique()) b, se_c = float(cl.params[1]), float(cl.bse[1]) tcrit = stats.t.ppf(0.975, G - 1) # t with G-1 df for clustered inference res = { "task_id": "F2-smoke-01", "beta_hat": b, "alpha_hat": float(cl.params[0]), "se_ols": float(ols.bse[1]), "se_cluster": se_c, "beta_ci_low": b - tcrit * se_c, "beta_ci_high": b + tcrit * se_c, "n_analysed": int(len(d)), "n_clusters": G, "feasibility": "complete", "feasibility_reason": None, "data_issues": issues, "n_input_rows": {"panel.csv": int(n_in)}, "random_seed": None, # supplementary diagnostics "se_cluster_ratio_to_ols": se_c / float(ols.bse[1]), "ci_t_df": G - 1, "beta_ci_low_normal": b - 1.959963985 * se_c, "beta_ci_high_normal": b + 1.959963985 * se_c, "beta_ci_low_ols": float(ols.conf_int()[1, 0]), "beta_ci_high_ols": float(ols.conf_int()[1, 1]), } 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))