Files
PythagorasGoal/scripts/research/xsec/runs/XD03.py
T
Adriano Dal Pastro 9612560479 research(xsec): sweep cross-sectional su Hyperliquid (43 script/257 config) + verifica avversariale
Nuova harness condivisa xslib.py (panel HL certificato, score per-asset causale, book
long-k/short-k vol-targeted leak-free) + 43 script in runs/ su 11 famiglie (MOM/REV/VOL/
DIST/LIQ/VAL/STRUCT/UNIV). Scoring = earns_slot (full>0 AND hold-out>0 AND marginal ADDS
al portafoglio live AND corr XS01<0.6, con jackknife drop-one-month).

Find: 42/257 config earns_slot=True, ma TUTTE con corr TP01 -0.2..-0.4 e PnL ~solo 2025.
Verify (verify_survivors.py, 3 scettici deterministici):
 - S1 redundancy: cluster low-vol = UNA scommessa (XV01=XU02=1.00, XV02/XV03 r 0.44-0.67);
   XM09/XL02/XS06b/XR02 distinti (corr media off-diag +0.20).
 - S2 short-beta: cluster low-vol carica 0.44-0.70 su short-market -> NON market-neutral,
   e' un tilt short-alt-beta di regime. XM09(0.08)/XR02(-0.21) NON short-beta.
 - S3 per-anno: cluster low-vol decade (XV01/XU02 2026 -0.09); XL02 morto (2025 -0.14,
   2026 -0.43); XM09 (0.82/0.50/0.74) e XR02 (0.84/0.40/2.68) positivi in tutti e 3 gli anni.

Esito: nessuna sleeve nuova. Cluster low-vol RIGETTATO (regime-bet), XL02 RIGETTATO (overfit).
2 LEAD genuini (XM09 trend-gated x-sec momentum, XR02 reversal vol-gated) -> forward-monitor,
non deployabili (panel 2.5y regime unico + STAT-MODE esecuzione). Portafoglio live invariato.

Incluso anche options_vrp_managed.py (A/B VRP01 hold-to-expiry vs gestione attiva del doc
credit-spread): la gestione attiva DISTRUGGE l'edge (combo FULL managed Sh -1.29 vs HtE +0.96,
il delta-exit taglia i vincenti) -> scartata, VRP01 resta hold-to-expiry.

Diari: 2026-06-20-xsec-strategies-sweep.md, 2026-06-20-vrp-active-management.md.
gitignore: data/paper_portfolio/ (stato runtime paper) + scripts/research/xsec/runs/out/ (output rigenerabile).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-20 21:36:57 +00:00

137 lines
4.5 KiB
Python

"""XD03 — Coskewness with Market
Mechanism: For each asset, compute rolling coskewness of asset returns
with the equal-weight market return. Assets with LOW coskewness (they do
not co-skew positively with the market) tend to earn a premium because
investors disfavor assets with negative coskewness (they hurt in crashes
when skewness matters most). Classic Harvey & Siddique (2000) anomaly.
Coskew(i, M) = E[(r_i - mu_i)(r_M - mu_M)^2] / (sigma_i * sigma_M^2)
Causally computed. LOWER coskew = LONG signal.
Grid: 5 backtests varying (win, H, k, universe, long_short).
"""
import sys
sys.path.insert(0, "/opt/docker/PythagorasGoal/scripts/research/xsec")
import xslib as xs
import numpy as np
import pandas as pd
def coskew_score(ret: np.ndarray, win: int = 60) -> np.ndarray:
"""Rolling coskewness of each asset with the equal-weight market.
coskew(i, M) = E[(r_i - mu_i)(r_M - mu_M)^2] / (std_i * std_M^2)
Returns (n_days x n_assets). LOWER = should be LONG (earns premium).
So for long-low, negate: score = -coskew
"""
n, A = ret.shape
mkt = xs.market_ret(ret) # (n,)
out = np.full((n, A), np.nan)
# Use pandas rolling for causality
mkt_s = pd.Series(mkt)
for a in range(A):
asset_s = pd.Series(ret[:, a])
# Rolling window stats
mu_a = asset_s.rolling(win, min_periods=max(10, win // 3)).mean()
mu_m = mkt_s.rolling(win, min_periods=max(10, win // 3)).mean()
std_a = asset_s.rolling(win, min_periods=max(10, win // 3)).std()
std_m = mkt_s.rolling(win, min_periods=max(10, win // 3)).std()
# Centered series (element-wise)
da = asset_s - mu_a
dm = mkt_s - mu_m
# coskew numerator = mean(da * dm^2)
coskew_num = (da * dm ** 2).rolling(win, min_periods=max(10, win // 3)).mean()
# Normalize by std_a * std_m^2
denom = std_a * std_m ** 2
denom = denom.replace(0, np.nan)
coskew = coskew_num / denom
out[:, a] = coskew.values
return out
def score_fn_60(P):
"""Long low-coskew: negate so that lower coskew = higher score."""
return -coskew_score(P.ret, win=60)
def score_fn_90(P):
"""Longer lookback for coskewness."""
return -coskew_score(P.ret, win=90)
def score_fn_30(P):
"""Shorter lookback — more reactive."""
return -coskew_score(P.ret, win=30)
if __name__ == "__main__":
print("=== XD03: Coskewness with Market ===\n")
results = []
# Run 1: baseline config (win=60, all, H=10, k=5, LS)
print("Run 1/5: win=60, universe=all, H=10, k=5, long_short=True")
r1 = xs.study_xs("XD03-w60-H10-k5-LS", score_fn_60,
universe="all", H=10, k=5, long_short=True)
print(xs.fmt(r1))
print("JSON:", xs.as_json(r1))
results.append(r1)
# Run 2: vary rebalance period (H=20, looser)
print("\nRun 2/5: win=60, universe=all, H=20, k=5, long_short=True")
r2 = xs.study_xs("XD03-w60-H20-k5-LS", score_fn_60,
universe="all", H=20, k=5, long_short=True)
print(xs.fmt(r2))
print("JSON:", xs.as_json(r2))
results.append(r2)
# Run 3: longer win=90 (more stable coskewness estimate)
print("\nRun 3/5: win=90, universe=all, H=10, k=5, long_short=True")
r3 = xs.study_xs("XD03-w90-H10-k5-LS", score_fn_90,
universe="all", H=10, k=5, long_short=True)
print(xs.fmt(r3))
print("JSON:", xs.as_json(r3))
results.append(r3)
# Run 4: majors only (19 assets, cleaner signal)
print("\nRun 4/5: win=60, universe=majors, H=10, k=5, long_short=True")
r4 = xs.study_xs("XD03-w60-H10-k5-LS-maj", score_fn_60,
universe="majors", H=10, k=5, long_short=True)
print(xs.fmt(r4))
print("JSON:", xs.as_json(r4))
results.append(r4)
# Run 5: long-only on majors (captures risk-premium differently)
print("\nRun 5/5: win=60, universe=majors, H=10, k=5, long_only")
r5 = xs.study_xs("XD03-w60-H10-k5-LO-maj", score_fn_60,
universe="majors", H=10, k=5, long_short=False)
print(xs.fmt(r5))
print("JSON:", xs.as_json(r5))
results.append(r5)
# Summary: pick best by (earns_slot, then hold-out sharpe, then full sharpe)
def rank_key(r):
es = 1 if r["earns_slot"] else 0
hs = r["holdout"].get("sharpe", -99)
fs = r["full"]["sharpe"]
corr_ok = (r.get("corr_xs01") or 1.0) < 0.6
return (es, int(corr_ok), hs, fs)
best = max(results, key=rank_key)
print("\n\n=== BEST CONFIG ===")
print(xs.fmt(best))
print("JSON:", xs.as_json(best))