Cibles evenementielles locales, abstention relative et dates strictement passees
This commit is contained in:
79
tests/test_events.py
Normal file
79
tests/test_events.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import copy
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import pytest
|
||||
from test_clean_pipeline import states
|
||||
|
||||
import storage
|
||||
from clean_backtest import feature_rows
|
||||
from clean_features import features_asof
|
||||
from event_backtest import add, empty_stats, evaluate, probabilities
|
||||
from event_targets import event_target
|
||||
|
||||
|
||||
def test_future_observations_rejected_with_explicit_clock_tolerance(store):
|
||||
now = datetime(2026, 9, 13, 6, tzinfo=timezone.utc)
|
||||
assert storage.observation_not_future("2026-09-13T06:04:00Z", now)
|
||||
assert not storage.observation_not_future("2026-09-13T06:06:00Z", now)
|
||||
assert not storage.observation_not_future("2026-09-14", now)
|
||||
assert store.save_upstream("test", [dict(day="2026-09-14", value=1)], "u", "s", now=now) == 0
|
||||
row = dict(
|
||||
station_id="future",
|
||||
fuel="Gazole",
|
||||
day="2026-09-14",
|
||||
declared_at="2026-09-14T00:00:00",
|
||||
price=2,
|
||||
lat=44,
|
||||
lon=0,
|
||||
)
|
||||
assert store.save_archive_observations([row], "archive:test", now=now) == 0
|
||||
|
||||
|
||||
def test_feature_rows_stop_at_run_date():
|
||||
result = feature_rows(states(), as_of="2020-04-03")
|
||||
assert max(result) == "2020-04-03"
|
||||
prefix = list(states().values())[:90]
|
||||
prefix[-1]["day"] = "2099-01-01"
|
||||
with pytest.raises(ValueError):
|
||||
features_asof(prefix, "2020-04-01")
|
||||
|
||||
|
||||
def test_dip_and_rebound_detected_with_fixed_panel():
|
||||
data = states()
|
||||
t = "2020-03-31"
|
||||
data["2020-04-02"]["prices"] = {str(i): 2.0 for i in range(4)}
|
||||
data["2020-04-02"]["prices"]["opening"] = 0.1
|
||||
result = event_target(data, t, 7)
|
||||
assert result["events"]["down_5"]["hit"]
|
||||
assert result["events"]["down_5"]["delay"] == 3
|
||||
assert result["panel"] == 4
|
||||
assert result["future_min_delta"] < -5
|
||||
assert result["events"]["down_5"]["terminal"] < 0 # rebond : cible != seul dernier jour
|
||||
# Données futures ne changent pas les features déjà connues.
|
||||
prefix = [p for p in data.values() if p["available_at"] <= t][-90:]
|
||||
before = features_asof(prefix, t)
|
||||
data["2020-04-02"]["prices"] = {str(i): 999 for i in range(4)}
|
||||
assert before == features_asof(prefix, t)
|
||||
|
||||
|
||||
def test_probabilities_nested_and_no_training_mutation():
|
||||
stat = empty_stats()
|
||||
original = copy.deepcopy(stat)
|
||||
p = probabilities(stat, empty_stats())
|
||||
assert stat == original
|
||||
assert p["frequency"]["down_2"] == 0.5
|
||||
row = dict(target=dict(events={f"{d}_{q}": dict(hit=q <= 3) for d in ("up", "down") for q in (2, 3, 5)}))
|
||||
add(stat, row)
|
||||
p = probabilities(stat, empty_stats())
|
||||
assert p["local_regime"]["down_2"] >= p["local_regime"]["down_3"] >= p["local_regime"]["down_5"]
|
||||
|
||||
|
||||
def test_abstention_reports_no_signal_without_invented_precision():
|
||||
y = dict(hit=False, amplitude=0, delay=None, terminal=-1)
|
||||
out = evaluate(
|
||||
[dict(prob={"frequency": {"down_3": 0.2}}, target={"events": {"down_3": y}})], "down_3", "frequency"
|
||||
)
|
||||
empty = [a for a in out["abstention"] if a["label"] == "p>=0.4"][0]
|
||||
assert empty["coverage"] == 0 and empty["precision"] is None and empty["false_positives"] == 0
|
||||
# Les paliers « top X % » sont des rangs, pas des seuils inventés.
|
||||
assert {a["label"] for a in out["abstention"]} >= {"top_5pct", "top_50pct", "p>=0.15"}
|
||||
Reference in New Issue
Block a user