adjusted the profiling pipelines: multipliers & (potential) founders, explorative analyses
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
2026-08-27 13:53:10 +02:00
parent 88e0b44d54
commit c26c3f33f5
154 changed files with 18795 additions and 0 deletions
@@ -0,0 +1,69 @@
"""Cox proportional hazards survival analysis for RQ2 (time-to-founding).
Complements the fixed-horizon classifiers in run_rq2.py: instead of one yes/no
label at a chosen cutoff, this fits a single time-to-event model and reads off
P(founds within k years | features) for any k, correctly treating non-founders
still under observation as censored rather than as permanent negatives.
Simplification: covariates are the same full-career aggregates used for RQ1
(observed up to the founding year for founders, up to the last observed year for
non-founders) rather than a fully time-varying design. A stricter version would
use a counting-process (start, stop, event) long format with the half-profile
snapshots as time-varying covariates; noted as future work in the README.
"""
import numpy as np
import pandas as pd
from lifelines import CoxPHFitter
from lifelines.utils import concordance_index
from . import config
def build_survival_frame(df_raw):
df = df_raw.copy()
is_founder = df[config.LABEL_COLUMN] == 1
duration = np.where(
is_founder,
df["founded_on"] - df["first_publication_year"],
df["last_publication_year"] - df["first_publication_year"],
)
event = is_founder.astype(int)
frame = pd.DataFrame({"duration": duration, "event": event})
numeric_cols = [c for c in config.RQ1_NUMERIC_COLUMNS if not c.endswith("_year")]
numeric = df[numeric_cols].apply(pd.to_numeric, errors="coerce").fillna(0)
for col in config.RQ1_LOG_COLUMNS:
if col in numeric.columns:
numeric[col] = np.log1p(numeric[col].clip(lower=0))
institution = pd.get_dummies(df["institution"], prefix="institution", drop_first=True)
frame = pd.concat([frame, numeric.reset_index(drop=True), institution.reset_index(drop=True)], axis=1)
frame = frame[frame["duration"] > 0]
frame = frame.replace([np.inf, -np.inf], np.nan).dropna()
return frame.reset_index(drop=True)
def fit_coxph(train_frame, penalizer=0.1):
cph = CoxPHFitter(penalizer=penalizer)
cph.fit(train_frame, duration_col="duration", event_col="event")
return cph
def evaluate_concordance(cph, test_frame):
partial_hazard = cph.predict_partial_hazard(test_frame)
return concordance_index(test_frame["duration"], -partial_hazard, test_frame["event"])
def probability_of_founding_within(cph, frame, years):
survival = cph.predict_survival_function(frame, times=[years])
return 1 - survival.loc[years].values
def landmark_labels(frame, years):
"""Rows with enough follow-up to know the true within-`years` outcome, plus
the corresponding binary label, avoiding immortal-time bias."""
eligible = ((frame["event"] == 1) & (frame["duration"] <= years)) | (frame["duration"] >= years)
label = ((frame["event"] == 1) & (frame["duration"] <= years)).astype(int)
return eligible, label