Initial release: sf-ml-baseline v0.1 (LightGBM + XGBoost + CatBoost ensemble)
Browse files- .gitattributes +6 -0
- LICENSE +52 -0
- README.md +169 -0
- sf_ml_baseline.py +142 -0
- weights/cat_v1_t1_seed137.cbm +3 -0
- weights/cat_v1_t1_seed2026.cbm +3 -0
- weights/cat_v1_t1_seed42.cbm +3 -0
- weights/cat_v2_t4_seed137.cbm +3 -0
- weights/cat_v2_t4_seed2026.cbm +3 -0
- weights/cat_v2_t4_seed42.cbm +3 -0
- weights/lgbm_v1_t1_seed137.txt +0 -0
- weights/lgbm_v1_t1_seed2026.txt +0 -0
- weights/lgbm_v1_t1_seed42.txt +0 -0
- weights/lgbm_v2_t4_seed137.txt +0 -0
- weights/lgbm_v2_t4_seed2026.txt +0 -0
- weights/lgbm_v2_t4_seed42.txt +0 -0
- weights/xgb_v1_t1_seed137.json +0 -0
- weights/xgb_v1_t1_seed2026.json +0 -0
- weights/xgb_v1_t1_seed42.json +0 -0
- weights/xgb_v2_t4_seed137.json +0 -0
- weights/xgb_v2_t4_seed2026.json +0 -0
- weights/xgb_v2_t4_seed42.json +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,9 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
weights/cat_v1_t1_seed137.cbm filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
weights/cat_v1_t1_seed2026.cbm filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
weights/cat_v1_t1_seed42.cbm filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
weights/cat_v2_t4_seed137.cbm filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
weights/cat_v2_t4_seed2026.cbm filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
weights/cat_v2_t4_seed42.cbm filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Model Weights License — sf-ml-baseline v0.1
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2026 SimpleFunctions (simplefunctions.dev)
|
| 4 |
+
|
| 5 |
+
Licensed under the Creative Commons Attribution 4.0 International License
|
| 6 |
+
(CC-BY-4.0), the full text of which is at:
|
| 7 |
+
https://creativecommons.org/licenses/by/4.0/legalcode
|
| 8 |
+
|
| 9 |
+
In addition to the CC-BY-4.0 requirements, the following SimpleFunctions
|
| 10 |
+
Attribution Addendum applies:
|
| 11 |
+
|
| 12 |
+
1. Attribution. You MUST retain a readable credit to SimpleFunctions
|
| 13 |
+
whenever you distribute, serve, or display predictions generated by
|
| 14 |
+
these weights, in any form.
|
| 15 |
+
|
| 16 |
+
Acceptable credit formats include, but are not limited to:
|
| 17 |
+
(a) "Powered by sf-ml-baseline (SimpleFunctions, simplefunctions.dev)"
|
| 18 |
+
(b) "Forecast by sf-ml-baseline — https://simplefunctions.dev/opensource/sf-ml-baseline"
|
| 19 |
+
(c) "SimpleFunctions sf-ml-baseline v0.1" in a documentation footnote,
|
| 20 |
+
`X-Attribution` HTTP header, or equivalent machine-readable field.
|
| 21 |
+
|
| 22 |
+
2. Derived weights. If you fine-tune, distill, or otherwise derive new
|
| 23 |
+
weights from these, you MUST:
|
| 24 |
+
(a) make the same CC-BY-4.0 license apply to the derivative,
|
| 25 |
+
(b) prefix the derivative model name with "sf-ml-baseline-" (e.g.,
|
| 26 |
+
"sf-ml-baseline-v0.2-crypto-specialist"), and
|
| 27 |
+
(c) document in the derivative's README the training data,
|
| 28 |
+
procedure, and how it differs from upstream.
|
| 29 |
+
|
| 30 |
+
3. Attribution does not grant endorsement. Your use of the name
|
| 31 |
+
"SimpleFunctions" or "sf-ml-baseline" does not imply endorsement of
|
| 32 |
+
your product, service, or paper by SimpleFunctions.
|
| 33 |
+
|
| 34 |
+
4. Non-warranty. THE WEIGHTS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF
|
| 35 |
+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
| 36 |
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
| 37 |
+
NONINFRINGEMENT. SIMPLEFUNCTIONS DISCLAIMS ALL LIABILITY FOR ANY
|
| 38 |
+
PREDICTION, DECISION, OR OUTCOME DERIVED FROM USE OF THESE WEIGHTS,
|
| 39 |
+
INCLUDING BUT NOT LIMITED TO FINANCIAL LOSS FROM TRADING DECISIONS.
|
| 40 |
+
|
| 41 |
+
5. Training data. These weights were trained on SimpleFunctions'
|
| 42 |
+
public-domain prediction-market indicator dataset, itself released
|
| 43 |
+
under CC-BY-4.0 per the SimpleFunctions Terms of Service §13.
|
| 44 |
+
See https://simplefunctions.dev/data-license for details.
|
| 45 |
+
|
| 46 |
+
6. Responsible use. These weights are intended for academic research,
|
| 47 |
+
software integration, and personal analysis. Do NOT use them to
|
| 48 |
+
provide unlicensed financial advice, market manipulation, or
|
| 49 |
+
deceptive trading signals. You are responsible for all use.
|
| 50 |
+
|
| 51 |
+
Contact: patrick@simplefunctions.dev
|
| 52 |
+
License questions: see https://simplefunctions.dev/terms
|
README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
license_name: cc-by-4-0-with-sf-attribution
|
| 4 |
+
license_link: LICENSE
|
| 5 |
+
language:
|
| 6 |
+
- en
|
| 7 |
+
library_name: lightgbm
|
| 8 |
+
tags:
|
| 9 |
+
- prediction-markets
|
| 10 |
+
- forecasting
|
| 11 |
+
- calibration
|
| 12 |
+
- brier
|
| 13 |
+
- tabular-classification
|
| 14 |
+
- gradient-boosting
|
| 15 |
+
- lightgbm
|
| 16 |
+
- xgboost
|
| 17 |
+
- catboost
|
| 18 |
+
- time-series
|
| 19 |
+
- kalshi
|
| 20 |
+
- polymarket
|
| 21 |
+
- simplefunctions
|
| 22 |
+
pipeline_tag: tabular-classification
|
| 23 |
+
model-index:
|
| 24 |
+
- name: sf-ml-baseline-v0.1
|
| 25 |
+
results:
|
| 26 |
+
- task:
|
| 27 |
+
type: tabular-classification
|
| 28 |
+
name: 24h direction forecast (V1 x T1)
|
| 29 |
+
metrics:
|
| 30 |
+
- type: brier
|
| 31 |
+
value: 0.2294
|
| 32 |
+
name: Brier score
|
| 33 |
+
- type: brier_improvement
|
| 34 |
+
value: 0.0206
|
| 35 |
+
name: Improvement vs coinflip baseline (0.2500)
|
| 36 |
+
- task:
|
| 37 |
+
type: tabular-classification
|
| 38 |
+
name: 24h resolution forecast (V2 x T4)
|
| 39 |
+
metrics:
|
| 40 |
+
- type: brier
|
| 41 |
+
value: 0.1681
|
| 42 |
+
name: Brier score (XGBoost)
|
| 43 |
+
- type: brier_improvement
|
| 44 |
+
value: 0.0086
|
| 45 |
+
name: Improvement vs market-price/100 baseline (0.1767)
|
| 46 |
+
---
|
| 47 |
+
|
| 48 |
+
# sf-ml-baseline v0.1
|
| 49 |
+
|
| 50 |
+
**What it is**: gradient-boosted tree ensembles that predict prediction-market outcomes from engineered microstructure features.
|
| 51 |
+
|
| 52 |
+
**Published**: 2026-04-19 (initial, time-capsule — see "Retrain plan" below).
|
| 53 |
+
**Trained on**: 11 days of SimpleFunctions (`market_indicator_history` + `marketwide_resolutions`) — 2026-04-08 → 2026-04-19.
|
| 54 |
+
**License**: CC-BY-4.0 with SimpleFunctions attribution — see `LICENSE`.
|
| 55 |
+
**Author**: SimpleFunctions — https://simplefunctions.dev
|
| 56 |
+
**Model repo**: https://huggingface.co/SimpleFunctions/sf-ml-baseline *(pending upload)*
|
| 57 |
+
|
| 58 |
+
## Why release this
|
| 59 |
+
|
| 60 |
+
Nobody has published a calibrated feature-based baseline for prediction-market forecasting. All prior art (Halawi 2024, Schoenegger 2024, AIA 2025) uses LLM + news retrieval. We release this as the **feature-based reference** that LLM systems should ensemble with.
|
| 61 |
+
|
| 62 |
+
**Brier scores (vs market-implied baseline, 95% CI):**
|
| 63 |
+
|
| 64 |
+
| Task | Model | Brier | CI | Δ vs baseline |
|
| 65 |
+
|------|-------|------:|---|---:|
|
| 66 |
+
| V1 × T1: direction 24h | LGBM 3-seed | 0.2295 | [0.2290, 0.2299] | **−0.0205** (vs coinflip 0.2500) |
|
| 67 |
+
| V1 × T1: direction 24h | XGBoost 3-seed | 0.2296 | [0.2292, 0.2301] | −0.0204 |
|
| 68 |
+
| V1 × T1: direction 24h | CatBoost 3-seed | 0.2295 | [0.2290, 0.2299] | −0.0205 |
|
| 69 |
+
| **V1 × T1: direction 24h** | **Ensemble (3-model × 3-seed = 9)** | **0.2294** | [0.2289, 0.2299] | **−0.0206** |
|
| 70 |
+
| V2 × T4: resolution 24h | XGBoost 3-seed | 0.1681 | [0.1605, 0.1759] | −0.0086 (vs price/100 = 0.1767) |
|
| 71 |
+
|
| 72 |
+
Statistically significant (non-overlapping 95% CI) on V1 × T1 at 246,862 test samples.
|
| 73 |
+
|
| 74 |
+
## Install
|
| 75 |
+
|
| 76 |
+
```bash
|
| 77 |
+
pip install lightgbm xgboost catboost numpy pandas
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
## Use
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
from pathlib import Path
|
| 84 |
+
from sf_ml_baseline import SFBaseline
|
| 85 |
+
|
| 86 |
+
model = SFBaseline(weights_dir='sf-ml-baseline/weights')
|
| 87 |
+
|
| 88 |
+
# Direction forecast: probability that the 24h-forward price will be HIGHER than now.
|
| 89 |
+
# Features: current price (cents, 0-100), 24h price delta (cents),
|
| 90 |
+
# implicit yield (%), calibration ratio index (unitless), calibration variability ratio.
|
| 91 |
+
p_up = model.predict_direction(price_cents=55, delta_cents=3, iy=12.5, cri=0.6, cvr=0.8)
|
| 92 |
+
print(f'P(price rises in next 24h) = {p_up:.3f}')
|
| 93 |
+
|
| 94 |
+
# Or batch-predict from a DataFrame:
|
| 95 |
+
import pandas as pd
|
| 96 |
+
df = pd.DataFrame([
|
| 97 |
+
{'price_cents': 55, 'delta_cents': 3, 'iy': 12.5, 'cri': 0.6, 'cvr': 0.8},
|
| 98 |
+
{'price_cents': 82, 'delta_cents': -1, 'iy': 4.5, 'cri': 0.3, 'cvr': 0.9},
|
| 99 |
+
])
|
| 100 |
+
probas = model.predict_direction_batch(df)
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
See `predict.py` for the full inference code.
|
| 104 |
+
|
| 105 |
+
## Architecture
|
| 106 |
+
|
| 107 |
+
- **Features (V1)**: `price_cents`, `delta_cents` (24h price change), `iy` (implicit yield), `cri` (calibration ratio index), `cvr` (calibration variability ratio). Spec: SimpleFunctions [indicator documentation](https://simplefunctions.dev/concepts).
|
| 108 |
+
- **Models**: 3 LightGBM + 3 XGBoost + 3 CatBoost, each trained with different seeds. Ensemble predictions by simple mean.
|
| 109 |
+
- **Split**: temporal — 80% train / 24h embargo / 20% test. 90/10 inner train/val split for early stopping.
|
| 110 |
+
- **Target T1**: binary `sign(price(t+24h) - price(t))`, excludes no-move rows (delta==0 at t+24h).
|
| 111 |
+
- **Target T4**: `resolved_outcome` ∈ {0, 1} for markets that resolved in 22-26h after the feature capture time.
|
| 112 |
+
|
| 113 |
+
## Known limitations
|
| 114 |
+
|
| 115 |
+
1. **Only 11 days of training data.** The full feature history table (`market_indicator_history`) was introduced to SF's data pipeline on 2026-04-08. A proper 30d+ / 180d+ re-train is scheduled; see "Retrain plan" below.
|
| 116 |
+
2. **5 base features only.** `market_indicator_history` holds a compact subset of the full indicator stack. The live `market_indicators` table has ~20 features (`iyYes`, `iyNo`, `ee`, `las`, `vr`, `iar`, `rv`, `adjIy`, `cvrDelta`, `overround`, etc.) but only for the current snapshot. Future versions will store history for the full feature set.
|
| 117 |
+
3. **`cvr` has 0% feature importance in the direction model.** Investigate whether the window/computation needs tuning.
|
| 118 |
+
4. **V2 × T4 (rolling features + resolution label) is below the 0.01 Brier gate globally** — works well on Crypto (Δ=−0.041) and Commodities (Δ=−0.036) but not Sports (Δ=−0.006) or Financials (+0.004, model worse).
|
| 119 |
+
5. **Do not use for live trading without backtesting against your own execution model.** This is a calibration baseline, not a PnL strategy.
|
| 120 |
+
|
| 121 |
+
## Retrain plan
|
| 122 |
+
|
| 123 |
+
**v0.1 is a time-capsule.** Planned retrains:
|
| 124 |
+
|
| 125 |
+
| Version | Trigger | When | What changes |
|
| 126 |
+
|---------|---------|------|------|
|
| 127 |
+
| v0.2 | R2 dump archive ≥ 30d of indicator history | ~2026-05-20 | Same architecture, more data; per-category specialist models for Crypto/Commodities/Sports |
|
| 128 |
+
| v0.3 | Full indicator feature set stored in history (schema change) | TBD | V1 grows from 5 → 20 features; re-run Phase A.3 full grid |
|
| 129 |
+
| v1.0 | ≥ 6 months of R2 data | ~2026-10 | FT-Transformer + TabPFN + ensemble; formal paper submission (ICLR 2027 FinAI Workshop) |
|
| 130 |
+
|
| 131 |
+
## Reproduce
|
| 132 |
+
|
| 133 |
+
```bash
|
| 134 |
+
git clone https://github.com/spfunctions/simplefunctions-landing # (private — OSS mirror pending)
|
| 135 |
+
cd simplefunctions-landing
|
| 136 |
+
source scripts/ml/.venv/bin/activate
|
| 137 |
+
|
| 138 |
+
# Data pull (uses DIRECT_DATABASE_URL in .env.local)
|
| 139 |
+
python scripts/ml/phase-a/01-pull-training-data.py
|
| 140 |
+
|
| 141 |
+
# Train
|
| 142 |
+
python scripts/ml/phase-a/02-train-lgbm.py
|
| 143 |
+
python scripts/ml/phase-a/04-bakeoff.py
|
| 144 |
+
|
| 145 |
+
# Evaluate
|
| 146 |
+
python scripts/ml/phase-a/03-evaluate.py
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
All hyperparameters are documented inline. 3-seed ensembling uses {42, 137, 2026}.
|
| 150 |
+
|
| 151 |
+
## Citation
|
| 152 |
+
|
| 153 |
+
```bibtex
|
| 154 |
+
@software{sf_ml_baseline_v0_1,
|
| 155 |
+
author = {SimpleFunctions},
|
| 156 |
+
title = {{sf-ml-baseline}: A feature-based prediction-market forecaster},
|
| 157 |
+
year = {2026},
|
| 158 |
+
version = {0.1},
|
| 159 |
+
publisher = {SimpleFunctions},
|
| 160 |
+
url = {https://simplefunctions.dev/opensource/sf-ml-baseline},
|
| 161 |
+
license = {CC-BY-4.0 with SimpleFunctions attribution}
|
| 162 |
+
}
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
## See also
|
| 166 |
+
|
| 167 |
+
- `docs/ml/phase-a-investigation.md` — 10 open-question investigation (SPEC-19 §13)
|
| 168 |
+
- `docs/ml/phase-a-results.md` — gate decision + per-category breakdown
|
| 169 |
+
- `.claude/specs/SPEC-19-model-deep-investigation.md` — full 6-phase research plan
|
sf_ml_baseline.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
sf-ml-baseline v0.1 — inference helpers.
|
| 3 |
+
|
| 4 |
+
Load pre-trained LightGBM + XGBoost + CatBoost ensembles and predict
|
| 5 |
+
24h prediction-market outcomes from 5 engineered indicator features.
|
| 6 |
+
|
| 7 |
+
Model weights are in `./weights/`. Licensed CC-BY-4.0 with attribution
|
| 8 |
+
— see LICENSE.
|
| 9 |
+
|
| 10 |
+
Example:
|
| 11 |
+
from sf_ml_baseline import SFBaseline
|
| 12 |
+
model = SFBaseline()
|
| 13 |
+
p_up = model.predict_direction(price_cents=55, delta_cents=3,
|
| 14 |
+
iy=12.5, cri=0.6, cvr=0.8)
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
from pathlib import Path
|
| 19 |
+
from typing import Iterable, Sequence
|
| 20 |
+
import numpy as np
|
| 21 |
+
import pandas as pd
|
| 22 |
+
import lightgbm as lgb
|
| 23 |
+
import xgboost as xgb
|
| 24 |
+
from catboost import CatBoostClassifier
|
| 25 |
+
|
| 26 |
+
FEATURE_COLS_V1 = ('price_cents', 'delta_cents', 'iy', 'cri', 'cvr')
|
| 27 |
+
SEEDS = (42, 137, 2026)
|
| 28 |
+
|
| 29 |
+
class SFBaseline:
|
| 30 |
+
"""9-model ensemble (3 architectures × 3 seeds) for 24h direction forecasting."""
|
| 31 |
+
|
| 32 |
+
def __init__(self, weights_dir: str | Path | None = None):
|
| 33 |
+
if weights_dir is None:
|
| 34 |
+
weights_dir = Path(__file__).parent / 'weights'
|
| 35 |
+
self.weights_dir = Path(weights_dir)
|
| 36 |
+
if not self.weights_dir.is_dir():
|
| 37 |
+
raise FileNotFoundError(f'weights dir not found: {self.weights_dir}')
|
| 38 |
+
self._load_t1_models()
|
| 39 |
+
self._load_t4_models()
|
| 40 |
+
|
| 41 |
+
def _load_t1_models(self):
|
| 42 |
+
"""V1 × T1 — direction forecast."""
|
| 43 |
+
self.lgb_t1 = [lgb.Booster(model_file=str(self.weights_dir / f'lgbm_v1_t1_seed{s}.txt'))
|
| 44 |
+
for s in SEEDS]
|
| 45 |
+
self.xgb_t1 = []
|
| 46 |
+
for s in SEEDS:
|
| 47 |
+
m = xgb.XGBClassifier()
|
| 48 |
+
m.load_model(str(self.weights_dir / f'xgb_v1_t1_seed{s}.json'))
|
| 49 |
+
self.xgb_t1.append(m)
|
| 50 |
+
self.cat_t1 = []
|
| 51 |
+
for s in SEEDS:
|
| 52 |
+
m = CatBoostClassifier()
|
| 53 |
+
m.load_model(str(self.weights_dir / f'cat_v1_t1_seed{s}.cbm'))
|
| 54 |
+
self.cat_t1.append(m)
|
| 55 |
+
|
| 56 |
+
def _load_t4_models(self):
|
| 57 |
+
"""V2 × T4 — resolution forecast. Requires 35 features (price + rolling stats) —
|
| 58 |
+
see docs/ml/phase-a-results.md. This helper loads them but the user must
|
| 59 |
+
supply the full 35-feature vector (via the training-data pipeline)."""
|
| 60 |
+
try:
|
| 61 |
+
self.lgb_t4 = [lgb.Booster(model_file=str(self.weights_dir / f'lgbm_v2_t4_seed{s}.txt'))
|
| 62 |
+
for s in SEEDS]
|
| 63 |
+
self.xgb_t4 = []
|
| 64 |
+
for s in SEEDS:
|
| 65 |
+
m = xgb.XGBClassifier()
|
| 66 |
+
m.load_model(str(self.weights_dir / f'xgb_v2_t4_seed{s}.json'))
|
| 67 |
+
self.xgb_t4.append(m)
|
| 68 |
+
self.cat_t4 = []
|
| 69 |
+
for s in SEEDS:
|
| 70 |
+
m = CatBoostClassifier()
|
| 71 |
+
m.load_model(str(self.weights_dir / f'cat_v2_t4_seed{s}.cbm'))
|
| 72 |
+
self.cat_t4.append(m)
|
| 73 |
+
self.t4_features = list(self.lgb_t4[0].feature_name())
|
| 74 |
+
except Exception:
|
| 75 |
+
self.lgb_t4 = self.xgb_t4 = self.cat_t4 = None
|
| 76 |
+
self.t4_features = None
|
| 77 |
+
|
| 78 |
+
# --- direction forecast (T1) ---
|
| 79 |
+
|
| 80 |
+
def predict_direction_batch(self, df: pd.DataFrame) -> np.ndarray:
|
| 81 |
+
"""Return P(up-move in 24h) for each row in df.
|
| 82 |
+
df must have columns: price_cents, delta_cents, iy, cri, cvr."""
|
| 83 |
+
missing = [c for c in FEATURE_COLS_V1 if c not in df.columns]
|
| 84 |
+
if missing:
|
| 85 |
+
raise ValueError(f'missing feature columns: {missing}')
|
| 86 |
+
X = df[list(FEATURE_COLS_V1)].astype('float32').values
|
| 87 |
+
return self._predict_t1(X)
|
| 88 |
+
|
| 89 |
+
def predict_direction(
|
| 90 |
+
self,
|
| 91 |
+
price_cents: float,
|
| 92 |
+
delta_cents: float,
|
| 93 |
+
iy: float,
|
| 94 |
+
cri: float,
|
| 95 |
+
cvr: float,
|
| 96 |
+
) -> float:
|
| 97 |
+
"""Single-row prediction. Returns scalar probability."""
|
| 98 |
+
X = np.array([[price_cents, delta_cents, iy, cri, cvr]], dtype='float32')
|
| 99 |
+
return float(self._predict_t1(X)[0])
|
| 100 |
+
|
| 101 |
+
def _predict_t1(self, X: np.ndarray) -> np.ndarray:
|
| 102 |
+
"""9-model ensemble: 3 LGBM + 3 XGB + 3 Cat, equal weight."""
|
| 103 |
+
preds = []
|
| 104 |
+
for m in self.lgb_t1:
|
| 105 |
+
preds.append(m.predict(X))
|
| 106 |
+
for m in self.xgb_t1:
|
| 107 |
+
preds.append(m.predict_proba(X)[:, 1])
|
| 108 |
+
for m in self.cat_t1:
|
| 109 |
+
preds.append(m.predict_proba(X)[:, 1])
|
| 110 |
+
return np.mean(preds, axis=0)
|
| 111 |
+
|
| 112 |
+
# --- resolution forecast (T4) ---
|
| 113 |
+
|
| 114 |
+
def predict_resolution_batch(self, df: pd.DataFrame) -> np.ndarray:
|
| 115 |
+
"""Return P(YES resolution in 24h) for each row in df.
|
| 116 |
+
df must have all 35 V2 features (base 5 + rolling stats). See self.t4_features."""
|
| 117 |
+
if self.lgb_t4 is None:
|
| 118 |
+
raise RuntimeError('T4 weights not loaded')
|
| 119 |
+
missing = [c for c in self.t4_features if c not in df.columns]
|
| 120 |
+
if missing:
|
| 121 |
+
raise ValueError(f'missing V2 feature columns: {missing[:5]}... ({len(missing)} total)')
|
| 122 |
+
X = df[self.t4_features].astype('float32').values
|
| 123 |
+
preds = []
|
| 124 |
+
for m in self.lgb_t4:
|
| 125 |
+
preds.append(m.predict(X))
|
| 126 |
+
for m in self.xgb_t4:
|
| 127 |
+
preds.append(m.predict_proba(X)[:, 1])
|
| 128 |
+
for m in self.cat_t4:
|
| 129 |
+
preds.append(m.predict_proba(X)[:, 1])
|
| 130 |
+
return np.mean(preds, axis=0)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
if __name__ == '__main__':
|
| 134 |
+
# smoke test
|
| 135 |
+
model = SFBaseline()
|
| 136 |
+
p = model.predict_direction(price_cents=55, delta_cents=3, iy=12.5, cri=0.6, cvr=0.8)
|
| 137 |
+
print(f'P(price rises in 24h | market at 55c, +3c delta, iy=12.5%, cri=0.6, cvr=0.8) = {p:.3f}')
|
| 138 |
+
p_batch = model.predict_direction_batch(pd.DataFrame([
|
| 139 |
+
{'price_cents': 55, 'delta_cents': 3, 'iy': 12.5, 'cri': 0.6, 'cvr': 0.8},
|
| 140 |
+
{'price_cents': 82, 'delta_cents': -1, 'iy': 4.5, 'cri': 0.3, 'cvr': 0.9},
|
| 141 |
+
]))
|
| 142 |
+
print(f'Batch predictions: {p_batch}')
|
weights/cat_v1_t1_seed137.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:51a0aa6b9fcc8e5ea6ec594e3eb396bde99b1966089cec9a07db5582a482cccc
|
| 3 |
+
size 1394536
|
weights/cat_v1_t1_seed2026.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:03e6ce1ad122784e88b42409f404a22c11b9bf8ae40f740e3503cf70bd4d324e
|
| 3 |
+
size 2134656
|
weights/cat_v1_t1_seed42.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d0f7d0074f0ff73e648148a1d43834abbe8884852717fb623db8d6048c574505
|
| 3 |
+
size 1074240
|
weights/cat_v2_t4_seed137.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0501566f668270f0e17a38934c422f0ef6e71cfbd24babb4de287d4527127a52
|
| 3 |
+
size 418272
|
weights/cat_v2_t4_seed2026.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:73aef991e22dececf5bba619aef7f7304f8e641124e3616fcc34049c93c35291
|
| 3 |
+
size 348656
|
weights/cat_v2_t4_seed42.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c6e168b8a1baa3ee018a20e1c4f1b07c493caccc019750b3705af57cfc480564
|
| 3 |
+
size 454152
|
weights/lgbm_v1_t1_seed137.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/lgbm_v1_t1_seed2026.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/lgbm_v1_t1_seed42.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/lgbm_v2_t4_seed137.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/lgbm_v2_t4_seed2026.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/lgbm_v2_t4_seed42.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/xgb_v1_t1_seed137.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/xgb_v1_t1_seed2026.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/xgb_v1_t1_seed42.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/xgb_v2_t4_seed137.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/xgb_v2_t4_seed2026.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
weights/xgb_v2_t4_seed42.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|