Conseil simule aligne sur le moteur reel et validation par regimes
Le conseiller du backtest raisonne desormais comme le moteur de decision : besoin d abord, anticipation seulement si le gain depasse le seuil du vehicule, et plancher d appoint qui interdit de repasser a la pompe pour quelques litres. La campagne precedente le montrait : sans ce plancher, la strategie multipliait les passages quotidiens et le verdict la refusait a juste titre. Ajout de la validation par regimes : chaque periode atypique est evaluee avec un apprentissage arrete la veille de son debut, plus l experience ou la crise actuelle reste entierement hors entrainement.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
"""Deux indicateurs distincts : position dans l'espace (aujourd'hui) et dans le temps (historique)."""
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
import storage
|
||||
@@ -10,75 +11,88 @@ def days_ago(n):
|
||||
|
||||
def seed_observation(store, station_id, fuel, day, price, lat=44.7737872, lon=-0.5595029):
|
||||
store.save_archive_observations(
|
||||
[dict(station_id=station_id, fuel=fuel, day=day, price=price,
|
||||
declared_at=f'{day}T10:00:00', lat=lat, lon=lon)], 'archive:test')
|
||||
[
|
||||
dict(
|
||||
station_id=station_id,
|
||||
fuel=fuel,
|
||||
day=day,
|
||||
price=price,
|
||||
declared_at=f"{day}T10:00:00",
|
||||
lat=lat,
|
||||
lon=lon,
|
||||
)
|
||||
],
|
||||
"archive:test",
|
||||
)
|
||||
|
||||
|
||||
def test_local_position_ranks_stations_today(store):
|
||||
for index, price in enumerate((2.30, 2.10, 1.99, 2.20)):
|
||||
seed_observation(store, f'station{index}', 'E10', days_ago(0), price)
|
||||
local = store.local_position(44.7737872, -0.5595029, 15, 'E10')
|
||||
assert local['kind'] == 'local'
|
||||
assert local['stations'] == 4
|
||||
assert local['percentile'] == 0.25, 'le meilleur prix est le 1ᵉʳ des 4'
|
||||
assert local['best'] == 1.99
|
||||
assert 'dans l’espace' in local['summary'] and 'pas dans le temps' in local['summary']
|
||||
seed_observation(store, f"station{index}", "E10", days_ago(0), price)
|
||||
local = store.local_position(44.7737872, -0.5595029, 15, "E10")
|
||||
assert local["kind"] == "local"
|
||||
assert local["stations"] == 4
|
||||
assert local["percentile"] == 0.25, "le meilleur prix est le 1ᵉʳ des 4"
|
||||
assert local["best"] == 1.99
|
||||
assert "dans l’espace" in local["summary"] and "pas dans le temps" in local["summary"]
|
||||
|
||||
|
||||
def test_historical_position_uses_daily_medians_over_windows(store):
|
||||
"""Un jour = un point (médiane du panel), pour que les jours chargés ne pèsent pas plus."""
|
||||
for offset in range(40, 0, -1):
|
||||
day = days_ago(offset)
|
||||
seed_observation(store, 'a', 'E10', day, 2.20 + (offset - 40) * 0.01)
|
||||
seed_observation(store, 'b', 'E10', day, 2.30 + (offset - 40) * 0.01)
|
||||
seed_observation(store, 'a', 'E10', days_ago(39), 2.19)
|
||||
seed_observation(store, 'b', 'E10', days_ago(39), 2.29)
|
||||
seed_observation(store, 'c', 'E10', days_ago(39), 2.39)
|
||||
seed_observation(store, "a", "E10", day, 2.20 + (offset - 40) * 0.01)
|
||||
seed_observation(store, "b", "E10", day, 2.30 + (offset - 40) * 0.01)
|
||||
seed_observation(store, "a", "E10", days_ago(39), 2.19)
|
||||
seed_observation(store, "b", "E10", days_ago(39), 2.29)
|
||||
seed_observation(store, "c", "E10", days_ago(39), 2.39)
|
||||
for index, price in enumerate((1.95, 2.05, 2.15)):
|
||||
seed_observation(store, f'today{index}', 'E10', days_ago(0), price)
|
||||
position = store.historical_position(44.7737872, -0.5595029, 15, 'E10',
|
||||
windows=(30, 90, 365))
|
||||
assert position['kind'] == 'historique'
|
||||
seed_observation(store, f"today{index}", "E10", days_ago(0), price)
|
||||
position = store.historical_position(44.7737872, -0.5595029, 15, "E10", windows=(30, 90, 365))
|
||||
assert position["kind"] == "historique"
|
||||
# Reconstruction : les stations qui n'ont pas déclaré aujourd'hui comptent encore, avec leur
|
||||
# dernière valeur connue. La médiane n'est donc pas celle des seules déclarations du jour.
|
||||
assert position['daily_series'][1]['stations'] == 3, 'les 3 stations déclarées la veille'
|
||||
assert position['current'] == 1.95, 'a et b comptent encore (≈1,81 et ≈1,91), d’où 1,95'
|
||||
assert 'aucune interpolation' in position['basis']
|
||||
assert position['windows']['30']['percentile'] is not None
|
||||
assert position['windows']['30']['days'] <= 31
|
||||
assert position['windows']['365']['percentile'] is None, \
|
||||
'40 jours d’historique ne suffisent pas pour un percentile à 365 jours'
|
||||
assert 'Historique' in position['windows']['365']['summary'] or \
|
||||
'insuffisant' in position['windows']['365']['summary']
|
||||
assert position["daily_series"][1]["stations"] == 3, "les 3 stations déclarées la veille"
|
||||
assert position["current"] == 1.95, "a et b comptent encore (≈1,81 et ≈1,91), d’où 1,95"
|
||||
assert "aucune interpolation" in position["basis"]
|
||||
assert position["windows"]["30"]["percentile"] is not None
|
||||
assert position["windows"]["30"]["days"] <= 31
|
||||
assert position["windows"]["365"]["percentile"] is None, (
|
||||
"40 jours d’historique ne suffisent pas pour un percentile à 365 jours"
|
||||
)
|
||||
assert (
|
||||
"Historique" in position["windows"]["365"]["summary"]
|
||||
or "insuffisant" in position["windows"]["365"]["summary"]
|
||||
)
|
||||
|
||||
|
||||
def test_historical_position_is_insufficient_without_history(store):
|
||||
for index, price in enumerate((2.0, 2.1, 2.2)):
|
||||
seed_observation(store, f'today{index}', 'E10', days_ago(0), price)
|
||||
position = store.historical_position(44.7737872, -0.5595029, 15, 'E10')
|
||||
assert position['percentile'] is None
|
||||
assert position['windows']['30']['percentile'] is None
|
||||
assert 'insuffisant' in position['windows']['30']['summary']
|
||||
seed_observation(store, f"today{index}", "E10", days_ago(0), price)
|
||||
position = store.historical_position(44.7737872, -0.5595029, 15, "E10")
|
||||
assert position["percentile"] is None
|
||||
assert position["windows"]["30"]["percentile"] is None
|
||||
assert "insuffisant" in position["windows"]["30"]["summary"]
|
||||
|
||||
|
||||
def test_positions_are_two_different_distributions(store):
|
||||
"""Le même jour peut être excellent localement et cher dans le temps."""
|
||||
for offset in range(25, 0, -1):
|
||||
for index, base in enumerate((1.70, 1.75, 1.80)):
|
||||
seed_observation(store, f'old{index}', 'E10', days_ago(offset), base)
|
||||
seed_observation(store, f"old{index}", "E10", days_ago(offset), base)
|
||||
for index, price in enumerate((2.40, 2.45, 2.50)):
|
||||
seed_observation(store, f'today{index}', 'E10', days_ago(0), price)
|
||||
local = store.local_position(44.7737872, -0.5595029, 15, 'E10')
|
||||
historical = store.historical_position(44.7737872, -0.5595029, 15, 'E10')
|
||||
assert local['percentile'] == round(1 / 3, 3), 'meilleur prix du jour'
|
||||
assert historical['percentile'] >= 0.9, 'mais cher par rapport à son propre passé'
|
||||
assert local['percentile'] < 0.5 < historical['percentile']
|
||||
seed_observation(store, f"today{index}", "E10", days_ago(0), price)
|
||||
local = store.local_position(44.7737872, -0.5595029, 15, "E10")
|
||||
historical = store.historical_position(44.7737872, -0.5595029, 15, "E10")
|
||||
assert local["percentile"] == round(1 / 3, 3), "meilleur prix du jour"
|
||||
assert historical["percentile"] >= 0.9, "mais cher par rapport à son propre passé"
|
||||
assert local["percentile"] < 0.5 < historical["percentile"]
|
||||
|
||||
|
||||
def test_stations_outside_the_radius_are_excluded(store):
|
||||
seed_observation(store, 'loin', 'E10', days_ago(0), 1.50, lat=48.8566, lon=2.3522)
|
||||
seed_observation(store, 'proche', 'E10', days_ago(1), 2.00)
|
||||
seed_observation(store, 'proche', 'E10', days_ago(0), 2.05)
|
||||
local = store.local_position(44.7737872, -0.5595029, 15, 'E10')
|
||||
assert local['stations'] == 1, 'la station parisienne est hors rayon'
|
||||
assert local['best'] == 2.05
|
||||
seed_observation(store, "loin", "E10", days_ago(0), 1.50, lat=48.8566, lon=2.3522)
|
||||
seed_observation(store, "proche", "E10", days_ago(1), 2.00)
|
||||
seed_observation(store, "proche", "E10", days_ago(0), 2.05)
|
||||
local = store.local_position(44.7737872, -0.5595029, 15, "E10")
|
||||
assert local["stations"] == 1, "la station parisienne est hors rayon"
|
||||
assert local["best"] == 2.05
|
||||
|
||||
Reference in New Issue
Block a user