SPB Git

Repository Public Research preview

06c_noise.py 1.9 KB 20 lines python

1"""Microstructure-noise benchmark: correlation between level and slope mimicking portfolios implied by independent tick-size noise alone,2and the realized-variance signature (1/5/30 min) already computed. Tick sizes (price % of par): ZT 1/128 (0.0078125), ZF 1/128, ZN 1/64 (0.015625),3TN 1/64, US 1/32 (0.03125), UB 1/32 — current CME specifications (historical changes not tracked)."""4import json, numpy as np5from pathlib import Path6ROOT = Path(__file__).resolve().parents[1]; OUT = ROOT / "results/metrics"7TICK = {"ZT": 100 / 128 / 100, "ZF": 100 / 128 / 100, "ZN": 100 / 64 / 100, "TN": 100 / 64 / 100, "US": 100 / 32 / 100, "UB": 100 / 32 / 100}  # percent of price8W = json.load(open(OUT / "mimicking_weights_quarterly.json"))9rows = []10for w in W:11    roots = w["roots"]; D = np.diag([TICK[r] ** 2 / 12 for r in roots])  # uniform rounding noise variance tick^2/12 ... times 2 for a difference12    D = 2 * D13    vec = {f: np.array([w["weights"][f][r] for r in roots]) for f in ("level", "slope", "curvature")}14    def c(a, b): return float(vec[a] @ D @ vec[b] / np.sqrt((vec[a] @ D @ vec[a]) * (vec[b] @ D @ vec[b])))15    rows.append({"quarter": w["quarter"], "noise_corr_LS": c("level", "slope"), "noise_corr_LC": c("level", "curvature"), "noise_corr_SC": c("slope", "curvature"),16                 "noise_sd_bp_per_min": {f: float(np.sqrt(vec[f] @ D @ vec[f]) * 100) for f in vec}})17res = {"by_quarter": rows, "mean_noise_corr_LS": float(np.mean([r["noise_corr_LS"] for r in rows])), "mean_noise_corr_LC": float(np.mean([r["noise_corr_LC"] for r in rows])),18       "mean_noise_corr_SC": float(np.mean([r["noise_corr_SC"] for r in rows])), "mean_noise_sd_bp_per_min": {f: float(np.mean([r["noise_sd_bp_per_min"][f] for r in rows])) for f in ("level", "slope", "curvature")}}19json.dump(res, open(OUT / "noise_benchmark.json", "w"), indent=1); print({k: v for k, v in res.items() if k != "by_quarter"})20
Files main
Menu

Theme