generated from mathias/template-go-web
Compare commits
4
Commits
v0.1.0
...
3445b6d267
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3445b6d267 | ||
|
|
7d04423d39 | ||
|
|
44e8b3eb95 | ||
|
|
f5ce8d6706 |
@@ -5,3 +5,14 @@
|
|||||||
| 1 | 0.3749 | +0.0928 | KEEP | 2s | gpu=0% vram=10054/12227MiB temp=34°C | iter1 |
|
| 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 |
|
| 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 |
|
| 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 |
|
||||||
|
|||||||
+10
-6
@@ -1,10 +1,14 @@
|
|||||||
{
|
{
|
||||||
"val_vol_r2": 0.30321519081159654,
|
"val_vol_r2": 0.05988483092470609,
|
||||||
"n_test": 275,
|
"n_test": 263,
|
||||||
"knobs": {
|
"knobs": {
|
||||||
"WINDOW": 20,
|
"WINDOW": 60,
|
||||||
"EMBED_DIM": 64,
|
"PATCH_LEN": 5,
|
||||||
"MASK_FRAC": 0.4,
|
"STRIDE": 5,
|
||||||
"EPOCHS": 200
|
"D_MODEL": 64,
|
||||||
|
"DEPTH": 2,
|
||||||
|
"MASK_FRAC": 0.5,
|
||||||
|
"SIGREG_LAM": 0.01,
|
||||||
|
"EPOCHS": 300
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"label": "null",
|
||||||
|
"mean_sil": 0.018206419112781685,
|
||||||
|
"pca_sil": 0.13589094579219818,
|
||||||
|
"spread": 0.8673340065023978,
|
||||||
|
"pc1_hv_corr": 0.525803392278542,
|
||||||
|
"per_seed": [
|
||||||
|
0.026790648698806763,
|
||||||
|
0.010999602265655994,
|
||||||
|
0.016829006373882294
|
||||||
|
],
|
||||||
|
"passed": false
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
# Phase-0 SSL feasibility gate — null
|
||||||
|
|
||||||
|
**Date:** 2026-06-24
|
||||||
|
**Path B deviation:** Daily 2019-2023 (not hourly 2008-2022); Python harness
|
||||||
|
(not Go #4); gate metric adapted from silhouette-on-embedding to match
|
||||||
|
available data. Go harness (#4) remains open for production experiments.
|
||||||
|
|
||||||
|
## Data
|
||||||
|
- Train: EUR/USD daily 2019-2021 (907 windows)
|
||||||
|
- OOS: EUR/USD daily 2022-2023 (593 windows)
|
||||||
|
- HV label: top-33% realized-vol days = high-volatility (196 days)
|
||||||
|
|
||||||
|
## Results
|
||||||
|
| | Value | Gate |
|
||||||
|
|---|---|---|
|
||||||
|
| TS-JEPA mean silhouette (3 seeds) | 0.0182 | > 0.20 → **False** |
|
||||||
|
| Beats PCA baseline (0.1359) | 0.0182 | > PCA → **False** |
|
||||||
|
| Seed stability (spread) | 86.73% | < 10% → **False** |
|
||||||
|
| PC1/HV correlation | 0.5258 | < 0.95 → **True** |
|
||||||
|
|
||||||
|
Per-seed: ['0.0268', '0.0110', '0.0168']
|
||||||
|
|
||||||
|
## Verdict: **NULL**
|
||||||
|
|
||||||
|
One or more gate criteria not met. See null result protocol in #5.
|
||||||
@@ -0,0 +1,237 @@
|
|||||||
|
"""Phase-0 SSL feasibility gate (path B — Python fast-close of #5).
|
||||||
|
|
||||||
|
Spec deviation documented: original spec (#5) required hourly 2008-2022 data
|
||||||
|
and a Go eval harness (#4). Path B uses daily 2019-2023 + Python harness to
|
||||||
|
close the gate quickly, since val_vol_r2 > 0 already demonstrates SSL
|
||||||
|
feasibility. The Go harness (#4) remains open for production experiments.
|
||||||
|
|
||||||
|
Gate criteria (from #5):
|
||||||
|
- Silhouette > 0.20 on held-out 2022-2023 (binary HV label: top-33% RV days)
|
||||||
|
- TS-JEPA silhouette > PCA baseline silhouette
|
||||||
|
- Rerun x3 seeds within ±10% of mean silhouette
|
||||||
|
- PC1/HV correlation < 0.95 (sanity: not trivially memorising the label)
|
||||||
|
|
||||||
|
python scripts/phase0_gate.py
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import math
|
||||||
|
import os
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
from sklearn.decomposition import PCA
|
||||||
|
from sklearn.metrics import silhouette_score
|
||||||
|
from sklearn.preprocessing import StandardScaler
|
||||||
|
|
||||||
|
SEEDS = [0, 1, 2]
|
||||||
|
WINDOW = 30
|
||||||
|
PATCH_LEN = 5
|
||||||
|
STRIDE = 5
|
||||||
|
D_MODEL = 32
|
||||||
|
DEPTH = 2
|
||||||
|
N_HEADS = 4
|
||||||
|
EPOCHS = 400
|
||||||
|
LR = 3e-4
|
||||||
|
SIGREG_LAM = 0.5
|
||||||
|
HV_PERCENTILE = 67 # top-33% = "high volatility"
|
||||||
|
|
||||||
|
dev = "cuda" if torch.cuda.is_available() else "cpu"
|
||||||
|
|
||||||
|
|
||||||
|
# ── SIGReg ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def sigreg(tokens: torch.Tensor, knots: int = 17) -> torch.Tensor:
|
||||||
|
B, T, D = tokens.shape
|
||||||
|
z = tokens.reshape(B * T, D).float()
|
||||||
|
t = torch.linspace(0, 3, knots, device=z.device, dtype=z.dtype)
|
||||||
|
dt = 3.0 / (knots - 1)
|
||||||
|
w = torch.full((knots,), 2 * dt, device=z.device, dtype=z.dtype)
|
||||||
|
w[0] = dt; w[-1] = dt
|
||||||
|
phi = torch.exp(-t.square() / 2.0)
|
||||||
|
A = torch.randn(D, 256, device=z.device, dtype=z.dtype)
|
||||||
|
A = A / A.norm(p=2, dim=0)
|
||||||
|
x_t = (z @ A).unsqueeze(-1) * t
|
||||||
|
err = (x_t.cos().mean(0) - phi).square() + x_t.sin().mean(0).square()
|
||||||
|
return ((err @ (w * phi)) * z.shape[0]).mean()
|
||||||
|
|
||||||
|
|
||||||
|
# ── Encoder ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
class PatchEncoder(nn.Module):
|
||||||
|
def __init__(self, in_feats, patch_len, stride, d_model, depth, n_heads):
|
||||||
|
super().__init__()
|
||||||
|
self.patch_len = patch_len
|
||||||
|
self.stride = stride
|
||||||
|
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):
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
# ── Data ─────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def load_data():
|
||||||
|
df = pd.read_parquet("data/processed/eurusd_daily.parquet").reset_index(drop=True)
|
||||||
|
df["date"] = pd.to_datetime(df["date"])
|
||||||
|
train = df[df["date"].dt.year <= 2021].copy()
|
||||||
|
oos = df[df["date"].dt.year >= 2022].copy()
|
||||||
|
|
||||||
|
feats_all = df[["ret", "realized_vol"]].to_numpy(np.float32)
|
||||||
|
target_all = df["realized_vol"].to_numpy(np.float32)
|
||||||
|
dates_all = df["date"].values
|
||||||
|
|
||||||
|
mu = feats_all[:len(train)].mean(0)
|
||||||
|
sd = feats_all[:len(train)].std(0) + 1e-8
|
||||||
|
|
||||||
|
def windows(df_subset, feats_norm, dates):
|
||||||
|
idx_start = df.index[df["date"].isin(df_subset["date"])][0]
|
||||||
|
X, oos_dates, oos_rv = [], [], []
|
||||||
|
for t in range(idx_start + WINDOW, idx_start + len(df_subset)):
|
||||||
|
X.append(feats_norm[t - WINDOW:t])
|
||||||
|
oos_dates.append(dates[t])
|
||||||
|
oos_rv.append(target_all[t])
|
||||||
|
return np.stack(X), np.array(oos_rv), np.array(oos_dates)
|
||||||
|
|
||||||
|
feats_norm = (feats_all - mu) / sd
|
||||||
|
|
||||||
|
Xtr, rvtr, _ = windows(train, feats_norm, dates_all)
|
||||||
|
Xte, rvte, te_dates = windows(oos, feats_norm, dates_all)
|
||||||
|
|
||||||
|
# binary HV label: top-33% realized vol days in OOS = "high volatility"
|
||||||
|
hv_threshold = np.percentile(rvte, HV_PERCENTILE)
|
||||||
|
hv_labels = (rvte >= hv_threshold).astype(int)
|
||||||
|
|
||||||
|
return Xtr, rvtr, Xte, rvte, hv_labels
|
||||||
|
|
||||||
|
|
||||||
|
# ── Train + embed ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def train_and_embed(Xtr, Xte, seed):
|
||||||
|
torch.manual_seed(seed)
|
||||||
|
np.random.seed(seed)
|
||||||
|
enc = PatchEncoder(Xtr.shape[2], PATCH_LEN, STRIDE, D_MODEL, DEPTH, N_HEADS).to(dev)
|
||||||
|
pred = nn.Sequential(nn.Linear(D_MODEL, D_MODEL), nn.GELU(),
|
||||||
|
nn.Linear(D_MODEL, D_MODEL)).to(dev)
|
||||||
|
opt = torch.optim.AdamW(list(enc.parameters()) + list(pred.parameters()), lr=LR)
|
||||||
|
|
||||||
|
Xtr_t = torch.tensor(Xtr, device=dev)
|
||||||
|
n_patches = (WINDOW - PATCH_LEN) // STRIDE + 1
|
||||||
|
n_mask = max(1, int(0.30 * n_patches))
|
||||||
|
|
||||||
|
for ep in range(EPOCHS):
|
||||||
|
idx_mask = torch.randperm(n_patches)[:n_mask]
|
||||||
|
tokens_ctx = enc(Xtr_t)
|
||||||
|
tokens_target = enc(Xtr_t).detach()
|
||||||
|
jepa_loss = ((pred(tokens_ctx[:, idx_mask, :]) -
|
||||||
|
tokens_target[:, idx_mask, :]) ** 2).mean()
|
||||||
|
reg = sigreg(tokens_ctx)
|
||||||
|
loss = jepa_loss + SIGREG_LAM * reg
|
||||||
|
opt.zero_grad(); loss.backward(); opt.step()
|
||||||
|
|
||||||
|
enc.eval()
|
||||||
|
with torch.no_grad():
|
||||||
|
Ete = enc(torch.tensor(Xte, device=dev)).mean(1).cpu().numpy()
|
||||||
|
|
||||||
|
return Ete
|
||||||
|
|
||||||
|
|
||||||
|
# ── Gate ─────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def pca_baseline(Xte, hv_labels):
|
||||||
|
flat = Xte.reshape(len(Xte), -1)
|
||||||
|
sc = StandardScaler().fit(flat)
|
||||||
|
emb = PCA(n_components=8).fit_transform(sc.transform(flat))
|
||||||
|
return silhouette_score(emb, hv_labels), emb
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
os.makedirs("results/summaries", exist_ok=True)
|
||||||
|
Xtr, rvtr, Xte, rvte, hv_labels = load_data()
|
||||||
|
print(f"train={len(Xtr)} OOS={len(Xte)} HV={hv_labels.sum()}/{len(hv_labels)}")
|
||||||
|
|
||||||
|
pca_sil, pca_emb = pca_baseline(Xte, hv_labels)
|
||||||
|
pc1 = pca_emb[:, 0]
|
||||||
|
pc1_hv_corr = abs(np.corrcoef(pc1, hv_labels)[0, 1])
|
||||||
|
print(f"PCA baseline silhouette = {pca_sil:.4f} | PC1/HV |r| = {pc1_hv_corr:.4f}")
|
||||||
|
|
||||||
|
sils = []
|
||||||
|
for seed in SEEDS:
|
||||||
|
emb = train_and_embed(Xtr, Xte, seed)
|
||||||
|
sc = StandardScaler().fit(emb)
|
||||||
|
sil = silhouette_score(sc.transform(emb), hv_labels)
|
||||||
|
sils.append(sil)
|
||||||
|
print(f" seed={seed} silhouette={sil:.4f}")
|
||||||
|
|
||||||
|
mean_sil = np.mean(sils)
|
||||||
|
spread = (max(sils) - min(sils)) / mean_sil if mean_sil != 0 else 99
|
||||||
|
|
||||||
|
# gate checks
|
||||||
|
g_sil = mean_sil > 0.20
|
||||||
|
g_beats = mean_sil > pca_sil
|
||||||
|
g_stable = spread < 0.10
|
||||||
|
g_corr = pc1_hv_corr < 0.95
|
||||||
|
passed = all([g_sil, g_beats, g_stable, g_corr])
|
||||||
|
|
||||||
|
label = "pass" if passed else "null"
|
||||||
|
print(f"\nsilhouette mean={mean_sil:.4f} spread={spread:.2%} PCA={pca_sil:.4f} PC1/HV={pc1_hv_corr:.4f}")
|
||||||
|
print(f"gate: sil>0.20={g_sil} beats_pca={g_beats} stable={g_stable} corr<0.95={g_corr}")
|
||||||
|
print(f"PHASE-0: {label.upper()}")
|
||||||
|
|
||||||
|
summary = f"""# Phase-0 SSL feasibility gate — {label}
|
||||||
|
|
||||||
|
**Date:** 2026-06-24
|
||||||
|
**Path B deviation:** Daily 2019-2023 (not hourly 2008-2022); Python harness
|
||||||
|
(not Go #4); gate metric adapted from silhouette-on-embedding to match
|
||||||
|
available data. Go harness (#4) remains open for production experiments.
|
||||||
|
|
||||||
|
## Data
|
||||||
|
- Train: EUR/USD daily 2019-2021 ({len(Xtr)} windows)
|
||||||
|
- OOS: EUR/USD daily 2022-2023 ({len(Xte)} windows)
|
||||||
|
- HV label: top-{100-HV_PERCENTILE}% realized-vol days = high-volatility ({hv_labels.sum()} days)
|
||||||
|
|
||||||
|
## Results
|
||||||
|
| | Value | Gate |
|
||||||
|
|---|---|---|
|
||||||
|
| TS-JEPA mean silhouette (3 seeds) | {mean_sil:.4f} | > 0.20 → **{g_sil}** |
|
||||||
|
| Beats PCA baseline ({pca_sil:.4f}) | {mean_sil:.4f} | > PCA → **{g_beats}** |
|
||||||
|
| Seed stability (spread) | {spread:.2%} | < 10% → **{g_stable}** |
|
||||||
|
| PC1/HV correlation | {pc1_hv_corr:.4f} | < 0.95 → **{g_corr}** |
|
||||||
|
|
||||||
|
Per-seed: {[f"{s:.4f}" for s in sils]}
|
||||||
|
|
||||||
|
## Verdict: **{label.upper()}**
|
||||||
|
|
||||||
|
{"All 4 gate criteria met. TS-JEPA embeddings separate HV regimes significantly above PCA baseline with stable reproducibility." if passed else "One or more gate criteria not met. See null result protocol in #5."}
|
||||||
|
"""
|
||||||
|
path = f"results/summaries/phase-0-{label}.md"
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write(summary)
|
||||||
|
print(f"Written: {path}")
|
||||||
|
|
||||||
|
result = {"label": label, "mean_sil": mean_sil, "pca_sil": pca_sil,
|
||||||
|
"spread": spread, "pc1_hv_corr": pc1_hv_corr,
|
||||||
|
"per_seed": sils, "passed": passed}
|
||||||
|
with open("results/summaries/phase-0-metrics.json", "w") as f:
|
||||||
|
json.dump(result, f, indent=2)
|
||||||
|
return 0 if passed else 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -1,24 +1,33 @@
|
|||||||
"""train.py — the ONLY file the autoresearch agent may edit (Phase-1 contract).
|
"""train.py — autoresearch agent file (only this may be edited).
|
||||||
|
|
||||||
Toy slice: a tiny self-supervised encoder (masked reconstruction of windowed
|
TS-JEPA backbone with SIGReg regularization (Balestriero & LeCun, LeJEPA
|
||||||
daily [return, realized_vol]) → FROZEN → linear probe predicts NEXT-day realized
|
arXiv:2511.08544; time-series placement from ChronoJEPA arXiv: 2505.XXXXX).
|
||||||
vol → val_vol_r2 = OOS R². The agent improves val_vol_r2 by editing the encoder /
|
|
||||||
objective / masking below. Writes metrics.json (the scalar the loop reads).
|
|
||||||
|
|
||||||
python train.py
|
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 json
|
||||||
|
import math
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import torch
|
import torch
|
||||||
import torch.nn as nn
|
import torch.nn as nn
|
||||||
|
|
||||||
# --- agent-tunable knobs ---
|
# --- agent-tunable knobs ---
|
||||||
WINDOW = 20
|
WINDOW = 60 # INCREASED lookback for better volatility persistence capture
|
||||||
EMBED_DIM = 64
|
PATCH_LEN = 5 # time-patch size (must divide WINDOW)
|
||||||
MASK_FRAC = 0.40
|
STRIDE = 5
|
||||||
EPOCHS = 200
|
D_MODEL = 64 # transformer hidden dim - INCREASED for capacity
|
||||||
LR = 1e-3
|
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
|
SEED = 0
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
|
||||||
@@ -27,65 +36,137 @@ np.random.seed(SEED)
|
|||||||
dev = "cuda" if torch.cuda.is_available() else "cpu"
|
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():
|
def build():
|
||||||
df = pd.read_parquet("data/processed/eurusd_daily.parquet").reset_index(drop=True)
|
df = pd.read_parquet("data/processed/eurusd_daily.parquet").reset_index(drop=True)
|
||||||
feats = df[["ret", "realized_vol"]].to_numpy(np.float32)
|
feats = df[["ret", "realized_vol"]].to_numpy(np.float32)
|
||||||
target = df["realized_vol"].to_numpy(np.float32) # predict NEXT-day RV
|
target = df["realized_vol"].to_numpy(np.float32)
|
||||||
X, y = [], []
|
X, y = [], []
|
||||||
for t in range(WINDOW, len(df) - 1):
|
for t in range(WINDOW, len(df) - 1):
|
||||||
X.append(feats[t - WINDOW:t])
|
X.append(feats[t - WINDOW:t])
|
||||||
y.append(target[t + 1])
|
y.append(target[t + 1])
|
||||||
X = np.stack(X); y = np.array(y, np.float32)
|
X = np.stack(X); y = np.array(y, np.float32)
|
||||||
n_tr = int(0.7 * len(X)) # time-ordered OOS split
|
n_tr = int(0.7 * len(X))
|
||||||
mu, sd = X[:n_tr].mean((0, 1)), X[:n_tr].std((0, 1)) + 1e-8 # train-only stats
|
mu = X[:n_tr].mean((0, 1))
|
||||||
|
sd = X[:n_tr].std((0, 1)) + 1e-8
|
||||||
X = (X - mu) / sd
|
X = (X - mu) / sd
|
||||||
return (X[:n_tr], y[:n_tr]), (X[n_tr:], y[n_tr:])
|
return (X[:n_tr], y[:n_tr]), (X[n_tr:], y[n_tr:])
|
||||||
|
|
||||||
|
|
||||||
class Encoder(nn.Module):
|
# ── Training ─────────────────────────────────────────────────────────────────
|
||||||
def __init__(self, win, emb):
|
|
||||||
super().__init__()
|
|
||||||
self.net = nn.Sequential(
|
|
||||||
nn.Flatten(),
|
|
||||||
nn.Linear(win * 2, 128),
|
|
||||||
nn.LayerNorm(128),
|
|
||||||
nn.GELU(),
|
|
||||||
nn.Linear(128, emb)
|
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
return self.net(x)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
(Xtr, ytr), (Xte, yte) = build()
|
(Xtr, ytr), (Xte, yte) = build()
|
||||||
|
n_feats = Xtr.shape[2]
|
||||||
Xtr_t = torch.tensor(Xtr, device=dev)
|
Xtr_t = torch.tensor(Xtr, device=dev)
|
||||||
enc = Encoder(WINDOW, EMBED_DIM).to(dev)
|
enc = PatchEncoder(n_feats, PATCH_LEN, STRIDE, D_MODEL, DEPTH, N_HEADS).to(dev)
|
||||||
dec = nn.Sequential(nn.Linear(EMBED_DIM, 128), nn.GELU(), nn.Linear(128, WINDOW * 2)).to(dev)
|
pred = Predictor(D_MODEL).to(dev)
|
||||||
opt = torch.optim.Adam(list(enc.parameters()) + list(dec.parameters()), lr=LR)
|
opt = torch.optim.AdamW(list(enc.parameters()) + list(pred.parameters()), lr=LR)
|
||||||
|
|
||||||
for _ in range(EPOCHS): # SSL: masked reconstruction of the window
|
n_patches = (WINDOW - PATCH_LEN) // STRIDE + 1
|
||||||
mask = (torch.rand_like(Xtr_t) > MASK_FRAC).float()
|
n_mask = max(1, int(MASK_FRAC * n_patches))
|
||||||
rec = dec(enc((Xtr_t * mask)))
|
|
||||||
loss = (((rec - Xtr_t.flatten(1)) ** 2) * (1 - mask.flatten(1))).mean()
|
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()
|
opt.zero_grad(); loss.backward(); opt.step()
|
||||||
|
|
||||||
enc.eval()
|
enc.eval()
|
||||||
with torch.no_grad(): # FROZEN embeddings
|
with torch.no_grad():
|
||||||
Etr = enc(Xtr_t).cpu().numpy()
|
def embed(X_np):
|
||||||
Ete = enc(torch.tensor(Xte, device=dev)).cpu().numpy()
|
t = torch.tensor(X_np, device=dev)
|
||||||
|
return enc(t).mean(1).cpu().numpy() # pool over time patches
|
||||||
|
|
||||||
# linear probe (ridge, closed form) on frozen embeddings → val_vol_r2 (OOS R²)
|
Etr = embed(Xtr)
|
||||||
|
Ete = embed(Xte)
|
||||||
|
|
||||||
|
# ridge linear probe (closed form)
|
||||||
A = np.hstack([Etr, np.ones((len(Etr), 1))])
|
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)
|
w = np.linalg.solve(A.T @ A + 1e-3 * np.eye(A.shape[1]), A.T @ ytr)
|
||||||
pred = np.hstack([Ete, np.ones((len(Ete), 1))]) @ w
|
pred_np = np.hstack([Ete, np.ones((len(Ete), 1))]) @ w
|
||||||
ss_res = ((yte - pred) ** 2).sum()
|
ss_res = ((yte - pred_np) ** 2).sum()
|
||||||
ss_tot = ((yte - yte.mean()) ** 2).sum()
|
ss_tot = ((yte - yte.mean()) ** 2).sum()
|
||||||
val_vol_r2 = float(1 - ss_res / ss_tot)
|
val_vol_r2 = float(1 - ss_res / ss_tot)
|
||||||
|
|
||||||
json.dump({"val_vol_r2": val_vol_r2, "n_test": len(yte),
|
json.dump({
|
||||||
"knobs": {"WINDOW": WINDOW, "EMBED_DIM": EMBED_DIM, "MASK_FRAC": MASK_FRAC, "EPOCHS": EPOCHS}},
|
"val_vol_r2": val_vol_r2, "n_test": len(yte),
|
||||||
open("metrics.json", "w"), indent=2)
|
"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))
|
print("val_vol_r2 = %.4f (n_test=%d, dev=%s)" % (val_vol_r2, len(yte), dev))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user