generated from mathias/template-go-web
Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d04423d39 | ||
|
|
44e8b3eb95 | ||
|
|
f5ce8d6706 | ||
|
|
ed85dc4a8c | ||
|
|
69784f59cf | ||
|
|
485fdaa9f9 | ||
|
|
df910e4336 | ||
|
|
e616575979 |
@@ -28,3 +28,9 @@ go.work.sum
|
|||||||
# Project-specific
|
# Project-specific
|
||||||
bin/
|
bin/
|
||||||
*.templ.go
|
*.templ.go
|
||||||
|
|
||||||
|
# python venv (autoresearch loop)
|
||||||
|
.venv/
|
||||||
|
|
||||||
|
# downloaded + processed market data (track via DVC/MinIO, #10 — not git)
|
||||||
|
data/
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
# Autoresearch STATUS
|
||||||
|
|
||||||
|
| iter | val_vol_r2 | delta | action | secs | gpu | change |
|
||||||
|
|------|-----------|-------|--------|------|-----|--------|
|
||||||
|
| 1 | 0.3749 | +0.0928 | KEEP | 2s | gpu=0% vram=10054/12227MiB temp=34°C | iter1 |
|
||||||
|
| 1 | 0.3011 | +0.0776 | KEEP | 2s | gpu=0% vram=10054/12227MiB temp=34°C | iter1 |
|
||||||
|
| 2 | 0.3032 | +0.0021 | KEEP | 2s | gpu=0% vram=10054/12227MiB temp=35°C | iter2 |
|
||||||
|
| 1 | 0.2759 | -0.0273 | revert | 2s | gpu=0% vram=10054/12227MiB temp=34°C | iter1 |
|
||||||
|
| 2 | 0.3442 | +0.0410 | KEEP | 2s | gpu=0% vram=10054/12227MiB temp=34°C | iter2 |
|
||||||
|
| 3 | 0.3371 | -0.0071 | revert | 2s | gpu=0% vram=10054/12227MiB temp=34°C | iter3 |
|
||||||
|
| 4 | 0.3143 | -0.0299 | revert | 2s | gpu=0% vram=10054/12227MiB temp=34°C | iter4 |
|
||||||
|
| 5 | 0.3355 | -0.0087 | revert | 2s | gpu=0% vram=10054/12227MiB temp=34°C | iter5 |
|
||||||
|
| 6 | 0.2377 | -0.1065 | revert | 2s | gpu=0% vram=10054/12227MiB temp=35°C | iter6 |
|
||||||
|
| 1 | -0.1247 | +0.0296 | KEEP | 4s | gpu=0% vram=10054/12227MiB temp=35°C | iter1 |
|
||||||
|
| 2 | -0.1203 | +0.0044 | KEEP | 4s | gpu=0% vram=10054/12227MiB temp=35°C | iter2 |
|
||||||
|
| 3 | -0.0716 | +0.0487 | KEEP | 4s | gpu=0% vram=10054/12227MiB temp=36°C | iter3 |
|
||||||
|
| 4 | 0.0590 | +0.1306 | KEEP | 5s | gpu=0% vram=10054/12227MiB temp=36°C | iter4 |
|
||||||
|
| 5 | 0.0599 | +0.0009 | KEEP | 5s | gpu=0% vram=10054/12227MiB temp=37°C | iter5 |
|
||||||
@@ -0,0 +1,205 @@
|
|||||||
|
"""loop.py — Karpathy-style autoresearch loop for jepa-fx-risk.
|
||||||
|
|
||||||
|
Agent (on iguana/berget — NOT koala, whose GPU is reserved for train.py) reads
|
||||||
|
program.md + train.py + STATUS.md, proposes ONE change to train.py, we run it,
|
||||||
|
keep if val_vol_r2 improved else git-revert. Appends per-iter record to STATUS.md.
|
||||||
|
|
||||||
|
LITELLM_KEY=xxx python loop.py [--iters N] [--model MODEL]
|
||||||
|
|
||||||
|
Env:
|
||||||
|
LITELLM_KEY — LiteLLM master key (required)
|
||||||
|
LITELLM_BASE — default http://localhost:30401/v1
|
||||||
|
LOOP_MODEL — default berget/gemma4-31b (non-thinking; iguana/berget only)
|
||||||
|
LOOP_ITERS — default 3
|
||||||
|
TRAIN_TIMEOUT — seconds per train.py run, default 120
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import textwrap
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
LITELLM_BASE = os.environ.get("LITELLM_BASE", "http://localhost:30401/v1")
|
||||||
|
LITELLM_KEY = os.environ.get("LITELLM_KEY", "")
|
||||||
|
LOOP_MODEL = os.environ.get("LOOP_MODEL", "berget/gemma4-31b")
|
||||||
|
LOOP_ITERS = int(os.environ.get("LOOP_ITERS", "3"))
|
||||||
|
TRAIN_TIMEOUT = int(os.environ.get("TRAIN_TIMEOUT", "120"))
|
||||||
|
STATUS_MD = Path("STATUS.md")
|
||||||
|
METRICS_JSON = Path("metrics.json")
|
||||||
|
TRAIN_PY = Path("train.py")
|
||||||
|
|
||||||
|
AGENT_SYSTEM = textwrap.dedent("""\
|
||||||
|
You are the autoresearch agent for jepa-fx-risk. Your job: propose ONE small,
|
||||||
|
targeted change to train.py to improve val_vol_r2 (OOS R² predicting 1-day
|
||||||
|
realized vol from frozen embeddings). Higher is better.
|
||||||
|
|
||||||
|
Rules:
|
||||||
|
- Return ONLY the full new content of train.py — nothing else, no explanation,
|
||||||
|
no markdown fence. Raw Python only.
|
||||||
|
- Change ONE thing at a time (one knob, one structural idea).
|
||||||
|
- Do NOT touch prepare_data.py, loop.py, or the data pipeline — only train.py.
|
||||||
|
- Do NOT add new data sources or new files.
|
||||||
|
- The metric is computed externally from your frozen embeddings; trust it.
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
def read_file(p: Path) -> str:
|
||||||
|
return p.read_text() if p.exists() else ""
|
||||||
|
|
||||||
|
|
||||||
|
def gpu_snapshot() -> str:
|
||||||
|
try:
|
||||||
|
out = subprocess.check_output(
|
||||||
|
["nvidia-smi", "--query-gpu=utilization.gpu,memory.used,memory.total,temperature.gpu",
|
||||||
|
"--format=csv,noheader,nounits"], timeout=5, text=True
|
||||||
|
).strip()
|
||||||
|
util, mem_used, mem_total, temp = [x.strip() for x in out.split(",")]
|
||||||
|
return "gpu=%s%% vram=%s/%sMiB temp=%s°C" % (util, mem_used, mem_total, temp)
|
||||||
|
except Exception:
|
||||||
|
return "gpu=N/A"
|
||||||
|
|
||||||
|
|
||||||
|
def read_metric() -> float | None:
|
||||||
|
if not METRICS_JSON.exists():
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return float(json.loads(METRICS_JSON.read_text())["val_vol_r2"])
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def run_train() -> tuple[float | None, float, str]:
|
||||||
|
"""Run train.py. Returns (val_vol_r2 or None, wall_secs, stderr_tail)."""
|
||||||
|
t0 = time.time()
|
||||||
|
gpu_before = gpu_snapshot()
|
||||||
|
try:
|
||||||
|
r = subprocess.run(
|
||||||
|
[sys.executable, "train.py"],
|
||||||
|
capture_output=True, text=True, timeout=TRAIN_TIMEOUT,
|
||||||
|
)
|
||||||
|
elapsed = time.time() - t0
|
||||||
|
if r.returncode != 0:
|
||||||
|
return None, elapsed, (r.stderr or r.stdout)[-300:]
|
||||||
|
metric = read_metric()
|
||||||
|
return metric, elapsed, ""
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
return None, TRAIN_TIMEOUT, "TIMEOUT"
|
||||||
|
|
||||||
|
|
||||||
|
def call_agent(iteration: int, best_so_far: float | None) -> str:
|
||||||
|
"""Ask the LLM agent to edit train.py. Returns new train.py content."""
|
||||||
|
context = "\n\n".join([
|
||||||
|
"# program.md\n" + read_file(Path("program.md")),
|
||||||
|
"# train.py (current)\n" + read_file(TRAIN_PY),
|
||||||
|
"# STATUS.md (history)\n" + read_file(STATUS_MD)[-2000:],
|
||||||
|
"# metrics.json (last run)\n" + read_file(METRICS_JSON),
|
||||||
|
"Iteration %d. Best val_vol_r2 so far: %s. Improve it." % (
|
||||||
|
iteration, "%.4f" % best_so_far if best_so_far is not None else "none yet"
|
||||||
|
),
|
||||||
|
])
|
||||||
|
payload = json.dumps({
|
||||||
|
"model": LOOP_MODEL,
|
||||||
|
"messages": [
|
||||||
|
{"role": "system", "content": AGENT_SYSTEM},
|
||||||
|
{"role": "user", "content": context},
|
||||||
|
],
|
||||||
|
"temperature": 0.7,
|
||||||
|
"max_tokens": 4096,
|
||||||
|
}).encode()
|
||||||
|
req = urllib.request.Request(
|
||||||
|
LITELLM_BASE + "/chat/completions",
|
||||||
|
data=payload,
|
||||||
|
headers={"Authorization": "Bearer " + LITELLM_KEY,
|
||||||
|
"Content-Type": "application/json"},
|
||||||
|
method="POST",
|
||||||
|
)
|
||||||
|
resp = urllib.request.urlopen(req, timeout=60)
|
||||||
|
data = json.load(resp)
|
||||||
|
return data["choices"][0]["message"]["content"]
|
||||||
|
|
||||||
|
|
||||||
|
def revert_train(original_content: str):
|
||||||
|
TRAIN_PY.write_text(original_content)
|
||||||
|
|
||||||
|
|
||||||
|
def append_status(line: str):
|
||||||
|
with open(STATUS_MD, "a") as f:
|
||||||
|
f.write(line + "\n")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if not LITELLM_KEY:
|
||||||
|
print("ERROR: set LITELLM_KEY"); sys.exit(1)
|
||||||
|
|
||||||
|
if not STATUS_MD.exists():
|
||||||
|
STATUS_MD.write_text("# Autoresearch STATUS\n\n| iter | val_vol_r2 | delta | action | secs | gpu | change |\n|------|-----------|-------|--------|------|-----|--------|\n")
|
||||||
|
|
||||||
|
# establish baseline
|
||||||
|
baseline = read_metric()
|
||||||
|
if baseline is None:
|
||||||
|
print("No metrics.json — running train.py for baseline...")
|
||||||
|
m, secs, err = run_train()
|
||||||
|
if m is None:
|
||||||
|
print("Baseline run failed:", err); sys.exit(1)
|
||||||
|
baseline = m
|
||||||
|
print("Baseline: val_vol_r2 = %.4f (%.1fs)" % (baseline, secs))
|
||||||
|
|
||||||
|
best = baseline
|
||||||
|
print("Starting loop | model=%s | iters=%d | baseline=%.4f" % (LOOP_MODEL, LOOP_ITERS, best))
|
||||||
|
|
||||||
|
for i in range(1, LOOP_ITERS + 1):
|
||||||
|
print("\n--- iter %d/%d ---" % (i, LOOP_ITERS))
|
||||||
|
original = TRAIN_PY.read_text()
|
||||||
|
|
||||||
|
print(" calling agent (%s)..." % LOOP_MODEL)
|
||||||
|
t_agent = time.time()
|
||||||
|
try:
|
||||||
|
new_code = call_agent(i, best)
|
||||||
|
except Exception as e:
|
||||||
|
print(" agent call failed:", e)
|
||||||
|
append_status("| %d | ERR | — | agent-fail | — | — | %s |" % (i, str(e)[:60]))
|
||||||
|
continue
|
||||||
|
agent_secs = time.time() - t_agent
|
||||||
|
print(" agent replied in %.1fs" % agent_secs)
|
||||||
|
|
||||||
|
# strip accidental markdown fences
|
||||||
|
if new_code.strip().startswith("```"):
|
||||||
|
lines = new_code.strip().splitlines()
|
||||||
|
new_code = "\n".join(lines[1:-1] if lines[-1].strip() == "```" else lines[1:])
|
||||||
|
|
||||||
|
TRAIN_PY.write_text(new_code)
|
||||||
|
|
||||||
|
gpu = gpu_snapshot()
|
||||||
|
print(" running train.py [%s]..." % gpu)
|
||||||
|
metric, secs, err = run_train()
|
||||||
|
|
||||||
|
if metric is None:
|
||||||
|
print(" train.py FAILED — reverting. err:", err[:100])
|
||||||
|
revert_train(original)
|
||||||
|
append_status("| %d | FAIL | — | revert | %.0fs | %s | run error |" % (i, secs, gpu))
|
||||||
|
continue
|
||||||
|
|
||||||
|
delta = metric - best
|
||||||
|
if metric > best:
|
||||||
|
best = metric
|
||||||
|
action = "KEEP"
|
||||||
|
else:
|
||||||
|
revert_train(original)
|
||||||
|
action = "revert"
|
||||||
|
|
||||||
|
summary = "| %d | %.4f | %+.4f | %s | %.0fs | %s | iter%d |" % (
|
||||||
|
i, metric, delta, action, secs, gpu, i)
|
||||||
|
append_status(summary)
|
||||||
|
print(" val_vol_r2=%.4f delta=%+.4f action=%s [%.0fs]" % (metric, delta, action, secs))
|
||||||
|
|
||||||
|
print("\nDone. Best val_vol_r2 = %.4f (baseline was %.4f, delta %+.4f)" % (best, baseline, best - baseline))
|
||||||
|
print("STATUS.md updated.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"val_vol_r2": 0.05988483092470609,
|
||||||
|
"n_test": 263,
|
||||||
|
"knobs": {
|
||||||
|
"WINDOW": 60,
|
||||||
|
"PATCH_LEN": 5,
|
||||||
|
"STRIDE": 5,
|
||||||
|
"D_MODEL": 64,
|
||||||
|
"DEPTH": 2,
|
||||||
|
"MASK_FRAC": 0.5,
|
||||||
|
"SIGREG_LAM": 0.01,
|
||||||
|
"EPOCHS": 300
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
# Python deps for the autoresearch loop (train.py + scripts). Install torch from
|
||||||
|
# the cu130 index FIRST (koala Blackwell sm_120, torch 2.12.1+cu130 verified):
|
||||||
|
# pip install torch --index-url https://download.pytorch.org/whl/cu130
|
||||||
|
# pip install -r requirements.txt
|
||||||
|
numpy>=2.0
|
||||||
|
pandas>=2.2
|
||||||
|
pyarrow>=16
|
||||||
|
histdata>=1.3 # histdata.com downloader (handles the tk token politely)
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
"""Phase-0 compute gate (brain wiki/jepa-fx/facts/autoresearch-integration-phase1):
|
||||||
|
PyTorch cu130 must see the koala Blackwell GPU and compute before any experiment.
|
||||||
|
|
||||||
|
python scripts/check_gpu.py # exits 0 if the GPU is usable, 1 otherwise
|
||||||
|
|
||||||
|
Note: koala shares this 12GB card with the llama-swap LLM stack. The autoresearch
|
||||||
|
agent should run on iguana/berget models so koala's GPU stays free for train.py.
|
||||||
|
"""
|
||||||
|
import sys
|
||||||
|
import torch
|
||||||
|
|
||||||
|
print("torch", torch.__version__)
|
||||||
|
if not torch.cuda.is_available():
|
||||||
|
print("CUDA NOT AVAILABLE — gate BLOCKED")
|
||||||
|
sys.exit(1)
|
||||||
|
print("device:", torch.cuda.get_device_name(0))
|
||||||
|
print("capability: sm_%d%d" % torch.cuda.get_device_capability(0))
|
||||||
|
x = torch.randn(2000, 2000, device="cuda")
|
||||||
|
(x @ x).sum().item()
|
||||||
|
torch.cuda.synchronize()
|
||||||
|
print("GPU matmul OK — Phase-0 compute gate GREEN")
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
"""Fetch EUR/USD M1 bars from histdata.com (free, research use).
|
||||||
|
|
||||||
|
Polite: one request per year, spaced; past years query month=None. Uses the
|
||||||
|
maintained `histdata` package which handles histdata's anti-hotlink tk token.
|
||||||
|
Output: data/raw/DAT_ASCII_EURUSD_M1_<year>.zip
|
||||||
|
|
||||||
|
YEARS=2019,2020,2021 python scripts/fetch_data.py
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
from histdata import download_hist_data
|
||||||
|
from histdata.api import Platform as P, TimeFrame as T
|
||||||
|
|
||||||
|
YEARS = [y.strip() for y in os.environ.get("YEARS", "2019,2020,2021").split(",")]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
os.makedirs("data/raw", exist_ok=True)
|
||||||
|
for yr in YEARS:
|
||||||
|
f = download_hist_data(
|
||||||
|
year=yr, month=None, pair="eurusd",
|
||||||
|
platform=P.GENERIC_ASCII, time_frame=T.ONE_MINUTE,
|
||||||
|
output_directory="data/raw",
|
||||||
|
)
|
||||||
|
print("fetched", yr, "->", f)
|
||||||
|
time.sleep(2) # be a good citizen
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
"""LOCKED data pipeline (toy) — agent must NOT edit (brain Phase-1 contract).
|
||||||
|
|
||||||
|
Parses histdata EUR/USD M1 zips → daily series with realized volatility (the
|
||||||
|
val_vol_r2 target = 1-day realized vol from intraday squared returns).
|
||||||
|
Output: data/processed/eurusd_daily.parquet [date, close, ret, realized_vol].
|
||||||
|
"""
|
||||||
|
import glob
|
||||||
|
import os
|
||||||
|
import zipfile
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
RAW = "data/raw"
|
||||||
|
OUT = "data/processed/eurusd_daily.parquet"
|
||||||
|
|
||||||
|
|
||||||
|
def load_m1() -> pd.DataFrame:
|
||||||
|
frames = []
|
||||||
|
for zp in sorted(glob.glob(os.path.join(RAW, "DAT_ASCII_EURUSD_M1_*.zip"))):
|
||||||
|
with zipfile.ZipFile(zp) as z:
|
||||||
|
csv = [n for n in z.namelist() if n.endswith(".csv")][0]
|
||||||
|
with z.open(csv) as f:
|
||||||
|
df = pd.read_csv(
|
||||||
|
f, sep=";", header=None,
|
||||||
|
names=["dt", "open", "high", "low", "close", "vol"],
|
||||||
|
)
|
||||||
|
df["ts"] = pd.to_datetime(df["dt"], format="%Y%m%d %H%M%S")
|
||||||
|
frames.append(df[["ts", "close"]])
|
||||||
|
out = pd.concat(frames).sort_values("ts").reset_index(drop=True)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
m1 = load_m1()
|
||||||
|
m1["r"] = np.log(m1["close"]).diff()
|
||||||
|
m1["day"] = m1["ts"].dt.normalize()
|
||||||
|
daily = m1.groupby("day").agg(
|
||||||
|
close=("close", "last"),
|
||||||
|
realized_vol=("r", lambda x: np.sqrt(np.nansum(x.values ** 2))),
|
||||||
|
n_min=("r", "count"),
|
||||||
|
).reset_index()
|
||||||
|
daily = daily[daily["n_min"] > 60] # drop thin days (holidays)
|
||||||
|
daily["ret"] = np.log(daily["close"]).diff()
|
||||||
|
daily = daily.dropna().reset_index(drop=True)
|
||||||
|
os.makedirs(os.path.dirname(OUT), exist_ok=True)
|
||||||
|
daily[["day", "close", "ret", "realized_vol"]].rename(columns={"day": "date"}).to_parquet(OUT)
|
||||||
|
print("rows:", len(daily), "| dates:", daily["day"].min().date(), "→", daily["day"].max().date())
|
||||||
|
# sanity: the COVID crash (March 2020) must show a realized-vol spike
|
||||||
|
rv = daily.set_index("day")["realized_vol"]
|
||||||
|
mar20 = rv["2020-03-01":"2020-03-31"].max()
|
||||||
|
typ = rv["2019-01-01":"2019-12-31"].median()
|
||||||
|
print("median 2019 RV: %.5f | max Mar-2020 RV: %.5f | spike x%.1f" % (typ, mar20, mar20 / typ))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,174 @@
|
|||||||
|
"""train.py — autoresearch agent file (only this may be edited).
|
||||||
|
|
||||||
|
TS-JEPA backbone with SIGReg regularization (Balestriero & LeCun, LeJEPA
|
||||||
|
arXiv:2511.08544; time-series placement from ChronoJEPA arXiv: 2505.XXXXX).
|
||||||
|
|
||||||
|
PatchTST-style encoder over windowed daily [return, realized_vol] → FREEZE →
|
||||||
|
linear probe predicts NEXT-day realized vol → val_vol_r2 (OOS R²).
|
||||||
|
Writes metrics.json — the single scalar the loop reads.
|
||||||
|
|
||||||
|
Agent may tune: encoder depth/width, patch geometry, mask strategy, SIGReg
|
||||||
|
lambda, optimizer. Do NOT touch prepare_data.py, loop.py, or the data pipeline.
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import math
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
|
||||||
|
# --- agent-tunable knobs ---
|
||||||
|
WINDOW = 60 # INCREASED lookback for better volatility persistence capture
|
||||||
|
PATCH_LEN = 5 # time-patch size (must divide WINDOW)
|
||||||
|
STRIDE = 5
|
||||||
|
D_MODEL = 64 # transformer hidden dim - INCREASED for capacity
|
||||||
|
DEPTH = 2 # transformer layers
|
||||||
|
N_HEADS = 4
|
||||||
|
MASK_FRAC = 0.50 # INCREASED mask fraction to force the encoder to learn better global representations
|
||||||
|
SIGREG_LAM = 0.01 # SIGReg weight (λ) - REDUCED to allow more representation capacity
|
||||||
|
EPOCHS = 300
|
||||||
|
LR = 3e-4
|
||||||
|
SEED = 0
|
||||||
|
# ---------------------------
|
||||||
|
|
||||||
|
torch.manual_seed(SEED)
|
||||||
|
np.random.seed(SEED)
|
||||||
|
dev = "cuda" if torch.cuda.is_available() else "cpu"
|
||||||
|
|
||||||
|
|
||||||
|
# ── SIGReg (from LeJEPA/ChronoJEPA, token-level placement) ─────────────────
|
||||||
|
|
||||||
|
def sigreg(tokens: torch.Tensor, knots: int = 17) -> torch.Tensor:
|
||||||
|
"""Epps-Pulley test statistic pushes token embeddings toward isotropic Gaussian.
|
||||||
|
|
||||||
|
tokens: (B, T, D) — applied per-token, averaged across B and T.
|
||||||
|
"""
|
||||||
|
B, T, D = tokens.shape
|
||||||
|
z = tokens.reshape(B * T, D) # (N, D)
|
||||||
|
t = torch.linspace(0, 3, knots, device=z.device, dtype=z.float().dtype)
|
||||||
|
dt = 3.0 / (knots - 1)
|
||||||
|
w = torch.full((knots,), 2 * dt, device=z.device, dtype=z.float().dtype)
|
||||||
|
w[0] = dt; w[-1] = dt
|
||||||
|
phi = torch.exp(-t.square() / 2.0)
|
||||||
|
|
||||||
|
A = torch.randn(D, 256, device=z.device, dtype=z.float().dtype)
|
||||||
|
A = A / A.norm(p=2, dim=0)
|
||||||
|
x_t = (z.float() @ A).unsqueeze(-1) * t # (N, 256, knots)
|
||||||
|
err = (x_t.cos().mean(0) - phi).square() + x_t.sin().mean(0).square()
|
||||||
|
return ((err @ (w * phi)) * z.shape[0]).mean()
|
||||||
|
|
||||||
|
|
||||||
|
# ── Encoder + Predictor ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
class PatchEncoder(nn.Module):
|
||||||
|
"""PatchTST-style encoder for univariate windows."""
|
||||||
|
def __init__(self, in_feats, patch_len, stride, d_model, depth, n_heads):
|
||||||
|
super().__init__()
|
||||||
|
self.patch_len = patch_len
|
||||||
|
self.stride = stride
|
||||||
|
self.d_model = d_model
|
||||||
|
self.embed = nn.Linear(patch_len * in_feats, d_model)
|
||||||
|
layer = nn.TransformerEncoderLayer(d_model, n_heads, 2 * d_model,
|
||||||
|
dropout=0.0, batch_first=True)
|
||||||
|
self.tf = nn.TransformerEncoder(layer, num_layers=depth)
|
||||||
|
n_patches = (WINDOW - patch_len) // stride + 1
|
||||||
|
pos = torch.zeros(n_patches, d_model)
|
||||||
|
for p in range(n_patches):
|
||||||
|
for i in range(0, d_model, 2):
|
||||||
|
pos[p, i] = math.sin(p / 10000 ** (i / d_model))
|
||||||
|
if i + 1 < d_model:
|
||||||
|
pos[p, i+1] = math.cos(p / 10000 ** (i / d_model))
|
||||||
|
self.register_buffer("pos", pos)
|
||||||
|
|
||||||
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
||||||
|
# x: (B, W, F) → patches → (B, T, D)
|
||||||
|
B, W, F = x.shape
|
||||||
|
n_patches = (W - self.patch_len) // self.stride + 1
|
||||||
|
patches = torch.stack([x[:, i*self.stride:i*self.stride+self.patch_len, :]
|
||||||
|
.reshape(B, -1) for i in range(n_patches)], dim=1)
|
||||||
|
tokens = self.embed(patches) + self.pos[:n_patches]
|
||||||
|
return self.tf(tokens) # (B, T, D)
|
||||||
|
|
||||||
|
|
||||||
|
class Predictor(nn.Module):
|
||||||
|
def __init__(self, d_model):
|
||||||
|
super().__init__()
|
||||||
|
self.net = nn.Sequential(nn.Linear(d_model, d_model), nn.GELU(),
|
||||||
|
nn.Linear(d_model, d_model))
|
||||||
|
def forward(self, x):
|
||||||
|
return self.net(x)
|
||||||
|
|
||||||
|
|
||||||
|
# ── Data ────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def build():
|
||||||
|
df = pd.read_parquet("data/processed/eurusd_daily.parquet").reset_index(drop=True)
|
||||||
|
feats = df[["ret", "realized_vol"]].to_numpy(np.float32)
|
||||||
|
target = df["realized_vol"].to_numpy(np.float32)
|
||||||
|
X, y = [], []
|
||||||
|
for t in range(WINDOW, len(df) - 1):
|
||||||
|
X.append(feats[t - WINDOW:t])
|
||||||
|
y.append(target[t + 1])
|
||||||
|
X = np.stack(X); y = np.array(y, np.float32)
|
||||||
|
n_tr = int(0.7 * len(X))
|
||||||
|
mu = X[:n_tr].mean((0, 1))
|
||||||
|
sd = X[:n_tr].std((0, 1)) + 1e-8
|
||||||
|
X = (X - mu) / sd
|
||||||
|
return (X[:n_tr], y[:n_tr]), (X[n_tr:], y[n_tr:])
|
||||||
|
|
||||||
|
|
||||||
|
# ── Training ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def main():
|
||||||
|
(Xtr, ytr), (Xte, yte) = build()
|
||||||
|
n_feats = Xtr.shape[2]
|
||||||
|
Xtr_t = torch.tensor(Xtr, device=dev)
|
||||||
|
enc = PatchEncoder(n_feats, PATCH_LEN, STRIDE, D_MODEL, DEPTH, N_HEADS).to(dev)
|
||||||
|
pred = Predictor(D_MODEL).to(dev)
|
||||||
|
opt = torch.optim.AdamW(list(enc.parameters()) + list(pred.parameters()), lr=LR)
|
||||||
|
|
||||||
|
n_patches = (WINDOW - PATCH_LEN) // STRIDE + 1
|
||||||
|
n_mask = max(1, int(MASK_FRAC * n_patches))
|
||||||
|
|
||||||
|
for ep in range(EPOCHS):
|
||||||
|
# JEPA: predict masked-out patch tokens from visible tokens
|
||||||
|
idx_mask = torch.randperm(n_patches)[:n_mask]
|
||||||
|
ctx_mask = torch.ones(n_patches, dtype=torch.bool, device=dev)
|
||||||
|
ctx_mask[idx_mask] = False
|
||||||
|
|
||||||
|
tokens_ctx = enc(Xtr_t) # encode all (B, T, D)
|
||||||
|
tokens_target = enc(Xtr_t).detach() # target (frozen): same input, no grad
|
||||||
|
pred_out = pred(tokens_ctx[:, idx_mask, :])
|
||||||
|
jepa_loss = ((pred_out - tokens_target[:, idx_mask, :]) ** 2).mean()
|
||||||
|
reg_loss = sigreg(tokens_ctx)
|
||||||
|
loss = jepa_loss + SIGREG_LAM * reg_loss
|
||||||
|
opt.zero_grad(); loss.backward(); opt.step()
|
||||||
|
|
||||||
|
enc.eval()
|
||||||
|
with torch.no_grad():
|
||||||
|
def embed(X_np):
|
||||||
|
t = torch.tensor(X_np, device=dev)
|
||||||
|
return enc(t).mean(1).cpu().numpy() # pool over time patches
|
||||||
|
|
||||||
|
Etr = embed(Xtr)
|
||||||
|
Ete = embed(Xte)
|
||||||
|
|
||||||
|
# ridge linear probe (closed form)
|
||||||
|
A = np.hstack([Etr, np.ones((len(Etr), 1))])
|
||||||
|
w = np.linalg.solve(A.T @ A + 1e-3 * np.eye(A.shape[1]), A.T @ ytr)
|
||||||
|
pred_np = np.hstack([Ete, np.ones((len(Ete), 1))]) @ w
|
||||||
|
ss_res = ((yte - pred_np) ** 2).sum()
|
||||||
|
ss_tot = ((yte - yte.mean()) ** 2).sum()
|
||||||
|
val_vol_r2 = float(1 - ss_res / ss_tot)
|
||||||
|
|
||||||
|
json.dump({
|
||||||
|
"val_vol_r2": val_vol_r2, "n_test": len(yte),
|
||||||
|
"knobs": {"WINDOW": WINDOW, "PATCH_LEN": PATCH_LEN, "STRIDE": STRIDE,
|
||||||
|
"D_MODEL": D_MODEL, "DEPTH": DEPTH, "MASK_FRAC": MASK_FRAC,
|
||||||
|
"SIGREG_LAM": SIGREG_LAM, "EPOCHS": EPOCHS},
|
||||||
|
}, open("metrics.json", "w"), indent=2)
|
||||||
|
print("val_vol_r2 = %.4f (n_test=%d, dev=%s)" % (val_vol_r2, len(yte), dev))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user