feat(mcp-macro): add compute_percentile + classify_extreme pure helpers
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
"""Pure-logic helpers per COT report parsing e analytics.
|
||||
|
||||
Niente HTTP qui — orchestrazione fetch sta in fetchers.py. Tutto testabile
|
||||
in isolamento.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Literal
|
||||
|
||||
ExtremeSignal = Literal["extreme_short", "extreme_long", "neutral"]
|
||||
|
||||
|
||||
def compute_percentile(value: float, history: list[float]) -> float | None:
|
||||
"""Percentile di `value` rispetto ad `history` (0-100, inclusive).
|
||||
|
||||
Restituisce None se history vuoto. Clipped a [0, 100] se value fuori range.
|
||||
"""
|
||||
if not history:
|
||||
return None
|
||||
n = len(history)
|
||||
below_or_eq = sum(1 for h in history if h <= value)
|
||||
pct = 100.0 * below_or_eq / n
|
||||
return max(0.0, min(100.0, pct))
|
||||
|
||||
|
||||
def classify_extreme(percentile: float | None, threshold: float = 5.0) -> ExtremeSignal:
|
||||
"""Classifica un percentile come estremo short/long o neutral.
|
||||
|
||||
threshold default 5 → flagga ≤ 5 come short, ≥ 100-5=95 come long.
|
||||
"""
|
||||
if percentile is None:
|
||||
return "neutral"
|
||||
if percentile <= threshold:
|
||||
return "extreme_short"
|
||||
if percentile >= 100.0 - threshold:
|
||||
return "extreme_long"
|
||||
return "neutral"
|
||||
Reference in New Issue
Block a user