Skip to main content
src/ml/train.py · chapter The big-move model, file 3 · 265 lines · open in the repository

What it does

Train and test the big-move model with Spark MLlib (decision 0007). The question: from what the stream knows about a retailer today, can a model tell which retailers will make a big move tomorrow (tomorrow’s |z_ar| >= ABNORMAL_RETURN_Z)? Daily returns are mostly noise, so accuracy is the wrong yardstick (always saying “no” is right ~95% of the time). The model is judged on how well it ranks tomorrow’s big movers: ROC-AUC (0.5 = chance), PR-AUC against the base rate, and the hit rate of the days it would flag. Steps:
  1. Read the streaming job’s daily output, add the features (src/ml/features.py) and the label (tomorrow’s abnormal_return), and drop days the stream had no model for yet.
  2. Split by time (common.study): train on the years up to ML_TRAIN_END_YEAR, choose the model on ML_VALIDATION_YEAR, report on ML_TEST_START_YEAR onwards. Never a random split, which would let the model learn from days after the ones it is tested on. The split is also purged: a row whose “tomorrow” falls in the next period is dropped.
  3. Try two MLlib pipelines on the validation year, each with a small grid: logistic regression on scaled features (a transparent baseline) and gradient-boosted trees (non-linear effects, interactions). Big moves are rare, so each class gets equal total weight.
  4. Refit the best on train + validation and score the test years, next to:
    • the same model without the attention features: what does Wikipedia add?
    • a baseline with no model that ranks by today’s |z_ar|: do big moves just cluster?
  5. Save the model for the streaming job with the probability cut-off that flags the top ML_ALERT_TOP_SHARE of stock-days, and write the tables and charts for the report.
Run from anywhere after the streaming job:
Output: data/models/big_move/ (+ big_move.json), and in data/results/: ml_metrics.csv, ml_test_predictions.parquet, ml_feature_importance.csv, charts/ml_*.png

What is inside

  • METRICS · line 58
  • TEST_PREDICTIONS · line 59
  • IMPORTANCE · line 60
  • CHARTS_DIR · line 61
  • CANDIDATES · line 65: Small grids: the aim is a fair comparison of two kinds of model, not squeezing out the last 0.001 of AUC on one year of validation data.
  • GBT_TREES · line 69
  • SEED · line 70: fixed so a rerun gives the same trees
  • pipeline(kind, params, features) · line 73: An MLlib pipeline: features -> vector -> (scale) -> classifier.
  • with_weights(df) · line 89: Weight each class to the same total, so rare big moves are not ignored.
  • score(model, df) · line 95: The model’s probability of a big move tomorrow, as a plain column.
  • evaluate(scored, score_col, model, params, features, split) · line 100: One ml_metrics row: AUCs from MLlib, precision of the top ML_ALERT_TOP_SHARE in pandas.
  • importances(model, kind, features) · line 115: How much each feature drives the model: tree importances, or |scaled coefficient|.
  • curve_points(y, s) · line 125: ROC (false and true positive rates) and precision-recall points for one score.
  • charts(curves, base_rate, imp) · line 135: ml_roc_pr.png (the test years) and ml_feature_importance.png.
  • main() · line 165

Connections

Uses: src/common/contracts.py, src/common/settings.py, src/common/study.py, src/eventstudy/car.py, src/ml/features.py

Source

src/ml/train.py