Download src/run_forecasts.py from ShubhamOza/chrono-lookahead-replication: direct link, hf CLI and curl.
- Browser
- Download file 1.51 kB
-
https://huggingface.co/ShubhamOza/chrono-lookahead-replication/resolve/main/src/run_forecasts.py
- Command line
-
hf download hf://ShubhamOza/chrono-lookahead-replication/src/run_forecasts.py
-
curl -L -o run_forecasts.py https://huggingface.co/ShubhamOza/chrono-lookahead-replication/resolve/main/src/run_forecasts.py
1.51 kB
| """Fit and cache every monthly ridge forecast the notebook needs. | |
| Usage: python -m src.run_forecasts | |
| Each model takes 15 to 20 minutes on a laptop CPU, so this is run once | |
| before the notebook. Forecasts already in data/forecasts are skipped. | |
| """ | |
| import os | |
| import time | |
| import numpy as np | |
| import pandas as pd | |
| from src import backtest | |
| ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| EMB = os.path.join(ROOT, "data", "embeddings", "full") | |
| FC = os.path.join(ROOT, "data", "forecasts") | |
| RUNS = ["chrono_realtime", "bert", "storieslm", "finbert", "modernbert", "chrono_2012", "chrono_2016", | |
| "chrono_2020", "chrono_2024", "chrono_realtime_masked"] | |
| SANITY = [("chrono_realtime", "ret_prev"), ("chrono_realtime", "ret"), ("finbert", "ret_prev"), ("finbert", "ret")] | |
| def main(): | |
| fd = pd.read_parquet(os.path.join(ROOT, "data", "processed", "firmdays.parquet"), | |
| columns=["date", "ret_prev", "ret", "ret_next"]) | |
| jobs = [(r, "ret_next", os.path.join(FC, f"{r}.parquet")) for r in RUNS] | |
| jobs += [(r, t, os.path.join(FC, f"{r}_{t}.parquet")) for r, t in SANITY] | |
| for run, target, path in jobs: | |
| if os.path.exists(path): | |
| continue | |
| t0 = time.time() | |
| X = np.load(os.path.join(EMB, run + ".npy"), mmap_mode="r") | |
| backtest.cached_forecasts(fd, X, path, target=target) | |
| print(f"{run} {target}: {time.time() - t0:.0f}s", flush=True) | |
| print("all forecasts done", flush=True) | |
| if __name__ == "__main__": | |
| main() | |