generated from mathias/template-go-web
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bde651b0df | ||
|
|
20aeecb971 | ||
|
|
e11e7d2524 | ||
|
|
f01bdde7c2 | ||
|
|
3445b6d267 |
+15
-3
@@ -5,16 +5,28 @@ tasks:
|
||||
desc: Run templ generate
|
||||
cmds: [templ generate]
|
||||
build:
|
||||
desc: Build the binary
|
||||
desc: Build all binaries
|
||||
deps: [generate]
|
||||
cmds: [go build -o bin/hostexecutor ./cmd/hostexecutor]
|
||||
cmds:
|
||||
- go build -o bin/jepa-fx-risk ./cmd/jepa-fx-risk
|
||||
- go build -o bin/eval ./cmd/eval
|
||||
run:
|
||||
deps: [build]
|
||||
cmds: [./bin/hostexecutor]
|
||||
cmds: [./bin/jepa-fx-risk]
|
||||
test:
|
||||
desc: Run all tests
|
||||
deps: [generate]
|
||||
cmds: [go test ./... -race]
|
||||
|
||||
eval:probe:
|
||||
desc: "Run linear-probe (val_vol_r2) on embeddings from metrics.json"
|
||||
cmds: [./bin/eval -metric probe]
|
||||
eval:silhouette:
|
||||
desc: "Run silhouette on embeddings vs binary HV labels"
|
||||
cmds: [./bin/eval -metric silhouette]
|
||||
eval:collapse:
|
||||
desc: "Run effective-rank collapse diagnostic"
|
||||
cmds: [./bin/eval -metric erank]
|
||||
lint:
|
||||
cmds: [golangci-lint run ./...]
|
||||
check:
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
// cmd/eval — CLI driver for the jepa-fx-risk evaluation harness.
|
||||
//
|
||||
// ./bin/eval -metric probe|silhouette|erank [-emb embeddings.json]
|
||||
//
|
||||
// embeddings.json format (from train.py EXPORT_EMBEDDINGS=1):
|
||||
//
|
||||
// {
|
||||
// "embeddings": [[...], ...], // OOS frozen embeddings
|
||||
// "realized_vol": [...], // OOS target (next-day RV)
|
||||
// "hv_label": [...], // binary HV label (top-33%)
|
||||
// "train_embeddings": [[...], ...], // train-set frozen embeddings
|
||||
// "train_realized_vol": [...] // train-set RV targets
|
||||
// }
|
||||
//
|
||||
// eval:probe standardises both sets using train statistics (no leakage).
|
||||
// Falls back to internal 70/30 split of OOS if train_embeddings absent.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
"gitea.d-ma.be/mathias/jepa-fx-risk/internal/eval"
|
||||
)
|
||||
|
||||
func main() {
|
||||
metric := flag.String("metric", "probe", "probe | silhouette | erank")
|
||||
embFile := flag.String("emb", "embeddings.json", "path to embeddings JSON")
|
||||
flag.Parse()
|
||||
|
||||
log := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
|
||||
d, err := readJSON(*embFile)
|
||||
if err != nil {
|
||||
log.Error("load embeddings", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Info("loaded", "oos", len(d.Embeddings), "dim", len(d.Embeddings[0]),
|
||||
"train", len(d.TrainEmbeddings), "metric", *metric)
|
||||
|
||||
switch *metric {
|
||||
case "probe":
|
||||
var r2 float64
|
||||
if len(d.TrainEmbeddings) > 0 {
|
||||
// standardise both sets using train statistics to prevent leakage
|
||||
trEmb, mu, sd := standardiseCompute(d.TrainEmbeddings)
|
||||
oosEmb := applyStandardise(d.Embeddings, mu, sd)
|
||||
r2 = eval.LinearProbeTrainTest(trEmb, d.TrainRealizedVol, oosEmb, d.RealizedVol, 1e-3)
|
||||
log.Info("probe mode", "fit_on", "train_embeddings", "eval_on", "oos")
|
||||
} else {
|
||||
// fallback: internal 70/30 split of OOS embeddings
|
||||
oosEmb, mu, sd := standardiseCompute(d.Embeddings)
|
||||
n70 := int(float64(len(oosEmb)) * 0.7)
|
||||
oos70 := applyStandardise(d.Embeddings[n70:], mu, sd)
|
||||
r2 = eval.LinearProbeTrainTest(oosEmb[:n70], d.RealizedVol[:n70],
|
||||
oos70, d.RealizedVol[n70:], 1e-3)
|
||||
log.Info("probe mode", "fit_on", "oos[0:70%]", "eval_on", "oos[70%:]")
|
||||
}
|
||||
fmt.Printf(`{"metric":"val_vol_r2","value":%.6f}`+"\n", r2)
|
||||
log.Info("linear probe", "val_vol_r2", fmt.Sprintf("%.4f", r2))
|
||||
|
||||
case "silhouette":
|
||||
if len(d.HVLabel) == 0 {
|
||||
log.Error("silhouette requires hv_label in embeddings.json")
|
||||
os.Exit(1)
|
||||
}
|
||||
oosEmb := standardise(d.Embeddings)
|
||||
sil, err := eval.Silhouette(oosEmb, d.HVLabel)
|
||||
if err != nil {
|
||||
log.Error("silhouette", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf(`{"metric":"silhouette","value":%.6f}`+"\n", sil)
|
||||
log.Info("silhouette", "score", fmt.Sprintf("%.4f", sil))
|
||||
|
||||
case "erank":
|
||||
oosEmb := standardise(d.Embeddings)
|
||||
er := eval.EffectiveRank(oosEmb)
|
||||
fmt.Printf(`{"metric":"effective_rank","value":%.6f}`+"\n", er)
|
||||
log.Info("effective rank", "erank", fmt.Sprintf("%.2f", er))
|
||||
|
||||
default:
|
||||
log.Error("unknown metric", "metric", *metric)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
type embJSON struct {
|
||||
Embeddings [][]float64 `json:"embeddings"`
|
||||
Dates []string `json:"dates"`
|
||||
RealizedVol []float64 `json:"realized_vol"`
|
||||
HVLabel []int `json:"hv_label"`
|
||||
TrainEmbeddings [][]float64 `json:"train_embeddings"`
|
||||
TrainRealizedVol []float64 `json:"train_realized_vol"`
|
||||
}
|
||||
|
||||
func readJSON(path string) (*embJSON, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open %s: %w", path, err)
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
var d embJSON
|
||||
if err := json.NewDecoder(f).Decode(&d); err != nil {
|
||||
return nil, fmt.Errorf("decode: %w", err)
|
||||
}
|
||||
if len(d.Embeddings) == 0 {
|
||||
return nil, fmt.Errorf("empty embeddings in %s", path)
|
||||
}
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
// standardise centres + scales to zero mean / unit std; returns normalised rows.
|
||||
func standardise(rows [][]float64) [][]float64 {
|
||||
out, _, _ := standardiseCompute(rows)
|
||||
return out
|
||||
}
|
||||
|
||||
// standardiseCompute centres + scales and returns (normalised, mu, sd) for reuse.
|
||||
func standardiseCompute(rows [][]float64) ([][]float64, []float64, []float64) {
|
||||
if len(rows) == 0 {
|
||||
return rows, nil, nil
|
||||
}
|
||||
n, dim := len(rows), len(rows[0])
|
||||
mu := make([]float64, dim)
|
||||
for _, r := range rows {
|
||||
for j, v := range r {
|
||||
mu[j] += v
|
||||
}
|
||||
}
|
||||
for j := range mu {
|
||||
mu[j] /= float64(n)
|
||||
}
|
||||
sd := make([]float64, dim)
|
||||
for _, r := range rows {
|
||||
for j, v := range r {
|
||||
diff := v - mu[j]
|
||||
sd[j] += diff * diff
|
||||
}
|
||||
}
|
||||
for j := range sd {
|
||||
sd[j] = math.Sqrt(sd[j]/float64(n)) + 1e-8
|
||||
}
|
||||
out := make([][]float64, n)
|
||||
for i, r := range rows {
|
||||
out[i] = make([]float64, dim)
|
||||
for j, v := range r {
|
||||
out[i][j] = (v - mu[j]) / sd[j]
|
||||
}
|
||||
}
|
||||
return out, mu, sd
|
||||
}
|
||||
|
||||
// applyStandardise normalises rows using pre-computed mu and sd.
|
||||
func applyStandardise(rows [][]float64, mu, sd []float64) [][]float64 {
|
||||
out := make([][]float64, len(rows))
|
||||
for i, r := range rows {
|
||||
out[i] = make([]float64, len(r))
|
||||
for j, v := range r {
|
||||
out[i][j] = (v - mu[j]) / sd[j]
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"gitea.d-ma.be/mathias/hostexecutor/internal/web"
|
||||
"gitea.d-ma.be/mathias/jepa-fx-risk/internal/web"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -1,7 +1,5 @@
|
||||
module gitea.d-ma.be/mathias/hostexecutor
|
||||
module gitea.d-ma.be/mathias/jepa-fx-risk
|
||||
|
||||
go 1.26
|
||||
|
||||
require (
|
||||
github.com/a-h/templ v0.2.778
|
||||
)
|
||||
require github.com/a-h/templ v0.3.1020
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
github.com/a-h/templ v0.3.1020 h1:ypAT/L5ySWEnZ6Zft/5yfoWXYYkhFNvEFOeeqecg4tw=
|
||||
github.com/a-h/templ v0.3.1020/go.mod h1:A2DlK61v+K+NRoGnhmYbNYVmtYHcFO5/AisMvBdDxTM=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
@@ -0,0 +1,383 @@
|
||||
// Package eval implements the Go evaluation harness for jepa-fx-risk (#4).
|
||||
// Three diagnostics on frozen embeddings exported from train.py:
|
||||
// - LinearProbe — val_vol_r2: OOS R² of a ridge probe predicting next-day realized vol
|
||||
// - Silhouette — mean silhouette score of embeddings vs a binary label (HV regime)
|
||||
// - EffectiveRank — Roy's effective rank: exp(H(σ²)) where H is entropy of normalised singular values
|
||||
package eval
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
)
|
||||
|
||||
// LinearProbeTrainTest fits ridge regression on (trainEmb, trainY) and evaluates
|
||||
// on (testEmb, testY). Returns OOS R². Use this for proper held-out evaluation.
|
||||
func LinearProbeTrainTest(trainEmb [][]float64, trainY []float64,
|
||||
testEmb [][]float64, testY []float64, lambda float64) float64 {
|
||||
n := len(trainEmb)
|
||||
if n == 0 || len(testEmb) == 0 {
|
||||
return 0
|
||||
}
|
||||
d := len(trainEmb[0])
|
||||
p := d + 1
|
||||
|
||||
A := make([][]float64, n)
|
||||
for i, e := range trainEmb {
|
||||
row := make([]float64, p)
|
||||
copy(row, e)
|
||||
row[d] = 1.0
|
||||
A[i] = row
|
||||
}
|
||||
AtA := make([][]float64, p)
|
||||
for i := range AtA {
|
||||
AtA[i] = make([]float64, p)
|
||||
}
|
||||
Aty := make([]float64, p)
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < p; j++ {
|
||||
Aty[j] += A[i][j] * trainY[i]
|
||||
for k := 0; k < p; k++ {
|
||||
AtA[j][k] += A[i][j] * A[i][k]
|
||||
}
|
||||
}
|
||||
}
|
||||
for j := 0; j < p; j++ {
|
||||
AtA[j][j] += lambda
|
||||
}
|
||||
w := solveCholesky(AtA, Aty)
|
||||
|
||||
yMean := mean(testY)
|
||||
var ssRes, ssTot float64
|
||||
for i, e := range testEmb {
|
||||
row := make([]float64, p)
|
||||
copy(row, e)
|
||||
row[d] = 1.0
|
||||
pred := dot(row, w)
|
||||
ssRes += (testY[i] - pred) * (testY[i] - pred)
|
||||
ssTot += (testY[i] - yMean) * (testY[i] - yMean)
|
||||
}
|
||||
if ssTot == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1 - ssRes/ssTot
|
||||
}
|
||||
|
||||
// LinearProbe fits a ridge regression (closed-form) on (emb, y) with regularisation λ
|
||||
// and returns R² on the same data. Call with train embeddings; probe on held-out by
|
||||
// splitting before calling.
|
||||
//
|
||||
// emb[i] is the embedding vector for sample i; y[i] is the scalar target.
|
||||
func LinearProbe(emb [][]float64, y []float64, lambda float64) float64 {
|
||||
n := len(emb)
|
||||
if n == 0 {
|
||||
return 0
|
||||
}
|
||||
d := len(emb[0])
|
||||
|
||||
// Build augmented design matrix A = [emb | 1] (n × d+1)
|
||||
A := make([][]float64, n)
|
||||
for i, e := range emb {
|
||||
row := make([]float64, d+1)
|
||||
copy(row, e)
|
||||
row[d] = 1.0
|
||||
A[i] = row
|
||||
}
|
||||
|
||||
// Normal equations: (AᵀA + λI) w = Aᵀy (ridge)
|
||||
p := d + 1
|
||||
AtA := make([][]float64, p)
|
||||
for i := range AtA {
|
||||
AtA[i] = make([]float64, p)
|
||||
}
|
||||
Aty := make([]float64, p)
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < p; j++ {
|
||||
Aty[j] += A[i][j] * y[i]
|
||||
for k := 0; k < p; k++ {
|
||||
AtA[j][k] += A[i][j] * A[i][k]
|
||||
}
|
||||
}
|
||||
}
|
||||
for j := 0; j < p; j++ {
|
||||
AtA[j][j] += lambda
|
||||
}
|
||||
|
||||
w := solveCholesky(AtA, Aty)
|
||||
|
||||
// R² = 1 - SS_res / SS_tot
|
||||
yMean := mean(y)
|
||||
var ssRes, ssTot float64
|
||||
for i := 0; i < n; i++ {
|
||||
pred := dot(A[i], w)
|
||||
ssRes += (y[i] - pred) * (y[i] - pred)
|
||||
ssTot += (y[i] - yMean) * (y[i] - yMean)
|
||||
}
|
||||
if ssTot == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1 - ssRes/ssTot
|
||||
}
|
||||
|
||||
// Silhouette returns the mean silhouette coefficient of the embeddings with respect
|
||||
// to the given integer labels. Distances are Euclidean. Returns an error if fewer
|
||||
// than 2 distinct labels are present.
|
||||
func Silhouette(emb [][]float64, labels []int) (float64, error) {
|
||||
n := len(emb)
|
||||
if n == 0 {
|
||||
return 0, errors.New("eval: empty embeddings")
|
||||
}
|
||||
|
||||
// count distinct labels
|
||||
labelSet := map[int]struct{}{}
|
||||
for _, l := range labels {
|
||||
labelSet[l] = struct{}{}
|
||||
}
|
||||
if len(labelSet) < 2 {
|
||||
return 0, errors.New("eval: silhouette requires at least 2 distinct labels")
|
||||
}
|
||||
|
||||
// group indices by label
|
||||
groups := map[int][]int{}
|
||||
for i, l := range labels {
|
||||
groups[l] = append(groups[l], i)
|
||||
}
|
||||
|
||||
var total float64
|
||||
for i := 0; i < n; i++ {
|
||||
li := labels[i]
|
||||
|
||||
// a(i) = mean intra-cluster distance
|
||||
var aSum float64
|
||||
inGroup := groups[li]
|
||||
for _, j := range inGroup {
|
||||
if j != i {
|
||||
aSum += euclidean(emb[i], emb[j])
|
||||
}
|
||||
}
|
||||
var a float64
|
||||
if len(inGroup) > 1 {
|
||||
a = aSum / float64(len(inGroup)-1)
|
||||
}
|
||||
|
||||
// b(i) = min mean inter-cluster distance
|
||||
b := math.MaxFloat64
|
||||
for l, idxs := range groups {
|
||||
if l == li {
|
||||
continue
|
||||
}
|
||||
var dSum float64
|
||||
for _, j := range idxs {
|
||||
dSum += euclidean(emb[i], emb[j])
|
||||
}
|
||||
avg := dSum / float64(len(idxs))
|
||||
if avg < b {
|
||||
b = avg
|
||||
}
|
||||
}
|
||||
|
||||
s := (b - a) / math.Max(a, b)
|
||||
total += s
|
||||
}
|
||||
return total / float64(n), nil
|
||||
}
|
||||
|
||||
// EffectiveRank computes Roy's effective rank of the embedding matrix:
|
||||
// exp(H) where H = -∑ pᵢ log(pᵢ) is the Shannon entropy of the normalised
|
||||
// squared singular values. Returns 1 for a rank-1 matrix and ≈ dim for
|
||||
// a full-rank isotropic matrix.
|
||||
func EffectiveRank(emb [][]float64) float64 {
|
||||
n := len(emb)
|
||||
if n == 0 {
|
||||
return 0
|
||||
}
|
||||
d := len(emb[0])
|
||||
|
||||
// Compute covariance-like matrix CᵀC where C is mean-centered embedding.
|
||||
mu := make([]float64, d)
|
||||
for _, e := range emb {
|
||||
for j, v := range e {
|
||||
mu[j] += v
|
||||
}
|
||||
}
|
||||
for j := range mu {
|
||||
mu[j] /= float64(n)
|
||||
}
|
||||
|
||||
// C = emb - mu (n × d); compute CᵀC (d × d)
|
||||
CtC := make([][]float64, d)
|
||||
for i := range CtC {
|
||||
CtC[i] = make([]float64, d)
|
||||
}
|
||||
for _, e := range emb {
|
||||
for j := 0; j < d; j++ {
|
||||
cj := e[j] - mu[j]
|
||||
for k := 0; k < d; k++ {
|
||||
CtC[j][k] += cj * (e[k] - mu[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Eigenvalues of CᵀC via power iteration approximation isn't great;
|
||||
// use the Frobenius / trace approach: σᵢ² ∝ eigenvalues of CᵀC.
|
||||
// For a pure-Go impl without LAPACK: use the fact that the normalised
|
||||
// squared singular values equal normalised eigenvalues of CᵀC.
|
||||
// Compute them via Jacobi iteration for small d, or use the analytical
|
||||
// formula for 2×2, or use iterative QR for general d.
|
||||
eigs := jacobiEigenvalues(CtC)
|
||||
|
||||
// normalise to sum-1 distribution
|
||||
var sumEig float64
|
||||
for _, v := range eigs {
|
||||
if v > 0 {
|
||||
sumEig += v
|
||||
}
|
||||
}
|
||||
if sumEig == 0 {
|
||||
return 1
|
||||
}
|
||||
var H float64
|
||||
for _, v := range eigs {
|
||||
if v > 0 {
|
||||
p := v / sumEig
|
||||
H -= p * math.Log(p)
|
||||
}
|
||||
}
|
||||
return math.Exp(H)
|
||||
}
|
||||
|
||||
// ── internal helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
func euclidean(a, b []float64) float64 {
|
||||
var s float64
|
||||
for i := range a {
|
||||
d := a[i] - b[i]
|
||||
s += d * d
|
||||
}
|
||||
return math.Sqrt(s)
|
||||
}
|
||||
|
||||
func dot(a, b []float64) float64 {
|
||||
var s float64
|
||||
for i := range a {
|
||||
s += a[i] * b[i]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func mean(y []float64) float64 {
|
||||
var s float64
|
||||
for _, v := range y {
|
||||
s += v
|
||||
}
|
||||
return s / float64(len(y))
|
||||
}
|
||||
|
||||
// solveCholesky solves Ax = b for symmetric positive-definite A via
|
||||
// Cholesky decomposition. Falls back to pseudo-inverse on failure.
|
||||
func solveCholesky(A [][]float64, b []float64) []float64 {
|
||||
n := len(A)
|
||||
// Cholesky decomposition: A = LLᵀ
|
||||
L := make([][]float64, n)
|
||||
for i := range L {
|
||||
L[i] = make([]float64, n)
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j <= i; j++ {
|
||||
s := A[i][j]
|
||||
for k := 0; k < j; k++ {
|
||||
s -= L[i][k] * L[j][k]
|
||||
}
|
||||
if i == j {
|
||||
if s <= 0 {
|
||||
s = 1e-12
|
||||
}
|
||||
L[i][j] = math.Sqrt(s)
|
||||
} else {
|
||||
L[i][j] = s / L[j][j]
|
||||
}
|
||||
}
|
||||
}
|
||||
// Forward substitution Ly = b
|
||||
y := make([]float64, n)
|
||||
for i := 0; i < n; i++ {
|
||||
s := b[i]
|
||||
for k := 0; k < i; k++ {
|
||||
s -= L[i][k] * y[k]
|
||||
}
|
||||
y[i] = s / L[i][i]
|
||||
}
|
||||
// Back substitution Lᵀx = y
|
||||
x := make([]float64, n)
|
||||
for i := n - 1; i >= 0; i-- {
|
||||
s := y[i]
|
||||
for k := i + 1; k < n; k++ {
|
||||
s -= L[k][i] * x[k]
|
||||
}
|
||||
x[i] = s / L[i][i]
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// jacobiEigenvalues returns eigenvalues of a symmetric matrix via Jacobi iteration.
|
||||
func jacobiEigenvalues(A [][]float64) []float64 {
|
||||
n := len(A)
|
||||
// copy
|
||||
a := make([][]float64, n)
|
||||
for i := range a {
|
||||
a[i] = make([]float64, n)
|
||||
copy(a[i], A[i])
|
||||
}
|
||||
|
||||
const maxIter = 100
|
||||
const tol = 1e-10
|
||||
for iter := 0; iter < maxIter; iter++ {
|
||||
// find largest off-diagonal element
|
||||
p, q, amax := 0, 1, 0.0
|
||||
for i := 0; i < n; i++ {
|
||||
for j := i + 1; j < n; j++ {
|
||||
if v := math.Abs(a[i][j]); v > amax {
|
||||
amax = v
|
||||
p, q = i, j
|
||||
}
|
||||
}
|
||||
}
|
||||
if amax < tol {
|
||||
break
|
||||
}
|
||||
// Jacobi rotation
|
||||
theta := 0.5 * math.Atan2(2*a[p][q], a[q][q]-a[p][p])
|
||||
c, s := math.Cos(theta), math.Sin(theta)
|
||||
// apply rotation
|
||||
newA := make([][]float64, n)
|
||||
for i := range newA {
|
||||
newA[i] = make([]float64, n)
|
||||
copy(newA[i], a[i])
|
||||
}
|
||||
app := c*c*a[p][p] + 2*c*s*a[p][q] + s*s*a[q][q]
|
||||
aqq := s*s*a[p][p] - 2*c*s*a[p][q] + c*c*a[q][q]
|
||||
apq := 0.0
|
||||
newA[p][p] = app
|
||||
newA[q][q] = aqq
|
||||
newA[p][q] = apq
|
||||
newA[q][p] = apq
|
||||
for r := 0; r < n; r++ {
|
||||
if r == p || r == q {
|
||||
continue
|
||||
}
|
||||
arp := c*a[r][p] + s*a[r][q]
|
||||
arq := -s*a[r][p] + c*a[r][q]
|
||||
newA[r][p] = arp
|
||||
newA[p][r] = arp
|
||||
newA[r][q] = arq
|
||||
newA[q][r] = arq
|
||||
}
|
||||
a = newA
|
||||
}
|
||||
|
||||
eigs := make([]float64, n)
|
||||
for i := range eigs {
|
||||
eigs[i] = a[i][i]
|
||||
}
|
||||
return eigs
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package eval_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"gitea.d-ma.be/mathias/jepa-fx-risk/internal/eval"
|
||||
)
|
||||
|
||||
func seededRNG(seed int64) *rand.Rand {
|
||||
return rand.New(rand.NewSource(seed))
|
||||
}
|
||||
|
||||
// ── LinearProbe (val_vol_r2) ──────────────────────────────────────────────────
|
||||
|
||||
func TestLinearProbe_Perfect(t *testing.T) {
|
||||
n := 50
|
||||
emb := make([][]float64, n)
|
||||
y := make([]float64, n)
|
||||
for i := range emb {
|
||||
emb[i] = []float64{float64(i)}
|
||||
y[i] = float64(i)
|
||||
}
|
||||
r2 := eval.LinearProbe(emb, y, 1e-3)
|
||||
if r2 < 0.99 {
|
||||
t.Fatalf("perfect predictor: want R²≥0.99, got %.4f", r2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinearProbe_ConstantTarget(t *testing.T) {
|
||||
n := 40
|
||||
emb := make([][]float64, n)
|
||||
y := make([]float64, n)
|
||||
for i := range emb {
|
||||
emb[i] = []float64{float64(i), float64(i * i)}
|
||||
y[i] = 3.0
|
||||
}
|
||||
r2 := eval.LinearProbe(emb, y, 1e-3)
|
||||
if r2 > 0.01 {
|
||||
t.Fatalf("constant target: want R²≤0.01, got %.4f", r2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinearProbe_NoiseEmbedding(t *testing.T) {
|
||||
rng := seededRNG(42)
|
||||
n := 80
|
||||
emb := make([][]float64, n)
|
||||
y := make([]float64, n)
|
||||
for i := range emb {
|
||||
emb[i] = []float64{rng.NormFloat64(), rng.NormFloat64()}
|
||||
y[i] = float64(i)
|
||||
}
|
||||
r2 := eval.LinearProbe(emb, y, 1e-3)
|
||||
if r2 > 0.10 {
|
||||
t.Fatalf("noise embedding: want R²<0.10, got %.4f", r2)
|
||||
}
|
||||
}
|
||||
|
||||
// ── Silhouette ────────────────────────────────────────────────────────────────
|
||||
|
||||
func TestSilhouette_PerfectClusters(t *testing.T) {
|
||||
emb := make([][]float64, 40)
|
||||
labels := make([]int, 40)
|
||||
for i := range emb {
|
||||
if i < 20 {
|
||||
emb[i] = []float64{0.0, 0.0}
|
||||
labels[i] = 0
|
||||
} else {
|
||||
emb[i] = []float64{1000.0, 1000.0}
|
||||
labels[i] = 1
|
||||
}
|
||||
}
|
||||
sil, err := eval.Silhouette(emb, labels)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sil < 0.95 {
|
||||
t.Fatalf("perfect clusters: want sil≥0.95, got %.4f", sil)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSilhouette_SingleLabel(t *testing.T) {
|
||||
emb := [][]float64{{1, 2}, {3, 4}, {5, 6}}
|
||||
labels := []int{0, 0, 0}
|
||||
_, err := eval.Silhouette(emb, labels)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for single-label input")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSilhouette_RandomClusters(t *testing.T) {
|
||||
rng := seededRNG(7)
|
||||
n := 60
|
||||
emb := make([][]float64, n)
|
||||
labels := make([]int, n)
|
||||
for i := range emb {
|
||||
emb[i] = []float64{rng.NormFloat64(), rng.NormFloat64()}
|
||||
labels[i] = i % 2
|
||||
}
|
||||
sil, err := eval.Silhouette(emb, labels)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if math.Abs(sil) > 0.30 {
|
||||
t.Fatalf("random clusters: want |sil|≤0.30, got %.4f", sil)
|
||||
}
|
||||
}
|
||||
|
||||
// ── EffectiveRank ─────────────────────────────────────────────────────────────
|
||||
|
||||
func TestEffectiveRank_Rank1(t *testing.T) {
|
||||
emb := make([][]float64, 30)
|
||||
for i := range emb {
|
||||
emb[i] = []float64{1.0, 2.0, 3.0, 4.0}
|
||||
}
|
||||
er := eval.EffectiveRank(emb)
|
||||
if er > 1.5 {
|
||||
t.Fatalf("rank-1 matrix: want erank≤1.5, got %.4f", er)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveRank_FullRank(t *testing.T) {
|
||||
rng := seededRNG(99)
|
||||
dim := 8
|
||||
emb := make([][]float64, 200)
|
||||
for i := range emb {
|
||||
row := make([]float64, dim)
|
||||
for j := range row {
|
||||
row[j] = rng.NormFloat64()
|
||||
}
|
||||
emb[i] = row
|
||||
}
|
||||
er := eval.EffectiveRank(emb)
|
||||
if er < float64(dim)*0.7 {
|
||||
t.Fatalf("full-rank: want erank≥%.1f, got %.4f", float64(dim)*0.7, er)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
// Code generated by templ - DO NOT EDIT.
|
||||
|
||||
// templ: version: v0.3.1020
|
||||
package web
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
func Index() templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var1 == nil {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<h1 class=\"text-3xl font-semibold mb-6\">hostexecutor</h1><button hx-get=\"/api/hello\" hx-target=\"#out\" class=\"px-4 py-2 bg-slate-900 text-white rounded-md hover:bg-slate-700\">Say hello</button><div id=\"out\" class=\"mt-6 text-slate-700\"></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
templ_7745c5c3_Err = Layout("hostexecutor").Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func Hello(name string) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var3 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var3 == nil {
|
||||
templ_7745c5c3_Var3 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<p>Hello, ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var4 string
|
||||
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(name)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/index.templ`, Line: 15, Col: 17}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "!</p>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
var _ = templruntime.GeneratedTemplate
|
||||
@@ -0,0 +1,61 @@
|
||||
// Code generated by templ - DO NOT EDIT.
|
||||
|
||||
// templ: version: v0.3.1020
|
||||
package web
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
func Layout(title string) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var1 == nil {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><title>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var2 string
|
||||
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(title)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/web/layout.templ`, Line: 9, Col: 17}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</title><script src=\"https://unpkg.com/htmx.org@2.0.0\"></script><script src=\"https://cdn.tailwindcss.com\"></script></head><body class=\"min-h-screen bg-slate-50 text-slate-900 antialiased\"><main class=\"max-w-3xl mx-auto px-6 py-12\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templ_7745c5c3_Var1.Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</main></body></html>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
var _ = templruntime.GeneratedTemplate
|
||||
@@ -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())
|
||||
@@ -0,0 +1,102 @@
|
||||
"""Failing tests for HEPA backbone in train.py.
|
||||
|
||||
Run: cd ~/dev/AI/jepa-fx-risk && .venv/bin/python -m pytest tests/test_hepa.py -v
|
||||
These tests define what the new backbone must satisfy BEFORE implementation.
|
||||
"""
|
||||
import math
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import pytest
|
||||
|
||||
# ── Tests import the classes from train.py ────────────────────────────────────
|
||||
# They will fail until train.py implements: CausalEncoder, HorizonPredictor, vicreg_loss
|
||||
|
||||
|
||||
def _import():
|
||||
import importlib.util, sys
|
||||
spec = importlib.util.spec_from_file_location("train", "train.py")
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mod)
|
||||
return mod
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def train_mod():
|
||||
return _import()
|
||||
|
||||
|
||||
# 1. CausalEncoder exists and has correct output shape
|
||||
def test_causal_encoder_shape(train_mod):
|
||||
enc = train_mod.CausalEncoder(n_channels=2, patch_len=10, d_model=32, n_heads=4, depth=1)
|
||||
x = torch.randn(4, 60, 2)
|
||||
tokens = enc(x) # should return all tokens (B, N, D) for JEPA pretraining
|
||||
assert tokens.shape == (4, 6, 32), f"expected (4, 6, 32), got {tokens.shape}"
|
||||
|
||||
|
||||
# 2. CausalEncoder is actually causal: earlier token outputs don't change when later inputs change
|
||||
def test_causal_masking(train_mod):
|
||||
enc = train_mod.CausalEncoder(n_channels=2, patch_len=10, d_model=32, n_heads=4, depth=2)
|
||||
enc.eval()
|
||||
torch.manual_seed(0)
|
||||
x = torch.randn(1, 60, 2)
|
||||
x_perturbed = x.clone()
|
||||
# non-uniform noise (constant shift absorbed by per-patch LayerNorm; variance change is not)
|
||||
torch.manual_seed(99)
|
||||
x_perturbed[:, 30:, :] += torch.randn_like(x[:, 30:, :]) * 5.0
|
||||
|
||||
with torch.no_grad():
|
||||
h1 = enc(x)
|
||||
h2 = enc(x_perturbed)
|
||||
|
||||
# First 3 tokens must be identical (causal — don't see future patches)
|
||||
assert torch.allclose(h1[:, :3, :], h2[:, :3, :], atol=1e-5), \
|
||||
"causal masking broken: early tokens change when later input changes"
|
||||
# Last token should differ (it can see the perturbed patches)
|
||||
assert not torch.allclose(h1[:, -1, :], h2[:, -1, :], atol=1e-5), \
|
||||
"last token should differ when later input changes"
|
||||
|
||||
|
||||
# 3. HorizonPredictor exists, takes (h, delta_t_float) → same shape as h
|
||||
def test_horizon_predictor_shape(train_mod):
|
||||
pred = train_mod.HorizonPredictor(d_model=32)
|
||||
h = torch.randn(4, 32)
|
||||
dt = torch.tensor([1.0, 2.0, 3.0, 1.0])
|
||||
out = pred(h, dt)
|
||||
assert out.shape == (4, 32), f"expected (4, 32), got {out.shape}"
|
||||
|
||||
|
||||
# 4. vicreg_loss is a scalar and backward doesn't error
|
||||
def test_vicreg_loss_backward(train_mod):
|
||||
h_pred = torch.randn(8, 32, requires_grad=True)
|
||||
h_target = torch.randn(8, 32)
|
||||
loss = train_mod.vicreg_loss(h_pred, h_target, alpha=0.1)
|
||||
assert loss.shape == (), f"expected scalar, got {loss.shape}"
|
||||
loss.backward()
|
||||
assert h_pred.grad is not None
|
||||
|
||||
|
||||
# 5. Full JEPA step: encode context, predict future, compute loss, backward
|
||||
def test_jepa_step_end_to_end(train_mod):
|
||||
enc = train_mod.CausalEncoder(n_channels=2, patch_len=10, d_model=32, n_heads=4, depth=1)
|
||||
pred = train_mod.HorizonPredictor(d_model=32)
|
||||
opt = torch.optim.SGD(list(enc.parameters()) + list(pred.parameters()), lr=1e-3)
|
||||
|
||||
x = torch.randn(4, 60, 2)
|
||||
tokens = enc(x) # (4, 6, 32)
|
||||
c, dt = 2, 2 # context position 2, horizon 2
|
||||
h_ctx = tokens[:, c, :]
|
||||
h_tgt = tokens[:, c + dt, :].detach()
|
||||
h_hat = pred(h_ctx, torch.full((4,), float(dt)))
|
||||
loss = train_mod.vicreg_loss(h_hat, h_tgt, alpha=0.1)
|
||||
opt.zero_grad(); loss.backward(); opt.step()
|
||||
assert loss.item() < 100, "loss exploded"
|
||||
|
||||
|
||||
# 6. build() still returns year-based OOS split (2022-2023)
|
||||
def test_build_year_split(train_mod):
|
||||
(Xtr, ytr), (Xte, yte) = train_mod.build()
|
||||
assert Xtr.shape[1] == train_mod.WINDOW
|
||||
assert Xte.shape[1] == train_mod.WINDOW
|
||||
assert len(Xtr) > 0 and len(Xte) > 0
|
||||
# OOS set should be ~600 windows (2 years of daily data)
|
||||
assert 400 < len(Xte) < 900, f"OOS size unexpected: {len(Xte)}"
|
||||
@@ -1,14 +1,13 @@
|
||||
"""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).
|
||||
HEPA backbone (Petersen et al., arXiv:2605.11130, ICML 2026 Spotlight):
|
||||
Causal Transformer pre-trained via horizon-conditioned JEPA. Predictor
|
||||
maps (h_t, Δt) → predicted future embedding; loss = VICReg (L1 alignment
|
||||
on L2-normalised reps + variance-covariance regulariser, no stop-gradient).
|
||||
Probe: ridge regression on the last-token embedding (true OOS split).
|
||||
|
||||
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.
|
||||
Agent may tune: encoder depth/width, patch geometry, ALPHA, DELTA_T_MAX,
|
||||
optimizer, LR. Do NOT touch prepare_data.py, loop.py, or the data pipeline.
|
||||
"""
|
||||
import json
|
||||
import math
|
||||
@@ -16,19 +15,19 @@ import numpy as np
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
# --- 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
|
||||
WINDOW = 60
|
||||
PATCH_LEN = 10 # non-overlapping patches (6 tokens per window)
|
||||
D_MODEL = 128
|
||||
DEPTH = 2
|
||||
N_HEADS = 4
|
||||
ALPHA = 0.1 # VICReg mixing weight (fixed at 0.1 in HEPA paper)
|
||||
DELTA_T_MAX = 3 # max prediction horizon in patches (1..min(DELTA_T_MAX, N-1-c))
|
||||
EPOCHS = 300
|
||||
LR = 3e-4
|
||||
SEED = 0
|
||||
# ---------------------------
|
||||
|
||||
torch.manual_seed(SEED)
|
||||
@@ -36,139 +35,197 @@ np.random.seed(SEED)
|
||||
dev = "cuda" if torch.cuda.is_available() else "cpu"
|
||||
|
||||
|
||||
# ── SIGReg (from LeJEPA/ChronoJEPA, token-level placement) ─────────────────
|
||||
# ── VICReg pretraining loss ──────────────────────────────────────────────────
|
||||
|
||||
def sigreg(tokens: torch.Tensor, knots: int = 17) -> torch.Tensor:
|
||||
"""Epps-Pulley test statistic pushes token embeddings toward isotropic Gaussian.
|
||||
def vicreg_loss(h_pred: torch.Tensor, h_target: torch.Tensor, alpha: float = 0.1) -> torch.Tensor:
|
||||
"""L = (1-α)·L1(normalize(ĥ), normalize(h*)) + α·(L_var + L_cov).
|
||||
|
||||
tokens: (B, T, D) — applied per-token, averaged across B and T.
|
||||
Both encoders receive gradients (joint training — no stop-grad on h_target).
|
||||
Variance-covariance terms prevent embedding collapse.
|
||||
"""
|
||||
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()
|
||||
pred_n = F.normalize(h_pred, dim=-1)
|
||||
targ_n = F.normalize(h_target, dim=-1)
|
||||
l1 = F.l1_loss(pred_n, targ_n)
|
||||
# variance hinge: push each feature std toward ≥ 1
|
||||
std = h_pred.std(dim=0) + 1e-4
|
||||
l_var = F.relu(1.0 - std).mean()
|
||||
# covariance penalty: decorrelate features
|
||||
B, D = h_pred.shape
|
||||
h_c = h_pred - h_pred.mean(dim=0, keepdim=True)
|
||||
cov = (h_c.t() @ h_c) / max(B - 1, 1)
|
||||
off = cov - torch.diag(torch.diag(cov))
|
||||
l_cov = (off ** 2).sum() / D
|
||||
return (1 - alpha) * l1 + alpha * (l_var + l_cov)
|
||||
|
||||
|
||||
# ── Encoder + Predictor ─────────────────────────────────────────────────────
|
||||
# ── CausalEncoder ─────────────────────────────────────────────────────────────
|
||||
|
||||
class PatchEncoder(nn.Module):
|
||||
"""PatchTST-style encoder for univariate windows."""
|
||||
def __init__(self, in_feats, patch_len, stride, d_model, depth, n_heads):
|
||||
class CausalEncoder(nn.Module):
|
||||
"""Non-overlapping patches → per-patch LayerNorm → causal Transformer → all tokens (B, N, D).
|
||||
|
||||
Per-patch LayerNorm instead of full-window RevIN: each patch is normalised
|
||||
using only its own timesteps, so no future statistics leak into past tokens.
|
||||
Use [:, -1, :] for probing (last token sees full context).
|
||||
Use [:, c, :] for JEPA pretraining (context-at-c).
|
||||
"""
|
||||
def __init__(self, n_channels: int, patch_len: int, d_model: int,
|
||||
n_heads: int, depth: int):
|
||||
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)
|
||||
patch_dim = patch_len * n_channels
|
||||
self.patch_norm = nn.LayerNorm(patch_dim) # applied per-patch, no future leakage
|
||||
self.embed = nn.Linear(patch_dim, 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)
|
||||
self.tf = nn.TransformerEncoder(layer, num_layers=depth)
|
||||
self.norm = nn.LayerNorm(d_model)
|
||||
|
||||
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)
|
||||
P = self.patch_len
|
||||
N = W // P
|
||||
tokens = x[:, :N * P, :].reshape(B, N, P * F)
|
||||
tokens = self.embed(self.patch_norm(tokens))
|
||||
# sinusoidal PE
|
||||
pos = torch.arange(N, device=x.device).float()
|
||||
div = torch.exp(torch.arange(0, self.d_model, 2, device=x.device).float()
|
||||
* -(math.log(10000.0) / self.d_model))
|
||||
pe = torch.zeros(N, self.d_model, device=x.device)
|
||||
pe[:, 0::2] = torch.sin(pos.unsqueeze(1) * div)
|
||||
pe[:, 1::2] = torch.cos(pos.unsqueeze(1) * div)
|
||||
tokens = tokens + pe
|
||||
# causal mask
|
||||
mask = nn.Transformer.generate_square_subsequent_mask(N, device=x.device)
|
||||
return self.norm(self.tf(tokens, mask=mask, is_causal=True))
|
||||
|
||||
|
||||
class Predictor(nn.Module):
|
||||
def __init__(self, d_model):
|
||||
# ── HorizonPredictor ─────────────────────────────────────────────────────────
|
||||
|
||||
class HorizonPredictor(nn.Module):
|
||||
"""MLP(cat(h_t, Δt)) → predicted future embedding."""
|
||||
def __init__(self, d_model: int):
|
||||
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)
|
||||
self.net = nn.Sequential(
|
||||
nn.Linear(d_model + 1, d_model), nn.GELU(),
|
||||
nn.Linear(d_model, d_model), nn.GELU(),
|
||||
nn.Linear(d_model, d_model),
|
||||
)
|
||||
|
||||
def forward(self, h: torch.Tensor, delta_t: torch.Tensor) -> torch.Tensor:
|
||||
dt = delta_t.float().unsqueeze(-1)
|
||||
return self.net(torch.cat([h, dt], dim=-1))
|
||||
|
||||
|
||||
# ── Data ────────────────────────────────────────────────────────────────────
|
||||
# ── Data ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
def build():
|
||||
"""Year-based split: encoder trains on 2019-2021; probe evaluates on 2022-2023 OOS."""
|
||||
df = pd.read_parquet("data/processed/eurusd_daily.parquet").reset_index(drop=True)
|
||||
df["date"] = pd.to_datetime(df["date"])
|
||||
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:])
|
||||
tr_idx = df.index[df["date"].dt.year <= 2021].tolist()
|
||||
te_idx = df.index[df["date"].dt.year >= 2022].tolist()
|
||||
mu = feats[:tr_idx[-1]+1].mean(0)
|
||||
sd = feats[:tr_idx[-1]+1].std(0) + 1e-8
|
||||
fn = (feats - mu) / sd
|
||||
def windows(idx):
|
||||
X, y = [], []
|
||||
for t in idx:
|
||||
if t - WINDOW >= 0 and t + 1 < len(df):
|
||||
X.append(fn[t - WINDOW:t]); y.append(target[t + 1])
|
||||
return np.stack(X).astype(np.float32), np.array(y, np.float32)
|
||||
return windows(tr_idx), windows(te_idx)
|
||||
|
||||
|
||||
# ── Training ─────────────────────────────────────────────────────────────────
|
||||
# ── 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_feats = Xtr.shape[2]
|
||||
n_patches = WINDOW // PATCH_LEN
|
||||
Xtr_t = torch.tensor(Xtr, device=dev)
|
||||
|
||||
n_patches = (WINDOW - PATCH_LEN) // STRIDE + 1
|
||||
n_mask = max(1, int(MASK_FRAC * n_patches))
|
||||
enc = CausalEncoder(n_feats, PATCH_LEN, D_MODEL, N_HEADS, DEPTH).to(dev)
|
||||
pred = HorizonPredictor(D_MODEL).to(dev)
|
||||
opt = torch.optim.AdamW(list(enc.parameters()) + list(pred.parameters()), lr=LR)
|
||||
|
||||
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
|
||||
# Sample random context position and horizon; Δt log-biased toward short
|
||||
c = torch.randint(0, n_patches - 1, ()).item()
|
||||
dt = torch.randint(1, max(2, min(DELTA_T_MAX, n_patches - 1 - c) + 1), ()).item()
|
||||
|
||||
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
|
||||
tokens = enc(Xtr_t) # (B, N, D)
|
||||
h_ctx = tokens[:, c, :] # context embedding
|
||||
h_tgt = tokens[:, c + dt, :] # target embedding (joint training)
|
||||
h_hat = pred(h_ctx, torch.full((len(Xtr),), float(dt), device=dev))
|
||||
loss = vicreg_loss(h_hat, h_tgt, alpha=ALPHA)
|
||||
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
|
||||
return enc(t)[:, -1, :].cpu().numpy() # last token = full-context summary
|
||||
|
||||
Etr = embed(Xtr)
|
||||
Ete = embed(Xte)
|
||||
|
||||
# ridge linear probe (closed form)
|
||||
A = np.hstack([Etr, np.ones((len(Etr), 1))])
|
||||
# Ridge probe: fit on train, evaluate on OOS (true OOS R²)
|
||||
mu_e = Etr.mean(0); sd_e = Etr.std(0) + 1e-8
|
||||
Etr_n = (Etr - mu_e) / sd_e
|
||||
Ete_n = (Ete - mu_e) / sd_e
|
||||
A = np.hstack([Etr_n, np.ones((len(Etr_n), 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
|
||||
pred_np = np.hstack([Ete_n, np.ones((len(Ete_n), 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},
|
||||
"knobs": {"WINDOW": WINDOW, "PATCH_LEN": PATCH_LEN,
|
||||
"D_MODEL": D_MODEL, "DEPTH": DEPTH, "ALPHA": ALPHA,
|
||||
"DELTA_T_MAX": DELTA_T_MAX, "EPOCHS": EPOCHS},
|
||||
}, open("metrics.json", "w"), indent=2)
|
||||
print("val_vol_r2 = %.4f (n_test=%d, dev=%s)" % (val_vol_r2, len(yte), dev))
|
||||
|
||||
# ── EXPORT BLOCK — do NOT edit (agent boundary) ──────────────────────────
|
||||
# Set EXPORT_EMBEDDINGS=1 to write embeddings.json for the Go eval harness.
|
||||
import os
|
||||
if os.environ.get("EXPORT_EMBEDDINGS") == "1":
|
||||
df2 = pd.read_parquet("data/processed/eurusd_daily.parquet").reset_index(drop=True)
|
||||
df2["date"] = pd.to_datetime(df2["date"])
|
||||
tr_mask = df2["date"].dt.year <= 2021
|
||||
feats2 = df2[["ret", "realized_vol"]].to_numpy(np.float32)
|
||||
mu2 = feats2[tr_mask].mean(0); sd2 = feats2[tr_mask].std(0) + 1e-8
|
||||
fn2 = (feats2 - mu2) / sd2
|
||||
def _export_windows(year_mask):
|
||||
idx = df2.index[year_mask].tolist()
|
||||
Xs, dates, rvs = [], [], []
|
||||
for t in idx:
|
||||
if t - WINDOW >= 0:
|
||||
Xs.append(fn2[t - WINDOW:t])
|
||||
dates.append(str(df2["date"].iloc[t].date()))
|
||||
rvs.append(float(df2["realized_vol"].iloc[t]))
|
||||
if not Xs:
|
||||
return [], [], []
|
||||
with torch.no_grad():
|
||||
E = enc(torch.tensor(np.stack(Xs), device=dev))[:, -1, :].cpu().numpy().tolist()
|
||||
return E, dates, rvs
|
||||
Etr2, dates_tr, rv_tr = _export_windows(tr_mask)
|
||||
Eoos, dates_oos, rv_oos = _export_windows(df2["date"].dt.year >= 2022)
|
||||
hv_thr = float(np.percentile(rv_oos, 67))
|
||||
hv_label = [1 if v >= hv_thr else 0 for v in rv_oos]
|
||||
json.dump({"embeddings": Eoos, "dates": dates_oos,
|
||||
"realized_vol": rv_oos, "hv_label": hv_label,
|
||||
"train_embeddings": Etr2, "train_realized_vol": rv_tr},
|
||||
open("embeddings.json", "w"))
|
||||
print("exported embeddings.json train=%d oos=%d HV=%d/%d" % (
|
||||
len(Etr2), len(Eoos), sum(hv_label), len(hv_label)))
|
||||
# ── END EXPORT BLOCK ─────────────────────────────────────────────────────
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user