adjusted the profiling pipelines: multipliers & (potential) founders, explorative analyses
LocationBasedServices/smartguide/pipeline/head This commit looks good
LocationBasedServices/smartguide/pipeline/head This commit looks good
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
"""Founder-only sequence modeling: can the *shape* of pre-founding academic/
|
||||
patent/grant activity (not just career totals) predict how early or late in a
|
||||
founder's career they started their company?
|
||||
|
||||
Task: predict `founding_age = founded_on (year) - first_publication_year` from
|
||||
the chronological pre-founding window `hist_{pub,pat,grant}_FN-20..FN-1`,
|
||||
restricted to founders where founding_age <= 20 (see `sequence_data.py` for why
|
||||
that restriction is necessary -- the window can't resolve longer careers).
|
||||
|
||||
Compared approaches (weakest to strongest, to see whether the extra sequence-
|
||||
modeling complexity actually earns its keep over simpler baselines):
|
||||
1. naive_first_activity: deterministic "20 - (leading zero years)" heuristic,
|
||||
no fitting at all.
|
||||
2. aggregate_rf: RandomForestRegressor on hand-crafted summary stats (total
|
||||
activity / nonzero-year count / last-3-years activity per channel).
|
||||
3. lstm: sequence model over the raw chronological window.
|
||||
4. transformer: small Transformer encoder over the same window.
|
||||
|
||||
Run with: conda run -n potential python experiments/run_sequence_founders.py
|
||||
"""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.ensemble import RandomForestRegressor
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
from src import config, evaluate, sequence_data, sequence_models
|
||||
|
||||
RESULTS_DIR = config.RESULTS_DIR / "sequence_founders"
|
||||
|
||||
|
||||
def main():
|
||||
print("Building founder-only sequence dataset...")
|
||||
sequences, founding_age, meta = sequence_data.build_founder_sequence_dataset()
|
||||
print(f" final dataset: n={len(sequences)} founding_age range "
|
||||
f"[{founding_age.min():.0f}, {founding_age.max():.0f}] mean={founding_age.mean():.2f}\n")
|
||||
|
||||
idx = np.arange(len(sequences))
|
||||
idx_train, idx_test = train_test_split(idx, test_size=0.2, random_state=config.RANDOM_STATE)
|
||||
X_train, X_test = sequences[idx_train], sequences[idx_test]
|
||||
y_train, y_test = founding_age[idx_train], founding_age[idx_test]
|
||||
|
||||
rows = []
|
||||
curves = {}
|
||||
|
||||
print("1. Naive first-activity heuristic (no fitting)...")
|
||||
pred = sequence_data.naive_first_activity_baseline(X_test)
|
||||
metrics = evaluate.regression_metrics(y_test, pred)
|
||||
rows.append({"model": "naive_first_activity", **metrics})
|
||||
curves["naive_first_activity"] = (y_test, pred)
|
||||
print(f" MAE={metrics['mae']:.3f} RMSE={metrics['rmse']:.3f} R2={metrics['r2']:.3f}")
|
||||
|
||||
print("2. Aggregate-feature RandomForestRegressor...")
|
||||
agg_train = sequence_data.aggregate_features(X_train)
|
||||
agg_test = sequence_data.aggregate_features(X_test)
|
||||
rf = RandomForestRegressor(n_estimators=400, max_depth=8, random_state=config.RANDOM_STATE, n_jobs=-1)
|
||||
rf.fit(agg_train, y_train)
|
||||
pred = rf.predict(agg_test)
|
||||
metrics = evaluate.regression_metrics(y_test, pred)
|
||||
rows.append({"model": "aggregate_rf", **metrics})
|
||||
curves["aggregate_rf"] = (y_test, pred)
|
||||
print(f" MAE={metrics['mae']:.3f} RMSE={metrics['rmse']:.3f} R2={metrics['r2']:.3f}")
|
||||
|
||||
print("3. LSTM sequence regressor...")
|
||||
lstm = sequence_models.LSTMRegressor(hidden_size=64, num_layers=1, dropout=0.2)
|
||||
lstm.fit(X_train, y_train)
|
||||
pred = lstm.predict(X_test)
|
||||
metrics = evaluate.regression_metrics(y_test, pred)
|
||||
rows.append({"model": "lstm", **metrics})
|
||||
curves["lstm"] = (y_test, pred)
|
||||
print(f" MAE={metrics['mae']:.3f} RMSE={metrics['rmse']:.3f} R2={metrics['r2']:.3f}")
|
||||
|
||||
print("4. Transformer sequence regressor...")
|
||||
transformer = sequence_models.TransformerRegressor(d_model=32, n_heads=4, n_layers=2, dropout=0.2)
|
||||
transformer.fit(X_train, y_train)
|
||||
pred = transformer.predict(X_test)
|
||||
metrics = evaluate.regression_metrics(y_test, pred)
|
||||
rows.append({"model": "transformer", **metrics})
|
||||
curves["transformer"] = (y_test, pred)
|
||||
print(f" MAE={metrics['mae']:.3f} RMSE={metrics['rmse']:.3f} R2={metrics['r2']:.3f}")
|
||||
|
||||
table = pd.DataFrame(rows).set_index("model").sort_values("mae")
|
||||
print("\n", table)
|
||||
evaluate.save_table(table, RESULTS_DIR / "sequence_founders_comparison.csv", RESULTS_DIR / "sequence_founders_comparison.md")
|
||||
evaluate.plot_predicted_vs_actual(curves, RESULTS_DIR / "plots" / "predicted_vs_actual.png")
|
||||
|
||||
print(f"\nAll sequence-founders results written to {RESULTS_DIR}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user