Identite Fuel Advisor, assets locaux et audit economique corrige
Renommage Fuel Advisor (titre, header, favicon SVG/PNG, apple-touch-icon, logo SVG servis localement). Oracle: contrainte de penalty corrigee (portee inversee), quantites non entieres ecartees avant rejeu, double comptage de penalite supprime. Audit economique regenerer: plafonds omniscients coherents et positifs; deux lignes non resolues conservees telles quelles.
This commit is contained in:
119
research/economic_audit/run_economic_audit.py
Normal file
119
research/economic_audit/run_economic_audit.py
Normal file
@@ -0,0 +1,119 @@
|
||||
"""Campagne d'audit économique. Règles et modes écrits avant lecture des nouveaux résultats."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from statistics import fmean
|
||||
|
||||
from economic_engine import compare, simulate
|
||||
from timing_oracle import lp_ceiling, oracle
|
||||
|
||||
from signal_audit import quantile
|
||||
|
||||
ROOT = Path(__file__).parent
|
||||
PERIODS = [("2024-01-01", "2024-12-31", "diagnostic_2024"), ("2025-01-01", "2026-08-30", "frozen_2025_2026")]
|
||||
FORCED = [("forced_12", 12.0), ("forced_50", 50.0)]
|
||||
|
||||
|
||||
def frozen_forecasts(fuel, cutoff):
|
||||
"""Paramètres gelés avant `cutoff` : q90, régime local et amplitude attendue."""
|
||||
rows = json.loads((ROOT / f"audit-rows-{fuel}.json").read_text())
|
||||
points = json.loads((ROOT / f"audit-signals-{fuel}.json").read_text())
|
||||
past = [p for p in points if p["t"] < cutoff][-730:]
|
||||
threshold = quantile([p["score"] for p in past])
|
||||
mature = [r for r in rows if r["target"]["available_at"] < cutoff]
|
||||
fired = {p["t"] for p in points if p["t"] < cutoff and p["fired"]}
|
||||
amplitudes = [r["target"]["future_max_delta"] for r in mature if r["t"] in fired][-730:]
|
||||
expected = fmean(amplitudes)
|
||||
base = (sum(r["target"]["events"]["up_3"]["hit"] for r in mature) + 1) / (len(mature) + 2)
|
||||
|
||||
def local(group):
|
||||
group_rows = [r for r in mature if r["regime"] == group]
|
||||
return (sum(r["target"]["events"]["up_3"]["hit"] for r in group_rows) + 30 * base) / (
|
||||
len(group_rows) + 30
|
||||
)
|
||||
|
||||
scores = {tuple(r["regime"]): local(r["regime"]) for r in mature}
|
||||
out = {
|
||||
r["t"]: expected if scores.get(tuple(r["regime"]), 0) >= threshold else 0
|
||||
for r in rows
|
||||
if r["t"] >= cutoff
|
||||
}
|
||||
freeze = dict(
|
||||
cutoff=cutoff,
|
||||
threshold=threshold,
|
||||
expected_amplitude_all_signals=expected,
|
||||
signal_history=len(amplitudes),
|
||||
training_available_at_max=max(r["target"]["available_at"] for r in mature),
|
||||
threshold_source_max=past[-1]["t"],
|
||||
note="paramètres gelés avant la période évaluée, features as-of",
|
||||
)
|
||||
return out, freeze
|
||||
|
||||
|
||||
def run():
|
||||
data = json.loads((ROOT / "economic-inputs.json").read_text())
|
||||
report = {"status": "offline_audit", "results": [], "failures": []}
|
||||
for start, end, label in PERIODS:
|
||||
for key, vehicle in data["profiles"].items():
|
||||
points = [p for p in data["fuels"][vehicle["fuel"]] if start <= p["day"] <= end]
|
||||
days = [p["day"] for p in points]
|
||||
prices = [sorted(p["prices"].values())[len(p["prices"]) // 2] for p in points]
|
||||
forecasts, freeze = frozen_forecasts(vehicle["fuel"], start)
|
||||
base = simulate(days, prices, vehicle)
|
||||
entry = {
|
||||
"profile": key,
|
||||
"period": label,
|
||||
"days": len(days),
|
||||
"freeze": freeze,
|
||||
"baseline": base,
|
||||
"strategies": {},
|
||||
}
|
||||
modes = [
|
||||
("local", forecasts, False),
|
||||
*[(name, {d: c for d in days}, False) for name, c in FORCED],
|
||||
("oracle_no_extra", None, True),
|
||||
("oracle_penalized", None, True),
|
||||
]
|
||||
for name, signals, is_oracle in modes:
|
||||
try:
|
||||
if is_oracle:
|
||||
result = oracle(days, prices, vehicle, base, noextra=name == "oracle_no_extra")
|
||||
else:
|
||||
result = simulate(days, prices, vehicle, "local", signals)
|
||||
entry["strategies"][name] = {
|
||||
"summary": compare(base, result, days, penalty_included=is_oracle),
|
||||
"raw": result,
|
||||
"penalty_included": is_oracle,
|
||||
}
|
||||
print(label, key, name, entry["strategies"][name]["summary"], flush=True)
|
||||
except ValueError as exc:
|
||||
entry["strategies"][name] = {"error": str(exc)}
|
||||
report["failures"].append([label, key, name, str(exc)])
|
||||
print("FAILED", label, key, name, exc, flush=True)
|
||||
if label == "frozen_2025_2026":
|
||||
for name, kwargs in (
|
||||
("oracle_full_bound", {"full": True, "limit": 240}),
|
||||
("lp_optimistic_ceiling", None),
|
||||
):
|
||||
try:
|
||||
if kwargs:
|
||||
result = oracle(days, prices, vehicle, base, **kwargs)
|
||||
entry["strategies"][name] = {
|
||||
"summary": compare(base, result, days, penalty_included=True),
|
||||
"raw": result,
|
||||
"penalty_included": True,
|
||||
}
|
||||
else:
|
||||
entry["strategies"][name] = lp_ceiling(days, prices, vehicle, base)
|
||||
print(label, key, name, entry["strategies"][name], flush=True)
|
||||
except ValueError as exc:
|
||||
entry["strategies"][name] = {"error": str(exc)}
|
||||
report["failures"].append([label, key, name, str(exc)])
|
||||
print("FAILED", label, key, name, exc, flush=True)
|
||||
report["results"].append(entry)
|
||||
(ROOT / "economic-audit-results.json").write_text(json.dumps(report, indent=1))
|
||||
(ROOT / "economic-audit-results.json").write_text(json.dumps(report, indent=1))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
Reference in New Issue
Block a user