File size: 5,928 Bytes
756aa06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""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)