Historique officiel 2007-2026, percentiles separes espace/temps, cout du detour et banc de validation
Les archives annuelles officielles de prix-carburants.gouv.fr sont importees retrospectivement depuis 2007 (20 annees, 354597 lignes sur 117 stations) avec la date declaree par la station, un marqueur d origine, sans ecrasement ni continuite inventee. Le percentile spatial et le percentile historique sont desormais deux indicateurs distincts : seul le second peut declencher un achat anticipe, et son affichage exige une fenetre reellement couverte. Le cout du detour est calcule automatiquement (distance routiere supplementaire x consommation x prix) et deduit du gain, tandis que le seuil de gain net pour un ravitaillement supplementaire reste configurable par vehicule. La quantite de carburant du reservoir est clairement presentee comme une estimation. Le banc de validation impose un backtest chronologique sans fuite, des metriques par horizon avec calibration de la confiance, et une simulation economique naive contre Fuel Advisor ; aucune prevision ne sera branchee avant que les criteres ne soient atteints.
This commit is contained in:
@@ -60,8 +60,32 @@ def outlook_up(confidence=0.81, days_max=7, low=0.05, high=0.08):
|
||||
)
|
||||
|
||||
|
||||
def position(percentile, current=1.62):
|
||||
return dict(percentile=percentile, current=current, observations=500, days=90)
|
||||
def historical_position(percentile, current=1.62, days=120):
|
||||
"""Position **historique** (fenêtres 30/90/365 jours). Seule utilisée pour anticiper."""
|
||||
return dict(
|
||||
kind="historique",
|
||||
percentile=percentile,
|
||||
current=current,
|
||||
days=days,
|
||||
windows={
|
||||
str(window): dict(percentile=percentile, days=days, current=current, median=1.60, summary="test")
|
||||
for window in (30, 90, 365)
|
||||
},
|
||||
summary="1.620 €/L se situe au test.",
|
||||
)
|
||||
|
||||
|
||||
def local_position(percentile, stations=36):
|
||||
"""Position **locale** du jour : classement dans l'espace, jamais dans le temps."""
|
||||
return dict(
|
||||
kind="local",
|
||||
percentile=percentile,
|
||||
stations=stations,
|
||||
day="2026-09-12",
|
||||
best=1.99,
|
||||
median=2.11,
|
||||
summary="test local",
|
||||
)
|
||||
|
||||
|
||||
def test_bands_refuse_false_precision():
|
||||
@@ -78,7 +102,11 @@ def test_daily_km_never_assumes_an_average_for_occasional_use():
|
||||
def test_wait_plan_buys_the_minimum_and_respects_comfort_floor():
|
||||
"""Baisse annoncée : appoint minimal, jamais 4 L qui obligeraient à revenir demain."""
|
||||
out = quantity.plan(
|
||||
REGULAR, remaining_km=150, price_now=1.62, position=position(0.5), outlook=outlook_down()
|
||||
REGULAR,
|
||||
remaining_km=150,
|
||||
price_now=1.62,
|
||||
historical_position=historical_position(0.5),
|
||||
outlook=outlook_down(),
|
||||
)
|
||||
assert out["action"] == "topup"
|
||||
assert out["usable_km"] == 70.0
|
||||
@@ -94,7 +122,11 @@ def test_wait_plan_buys_the_minimum_and_respects_comfort_floor():
|
||||
|
||||
def test_wait_plan_buys_nothing_when_the_autonomy_already_covers_the_window():
|
||||
out = quantity.plan(
|
||||
REGULAR, remaining_km=195, price_now=1.62, position=position(0.5), outlook=outlook_down()
|
||||
REGULAR,
|
||||
remaining_km=195,
|
||||
price_now=1.62,
|
||||
historical_position=historical_position(0.5),
|
||||
outlook=outlook_down(),
|
||||
)
|
||||
assert out["action"] == "wait"
|
||||
assert out["litres_low"] is None
|
||||
@@ -108,30 +140,38 @@ def test_wait_plan_warns_when_the_tank_cannot_reach_the_window():
|
||||
small_tank,
|
||||
remaining_km=95,
|
||||
price_now=1.62,
|
||||
position=position(0.5),
|
||||
historical_position=historical_position(0.5),
|
||||
outlook=outlook_down(days_min=14, days_max=17),
|
||||
)
|
||||
assert out["action"] == "topup"
|
||||
assert out["litres_target"] == pytest.approx(15.3, abs=0.4), "plafonné au réservoir"
|
||||
assert any("second ravitaillement" in caveat for caveat in out["caveats"])
|
||||
assert any("Quantité limitée" in caveat for caveat in out["caveats"])
|
||||
assert any("plafonnée" in caveat for caveat in out["caveats"])
|
||||
|
||||
|
||||
def test_full_tank_requires_both_favourable_price_and_upward_forecast():
|
||||
out = quantity.plan(
|
||||
REGULAR, remaining_km=195, price_now=1.55, position=position(0.10), outlook=outlook_up()
|
||||
REGULAR,
|
||||
remaining_km=195,
|
||||
price_now=1.55,
|
||||
historical_position=historical_position(0.10),
|
||||
outlook=outlook_up(),
|
||||
)
|
||||
assert out["action"] == "refill"
|
||||
assert out["litres_low"] == 30.0 and out["litres_high"] == 35.0
|
||||
assert out["economy"]["gain_low"] > 0 and out["economy"]["worth_it"] is True
|
||||
joined = " ".join(out["reasons"])
|
||||
assert "15% les plus bas" in joined
|
||||
assert "centile des prix journaliers" in joined
|
||||
assert "plancher garanti" in joined, "un bon prix n’est jamais présenté comme un plancher certain"
|
||||
|
||||
|
||||
def test_upward_forecast_with_ordinary_price_does_not_justify_a_full_tank():
|
||||
out = quantity.plan(
|
||||
REGULAR, remaining_km=195, price_now=1.72, position=position(0.70), outlook=outlook_up()
|
||||
REGULAR,
|
||||
remaining_km=195,
|
||||
price_now=1.72,
|
||||
historical_position=historical_position(0.70),
|
||||
outlook=outlook_up(),
|
||||
)
|
||||
assert out["action"] == "topup"
|
||||
assert out["action"] != "refill"
|
||||
@@ -143,11 +183,16 @@ def test_small_gain_is_rejected_to_avoid_a_pointless_stop():
|
||||
REGULAR,
|
||||
remaining_km=195,
|
||||
price_now=1.55,
|
||||
position=position(0.10),
|
||||
historical_position=historical_position(0.10),
|
||||
outlook=outlook_up(low=0.004, high=0.006),
|
||||
)
|
||||
assert out["economy"]["worth_it"] is False
|
||||
assert any("ne multipliez pas les passages" in caveat or "faible" in caveat for caveat in out["caveats"])
|
||||
assert any(
|
||||
"ne multipliez pas les passages" in caveat
|
||||
or "Gain net faible" in caveat
|
||||
or "inutile de multiplier" in caveat
|
||||
for caveat in out["caveats"]
|
||||
)
|
||||
|
||||
|
||||
def test_no_validated_forecast_means_no_anticipation(store):
|
||||
@@ -156,13 +201,15 @@ def test_no_validated_forecast_means_no_anticipation(store):
|
||||
REGULAR,
|
||||
remaining_km=420,
|
||||
price_now=1.55,
|
||||
position=position(0.10),
|
||||
historical_position=historical_position(0.10),
|
||||
local_position=local_position(0.16),
|
||||
outlook=dict(status="insufficient", reason="modèle non entraîné"),
|
||||
)
|
||||
assert out["action"] == "wait"
|
||||
assert out["litres_low"] is None
|
||||
assert out["forecast_note"] == "modèle non entraîné"
|
||||
assert any("bon prix dans l’historique" in reason for reason in out["reasons"])
|
||||
assert any("Position historique" in reason for reason in out["reasons"])
|
||||
assert any("Position locale" in reason for reason in out["reasons"])
|
||||
|
||||
|
||||
def test_without_forecast_a_short_runway_still_buys_the_need_only(store):
|
||||
@@ -171,7 +218,7 @@ def test_without_forecast_a_short_runway_still_buys_the_need_only(store):
|
||||
REGULAR,
|
||||
remaining_km=150,
|
||||
price_now=1.55,
|
||||
position=position(0.10),
|
||||
historical_position=historical_position(0.10),
|
||||
outlook=dict(status="insufficient", reason="modèle non entraîné"),
|
||||
)
|
||||
assert out["action"] == "topup"
|
||||
@@ -185,7 +232,7 @@ def test_low_confidence_forecast_does_not_drive_the_quantity():
|
||||
REGULAR,
|
||||
remaining_km=150,
|
||||
price_now=1.55,
|
||||
position=position(0.10),
|
||||
historical_position=historical_position(0.10),
|
||||
outlook=outlook_down(confidence=0.40),
|
||||
)
|
||||
assert out["horizon"] is None, "la prévision ne pilote pas la quantité sous le seuil"
|
||||
@@ -199,7 +246,7 @@ def test_no_forecast_but_immediate_need_still_buys_what_is_needed():
|
||||
REGULAR,
|
||||
remaining_km=110,
|
||||
price_now=1.62,
|
||||
position=None,
|
||||
historical_position=None,
|
||||
outlook=dict(status="insufficient", reason="modèle non entraîné"),
|
||||
)
|
||||
assert out["action"] == "topup"
|
||||
@@ -208,7 +255,9 @@ def test_no_forecast_but_immediate_need_still_buys_what_is_needed():
|
||||
|
||||
|
||||
def test_margin_reached_recommends_refuelling_without_waiting():
|
||||
out = quantity.plan(REGULAR, remaining_km=80, price_now=1.62, position=None, outlook=outlook_down())
|
||||
out = quantity.plan(
|
||||
REGULAR, remaining_km=80, price_now=1.62, historical_position=None, outlook=outlook_down()
|
||||
)
|
||||
assert out["action"] == "refuel"
|
||||
assert any("marge minimale est atteinte" in reason for reason in out["reasons"])
|
||||
|
||||
@@ -243,7 +292,11 @@ def test_occasional_vehicle_anticipates_before_a_rise_during_the_trip(store):
|
||||
|
||||
def test_declared_tank_caps_the_quantity(store):
|
||||
out = quantity.plan(
|
||||
REGULAR, remaining_km=780, price_now=1.55, position=position(0.10), outlook=outlook_up()
|
||||
REGULAR,
|
||||
remaining_km=780,
|
||||
price_now=1.55,
|
||||
historical_position=historical_position(0.10),
|
||||
outlook=outlook_up(),
|
||||
)
|
||||
assert out["action"] == "refill"
|
||||
assert out["litres_target"] == pytest.approx(4.0, abs=0.5), "réservoir presque plein"
|
||||
@@ -252,7 +305,11 @@ def test_declared_tank_caps_the_quantity(store):
|
||||
def test_missing_tank_is_declared_not_invented(store):
|
||||
vehicle = dict(REGULAR, tank_liters=None)
|
||||
out = quantity.plan(
|
||||
vehicle, remaining_km=150, price_now=1.62, position=position(0.5), outlook=outlook_down()
|
||||
vehicle,
|
||||
remaining_km=150,
|
||||
price_now=1.62,
|
||||
historical_position=historical_position(0.5),
|
||||
outlook=outlook_down(),
|
||||
)
|
||||
assert out["litres_target"] is not None
|
||||
assert any("Capacité du réservoir non déclarée" in caveat for caveat in out["caveats"])
|
||||
@@ -311,3 +368,87 @@ def test_absent_model_is_declared_absent(store):
|
||||
out = forecast.outlook("Gazole")
|
||||
assert out["status"] == "insufficient"
|
||||
assert "pas encore entraîné" in out["reason"]
|
||||
|
||||
|
||||
def test_local_rank_never_triggers_a_full_tank(store):
|
||||
"""Excellent localement mais cher dans le temps : aucune anticipation autorisée."""
|
||||
out = quantity.plan(
|
||||
REGULAR,
|
||||
remaining_km=200,
|
||||
historical_position=historical_position(0.72),
|
||||
local_position=local_position(0.01),
|
||||
outlook=outlook_up(),
|
||||
)
|
||||
assert out["action"] == "topup"
|
||||
assert out["action"] != "refill"
|
||||
assert any("n'est pas historiquement favorable" in reason for reason in out["reasons"])
|
||||
assert any("36 stations" in reason for reason in out["reasons"])
|
||||
|
||||
|
||||
def test_historical_rank_is_the_only_anticipation_trigger(store):
|
||||
out = quantity.plan(
|
||||
REGULAR,
|
||||
remaining_km=200,
|
||||
historical_position=historical_position(0.05),
|
||||
local_position=local_position(0.90),
|
||||
outlook=outlook_up(),
|
||||
)
|
||||
assert out["action"] == "refill"
|
||||
assert any("Position historique favorable" in reason for reason in out["reasons"])
|
||||
assert any("ne participe pas à cette décision" in caveat for caveat in out["caveats"])
|
||||
|
||||
|
||||
def test_detour_fuel_is_deducted_from_the_net_gain(store):
|
||||
"""Un détour peut annuler une économie : le gain net doit le montrer."""
|
||||
from quantity import detour_cost
|
||||
|
||||
detour = dict(extra_km=40.0, nearest_km=3.0, chosen_km=43.0)
|
||||
detour["cost_eur"] = detour_cost(detour, REGULAR, 1.55)
|
||||
assert detour["cost_eur"] == round(40 * 5 / 100 * 1.55, 2), "km × consommation × prix"
|
||||
out = quantity.plan(
|
||||
REGULAR,
|
||||
remaining_km=200,
|
||||
price_now=1.55,
|
||||
historical_position=historical_position(0.05),
|
||||
local_position=local_position(0.9),
|
||||
outlook=outlook_up(),
|
||||
detour=detour,
|
||||
)
|
||||
assert out["economy"]["detour_eur"] == detour["cost_eur"]
|
||||
assert out["economy"]["net_high"] == round(out["economy"]["gain_high"] - detour["cost_eur"], 2)
|
||||
assert out["economy"]["worth_it"] is False, "gain net sous le seuil : pas un passage de plus"
|
||||
assert any("Gain net faible" in caveat for caveat in out["caveats"])
|
||||
|
||||
|
||||
def test_a_small_detour_can_be_absorbed_by_the_expected_gain(store):
|
||||
from quantity import detour_cost
|
||||
|
||||
detour = dict(extra_km=2.0, nearest_km=3.0, chosen_km=5.0)
|
||||
detour["cost_eur"] = detour_cost(detour, REGULAR, 1.55)
|
||||
out = quantity.plan(
|
||||
REGULAR,
|
||||
remaining_km=200,
|
||||
price_now=1.55,
|
||||
historical_position=historical_position(0.05),
|
||||
local_position=local_position(0.9),
|
||||
outlook=outlook_up(),
|
||||
detour=detour,
|
||||
)
|
||||
assert out["economy"]["worth_it"] is True
|
||||
assert out["economy"]["net_high"] > out["economy"]["gain_high"] - 1.0
|
||||
|
||||
|
||||
def test_detour_cost_is_computed_by_the_engine(store):
|
||||
from quantity import detour_cost
|
||||
|
||||
assert detour_cost(None, REGULAR, 2.0) is None
|
||||
assert detour_cost(dict(extra_km=0.0), REGULAR, 2.0) == 0.0
|
||||
assert detour_cost(dict(extra_km=10.0), REGULAR, 2.0) == 1.0, "10 km × 5 L/100 × 2 €/L"
|
||||
|
||||
|
||||
def test_tank_content_is_always_an_estimate(store):
|
||||
out = quantity.plan(
|
||||
REGULAR, remaining_km=150, historical_position=historical_position(0.5), outlook=outlook_down()
|
||||
)
|
||||
assert out["estimated_in_tank_liters"] == 7.5, "estimation issue de l’autonomie affichée"
|
||||
assert any("estimé" in caveat or "estimation" in caveat for caveat in out["caveats"])
|
||||
|
||||
Reference in New Issue
Block a user