1"""Replication script for F2-smoke-01. Run from workspace root."""2import json, os3import numpy as np, pandas as pd, statsmodels.api as sm4from scipy import stats56d = pd.read_csv("data/panel.csv")7n_in = len(d)8issues = []9na = d[["firm_id", "x", "y"]].isna().any(axis=1)10if na.any():11 issues.append({"issue": f"{int(na.sum())} rows with missing values", "handling": "dropped (complete-case)"})12 d = d[~na]13dup = d.duplicated()14if dup.any():15 issues.append({"issue": f"{int(dup.sum())} exact duplicate rows", "handling": "retained (cannot distinguish from genuine repeats); flagged"})16fin = np.isfinite(d[["x", "y"]]).all(axis=1)17if not fin.all():18 issues.append({"issue": f"{int((~fin).sum())} non-finite values", "handling": "dropped"})19 d = d[fin]2021X = sm.add_constant(d["x"].to_numpy())22y = d["y"].to_numpy()23ols = sm.OLS(y, X).fit()24cl = sm.OLS(y, X).fit(cov_type="cluster", cov_kwds={"groups": d["firm_id"].to_numpy(), "use_correction": True})25G = int(d["firm_id"].nunique())26b, se_c = float(cl.params[1]), float(cl.bse[1])27tcrit = stats.t.ppf(0.975, G - 1) # t with G-1 df for clustered inference2829res = {30 "task_id": "F2-smoke-01",31 "beta_hat": b,32 "alpha_hat": float(cl.params[0]),33 "se_ols": float(ols.bse[1]),34 "se_cluster": se_c,35 "beta_ci_low": b - tcrit * se_c,36 "beta_ci_high": b + tcrit * se_c,37 "n_analysed": int(len(d)),38 "n_clusters": G,39 "feasibility": "complete",40 "feasibility_reason": None,41 "data_issues": issues,42 "n_input_rows": {"panel.csv": int(n_in)},43 "random_seed": None,44 # supplementary diagnostics45 "se_cluster_ratio_to_ols": se_c / float(ols.bse[1]),46 "ci_t_df": G - 1,47 "beta_ci_low_normal": b - 1.959963985 * se_c,48 "beta_ci_high_normal": b + 1.959963985 * se_c,49 "beta_ci_low_ols": float(ols.conf_int()[1, 0]),50 "beta_ci_high_ols": float(ols.conf_int()[1, 1]),51}52os.makedirs("out", exist_ok=True)53with open("out/results.json", "w") as f:54 json.dump(res, f, indent=2)55print(json.dumps(res, indent=2))56