Skip to content

Commit 7c3d66a

Browse files
committed
fix(features): keep missing Roll covariance windows undefined
1 parent 544e160 commit 7c3d66a

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

‎src/ml4t/engineer/features/microstructure/roll_spread_estimator.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def roll_spread_estimator(close: pl.Expr | str, period: int = 20) -> pl.Expr:
3232
Returns
3333
-------
3434
pl.Expr
35-
Estimated bid-ask spread
35+
Estimated bid-ask spread, null for an unobserved covariance window
3636
3737
Raises
3838
------
@@ -56,8 +56,8 @@ def roll_spread_estimator(close: pl.Expr | str, period: int = 20) -> pl.Expr:
5656
cov_approx = (price_changes * lag_changes).rolling_mean(period) - mean_change * mean_lag
5757

5858
# Roll spread = 2 * sqrt(max(-cov, 0))
59-
spread = 2 * pl.when(cov_approx.is_null()).then(None).when(cov_approx < 0).then(
60-
(-cov_approx).sqrt()
61-
).otherwise(0)
59+
spread = 2 * pl.when(cov_approx.is_null() | cov_approx.is_nan()).then(None).when(
60+
cov_approx < 0
61+
).then((-cov_approx).sqrt()).otherwise(0)
6262

6363
return spread

‎tests/features/test_microstructure.py‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
- Academic formula correctness
1111
"""
1212

13+
from io import StringIO
14+
1315
import numpy as np
1416
import polars as pl
1517
import pytest
@@ -424,14 +426,41 @@ def test_roll_spread_matches_window_covariance():
424426
if np.isnan(window_changes).any() or np.isnan(window_lagged).any():
425427
assert np.isnan(result[index])
426428
continue
427-
covariance = float(
428-
np.mean(window_changes * window_lagged)
429-
- np.mean(window_changes) * np.mean(window_lagged)
430-
)
429+
covariance = float(np.cov(window_changes, window_lagged, ddof=0)[0, 1])
431430
expected = 2.0 * np.sqrt(max(-covariance, 0.0))
432431
assert result[index] == pytest.approx(expected)
433432

434433

434+
@pytest.mark.parametrize("missing", ["", "NaN"])
435+
@pytest.mark.parametrize("period", [1, 2, 4, 7])
436+
@pytest.mark.parametrize("lazy", [False, True])
437+
def test_roll_spread_missing_csv_prices_do_not_report_zero(missing, period, lazy):
438+
prices = [100.0 + i % 2 if i < 20 else 100.0 + (i - 19) ** 2 / 16 for i in range(30)]
439+
rows = [f"2024-01-02T14:{30 + i:02d}:00Z,{price}" for i, price in enumerate(prices)]
440+
rows[8] = f"2024-01-02T14:38:00Z,{missing}"
441+
frame = pl.read_csv(
442+
StringIO("timestamp,close\n" + "\n".join(rows)),
443+
schema_overrides={"close": pl.Float64},
444+
try_parse_dates=True,
445+
)
446+
changes = np.diff(frame["close"].to_numpy(), prepend=np.nan)
447+
lagged = np.roll(changes, 1)
448+
lagged[0] = np.nan
449+
expected = np.full(frame.height, np.nan)
450+
for index in range(period + 1, frame.height):
451+
current = changes[index - period + 1 : index + 1]
452+
previous = lagged[index - period + 1 : index + 1]
453+
if np.isfinite(current).all() and np.isfinite(previous).all():
454+
covariance = np.cov(current, previous, ddof=0)[0, 1]
455+
expected[index] = 2 * np.sqrt(max(-covariance, 0))
456+
source = frame.lazy() if lazy else frame
457+
selected = source.select(roll_spread_estimator("close", period=period))
458+
result = selected.collect() if lazy else selected
459+
np.testing.assert_allclose(
460+
result.to_series().to_numpy(), expected, equal_nan=True, rtol=1e-12, atol=1e-12
461+
)
462+
463+
435464
def test_tick_rule_basic_functionality(sample_microstructure_data):
436465
"""Test effective tick rule basic calculation."""
437466
result = sample_microstructure_data.with_columns(

0 commit comments

Comments
 (0)