"""RQ2 (founds within N years) retrained on the symmetric landmark design. The shipped RQ2 stack was degenerate in production: p_within_3y and p_within_5y were exactly 1.0 for every scored author. Root cause is the same window asymmetry as RQ1: offset-truncated founders were paired with FULL-career non-founder profiles (data.load_rq2_train_dataset), so "profile smaller than a full career" itself separated the classes -- saturating the longer horizons. Here, for each horizon k the profile of EVERY person is truncated at (index_year - k): founding year for founders (label 1 = founds within k years), sampled pseudo-founding year for non-founders (label 0). Construction is identical across classes (src/symmetric.py). Rows whose truncated profile has no publications are dropped SYMMETRICALLY: they do not correspond to scoreable academics at prediction time, and one-sided empties were the original leak. The degeneracy check at the end verifies the predicted p_within_k distribution actually discriminates (the shipped model's failure mode). Run with: conda run -n potential python experiments/run_rq2_symmetric.py """ import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import joblib import numpy as np import pandas as pd from src import config, evaluate, models, splits, symmetric RESULTS_DIR = config.RESULTS_DIR / "rq2_symmetric" MODELS_DIR = RESULTS_DIR / "models" HORIZONS = [1, 3, 5] def main(): RESULTS_DIR.mkdir(parents=True, exist_ok=True) MODELS_DIR.mkdir(parents=True, exist_ok=True) summary_rows = [] for k in HORIZONS: print(f"\n=== Horizon: founds within {k} years ===") feats, _ = symmetric.build_symmetric_dataset(offset=k) nonempty = feats["pubs_at_index"] > 0 dropped = (~nonempty).groupby(feats[config.LABEL_COLUMN]).sum() print(f" empty-profile rows dropped: founders={int(dropped.get(1, 0))} " f"non-founders={int(dropped.get(0, 0))}") feats = feats[nonempty].reset_index(drop=True) X, y, groups = symmetric.build_matrix(feats, include_institution=True) print(f" rows={len(X)} founders={int(y.sum())}") idx_train, idx_test, _, _ = splits.stratified_holdout(X.index.to_series(), y, test_size=0.2) idx_train, idx_test = idx_train.values, idx_test.values X_train, X_test = X.loc[idx_train], X.loc[idx_test] y_train, y_test = y.loc[idx_train], y.loc[idx_test] spw = (y_train == 0).sum() / (y_train == 1).sum() rows, fitted = [], {} for name, model in models.advanced_models(scale_pos_weight=spw).items(): if name == "svm_rbf": continue model, metrics, pred, proba = evaluate.fit_and_evaluate(model, X_train, y_train, X_test, y_test) rows.append({"model": name, **metrics}) fitted[name] = (model, proba) table = evaluate.results_table(rows, sort_by="pr_auc") print(table) evaluate.save_table(table, RESULTS_DIR / f"horizon{k}_model_comparison.csv", RESULTS_DIR / f"horizon{k}_model_comparison.md") best = table.index[0] best_model, best_proba = fitted[best] joblib.dump(best_model, MODELS_DIR / f"p_within_{k}y_{best}.joblib") # Degeneracy check: the shipped model returned 1.0 for everyone at k=3,5. q = np.quantile(best_proba, [0.05, 0.25, 0.5, 0.75, 0.95]) frac_saturated = float((best_proba > 0.99).mean()) print(f" p_within_{k}y test-set quantiles p5..p95: {np.round(q, 3)} | frac >0.99: {frac_saturated:.3f}") summary_rows.append({ "horizon": k, "best_model": best, "rows": len(X), "roc_auc": table.iloc[0]["roc_auc"], "pr_auc": table.iloc[0]["pr_auc"], "p5": q[0], "p50": q[2], "p95": q[4], "frac_above_0.99": frac_saturated, }) ref = pd.DataFrame({ "quantile": np.linspace(0, 1, 1001), "score": np.quantile(best_model.predict_proba(X)[:, 1], np.linspace(0, 1, 1001)), }) ref.to_csv(MODELS_DIR / f"p_within_{k}y_reference_quantiles.csv", index=False) summary = pd.DataFrame(summary_rows).set_index("horizon") print("\n=== Horizon summary ===") print(summary) evaluate.save_table(summary, RESULTS_DIR / "horizon_summary.csv", RESULTS_DIR / "horizon_summary.md") print(f"\nAll results written to {RESULTS_DIR}") if __name__ == "__main__": main()