Files
Support-Agent/labeling/auto/etiqueteur/NEBRASKA_2026-10-01/outils/regression.py
T
maximeandClaude Opus 5.5 6fbd7510b7 Etiqueteur: October labels for 8 stations; its folder moves to labeling/auto/etiqueteur/, analyses and scripts versioned (D-126)
Labels for CLARIDEN, FLORIDA, JUNGFRAU (gaps 06-20.10, 26.10-01.11), LENZSPITZE,
NIEDERHORN, NORDEND, SNCF01, WILDSTRUBEL. Figures and tool outputs stay out of git.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 16:33:41 +01:00

45 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Régressions : tilt ~ température (+ temps) ; cx ~ tilt, cx ~ température ; résidus par jour."""
import csv, statistics as st
from datetime import datetime
import matplotlib; matplotlib.use("Agg")
import matplotlib.pyplot as plt
OUT = "labeling/auto/etiqueteur/NEBRASKA_2026-10-01"
R = [r for r in csv.DictReader(open(f"{OUT}/table.tsv"), delimiter="\t")]
f = lambda r, c: float(r[c]) if r[c] not in ("", "None", "nan") else None
R = [r for r in R if f(r, "A0_24h") is not None and f(r, "tilt") is not None]
day = lambda r: (datetime.fromisoformat(r["t"]) - datetime(2026, 10, 3)).total_seconds() / 86400
def ols(X, y): # moindres carrés par équations normales (petites dimensions)
n = len(X[0]); A = [[sum(a[i] * a[j] for a in X) for j in range(n)] for i in range(n)]; b = [sum(a[i] * v for a, v in zip(X, y)) for i in range(n)]
for i in range(n):
p = A[i][i]
for j in range(i + 1, n):
m = A[j][i] / p; A[j] = [u - m * w for u, w in zip(A[j], A[i])]; b[j] -= m * b[i]
c = [0] * n
for i in reversed(range(n)): c[i] = (b[i] - sum(A[i][j] * c[j] for j in range(i + 1, n))) / A[i][i]
res = [v - sum(ci * a for ci, a in zip(c, row)) for row, v in zip(X, y)]
return c, res
def r2(y, res): m = st.mean(y); return 1 - sum(e * e for e in res) / sum((v - m) ** 2 for v in y)
for nom, sel in [("mois", R), ("après 09.10 12h", [r for r in R if r["t"] >= "2026-10-09T12"])]:
T = [f(r, "A0_24h") for r in sel]; d = [day(r) for r in sel]; ti = [f(r, "tilt") for r in sel]; cx = [f(r, "cx") for r in sel]
for lab, y in [("tilt", ti), ("cx", cx)]:
c1, e1 = ols([[1, t] for t in T], y); c2, e2 = ols([[1, x] for x in d], y); c3, e3 = ols([[1, t, x] for t, x in zip(T, d)], y)
print(f"{nom} {lab}: R2 temp={r2(y,e1):.2f} (pente {c1[1]:.4g}/°C), R2 temps={r2(y,e2):.2f}, R2 temp+temps={r2(y,e3):.2f} (pente temp {c3[1]:.4g}, pente temps {c3[2]:.4g}/j)")
c, e = ols([[1, t] for t in [f(r, "tilt") for r in sel]], cx); print(f"{nom} cx~tilt R2={r2(cx,e):.2f} pente {c[1]:.4g}")
# résidu du tilt après la température, sur le mois, figure
T = [f(r, "A0_24h") for r in R]; ti = [f(r, "tilt") for r in R]; x = [datetime.fromisoformat(r["t"]) for r in R]
c, e = ols([[1, t] for t in T], ti)
cx = [f(r, "cx") for r in R]
sel = [i for i, r in enumerate(R) if r["t"] >= "2026-10-09T12"]
cc, ee = ols([[1, T[i]] for i in sel], [cx[i] for i in sel])
fig, ax = plt.subplots(3, 1, figsize=(14, 9), sharex=True)
ax[0].plot(x, e, ".-"); ax[0].set_ylabel("tilt − f(temp. 24 h)"); ax[0].axhline(0, color="k", lw=.5)
ax[1].plot([x[i] for i in sel], ee, ".-"); ax[1].set_ylabel("cx − f(temp. 24 h) après 09.10"); ax[1].axhline(0, color="k", lw=.5)
ax[2].plot(x, [f(r, "A0") for r in R], "-", lw=.6, label="A0 brut"); ax[2].plot(x, T, "r-", label="A0 24 h"); ax[2].plot(x, [f(r, "thermal") for r in R], "-", lw=.6, label="thermal"); ax[2].legend()
for a in ax: a.grid(alpha=.3)
fig.tight_layout(); fig.savefig(f"{OUT}/residus_temperature.png", dpi=80); print(f"{OUT}/residus_temperature.png")
by = {}
for r, ei in zip(R, e): by.setdefault(r["t"][:10], []).append(ei)
print("résidu tilt par jour:", " ".join(f"{k[5:]}:{st.mean(v):+.4f}" for k, v in by.items()))