Skip to content

Commit 16e7ffa

Browse files
committed
fix: annotate fee arrays as numpy.ndarray so the stable surface renders the same on every NumPy
NDArray[np.float64] expands differently across NumPy releases (NDArray[...] on the locked version, ndarray[tuple[Any, ...], dtype[float64]] on the minimum and nightly builds), so the compatibility snapshot failed on the Minimum Dependencies and Python 3.15 jobs. Plain numpy.ndarray renders identically.
1 parent 8013f2f commit 16e7ffa

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

‎src/ml4t/backtest/prediction_market_fees.py‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from typing import Any, Literal
2929

3030
import numpy as np
31-
from numpy.typing import NDArray
3231

3332
__all__ = [
3433
"FORECASTEX_EXCHANGE_FEE",
@@ -84,36 +83,36 @@
8483
# === Shared helpers ===
8584

8685

87-
def _floats(name: str, value: ArrayInput) -> NDArray[np.float64]:
86+
def _floats(name: str, value: ArrayInput) -> np.ndarray:
8887
array = np.asarray(value, dtype=np.float64)
8988
if not np.all(np.isfinite(array)):
9089
raise ValueError(f"{name} must be finite")
9190
return array
9291

9392

94-
def _prices(value: ArrayInput) -> NDArray[np.float64]:
93+
def _prices(value: ArrayInput) -> np.ndarray:
9594
price = _floats("price", value)
9695
if np.any((price < 0.0) | (price > 1.0)):
9796
raise ValueError("price must be in [0, 1] dollars per contract")
9897
return price
9998

10099

101-
def _contracts(value: ArrayInput) -> NDArray[np.float64]:
100+
def _contracts(value: ArrayInput) -> np.ndarray:
102101
contracts = _floats("contracts", value)
103102
if np.any(contracts < 0.0):
104103
raise ValueError("contracts must be non-negative; pass the direction as side")
105104
return contracts
106105

107106

108-
def _labels(name: str, value: ArrayInput, allowed: tuple[str, ...]) -> NDArray[np.str_]:
107+
def _labels(name: str, value: ArrayInput, allowed: tuple[str, ...]) -> np.ndarray:
109108
labels = np.asarray(value, dtype=np.str_)
110109
unknown = sorted(set(np.unique(labels).tolist()) - set(allowed))
111110
if unknown:
112111
raise ValueError(f"{name} must be one of {allowed}, got {unknown}")
113112
return labels
114113

115114

116-
def _snap(units: NDArray[np.float64]) -> NDArray[np.float64]:
115+
def _snap(units: np.ndarray) -> np.ndarray:
117116
"""Remove floating-point noise from values that are integers in exact arithmetic.
118117
119118
``0.07 * 100 * 0.25 * 1e6`` evaluates to ``1750000.0000000002``; ceiling it
@@ -140,7 +139,7 @@ def _kalshi_components(
140139
fee_type: ArrayInput,
141140
fee_multiplier: ArrayInput,
142141
rounding: str,
143-
) -> tuple[NDArray[np.float64], NDArray[np.float64], float]:
142+
) -> tuple[np.ndarray, np.ndarray, float]:
144143
"""Return trade fee and rounding fee per fill in micro-dollars, and the precision."""
145144
p = _prices(price)
146145
c = _contracts(contracts)
@@ -186,7 +185,7 @@ def kalshi_fee(
186185
fee_type: ArrayInput = "quadratic",
187186
fee_multiplier: ArrayInput = 1.0,
188187
rounding: KalshiRounding = "exact",
189-
) -> NDArray[np.float64]:
188+
) -> np.ndarray:
190189
"""Kalshi trading fee per fill, with each fill treated as its own order.
191190
192191
Taker fee: ``round_up(M * 0.07 * C * P * (1 - P))`` with ``C`` contracts at
@@ -246,7 +245,7 @@ def kalshi_order_fees(
246245
fee_type: ArrayInput = "quadratic",
247246
fee_multiplier: ArrayInput = 1.0,
248247
rounding: KalshiRounding = "cent",
249-
) -> NDArray[np.float64]:
248+
) -> np.ndarray:
250249
"""Net Kalshi fee per fill for the fills of one order, in execution order.
251250
252251
Each fill pays its trade fee plus a rounding fee that aligns the balance to
@@ -355,7 +354,7 @@ def forecastex_fee(
355354
*,
356355
exchange_fee: ArrayInput = FORECASTEX_EXCHANGE_FEE,
357356
broker_commission: ArrayInput = 0.0,
358-
) -> NDArray[np.float64]:
357+
) -> np.ndarray:
359358
"""ForecastEx fee per fill: a flat amount per contract per side.
360359
361360
ForecastEx charges 0.01 per contract per side at execution, "independent of
@@ -436,7 +435,7 @@ def polymarket_us_fee(
436435
fee_coefficient: ArrayInput = POLYMARKET_US_TAKER_COEFFICIENT,
437436
maker_rebate_coefficient: ArrayInput = POLYMARKET_US_MAKER_REBATE_COEFFICIENT,
438437
rounding: PolymarketUSRounding = "exact",
439-
) -> NDArray[np.float64]:
438+
) -> np.ndarray:
440439
"""Polymarket US fee per fill; a maker rebate is a negative fee.
441440
442441
Taker: ``theta * C * p * (1 - p)`` with ``theta = 0.0695`` (1,000 contracts

‎tests/compatibility/snapshots/v0.1.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,13 +2941,13 @@
29412941
"kind": "function",
29422942
"module": "ml4t.backtest.prediction_market_fees",
29432943
"qualname": "kalshi_fee",
2944-
"signature": "(price: Any, contracts: Any, *, liquidity: Any = 'taker', side: Any = 'buy', fee_type: Any = 'quadratic', fee_multiplier: Any = 1.0, rounding: Literal['exact', 'cent', 'direct'] = 'exact') -> NDArray[numpy.float64]"
2944+
"signature": "(price: Any, contracts: Any, *, liquidity: Any = 'taker', side: Any = 'buy', fee_type: Any = 'quadratic', fee_multiplier: Any = 1.0, rounding: Literal['exact', 'cent', 'direct'] = 'exact') -> numpy.ndarray"
29452945
},
29462946
"ml4t.backtest.prediction_market_fees:polymarket_us_fee": {
29472947
"kind": "function",
29482948
"module": "ml4t.backtest.prediction_market_fees",
29492949
"qualname": "polymarket_us_fee",
2950-
"signature": "(price: Any, contracts: Any, *, liquidity: Any = 'taker', fee_coefficient: Any = 0.0695, maker_rebate_coefficient: Any = 0.0125, rounding: Literal['exact', 'cent'] = 'exact') -> NDArray[numpy.float64]"
2950+
"signature": "(price: Any, contracts: Any, *, liquidity: Any = 'taker', fee_coefficient: Any = 0.0695, maker_rebate_coefficient: Any = 0.0125, rounding: Literal['exact', 'cent'] = 'exact') -> numpy.ndarray"
29512951
},
29522952
"ml4t.backtest.profiles:list_profiles": {
29532953
"kind": "function",

0 commit comments

Comments
 (0)