timestral-backtest-lab / lab /plotting.py
Segizu's picture
Timestral Backtest Lab (ZeroGPU)
756aa06 verified
Raw History Blame Contribute Delete
5.93 kB
"""Plotly figures for holdout backtest + future forecast bands."""
from __future__ import annotations
from typing import Any, Sequence
import numpy as np
import plotly.graph_objects as go
from lab.results import BacktestResult
def _axis(timestamps: Sequence[Any] | None, n: int) -> list[Any]:
if timestamps is not None and len(timestamps) >= n:
return list(timestamps[:n]) if len(timestamps) == n else list(timestamps)
return list(range(n))
def build_figure(
values: np.ndarray,
result: BacktestResult,
*,
timestamps: Sequence[Any] | None = None,
title: str = "Timestral Backtest Lab",
history_to_show: int = 400,
) -> go.Figure:
values = np.asarray(values, dtype=np.float64)
n = len(values)
holdout = len(result.holdout_actual)
future_h = len(result.future.median)
if timestamps is not None and len(timestamps) == n:
x_all = list(timestamps)
# Infer future index
try:
import pandas as pd
ts = pd.DatetimeIndex(timestamps)
delta = pd.Series(ts).diff().median()
if pd.isna(delta):
x_future = list(range(n, n + future_h))
else:
x_future = list(pd.date_range(ts[-1] + delta, periods=future_h, freq=delta))
except Exception: # noqa: BLE001
x_future = list(range(n, n + future_h))
else:
x_all = list(range(n))
x_future = list(range(n, n + future_h))
start = max(0, n - int(history_to_show))
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=x_all[start : n - holdout],
y=values[start : n - holdout],
mode="lines",
name="contexto (train)",
line=dict(color="#334155", width=1.6),
)
)
fig.add_trace(
go.Scatter(
x=x_all[n - holdout : n],
y=result.holdout_actual,
mode="lines",
name="holdout real",
line=dict(color="#0f766e", width=2.0),
)
)
fig.add_trace(
go.Scatter(
x=x_all[n - holdout : n],
y=result.model_holdout,
mode="lines",
name="Timestral (holdout)",
line=dict(color="#c2410c", width=2.0, dash="dash"),
)
)
fig.add_trace(
go.Scatter(
x=x_all[n - holdout : n],
y=result.last_value,
mode="lines",
name="baseline last-value",
line=dict(color="#64748b", width=1.2, dash="dot"),
)
)
fig.add_trace(
go.Scatter(
x=x_all[n - holdout : n],
y=result.seasonal_naive,
mode="lines",
name="baseline seasonal-naive",
line=dict(color="#7c3aed", width=1.2, dash="dot"),
)
)
# Future uncertainty band (80% q10–q90) — only where quantiles support it.
fig.add_trace(
go.Scatter(
x=x_future,
y=result.future.q_high,
mode="lines",
line=dict(width=0),
showlegend=False,
hoverinfo="skip",
)
)
fig.add_trace(
go.Scatter(
x=x_future,
y=result.future.q_low,
mode="lines",
line=dict(width=0),
fill="tonexty",
fillcolor="rgba(194, 65, 12, 0.20)",
name="banda 80% (q10–q90)",
hoverinfo="skip",
)
)
fig.add_trace(
go.Scatter(
x=x_future,
y=result.future.median,
mode="lines",
name="pronóstico futuro (mediana)",
line=dict(color="#c2410c", width=2.2),
)
)
fig.update_layout(
title=title,
template="plotly_white",
height=520,
margin=dict(l=40, r=20, t=60, b=40),
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="left", x=0),
hovermode="x unified",
xaxis_title="tiempo",
yaxis_title="valor",
)
return fig
def metrics_markdown(metrics: dict[str, dict[str, Any]]) -> str:
lines = [
"### Comparación en holdout (MAE / MASE)",
"",
"| Modelo | MAE | MASE |",
"|--------|-----|------|",
]
labels = {
"timestral": "Timestral-28M",
"last_value": "Last-value",
"seasonal_naive": "Seasonal-naive",
}
for key in ("timestral", "last_value", "seasonal_naive"):
row = metrics.get(key, {})
mae_v = row.get("mae", "")
mase_v = row.get("mase", "")
if isinstance(mae_v, float):
mae_s = f"{mae_v:.4f}"
else:
mae_s = str(mae_v)
lines.append(f"| {labels.get(key, key)} | {mae_s} | {mase_v} |")
lines.append("")
lines.append(
"_MASE usa solo la escala estacional del **entrenamiento**. "
"Si el denominador es ~0 (serie constante), se muestra **N/A** (no 0). "
"No se usan métricas porcentuales (indefinidas en ceros)._"
)
# Honest framing
try:
t_mae = float(metrics["timestral"]["mae"])
best_base = min(float(metrics["last_value"]["mae"]), float(metrics["seasonal_naive"]["mae"]))
if t_mae < best_base:
verdict = (
"**En este holdout**, Timestral mejora el mejor baseline en MAE. "
"No garantiza victoria en tus datos futuros."
)
elif t_mae > best_base:
verdict = (
"**En este holdout**, un baseline simple gana en MAE. "
"El modelo fundacional no siempre supera una regla ingenua."
)
else:
verdict = "MAE empatado con el mejor baseline en este holdout."
except Exception: # noqa: BLE001
verdict = "Compara MAE/MASE arriba para decidir si el modelo aporta valor en tus datos."
lines.append("")
lines.append(verdict)
return "\n".join(lines)