Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(factors): gtja191_098 keeps NaN through an unknown drift; the smoother exception to the NaN contract is stated and pinned (#1463)

Haozhe Wu0144aeb7d61b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f978a9cc0..1443a6522 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -91,14 +91,20 @@ This project adheres to [Semantic Versioning](https://semver.org/).
only as a whole field name, so the result recorded no evidence and a correct
answer quoting it was rejected. Both names are now tail-risk aliases.
Variances such as `residual_var` stay unmapped.
-- **Nineteen alphas no longer fill a missing input with a constant** (#1463).
- A halt left a comparison reading 0, a `.where(cond, 0)` reading a flat day, or
- an `np.fmax` returning the other side, and the value then fed every rolling
- window that reached back to the gap. Those cells are NaN now. On gap-free
- data every finite value is bit-identical to before. A perturbation sweep over
- all 462 alphas counts 36 still carrying a missing bar forward, down from 55.
- Most of them are recursive smoothers (`ewm` / SMA), whose policy is still
- open on the issue.
+- **Twenty alphas no longer fill a missing input with a constant** (#1463).
+ A halt left a comparison reading 0 or False, a `.where(cond, 0)` reading a
+ flat day, or an `np.fmax` returning the other side, and the value then fed
+ every rolling window that reached back to the gap. Those cells are NaN now.
+ On gap-free data every finite value is bit-identical to before. A
+ perturbation sweep over all 462 alphas counts 35 still carrying a missing
+ bar forward, down from 55, and every one of them is accounted for: 28 are
+ recursive statistics (the GTJA `SMA(A, n, m)` smoothers and one running
+ product), which by the policy set on #1463 skip a missing observation and
+ continue from their last state — the operator layer's header now states
+ that exception, and a test pins it; 3 declare their own partial window; 4
+ are the sweep's own rank-tie artifact. Nothing rewarms a smoother after a
+ gap, and the registry's look-back mask was measured and rejected: it hid
+ 4,270 legitimate cells to remove 41% of the fabricated ones.
- **A `local:` code in a backtest is served from your own dataset or not at
all** (#1467). The market-data tool and the README already treated
`local:AAPL.US` this way, but the backtest runner did not. With
diff --git a/agent/src/factors/base.py b/agent/src/factors/base.py
index fa807aa98..34ad17566 100644
--- a/agent/src/factors/base.py
+++ b/agent/src/factors/base.py
@@ -6,7 +6,18 @@ contract returns a DataFrame of the same shape — raw scores, NaN preserved
in warmup / missing data; +/- inf is forbidden (registry rejects it).
NaN policy: every operator propagates NaN; no silent ``fillna(0)``. A constant
-window for ``ts_corr`` / ``ts_cov`` returns NaN, not zero.
+window for ``ts_corr`` / ``ts_cov`` returns NaN, not zero. A missing input is
+never replaced by a constant — not a comparison read as False, not
+``.where(cond, 0)``, not ``np.fmax`` returning the other side (#1463) — and a
+full-window operator (``min_periods`` equal to its window) is NaN on every bar
+whose window holds the gap. The one exception, decided on #1463 (2026-09-18):
+a recursive statistic skips a missing observation and continues from its last
+state — the GTJA ``SMA(A, n, m)`` written as ``.ewm(alpha=m/n, adjust=False)``,
+a running product — and so does a window that declares its own partial
+coverage (``min_periods`` below its length, or a coverage rule in its notes).
+Those emit a value on the bars after a gap, computed from the observations
+they did see. Nothing rewarms them, and nothing should: the registry already
+masks the gap bar itself.
Lookahead ban: ``delta(df, d)`` requires ``d >= 1``; the negative-shift
``Ref(df, -n)`` form is intentionally absent.
diff --git a/agent/src/factors/zoo/gtja191/alpha_098.py b/agent/src/factors/zoo/gtja191/alpha_098.py
index 81899d93c..e8f67347c 100644
--- a/agent/src/factors/zoo/gtja191/alpha_098.py
+++ b/agent/src/factors/zoo/gtja191/alpha_098.py
@@ -48,7 +48,8 @@ __alpha_meta__ = {
def compute(panel: dict) -> pd.DataFrame:
c = panel["close"]
ma = ts_mean(c, 30)
- cond_a = safe_div(delta(ma, 30), c.shift(30)) <= 0.05
+ drift = safe_div(delta(ma, 30), c.shift(30))
branch1 = -1.0 * (c - ts_min(c, 30))
branch2 = -1.0 * delta(c, 3)
- return branch1.where(cond_a, branch2)
+ # An unknown drift selects neither branch; a NaN comparison is False, which picked branch2 (#1463).
+ return branch1.where(drift <= 0.05, branch2).where(drift.notna())
diff --git a/agent/tests/factors/test_missing_bar_fabrication.py b/agent/tests/factors/test_missing_bar_fabrication.py
index 4660d7668..b1466f175 100644
--- a/agent/tests/factors/test_missing_bar_fabrication.py
+++ b/agent/tests/factors/test_missing_bar_fabrication.py
@@ -13,8 +13,21 @@ dyadic (prices in sixteenths, integer volume) and differences are compared with
tight tolerance, so pandas' online rolling sums carry no rounding residue that
would read as a dependence.
-The recursive smoothers (``ewm`` / SMA) are deliberately not listed: whether a
-smoother may skip a missing observation is a policy decision still open on #1463.
+The recursive smoothers are held to a different rule, decided on #1463: a
+statistic that carries state — the GTJA ``SMA(A, n, m)`` written as
+``.ewm(alpha=m/n, adjust=False)``, a running product — skips the missing
+observation and continues from its last state, so the bars after a gap carry a
+value computed from the observations it saw. ``test_a_smoother_skips_the_gap``
+pins that: the gap bar itself is NaN (the registry masks it), the next bar is
+not, and the later values are not the gap-free ones.
+
+Four alphas the sweep still flags — ``alpha101_013``, ``alpha101_016``,
+``gtja191_083``, ``gtja191_099``, all ``rank(ts_cov(rank(x), rank(y), 5))`` —
+are the oracle's own artifact, not a dependence: their flagged cells sit 10 to
+241 bars past a 5-bar window, every one is a cross-sectional rank moving by
+exactly 1/48 or 1/24, and the covariances agree to twelve decimals between the
+two perturbed runs. Percentile ranks (k/24) are not dyadic, so the rolling
+covariance carries a rounding residue that breaks a tie differently.
"""
from __future__ import annotations
@@ -41,6 +54,7 @@ FIXED = [
"gtja191_058",
"gtja191_084",
"gtja191_094",
+ "gtja191_098",
"gtja191_112",
"gtja191_128",
"gtja191_129",
@@ -103,3 +117,56 @@ def test_no_value_after_a_missing_bar_depends_on_it(alpha_id: str) -> None:
assert moved.any(), "the perturbation reached no later bar; the oracle proves nothing"
assert fabricated.size == 0, f"bars after the gap computed from it: {fabricated.tolist()}"
+
+
+# The recursive statistics: 27 GTJA SMA(A, n, m) smoothers and one running product.
+SMOOTHERS = [
+ "gtja191_022",
+ "gtja191_023",
+ "gtja191_024",
+ "gtja191_028",
+ "gtja191_047",
+ "gtja191_057",
+ "gtja191_063",
+ "gtja191_067",
+ "gtja191_072",
+ "gtja191_079",
+ "gtja191_081",
+ "gtja191_082",
+ "gtja191_089",
+ "gtja191_096",
+ "gtja191_102",
+ "gtja191_111",
+ "gtja191_135",
+ "gtja191_143",
+ "gtja191_146",
+ "gtja191_151",
+ "gtja191_152",
+ "gtja191_155",
+ "gtja191_160",
+ "gtja191_162",
+ "gtja191_164",
+ "gtja191_169",
+ "gtja191_173",
+ "gtja191_174",
+]
+
+
[email protected]("alpha_id", SMOOTHERS)
+def test_a_smoother_skips_the_gap(alpha_id: str) -> None:
+ """Skip and continue, not NaN until rewarmed: the policy set on #1463."""
+ registry = get_default_registry()
+ deps = set(registry.get(alpha_id).meta.get("columns_required", []))
+ base = _base()
+ clean = registry.compute(alpha_id, _panel(base, deps, "base")).iloc[:, _SYMBOL].to_numpy(dtype=float)
+ gapped = registry.compute(alpha_id, _panel(base, deps, "missing")).iloc[:, _SYMBOL].to_numpy(dtype=float)
+
+ assert np.isnan(gapped[_GAP]), "the registry masks the bar whose input is missing"
+ # gtja191_146 averages the smoothed residual over a full 20-bar window, which
+ # is NaN while it holds the gap; the smoother underneath it never stops.
+ first = _GAP + (21 if alpha_id == "gtja191_146" else 1)
+ assert np.isfinite(gapped[first:]).all(), "the smoother continued from its last state"
+ both = np.isfinite(clean[first:]) & np.isfinite(gapped[first:])
+ assert not np.allclose(clean[first:][both], gapped[first:][both], rtol=1e-9, atol=1e-12), (
+ "the values after the gap were computed from the observations the smoother saw, not copied"
+ )