Modele J+0 vers J+21 : benchmarks simples, unites en centimes, regimes et couverture par carburant

Cinq modeles sont compares sur des previsions directes par horizon (naive, tendance locale, autoregressif pompe, ridge marche, k-NN borne), sans bibliotheque scientifique et avec une selection faite sur une fin d apprentissage chronologique, jamais sur le test. Les cibles sont exprimees en centimes par litre sur le panel comparable, avec intervalle et probabilites. La serie BZ=F est reconnue comme un future Brent : renommee, jamais utilisee en niveau absolu, avec detection des changements de contrat et conservation de la reference mensuelle DGEC a cote. Une date available_at explicite separe desormais periode decrite, publication reelle et disponibilite, avec des hypotheses conservatrices marquees comme estimees. Le backtest rend les performances par carburant, par horizon et par regime (Covid, 2022, periode calme, crise actuelle), refuse une forte confiance pendant un choc, et teste la strategie sur quatre profils d usage.
This commit is contained in:
root
2026-09-13 00:32:37 +02:00
parent 48039ed729
commit ad46a08d64
31 changed files with 2565 additions and 1858 deletions

View File

@@ -1,5 +1,4 @@
"""Cache de routes : origine normalisée, rattachement au favori, séparation statique / trafic."""
from datetime import datetime, timedelta, timezone
import pytest
@@ -15,11 +14,11 @@ def meters_between(origin, other):
def test_gps_jitter_of_a_few_dozens_of_meters_shares_one_cache_key(store):
target = (44.795280, -0.604509)
first = (44.78060, -0.57060)
second = (44.78063, -0.57064) # ~5 m plus loin : bruit normal d'une mesure téléphone
third = (44.78110, -0.57110) # ~70 m plus loin
second = (44.78063, -0.57064) # ~5 m plus loin : bruit normal d'une mesure téléphone
third = (44.78110, -0.57110) # ~70 m plus loin
assert meters_between(first, second) < 10
keys = {storage.route_key(point, target) for point in (first, second, third)}
assert len(keys) == 1, f"positions quasi identiques, clés distinctes : {keys}"
assert len(keys) == 1, f'positions quasi identiques, clés distinctes : {keys}'
def test_two_positions_hundreds_of_meters_apart_use_distinct_keys(store):
@@ -38,89 +37,77 @@ def test_distinct_stations_never_share_a_key(store):
def test_favourite_tag_replaces_raw_coordinates(store):
target = (44.795280, -0.604509)
key = storage.route_key((44.78055, -0.57051), target, origin_tag="fav:3")
assert key.startswith("fav:3|")
assert storage.route_key((44.79999, -0.57051), target, origin_tag="fav:3") == key
key = storage.route_key((44.78055, -0.57051), target, origin_tag='fav:3')
assert key.startswith('fav:3|')
assert storage.route_key((44.79999, -0.57051), target, origin_tag='fav:3') == key
def test_match_favorite_absorbs_gps_jitter(store):
storage.save_entity("favorites", dict(name="Villenave", lat=44.78060, lon=-0.57060, radius_km=10))
storage.save_entity('favorites', dict(name='Villenave', lat=44.78060, lon=-0.57060, radius_km=10))
favorite = storage.match_favorite(44.78068, -0.57070)
assert favorite and favorite["name"] == "Villenave"
assert favorite["distance_km"] <= 0.15
assert favorite and favorite['name'] == 'Villenave'
assert favorite['distance_km'] <= 0.15
assert storage.match_favorite(44.90000, -0.57070) is None
def test_osrm_duration_is_static_and_never_an_eta(store, monkeypatch):
monkeypatch.setitem(
routing.PROVIDERS,
"osrm",
lambda origin, targets: {
t[0]: dict(distance_m=5000.0, duration_s=600.0, duration_kind=routing.STATIC) for t in targets
},
)
monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "osrm")
routes, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)])
assert routes["A"]["duration_kind"] == routing.STATIC
assert routes["A"]["duration_label"] == "hors trafic"
assert routes["A"]["eta_min"] is None
assert info["eta_count"] == 0
assert "hors trafic" in info["eta_note"]
monkeypatch.setitem(routing.PROVIDERS, 'osrm',
lambda origin, targets: {t[0]: dict(distance_m=5000.0, duration_s=600.0,
duration_kind=routing.STATIC)
for t in targets})
monkeypatch.setenv('FUEL_ROUTING_PROVIDERS', 'osrm')
routes, info = routing.routes((44.78, -0.57), [('A', 44.79, -0.60)])
assert routes['A']['duration_kind'] == routing.STATIC
assert routes['A']['duration_label'] == 'hors trafic'
assert routes['A']['eta_min'] is None
assert info['eta_count'] == 0
assert 'hors trafic' in info['eta_note']
assert store.eta_lookup([storage.route_key((44.78, -0.57), (44.79, -0.60))]) == {}
def test_google_traffic_duration_goes_to_short_cache_only(store, monkeypatch):
monkeypatch.setattr(routing, "GOOGLE_KEY", "clé-de-test")
monkeypatch.setitem(
routing.PROVIDERS,
"google",
lambda origin, targets: {
t[0]: dict(distance_m=8000.0, duration_s=900.0, duration_kind=routing.TRAFFIC) for t in targets
},
)
monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "google")
routes, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)])
assert routes["A"]["distance_km"] == pytest.approx(8.0)
assert routes["A"]["duration_min"] is None, "une durée de trafic n’est pas une durée statique"
assert routes["A"]["eta_min"] == pytest.approx(15.0)
assert routes["A"]["eta_age_seconds"] is not None
assert info["eta_count"] == 1 and info["eta_provider"] == "google"
monkeypatch.setattr(routing, 'GOOGLE_KEY', 'clé-de-test')
monkeypatch.setitem(routing.PROVIDERS, 'google',
lambda origin, targets: {t[0]: dict(distance_m=8000.0, duration_s=900.0,
duration_kind=routing.TRAFFIC)
for t in targets})
monkeypatch.setenv('FUEL_ROUTING_PROVIDERS', 'google')
routes, info = routing.routes((44.78, -0.57), [('A', 44.79, -0.60)])
assert routes['A']['distance_km'] == pytest.approx(8.0)
assert routes['A']['duration_min'] is None, 'une durée de trafic n’est pas une durée statique'
assert routes['A']['eta_min'] == pytest.approx(15.0)
assert routes['A']['eta_age_seconds'] is not None
assert info['eta_count'] == 1 and info['eta_provider'] == 'google'
cached = store.route_lookup([storage.route_key((44.78, -0.57), (44.79, -0.60))])
assert list(cached.values())[0]["duration_s"] is None, "pas de durée trafic dans le cache long"
assert list(cached.values())[0]['duration_s'] is None, 'pas de durée trafic dans le cache long'
def test_stale_eta_is_ignored_and_static_duration_is_used(store, monkeypatch):
monkeypatch.setattr(routing, "GOOGLE_KEY", "clé-de-test")
monkeypatch.setitem(
routing.PROVIDERS,
"google",
lambda origin, targets: {
t[0]: dict(distance_m=8000.0, duration_s=900.0, duration_kind=routing.TRAFFIC) for t in targets
},
)
monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "google")
routing.routes((44.78, -0.57), [("A", 44.79, -0.60)])
monkeypatch.setattr(routing, 'GOOGLE_KEY', 'clé-de-test')
monkeypatch.setitem(routing.PROVIDERS, 'google',
lambda origin, targets: {t[0]: dict(distance_m=8000.0, duration_s=900.0,
duration_kind=routing.TRAFFIC)
for t in targets})
monkeypatch.setenv('FUEL_ROUTING_PROVIDERS', 'google')
routing.routes((44.78, -0.57), [('A', 44.79, -0.60)])
key = storage.route_key((44.78, -0.57), (44.79, -0.60))
stale = (datetime.now(timezone.utc) - timedelta(hours=3)).isoformat()
store.eta_save([dict(key=key, duration_s=1800.0, provider="google")])
store.eta_save([dict(key=key, duration_s=1800.0, provider='google')])
with store.connect() as db:
db.execute("UPDATE route_eta SET computed_at=? WHERE key=?", (stale, key))
routes, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)], eta_ttl=900)
assert routes["A"]["eta_min"] is None, "une ETA de 3 heures n’est pas une ETA actuelle"
assert info["eta_count"] == 0
assert "Aucune ETA de trafic" in info["eta_note"]
db.execute('UPDATE route_eta SET computed_at=? WHERE key=?', (stale, key))
routes, info = routing.routes((44.78, -0.57), [('A', 44.79, -0.60)], eta_ttl=900)
assert routes['A']['eta_min'] is None, 'une ETA de 3 heures n’est pas une ETA actuelle'
assert info['eta_count'] == 0
assert 'Aucune ETA de trafic' in info['eta_note']
def test_eta_key_is_separate_for_tagged_origin(store, monkeypatch):
monkeypatch.setattr(routing, "GOOGLE_KEY", "clé-de-test")
monkeypatch.setitem(
routing.PROVIDERS,
"google",
lambda origin, targets: {
t[0]: dict(distance_m=4000.0, duration_s=600.0, duration_kind=routing.TRAFFIC) for t in targets
},
)
monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "google")
_, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)], origin_tag="fav:7")
assert info["eta_count"] == 1
assert store.eta_lookup(["fav:7|44.7900|-0.6000"]) != {}
monkeypatch.setattr(routing, 'GOOGLE_KEY', 'clé-de-test')
monkeypatch.setitem(routing.PROVIDERS, 'google',
lambda origin, targets: {t[0]: dict(distance_m=4000.0, duration_s=600.0,
duration_kind=routing.TRAFFIC)
for t in targets})
monkeypatch.setenv('FUEL_ROUTING_PROVIDERS', 'google')
_, info = routing.routes((44.78, -0.57), [('A', 44.79, -0.60)], origin_tag='fav:7')
assert info['eta_count'] == 1
assert store.eta_lookup(['fav:7|44.7900|-0.6000']) != {}