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:
97
signal_audit.py
Normal file
97
signal_audit.py
Normal file
@@ -0,0 +1,97 @@
|
||||
"""Audit offline : seuil strictement passé et amplitudes de cibles mûres seulement."""
|
||||
|
||||
import json
|
||||
import math
|
||||
from collections import deque
|
||||
from pathlib import Path
|
||||
from statistics import fmean, median
|
||||
|
||||
|
||||
def quantile(values):
|
||||
ordered = sorted(values)
|
||||
return ordered[min(len(ordered) - 1, math.ceil(0.9 * (len(ordered) - 1)))]
|
||||
|
||||
|
||||
def audit_rows(rows, key="up_3", minimum=180, window=730):
|
||||
history, pending, matured, out = [], deque(), [], []
|
||||
for row in rows:
|
||||
t = row["t"]
|
||||
while pending and pending[0]["target"]["available_at"] < t:
|
||||
matured.append(pending.popleft())
|
||||
score = row["score"][key]
|
||||
if len(history) >= minimum:
|
||||
past = history[-window:]
|
||||
assert past[-1][0] < t
|
||||
threshold = quantile([v for _, v in past])
|
||||
fired = score >= threshold
|
||||
recent = matured[-window:]
|
||||
all_values = [r["target"]["future_max_delta"] for r in recent]
|
||||
hits = [r["target"]["future_max_delta"] for r in recent if r["target"]["events"][key]["hit"]]
|
||||
out.append(
|
||||
dict(
|
||||
t=t,
|
||||
score=score,
|
||||
threshold=threshold,
|
||||
fired=fired,
|
||||
threshold_source_max=past[-1][0],
|
||||
expected_amplitude_all_signals=fmean(all_values) if len(recent) >= 20 else None,
|
||||
median_amplitude_all_signals=median(all_values) if len(recent) >= 20 else None,
|
||||
amplitude_when_event_occurs=fmean(hits) if hits else None,
|
||||
amplitude_source_max=max((r["target"]["available_at"] for r in recent), default=None),
|
||||
amplitude_sample=len(recent),
|
||||
)
|
||||
)
|
||||
if fired:
|
||||
pending.append(row)
|
||||
history.append((t, score))
|
||||
return out
|
||||
|
||||
|
||||
def summarize(points):
|
||||
result = {}
|
||||
for year in ("2024", "2025", "2026"):
|
||||
group = [p for p in points if p["t"].startswith(year) and p["t"] <= "2026-08-30"]
|
||||
thresholds = [p["threshold"] for p in group]
|
||||
result[year] = dict(
|
||||
evaluable=len(group),
|
||||
signals=sum(p["fired"] for p in group),
|
||||
coverage=fmean(p["fired"] for p in group),
|
||||
score_median=median(p["score"] for p in group),
|
||||
q90_median=median(thresholds),
|
||||
q90_min=min(thresholds),
|
||||
q90_max=max(thresholds),
|
||||
)
|
||||
changes = []
|
||||
for i in range(28, len(points) - 28):
|
||||
p = points[i]
|
||||
if not "2024-01-01" <= p["t"] <= "2026-08-02":
|
||||
continue
|
||||
before = fmean(x["fired"] for x in points[i - 28 : i])
|
||||
after = fmean(x["fired"] for x in points[i : i + 28])
|
||||
changes.append((abs(after - before), i, before, after))
|
||||
selected = []
|
||||
for _delta, i, _before, _after in sorted(changes, reverse=True):
|
||||
if all(abs(i - j) >= 56 for j in selected):
|
||||
selected.append(i)
|
||||
if len(selected) == 3:
|
||||
break
|
||||
result["transitions"] = [dict(date=points[i]["t"], surrounding=points[i - 2 : i + 3]) for i in selected]
|
||||
return result
|
||||
|
||||
|
||||
def run():
|
||||
from enrich_campaign import local_rows
|
||||
|
||||
out = {}
|
||||
for fuel in ("Gazole", "SP95", "E10"):
|
||||
rows = local_rows(fuel, 14)
|
||||
points = audit_rows(rows)
|
||||
Path(f"reports/audit-rows-{fuel}.json").write_text(json.dumps(rows))
|
||||
Path(f"reports/audit-signals-{fuel}.json").write_text(json.dumps(points))
|
||||
out[fuel] = summarize(points)
|
||||
print(fuel, json.dumps(out[fuel], ensure_ascii=False), flush=True)
|
||||
Path("reports/signal-audit.json").write_text(json.dumps(out, ensure_ascii=False, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
Reference in New Issue
Block a user