Skip to content

Temporal Hierarchy Reconciliation (THieF) & Coherent Sample Paths

flowchart TD
    T1["1. Configure Reconciliation Mode (HierarchyConfig)<br/>Mode 1: spatial_only (alpha=1.0) · Mode 2: temporal_only (alpha=0.0 + OOF scoring)<br/>Mode 3: cross_temporal_mint (spatial_temporal_weight='auto' | float in [0, 1])"] --> T2["2. Build Cutoff-Aligned Temporal Hierarchy (THieF)<br/>default_temporal_aggregations(freq) -> S_temp across k in {1, 7, 14, 28} / {1, 3, 6, 12}"]
    T2 --> T3["3. Project & Refine via OOF-Optimal Alpha + Alternating Projections<br/>reconcile_temporal_forecasts · reconcile_cross_temporal -> < 1e-6 discrepancy on S_cs & S_temp"]
    T3 --> T4["4. Verify Coherence & Simulate Coherent Trajectories<br/>verify_temporal_coherence · verify_cross_temporal_coherence · sample_coherent_paths(n_paths=200)"]

temporal_reconciliation

Temporal Hierarchies (THieF) and Simultaneous Cross-Temporal Coherent Reconciliation.

Implements the Athanasopoulos et al. (2017) Forecasting with Temporal Hierarchies (THieF) and Di Fonzo & Girolimetto (2023) cross-temporal forecast reconciliation framework in pure pandas/NumPy:

  1. Temporal Hierarchy Construction (default_temporal_aggregations, build_temporal_hierarchy) — Resolves canonical aggregation factors from data.freq (e.g., [7, 14, 28] on daily "D", [4, 13, 52] on weekly "W", [3, 6, 12] on monthly "M"/"MS"/"ME", [2, 4] on quarterly "Q", and [6, 12, 24] on hourly "H"/"h") and compiles the single-cycle temporal summing matrix S_temp of shape (n_temp_nodes, macro_period) alongside the multi-cycle Kronecker summing matrix I_P \otimes S_temp.
  2. Non-Overlapping Temporal Aggregation (aggregate_temporal_series) — Aggregates base-frequency historical series or DataFrames into non-overlapping blocks of size k aligned to the cutoff timestamp (trimming T % k oldest observations from the front so every aggregation level terminates on the exact same cutoff date).
  3. Temporal Projection (reconcile_temporal_matrix, reconcile_series_temporal, reconcile_temporal_forecasts) — Projects multi-frequency forecasts onto the coherent temporal subspace using bottom_up, top_down, middle_out, ols, wls_struct (struc), wls_var, mint_shrink (Schäfer-Strimmer shrinkage), and mint_nonneg (non-negative least squares), cleanly handling non-divisible horizons H and short histories T < 2 * m_max.
  4. Simultaneous Cross-Temporal Reconciliation (reconcile_cross_temporal / cross_temporal_mint) — Reconciles hierarchical panels across both the cross-sectional summing matrix S_cs and the temporal summing matrix S_temp simultaneously, guaranteeing < 1e-6 discrepancy on both axes across base models, ensembles, OOF folds, and horizon predictions.

SafeArrayDict

Bases: dict[Any, Any]

Dictionary subclass comparing NumPy array and nested mapping values via np.array_equal.

TemporalHierarchySpec dataclass

TemporalHierarchySpec(
    base_freq,
    horizon,
    padded_horizon,
    macro_period,
    n_macro_periods,
    aggregations,
    level_names,
    level_slices,
    summing_matrix,
    full_summing_matrix,
)

Compiled Temporal Hierarchy (THieF) aggregation tree over a base horizon.

Attributes:

Name Type Description
base_freq str

Base pandas frequency string (e.g., "D", "W", "MS").

horizon int

Requested base-frequency forecast horizon H.

padded_horizon int

Padded multiple of macro_period (P * macro_period >= H).

macro_period int

Least common multiple m of all aggregation factors.

n_macro_periods int

Number of complete macro-cycles P = ceil(H / m).

aggregations tuple[int, ...]

Ascending tuple of integer aggregation factors starting with 1.

level_names tuple[str, ...]

Coarse-to-fine level identifiers (e.g., ("k28", "k14", "k7", "k1")).

level_slices dict[int, slice]

Row slice in summing_matrix for each aggregation factor k.

summing_matrix ndarray

Single-cycle binary summing matrix S_temp of shape (n_temp_nodes, macro_period), ordered from coarsest factor to k=1.

full_summing_matrix ndarray

Multi-cycle binary summing matrix I_P \otimes S_temp of shape (P * n_temp_nodes, padded_horizon).

S_temp property

S_temp

Alias for summing_matrix (single-cycle temporal summing matrix).

factors property

factors

Alias for aggregations (ascending temporal factors including 1).

full_structural_weights property

full_structural_weights

Number of base steps summed into each full-horizon temporal node.

m property

m

Alias for macro_period (single-cycle base steps).

n_bottom property

n_bottom

Total number of padded bottom steps across all n_macro_periods cycles.

n_bottom_per_cycle property

n_bottom_per_cycle

Number of base-frequency steps per macro-cycle (macro_period).

n_nodes property

n_nodes

Total number of temporal nodes across all n_macro_periods cycles.

n_total property

n_total

Total number of temporal nodes per macro-cycle.

n_total_per_cycle property

n_total_per_cycle

Number of temporal nodes per macro-cycle.

structural_weights property

structural_weights

Number of base steps summed into each single-cycle temporal node (S_temp @ 1).

aggregate_temporal_history

aggregate_temporal_history(y, k, *, target_col='y')

Aggregate a 1-D base series into non-overlapping blocks of length k aligned to the end.

Drops len(y) % k oldest observations from the front so the final block ends on the exact same cutoff timestamp as the base-frequency series.

Source code in src/scale_forecasting/temporal_reconciliation.py
def aggregate_temporal_history(
    y: pd.Series | np.ndarray | Sequence[float] | pd.DataFrame,
    k: int,
    *,
    target_col: str = "y",
) -> np.ndarray:
    """Aggregate a 1-D base series into non-overlapping blocks of length ``k`` aligned to the end.

    Drops ``len(y) % k`` oldest observations from the front so the final block ends on the exact
    same cutoff timestamp as the base-frequency series.
    """
    factor = int(k)
    if factor < 1:
        raise ConfigError(f"Temporal aggregation factor k must be >= 1; got {k}")
    if isinstance(y, pd.DataFrame):
        col = target_col if target_col in y.columns else ("yhat" if "yhat" in y.columns else None)
        if col is None:
            raise DataError(f"DataFrame is missing '{target_col}' or 'yhat' column")
        vals = y[col].to_numpy(dtype=float)
    elif isinstance(y, pd.Series):
        vals = y.to_numpy(dtype=float)
    else:
        vals = np.asarray(y, dtype=float).reshape(-1)

    if factor == 1:
        return vals.copy()
    n = len(vals)
    if n < factor:
        return np.empty(0, dtype=float)
    rem = n % factor
    trimmed = vals[rem:] if rem > 0 else vals
    return trimmed.reshape(-1, factor).sum(axis=1)

aggregate_temporal_series

aggregate_temporal_series(
    y,
    k,
    *,
    date_col="ds",
    target_col="y",
    ts_id_col="ts_id",
)

Aggregate a 1-D series (np.ndarray) or panel DataFrame into blocks of k.

Source code in src/scale_forecasting/temporal_reconciliation.py
def aggregate_temporal_series(
    y: pd.Series | np.ndarray | Sequence[float] | pd.DataFrame,
    k: int,
    *,
    date_col: str = "ds",
    target_col: str = "y",
    ts_id_col: str = "ts_id",
) -> np.ndarray | pd.DataFrame:
    """Aggregate a 1-D series (``np.ndarray``) or panel ``DataFrame`` into blocks of ``k``."""
    factor = int(k)
    if factor < 1:
        raise ConfigError(f"Temporal aggregation factor k must be >= 1; got {k}")
    if not isinstance(y, pd.DataFrame):
        return aggregate_temporal_history(y, factor, target_col=target_col)

    df = y.copy()
    if df.empty:
        return df
    dcol = (
        date_col
        if date_col in df.columns
        else ("forecast_date" if "forecast_date" in df.columns else None)
    )
    vcol = target_col if target_col in df.columns else ("yhat" if "yhat" in df.columns else None)
    if vcol is None:
        raise DataError(f"DataFrame must contain '{target_col}' or 'yhat' for temporal aggregation")

    has_id = ts_id_col in df.columns
    groups = df.groupby(ts_id_col, sort=False) if has_id else [(None, df)]
    out_frames: list[pd.DataFrame] = []
    for tid, grp in groups:
        sorted_grp = grp.sort_values(dcol, kind="mergesort") if dcol is not None else grp
        vals = sorted_grp[vcol].to_numpy(dtype=float)
        n = len(vals)
        if n < factor:
            continue
        rem = n % factor
        trimmed_grp = sorted_grp.iloc[rem:] if rem > 0 else sorted_grp
        agg_vals = trimmed_grp[vcol].to_numpy(dtype=float).reshape(-1, factor).sum(axis=1)
        end_rows = trimmed_grp.iloc[factor - 1 :: factor].copy().reset_index(drop=True)
        end_rows[vcol] = agg_vals
        end_rows["temporal_factor"] = factor
        if tid is not None:
            end_rows[ts_id_col] = tid
        out_frames.append(end_rows)

    return pd.concat(out_frames, ignore_index=True) if out_frames else df.iloc[0:0].copy()

build_temporal_hierarchy

build_temporal_hierarchy(
    freq="D",
    horizon=None,
    temporal_aggregations=None,
    *,
    m=None,
    aggregations=None,
    history_len=None,
)

Compile a TemporalHierarchySpec and summing matrices for freq and horizon.

Source code in src/scale_forecasting/temporal_reconciliation.py
def build_temporal_hierarchy(
    freq: str | int = "D",
    horizon: int | None = None,
    temporal_aggregations: Sequence[int] | None = None,
    *,
    m: int | None = None,
    aggregations: Sequence[int] | None = None,
    history_len: int | None = None,
) -> TemporalHierarchySpec:
    """Compile a `TemporalHierarchySpec` and summing matrices for ``freq`` and ``horizon``."""
    if m is not None:
        if int(m) < 1:
            raise ConfigError(f"Temporal period m must be >= 1; got {m}")
        eff_m = int(m)
        h = int(horizon) if horizon is not None else eff_m
        freq_key: str | int = eff_m if freq == "D" else freq
    elif isinstance(freq, (int, np.integer)) and not isinstance(freq, bool):
        if int(freq) < 1:
            raise ConfigError(f"Temporal period m must be >= 1; got {freq}")
        eff_m = int(freq)
        h = int(horizon) if horizon is not None else eff_m
        freq_key = eff_m
    else:
        h = int(horizon) if horizon is not None else 14
        freq_key = str(freq)

    if h < 1:
        raise ConfigError(f"horizon must be >= 1 for temporal reconciliation; got {h}")

    explicit = temporal_aggregations if temporal_aggregations is not None else aggregations
    if explicit is not None:
        raw_list = [int(k) for k in explicit]
        if not raw_list:
            raise ConfigError("temporal_aggregations must not be empty when explicitly provided")
        if any(k < 1 for k in raw_list):
            raise ConfigError(
                f"temporal_aggregations factors must be positive integers >= 1; got {raw_list}"
            )
        if len(set(raw_list)) != len(raw_list):
            raise ConfigError(f"temporal_aggregations contains duplicate factors: {raw_list}")
        coarse = [k for k in raw_list if k > 1]
        if history_len is not None:
            max_k = max(1, int(history_len) // 2)
            coarse = [k for k in coarse if k <= max_k]
    else:
        coarse = default_temporal_aggregations(freq_key, horizon=h, history_len=history_len)

    agg_tuple = tuple(sorted({1, *coarse}))
    macro_period = math.lcm(*agg_tuple) if len(agg_tuple) > 1 else 1
    n_macro = max(1, math.ceil(h / macro_period))
    padded_h = n_macro * macro_period

    blocks: list[np.ndarray] = []
    level_names: list[str] = []
    level_slices: dict[int, slice] = {}
    row_cursor = 0

    # Order from coarsest factor k_max down to base k=1 so the bottom macro_period rows are I_m.
    for k in reversed(agg_tuple):
        n_buckets = macro_period // k
        block = np.kron(np.eye(n_buckets, dtype=float), np.ones((1, k), dtype=float))
        blocks.append(block)
        level_names.append(f"k{k}")
        level_slices[int(k)] = slice(row_cursor, row_cursor + n_buckets)
        row_cursor += n_buckets

    s_temp = np.vstack(blocks) if blocks else np.eye(macro_period, dtype=float)
    full_s_temp = np.kron(np.eye(n_macro, dtype=float), s_temp) if n_macro > 1 else s_temp.copy()

    return TemporalHierarchySpec(
        base_freq=str(freq) if not isinstance(freq, (int, np.integer)) else "D",
        horizon=h,
        padded_horizon=padded_h,
        macro_period=macro_period,
        n_macro_periods=n_macro,
        aggregations=agg_tuple,
        level_names=tuple(level_names),
        level_slices=level_slices,
        summing_matrix=s_temp,
        full_summing_matrix=full_s_temp,
    )

build_temporal_summing_matrix

build_temporal_summing_matrix(m, aggregations)

Construct the single-cycle binary temporal summing matrix S_temp of shape (n_nodes, m).

Source code in src/scale_forecasting/temporal_reconciliation.py
def build_temporal_summing_matrix(m: int, aggregations: Sequence[int]) -> np.ndarray:
    """Construct the single-cycle binary temporal summing matrix ``S_temp`` of shape
    ``(n_nodes, m)``.
    """
    macro_period = int(m)
    if macro_period < 1:
        raise ConfigError(f"Temporal period m must be >= 1; got {m}")
    agg_tuple = tuple(sorted({1, *(int(k) for k in aggregations if int(k) >= 1)}))
    blocks: list[np.ndarray] = []
    for k in reversed(agg_tuple):
        if macro_period % k != 0:
            raise ConfigError(f"Aggregation factor k={k} must divide macro_period m={macro_period}")
        n_buckets = macro_period // k
        blocks.append(np.kron(np.eye(n_buckets, dtype=float), np.ones((1, k), dtype=float)))
    return np.vstack(blocks) if blocks else np.eye(macro_period, dtype=float)

default_temporal_aggregations

default_temporal_aggregations(
    freq="D",
    horizon=None,
    history_len=None,
    *,
    include_base=False,
)

Return canonical coarse temporal aggregation factors for freq (pure).

Standard coarse aggregation ladders: * Daily ("D" / "B"): [7, 14, 28] * Weekly ("W"): [4, 13, 52] * Monthly ("M" / "MS" / "ME"): [3, 6, 12] * Quarterly ("Q" / "QS" / "QE"): [2, 4] * Hourly ("H" / "h"): [6, 12, 24]

When an integer m is passed as freq, returns all integer divisors > 1 of m in ascending order (or including 1 when include_base=True). When history_len is provided, filters out any factor k where history_len < 2 * k so every retained coarse level has at least two complete historical blocks.

Source code in src/scale_forecasting/temporal_reconciliation.py
def default_temporal_aggregations(
    freq: str | int = "D",
    horizon: int | None = None,
    history_len: int | None = None,
    *,
    include_base: bool = False,
) -> list[int]:
    """Return canonical coarse temporal aggregation factors for ``freq`` (pure).

    Standard coarse aggregation ladders:
    * Daily (``"D"`` / ``"B"``): ``[7, 14, 28]``
    * Weekly (``"W"``): ``[4, 13, 52]``
    * Monthly (``"M"`` / ``"MS"`` / ``"ME"``): ``[3, 6, 12]``
    * Quarterly (``"Q"`` / ``"QS"`` / ``"QE"``): ``[2, 4]``
    * Hourly (``"H"`` / ``"h"``): ``[6, 12, 24]``

    When an integer ``m`` is passed as ``freq``, returns all integer divisors ``> 1`` of ``m``
    in ascending order (or including ``1`` when ``include_base=True``).
    When ``history_len`` is provided, filters out any factor ``k`` where ``history_len < 2 * k``
    so every retained coarse level has at least two complete historical blocks.
    """
    if isinstance(freq, (int, np.integer)) and not isinstance(freq, bool):
        m_val = int(freq)
        if m_val < 1:
            raise ConfigError(f"Temporal period m must be >= 1; got {freq}")
        candidates = [k for k in range(2, m_val + 1) if m_val % k == 0]
    else:
        fam = _normalize_freq_key(str(freq))
        if fam == "D":
            candidates = [7, 14, 28]
        elif fam == "W":
            candidates = [4, 13, 52]
        elif fam == "M":
            candidates = [3, 6, 12]
        elif fam == "Q":
            candidates = [2, 4]
        elif fam == "H":
            candidates = [6, 12, 24]
        else:
            target_h = max(2, int(horizon) if horizon is not None and horizon >= 2 else 12)
            candidates = [k for k in range(2, target_h + 1) if target_h % k == 0]
            if not candidates:
                candidates = [2, 4]

    if history_len is not None:
        max_k = max(1, int(history_len) // 2)
        candidates = [k for k in candidates if k <= max_k]

    if include_base:
        return sorted({1, *candidates})
    return sorted(set(candidates))

reconcile_cross_temporal

reconcile_cross_temporal(
    predictions_df,
    hier_spec,
    temp_spec=None,
    *,
    history_df=None,
    oof_df=None,
    cfg=None,
    freq=None,
    temporal_aggregations=None,
    method="mint_shrink",
    middle_level=None,
    spatial_temporal_weight=None,
    nonneg=False,
    stamp_method=False,
    return_aggregations=False,
    date_col="ds",
    ts_id_col="ts_id",
    target_col="y",
)

Simultaneous Cross-Sectional and Temporal (cross_temporal_mint) Reconciliation (pure).

Projects base forecasts across both the cross-sectional hierarchy hier_spec (S_cs) and the temporal hierarchy temp_spec (S_temp) via the Di Fonzo & Girolimetto (2023) cross-temporal Minimum-Trace projection:

  1. Temporally reconciles every cross-sectional node i in hier_spec.node_ids across temp_spec.aggregations to obtain coherent temporal trajectories Y_hat_temp.
  2. Cross-sectionally reconciles the base panel across hier_spec via G_cs and temporally reconciles the resulting cross-sectional trajectories.
  3. Combines the two orthogonal projections in bottom-leaf base-frequency space B_tilde using spatial_temporal_weight (explicit float in [0, 1] or "auto" inverse residual variance learning) with alternating projection refinement, and maps through Y_tilde = S_cs @ B_tilde and Y_tilde^(k) = Y_tilde @ S_temp^(k).T, guaranteeing < 1e-6 discrepancy across every cross-sectional level and every temporal aggregation factor k simultaneously.
Source code in src/scale_forecasting/temporal_reconciliation.py
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
def reconcile_cross_temporal(
    predictions_df: pd.DataFrame,
    hier_spec: HierarchySpec,
    temp_spec: TemporalHierarchySpec | None = None,
    *,
    history_df: pd.DataFrame | None = None,
    oof_df: pd.DataFrame | None = None,
    cfg: RunConfig | None = None,
    freq: str | None = None,
    temporal_aggregations: Sequence[int] | None = None,
    method: str = "mint_shrink",
    middle_level: Sequence[str] | None = None,
    spatial_temporal_weight: float | Literal["auto"] | None = None,
    nonneg: bool = False,
    stamp_method: bool = False,
    return_aggregations: bool = False,
    date_col: str = "ds",
    ts_id_col: str = "ts_id",
    target_col: str = "y",
) -> pd.DataFrame | tuple[pd.DataFrame, dict[tuple[str, int], np.ndarray]]:
    """Simultaneous Cross-Sectional and Temporal (`cross_temporal_mint`) Reconciliation (pure).

    Projects base forecasts across both the cross-sectional hierarchy ``hier_spec`` (``S_cs``) and
    the temporal hierarchy ``temp_spec`` (``S_temp``) via the Di Fonzo & Girolimetto (2023)
    cross-temporal Minimum-Trace projection:

    1. Temporally reconciles every cross-sectional node ``i in hier_spec.node_ids`` across
       ``temp_spec.aggregations`` to obtain coherent temporal trajectories ``Y_hat_temp``.
    2. Cross-sectionally reconciles the base panel across ``hier_spec`` via ``G_cs`` and
       temporally reconciles the resulting cross-sectional trajectories.
    3. Combines the two orthogonal projections in bottom-leaf base-frequency space ``B_tilde``
       using ``spatial_temporal_weight`` (explicit float in ``[0, 1]`` or ``"auto"`` inverse
       residual variance learning) with alternating projection refinement, and maps through
       ``Y_tilde = S_cs @ B_tilde`` and ``Y_tilde^(k) = Y_tilde @ S_temp^(k).T``, guaranteeing
       ``< 1e-6`` discrepancy across every cross-sectional level and every temporal aggregation
       factor ``k`` simultaneously.
    """
    if predictions_df.empty:
        empty_map: dict[tuple[str, int], np.ndarray] = {}
        return (predictions_df.copy(), empty_map) if return_aggregations else predictions_df.copy()

    eff_weight: float | Literal["auto"] = (
        spatial_temporal_weight
        if spatial_temporal_weight is not None
        else (cfg.hierarchy.spatial_temporal_weight if cfg is not None else "auto")
    )
    if eff_weight != "auto":
        if isinstance(eff_weight, bool) or not isinstance(eff_weight, (int, float)):
            raise ConfigError("spatial_temporal_weight must be 'auto' or a float in [0.0, 1.0].")
        w_val = float(eff_weight)
        if not math.isfinite(w_val) or w_val < 0.0 or w_val > 1.0:
            raise ConfigError(f"spatial_temporal_weight must be in [0.0, 1.0], got {eff_weight!r}.")
        eff_weight = w_val

    dcol = (
        date_col
        if date_col in predictions_df.columns
        else ("forecast_date" if "forecast_date" in predictions_df.columns else "ds")
    )
    predictions_df = ensure_hierarchy_nodes_present(
        predictions_df,
        hier_spec,
        ts_id_col=ts_id_col,
        date_col=dcol,
        target_col="yhat",
    )
    if oof_df is not None and not oof_df.empty:
        oof_dcol = "forecast_date" if "forecast_date" in oof_df.columns else "ds"
        oof_df = ensure_hierarchy_nodes_present(
            oof_df,
            hier_spec,
            ts_id_col=ts_id_col,
            date_col=oof_dcol,
            target_col="yhat",
        )
    if history_df is not None and not history_df.empty:
        hist_dcol = "ds" if "ds" in history_df.columns else dcol
        history_df = ensure_hierarchy_nodes_present(
            history_df,
            hier_spec,
            ts_id_col=ts_id_col,
            date_col=hist_dcol,
            target_col=target_col,
        )
    eff_freq = freq or (cfg.data.freq if cfg is not None else "D")
    eff_aggs = temporal_aggregations
    if eff_aggs is None and cfg is not None:
        eff_aggs = cfg.hierarchy.temporal_aggregations

    cs_method_map = {
        "cross_temporal_mint": "mint_shrink",
        "struc": "wls_struct",
    }
    norm_method = _normalize_temporal_method(method)
    cs_method: Any = cs_method_map.get(norm_method, norm_method)
    is_nonneg = bool(
        nonneg
        or norm_method == "mint_nonneg"
        or (cfg is not None and cfg.output.floor is not None and cfg.output.floor >= 0.0)
    )

    has_model = "model_type" in predictions_df.columns
    models = predictions_df["model_type"].drop_duplicates().tolist() if has_model else [None]

    all_reconciled_parts: list[pd.DataFrame] = []
    agg_by_node_k: dict[tuple[str, int], np.ndarray] = _SafeArrayDict()
    resolved_temp_spec = temp_spec
    last_alpha = 0.5 if eff_weight == "auto" else float(eff_weight)
    last_cs_first_disc = 0.0
    last_temp_first_disc = 0.0

    for model_name in models:
        m_preds = (
            predictions_df[predictions_df["model_type"] == model_name].copy()
            if model_name is not None
            else predictions_df.copy()
        )
        m_oof = (
            oof_df[oof_df["model_type"] == model_name].copy()
            if (
                oof_df is not None
                and not oof_df.empty
                and "model_type" in oof_df.columns
                and model_name is not None
            )
            else oof_df
        )

        dates = sorted(m_preds[dcol].unique())
        h = len(dates)
        if resolved_temp_spec is None or resolved_temp_spec.horizon != h:
            resolved_temp_spec = build_temporal_hierarchy(eff_freq, max(1, h), eff_aggs)
        tspec = resolved_temp_spec

        # Step 1: Temporal reconciliation of each cross-sectional node i in hier_spec.node_ids.
        temp_rec_res = reconcile_temporal_forecasts(
            m_preds,
            history_df=history_df,
            oof_df=m_oof,
            cfg=cfg,
            freq=eff_freq,
            horizon=h,
            temporal_aggregations=tspec.aggregations,
            method=norm_method,
            nonneg=is_nonneg,
            stamp_method=False,
            date_col=dcol,
            ts_id_col=ts_id_col,
            target_col=target_col,
        )
        assert isinstance(temp_rec_res, pd.DataFrame)
        temp_rec_df = temp_rec_res
        temp_first_agg_map = temp_rec_df.attrs.get("aggregated_by_node_k")

        # Step 2: Cross-sectional reconciliation first, then temporal reconciliation.
        cs_first_df = reconcile_forecasts(
            m_preds,
            hier_spec,
            cs_method,
            history_df=history_df,
            oof_df=m_oof,
            middle_level=middle_level,
            date_col=dcol,
            ts_id_col=ts_id_col,
            target_col=target_col,
            stamp_method=False,
        )
        cs_then_temp_res = reconcile_temporal_forecasts(
            cs_first_df,
            history_df=history_df,
            oof_df=m_oof,
            cfg=cfg,
            freq=eff_freq,
            horizon=h,
            temporal_aggregations=tspec.aggregations,
            method=norm_method,
            nonneg=is_nonneg,
            stamp_method=False,
            date_col=dcol,
            ts_id_col=ts_id_col,
            target_col=target_col,
        )
        assert isinstance(cs_then_temp_res, pd.DataFrame)
        cs_then_temp_df = cs_then_temp_res

        # Step 3: Cross-sectional reconciliation of the temporally reconciled node forecasts.
        temp_then_cs_df = reconcile_forecasts(
            temp_rec_df,
            hier_spec,
            cs_method,
            history_df=history_df,
            oof_df=m_oof,
            middle_level=middle_level,
            date_col=dcol,
            ts_id_col=ts_id_col,
            target_col=target_col,
            stamp_method=False,
        )

        # Record single-order discrepancies prior to joint blending & refinement.
        last_cs_first_disc = float(
            verify_coherence(
                cs_then_temp_df,
                hier_spec,
                date_col=dcol,
                ts_id_col=ts_id_col,
                atol=float("inf"),
            )
        )
        last_temp_first_disc = (
            float(
                verify_temporal_coherence(
                    temp_then_cs_df,
                    temp_first_agg_map,
                    spec=tspec,
                    raise_on_error=False,
                )
            )
            if temp_first_agg_map
            else 0.0
        )

        # Step 4: Blend the two cross-temporal projections on bottom leaves B_tilde(alpha).
        piv_a = (
            temp_then_cs_df.pivot_table(
                index=ts_id_col, columns=dcol, values="yhat", aggfunc="first"
            )
            .reindex(index=list(hier_spec.bottom_ids), columns=dates)
            .to_numpy(dtype=float)
        )
        piv_b = (
            cs_then_temp_df.pivot_table(
                index=ts_id_col, columns=dcol, values="yhat", aggfunc="first"
            )
            .reindex(index=list(hier_spec.bottom_ids), columns=dates)
            .to_numpy(dtype=float)
        )
        if eff_weight == "auto":
            alpha = _learn_spatial_temporal_alpha(
                hier_spec,
                tspec,
                oof_df=m_oof,
                history_df=history_df,
                piv_a=piv_a,
                piv_b=piv_b,
                ts_id_col=ts_id_col,
                date_col=dcol,
                target_col=target_col,
            )
        else:
            alpha = float(eff_weight)
        last_alpha = alpha

        b_tilde_h = alpha * piv_a + (1.0 - alpha) * piv_b
        if is_nonneg:
            b_tilde_h = np.maximum(0.0, b_tilde_h)

        # Compute padded bottom matrix and run alternating projection refinement across S_temp/S_cs.
        base_season = seasonal_period(eff_freq)
        b_tilde_padded = np.vstack(
            [
                _pad_base_forecast(b_tilde_h[b_idx], tspec.padded_horizon, base_season)
                for b_idx in range(hier_spec.n_bottom)
            ]
        )
        g_temp_struct = reconcile_temporal_matrix(tspec, "wls_struct", use_full_horizon=True)
        bot_row_indices = np.concatenate(
            [
                np.arange(
                    cyc * tspec.n_total_per_cycle + tspec.level_slices[1].start,
                    cyc * tspec.n_total_per_cycle + tspec.level_slices[1].stop,
                )
                for cyc in range(tspec.n_macro_periods)
            ]
        )
        p_temp_bottom = tspec.full_summing_matrix[bot_row_indices, :] @ g_temp_struct
        s_temp_bot_inv = tspec.full_summing_matrix
        for _iter in range(5):
            # Project each bottom series onto S_temp and enforce non-negativity when active.
            y_all_cycles = b_tilde_padded @ s_temp_bot_inv.T
            b_proj = y_all_cycles @ p_temp_bottom.T
            if is_nonneg:
                b_proj = np.maximum(0.0, b_proj)
            if float(np.max(np.abs(b_proj - b_tilde_padded))) < 1e-9:
                b_tilde_padded = b_proj
                break
            b_tilde_padded = b_proj

        b_tilde_h = b_tilde_padded[:, :h].copy()
        if is_nonneg:
            b_tilde_h = np.maximum(0.0, b_tilde_h)
            b_tilde_padded[:, :h] = b_tilde_h

        # Full cross-sectional hierarchy matrix Y_tilde of shape (n_nodes, H) = S_cs @ B_tilde_H.
        y_tilde_h = hier_spec.summing_matrix @ b_tilde_h
        y_tilde_padded = hier_spec.summing_matrix @ b_tilde_padded

        for node_idx, nid in enumerate(hier_spec.node_ids):
            node_series_padded = y_tilde_padded[node_idx]
            node_series_h = y_tilde_h[node_idx]
            agg_by_node_k[(nid, 1)] = node_series_h.copy()
            for k in tspec.aggregations:
                if k == 1:
                    continue
                # Aggregate over the padded horizon so every bucket b has exact sum of k base steps.
                agg_by_node_k[(nid, int(k))] = aggregate_temporal_history(
                    node_series_padded, int(k)
                )

        # Rebuild coherent DataFrame aligned with temp_then_cs_df schema and interval widths.
        node_to_idx = {nid: i for i, nid in enumerate(hier_spec.node_ids)}
        date_to_idx = {dt: j for j, dt in enumerate(dates)}

        rec_model_df = temp_then_cs_df.copy()
        new_yhat = np.array(
            [
                y_tilde_h[node_to_idx[str(row_id)], date_to_idx[row_dt]]
                for row_id, row_dt in zip(rec_model_df[ts_id_col], rec_model_df[dcol], strict=True)
            ],
            dtype=float,
        )
        old_yhat = rec_model_df["yhat"].to_numpy(dtype=float)
        delta_yhat = new_yhat - old_yhat
        rec_model_df["yhat"] = new_yhat
        if "yhat_raw" in rec_model_df.columns and rec_model_df["yhat_raw"].notna().all():
            rec_model_df["yhat_raw"] = rec_model_df["yhat_raw"].to_numpy(dtype=float) + delta_yhat
        if "yhat_adjusted" in rec_model_df.columns and rec_model_df["yhat_adjusted"].notna().all():
            rec_model_df["yhat_adjusted"] = new_yhat

        if (
            "yhat_lower" in rec_model_df.columns
            and "yhat_upper" in rec_model_df.columns
            and rec_model_df["yhat_lower"].notna().all()
            and rec_model_df["yhat_upper"].notna().all()
        ):
            hw = np.maximum(
                0.0,
                0.5
                * (
                    rec_model_df["yhat_upper"].to_numpy(dtype=float)
                    - rec_model_df["yhat_lower"].to_numpy(dtype=float)
                ),
            )
            lo = new_yhat - hw
            hi = new_yhat + hw
            if is_nonneg:
                lo = np.maximum(0.0, lo)
                hi = np.maximum(new_yhat, hi)
            rec_model_df["yhat_lower"] = np.minimum(lo, new_yhat)
            rec_model_df["yhat_upper"] = np.maximum(hi, new_yhat)

        if stamp_method and model_name is not None:
            rec_model_df["model_type"] = f"{model_name}_{method}"

        rec_model_df.attrs.clear()
        all_reconciled_parts.append(rec_model_df)

    out_df = (
        pd.concat(all_reconciled_parts, ignore_index=True)
        if all_reconciled_parts
        else predictions_df.copy()
    )
    out_df.attrs["hierarchy_spec"] = hier_spec
    if resolved_temp_spec is not None:
        out_df.attrs["temporal_spec"] = resolved_temp_spec
    out_df.attrs["aggregated_by_node_k"] = agg_by_node_k

    final_cs_disc = (
        float(
            verify_coherence(
                out_df,
                hier_spec,
                date_col=dcol,
                ts_id_col=ts_id_col,
                atol=float("inf"),
            )
        )
        if not out_df.empty
        else 0.0
    )
    final_temp_disc = (
        float(
            verify_temporal_coherence(
                out_df,
                agg_by_node_k,
                spec=resolved_temp_spec,
                raise_on_error=False,
            )
        )
        if not out_df.empty
        else 0.0
    )
    out_df.attrs["cross_temporal_diagnostics"] = {
        "mode": "cross_temporal",
        "spatial_weight_alpha": float(last_alpha),
        "cs_first_discrepancy": float(last_cs_first_disc),
        "temp_first_discrepancy": float(last_temp_first_disc),
        "final_cs_discrepancy": final_cs_disc,
        "final_temp_discrepancy": final_temp_disc,
    }

    if return_aggregations:
        return out_df, agg_by_node_k
    return out_df

reconcile_series_temporal

reconcile_series_temporal(
    series_df,
    base_predictions_df,
    cfg=None,
    *,
    freq=None,
    horizon=None,
    temporal_aggregations=None,
    model_name=None,
    method="mint_shrink",
    oof_df=None,
    coarse_forecasts=None,
    nonneg=False,
    date_col="ds",
    target_col="y",
)

Perform THieF temporal reconciliation for a single series (pure).

Returns (reconciled_predictions_df, reconciled_by_k) where reconciled_predictions_df has length horizon and reconciled_by_k[k] holds the coherent coarse trajectory at every aggregation factor k in spec.aggregations.

Source code in src/scale_forecasting/temporal_reconciliation.py
def reconcile_series_temporal(
    series_df: pd.DataFrame | None,
    base_predictions_df: pd.DataFrame,
    cfg: RunConfig | None = None,
    *,
    freq: str | None = None,
    horizon: int | None = None,
    temporal_aggregations: Sequence[int] | None = None,
    model_name: str | None = None,
    method: str = "mint_shrink",
    oof_df: pd.DataFrame | None = None,
    coarse_forecasts: Mapping[int, np.ndarray] | None = None,
    nonneg: bool = False,
    date_col: str = "ds",
    target_col: str = "y",
) -> tuple[pd.DataFrame, dict[int, np.ndarray]]:
    """Perform `THieF` temporal reconciliation for a single series (pure).

    Returns ``(reconciled_predictions_df, reconciled_by_k)`` where ``reconciled_predictions_df``
    has length ``horizon`` and ``reconciled_by_k[k]`` holds the coherent coarse trajectory at
    every aggregation factor ``k`` in ``spec.aggregations``.
    """
    if base_predictions_df.empty:
        return base_predictions_df.copy(), {1: np.empty(0, dtype=float)}

    eff_freq = freq or (cfg.data.freq if cfg is not None else "D")
    pred_df = base_predictions_df.copy()
    dcol = (
        date_col
        if date_col in pred_df.columns
        else ("forecast_date" if "forecast_date" in pred_df.columns else None)
    )
    if dcol is not None:
        pred_df = pred_df.sort_values(dcol, kind="mergesort").reset_index(drop=True)

    h = int(horizon) if horizon is not None else len(pred_df)
    eff_aggs = temporal_aggregations
    if eff_aggs is None and cfg is not None and cfg.hierarchy.temporal_aggregations is not None:
        eff_aggs = cfg.hierarchy.temporal_aggregations

    y_hist = np.empty(0, dtype=float)
    if series_df is not None and not series_df.empty:
        hist_dcol = (
            date_col
            if date_col in series_df.columns
            else ("ds" if "ds" in series_df.columns else None)
        )
        s_df = (
            series_df.sort_values(hist_dcol, kind="mergesort")
            if hist_dcol is not None
            else series_df
        )
        tcol = target_col if target_col in s_df.columns else ("y" if "y" in s_df.columns else None)
        if tcol is not None:
            raw_y = s_df[tcol].to_numpy(dtype=float)
            y_hist = raw_y[np.isfinite(raw_y)]

    hist_len = len(y_hist) if len(y_hist) > 0 else None
    spec = build_temporal_hierarchy(
        eff_freq,
        h,
        eff_aggs,
        history_len=hist_len if coarse_forecasts is None else None,
    )

    base_yhat = pred_df["yhat"].to_numpy(dtype=float)
    base_season = seasonal_period(eff_freq)
    padded_base = _pad_base_forecast(base_yhat, spec.padded_horizon, base_season)

    forecasts_by_k: dict[int, np.ndarray] = {1: padded_base}
    residuals_by_k: dict[int, np.ndarray] = {}
    history_by_k: dict[int, np.ndarray] = {}

    if len(y_hist) > 0:
        history_by_k[1] = y_hist

    # Extract k=1 residuals from oof_df if provided, else 1-step in-sample residuals from history.
    if oof_df is not None and not oof_df.empty and {"y_true", "yhat"}.issubset(oof_df.columns):
        residuals_by_k[1] = oof_df["y_true"].to_numpy(dtype=float) - oof_df["yhat"].to_numpy(
            dtype=float
        )
    elif len(y_hist) >= 2:
        _, res_1 = _fast_coarse_forecast_and_residuals(y_hist, spec.padded_horizon, base_season)
        residuals_by_k[1] = res_1

    for k in spec.aggregations:
        if k == 1:
            continue
        buckets_needed = spec.n_macro_periods * (spec.macro_period // int(k))
        if coarse_forecasts is not None and int(k) in coarse_forecasts:
            forecasts_by_k[int(k)] = _pad_base_forecast(
                np.asarray(coarse_forecasts[int(k)], dtype=float),
                buckets_needed,
                max(1, base_season // int(k)),
            )
        elif len(y_hist) >= 2 * int(k):
            y_k = aggregate_temporal_history(y_hist, int(k))
            history_by_k[int(k)] = y_k
            f_k, r_k = _fast_coarse_forecast_and_residuals(
                y_k,
                buckets_needed,
                max(1, base_season // int(k)),
            )
            forecasts_by_k[int(k)] = f_k
            residuals_by_k[int(k)] = r_k
        else:
            # Aggregate padded base forecast when history is absent or shorter than 2*k.
            forecasts_by_k[int(k)] = aggregate_temporal_history(padded_base, int(k))

    norm_method = _normalize_temporal_method(method)
    is_nonneg = bool(
        nonneg
        or norm_method == "mint_nonneg"
        or (cfg is not None and cfg.output.floor is not None and cfg.output.floor >= 0.0)
        or (len(y_hist) > 0 and np.all(y_hist >= 0.0) and np.all(base_yhat >= 0.0))
    )
    g_single = reconcile_temporal_matrix(
        spec,
        norm_method,
        residuals_by_k=residuals_by_k if residuals_by_k else None,
        history_by_k=history_by_k if history_by_k else None,
    )
    w_mat = _temporal_covariance(
        spec,
        norm_method,
        residuals_by_k=residuals_by_k if residuals_by_k else None,
    )
    reconciled_padded, reconciled_by_k = _project_temporal_cycles(
        spec,
        forecasts_by_k,
        g_single,
        nonneg=is_nonneg,
        w_mat=w_mat,
    )

    reconciled_yhat = reconciled_padded[: len(pred_df)]
    delta = reconciled_yhat - base_yhat
    pred_df["yhat"] = reconciled_yhat
    if "yhat_raw" in pred_df.columns and pred_df["yhat_raw"].notna().all():
        pred_df["yhat_raw"] = pred_df["yhat_raw"].to_numpy(dtype=float) + delta
    if "yhat_adjusted" in pred_df.columns and pred_df["yhat_adjusted"].notna().all():
        pred_df["yhat_adjusted"] = reconciled_yhat

    # Shift prediction intervals coherently around reconciled_yhat while preserving interval width.
    if (
        "yhat_lower" in pred_df.columns
        and "yhat_upper" in pred_df.columns
        and pred_df["yhat_lower"].notna().all()
        and pred_df["yhat_upper"].notna().all()
    ):
        half_w = np.maximum(
            0.0,
            0.5
            * (
                pred_df["yhat_upper"].to_numpy(dtype=float)
                - pred_df["yhat_lower"].to_numpy(dtype=float)
            ),
        )
        lo = reconciled_yhat - half_w
        hi = reconciled_yhat + half_w
        if is_nonneg:
            lo = np.maximum(0.0, lo)
            hi = np.maximum(reconciled_yhat, hi)
        pred_df["yhat_lower"] = np.minimum(lo, reconciled_yhat)
        pred_df["yhat_upper"] = np.maximum(hi, reconciled_yhat)

    for col in pred_df.columns:
        if col.startswith("yhat_lower_"):
            q_suffix = col[len("yhat_lower_") :]
            u_col = f"yhat_upper_{q_suffix}"
            if u_col in pred_df.columns:
                hw = np.maximum(
                    0.0,
                    0.5
                    * (pred_df[u_col].to_numpy(dtype=float) - pred_df[col].to_numpy(dtype=float)),
                )
                lo = reconciled_yhat - hw
                hi = reconciled_yhat + hw
                if is_nonneg:
                    lo = np.maximum(0.0, lo)
                    hi = np.maximum(reconciled_yhat, hi)
                pred_df[col] = np.minimum(lo, reconciled_yhat)
                pred_df[u_col] = np.maximum(hi, reconciled_yhat)

    if "quantiles" in pred_df.columns:
        new_q_col: list[Any] = []
        for idx, qdict in enumerate(pred_df["quantiles"]):
            if isinstance(qdict, dict) and qdict:
                shifted = {}
                for qk, qv in qdict.items():
                    val = float(qv) + float(delta[idx])
                    if is_nonneg:
                        val = max(0.0, val)
                    shifted[qk] = val
                new_q_col.append(shifted)
            else:
                new_q_col.append(qdict)
        pred_df["quantiles"] = new_q_col

    if model_name is not None and "model_type" in pred_df.columns:
        pred_df["model_type"] = model_name

    reconciled_by_k[1] = reconciled_yhat.copy()
    safe_by_k: dict[int, np.ndarray] = _SafeArrayDict(reconciled_by_k)
    pred_df.attrs["temporal_spec"] = spec
    pred_df.attrs["aggregated_by_k"] = safe_by_k
    return pred_df, safe_by_k

reconcile_temporal_forecasts

reconcile_temporal_forecasts(
    forecasts_df,
    *,
    history_df=None,
    oof_df=None,
    cfg=None,
    freq=None,
    horizon=None,
    temporal_aggregations=None,
    aggregations=None,
    method="mint_shrink",
    model_type=None,
    coarse_forecasts=None,
    nonneg=False,
    stamp_method=False,
    return_aggregations=False,
    date_col="ds",
    ts_id_col="ts_id",
    target_col="y",
)

Reconcile a single-series or multi-series forecast DataFrame across temporal aggregations.

Attaches df.attrs["temporal_spec"], df.attrs["aggregated_by_k"] (for single series), and df.attrs["aggregated_by_node_k"] (mapping (ts_id, k) -> np.ndarray) so downstream callers and verify_temporal_coherence can inspect exact bucket coherence at every factor.

Source code in src/scale_forecasting/temporal_reconciliation.py
def reconcile_temporal_forecasts(
    forecasts_df: pd.DataFrame,
    *,
    history_df: pd.DataFrame | None = None,
    oof_df: pd.DataFrame | None = None,
    cfg: RunConfig | None = None,
    freq: str | None = None,
    horizon: int | None = None,
    temporal_aggregations: Sequence[int] | None = None,
    aggregations: Sequence[int] | None = None,
    method: str = "mint_shrink",
    model_type: str | None = None,
    coarse_forecasts: Mapping[int, np.ndarray] | None = None,
    nonneg: bool = False,
    stamp_method: bool = False,
    return_aggregations: bool = False,
    date_col: str = "ds",
    ts_id_col: str = "ts_id",
    target_col: str = "y",
) -> pd.DataFrame | tuple[pd.DataFrame, dict[Any, np.ndarray]]:
    """Reconcile a single-series or multi-series forecast DataFrame across temporal aggregations.

    Attaches ``df.attrs["temporal_spec"]``, ``df.attrs["aggregated_by_k"]`` (for single series),
    and ``df.attrs["aggregated_by_node_k"]`` (mapping ``(ts_id, k) -> np.ndarray``) so downstream
    callers and `verify_temporal_coherence` can inspect exact bucket coherence at every factor.
    """
    if forecasts_df.empty:
        empty_agg: dict[Any, np.ndarray] = _SafeArrayDict()
        return (forecasts_df.copy(), empty_agg) if return_aggregations else forecasts_df.copy()

    df = forecasts_df.copy()
    if model_type is not None and "model_type" in df.columns:
        df = df[df["model_type"] == model_type].copy()
        if df.empty:
            raise DataError(f"No forecast rows found for model_type='{model_type}'")

    eff_aggs = temporal_aggregations if temporal_aggregations is not None else aggregations
    if eff_aggs is None and cfg is not None:
        eff_aggs = cfg.hierarchy.temporal_aggregations
    eff_freq = freq or (cfg.data.freq if cfg is not None else "D")

    has_ts_id = ts_id_col in df.columns
    has_model = "model_type" in df.columns
    group_cols = ([ts_id_col] if has_ts_id else []) + (["model_type"] if has_model else [])

    parts: list[pd.DataFrame] = []
    agg_by_node_k: dict[tuple[str, int], np.ndarray] = _SafeArrayDict()
    last_agg_by_k: dict[int, np.ndarray] = _SafeArrayDict()
    last_spec: TemporalHierarchySpec | None = None

    groups = df.groupby(group_cols, sort=False) if group_cols else [(None, df)]
    for key, grp in groups:
        if isinstance(key, tuple):
            tid = str(key[0]) if has_ts_id else "__series__"
            mname = str(key[1]) if has_model and len(key) > 1 else None
        elif key is not None:
            tid = str(key) if has_ts_id else "__series__"
            mname = str(key) if has_model and not has_ts_id else None
        else:
            tid = "__series__"
            mname = None

        s_hist: pd.DataFrame | None = None
        if history_df is not None and not history_df.empty:
            if ts_id_col in history_df.columns and has_ts_id:
                s_hist = history_df[history_df[ts_id_col].astype(str) == tid]
            else:
                s_hist = history_df

        s_oof: pd.DataFrame | None = None
        if oof_df is not None and not oof_df.empty:
            s_oof = oof_df
            if ts_id_col in s_oof.columns and has_ts_id:
                s_oof = s_oof[s_oof[ts_id_col].astype(str) == tid]
            if "model_type" in s_oof.columns and mname is not None:
                s_oof = s_oof[s_oof["model_type"] == mname]

        stamped = f"{mname}_{method}" if (stamp_method and mname is not None) else mname
        rec_grp, rec_by_k = reconcile_series_temporal(
            s_hist,
            grp,
            cfg,
            freq=eff_freq,
            horizon=horizon,
            temporal_aggregations=eff_aggs,
            model_name=stamped,
            method=method,
            oof_df=s_oof,
            coarse_forecasts=coarse_forecasts,
            nonneg=nonneg,
            date_col=date_col,
            target_col=target_col,
        )
        last_spec = rec_grp.attrs.get("temporal_spec")
        last_agg_by_k = _SafeArrayDict(rec_by_k)
        for k, arr in rec_by_k.items():
            agg_by_node_k[(tid, int(k))] = arr
        rec_grp.attrs.clear()
        parts.append(rec_grp)

    out = pd.concat(parts, ignore_index=True) if parts else df.copy()
    if last_spec is not None:
        out.attrs["temporal_spec"] = last_spec
    out.attrs["aggregated_by_k"] = last_agg_by_k
    out.attrs["aggregated_by_node_k"] = agg_by_node_k
    final_temp_disc = (
        float(
            verify_temporal_coherence(
                out,
                agg_by_node_k if has_ts_id else last_agg_by_k,
                spec=last_spec,
                raise_on_error=False,
            )
        )
        if not out.empty
        else 0.0
    )
    out.attrs["cross_temporal_diagnostics"] = {
        "mode": "temporal_only",
        "spatial_weight_alpha": 0.0,
        "cs_first_discrepancy": 0.0,
        "temp_first_discrepancy": 0.0,
        "final_cs_discrepancy": 0.0,
        "final_temp_discrepancy": final_temp_disc,
    }
    if return_aggregations:
        return out, (agg_by_node_k if has_ts_id else last_agg_by_k)
    return out

reconcile_temporal_matrix

reconcile_temporal_matrix(
    spec,
    method="wls_struct",
    *,
    residuals_by_k=None,
    history_by_k=None,
    use_full_horizon=False,
)

Compute the temporal reconciliation matrix G_temp of shape (m, n_temp_nodes).

Given stacked single-cycle forecasts y_hat_cycle ordered from coarsest factor k_max to finest k=1, the reconciled bottom-level trajectory is b_tilde = G_temp @ y_hat_cycle and the coherent hierarchy is y_tilde = S_temp @ b_tilde. When use_full_horizon=True, returns I_P \otimes G_temp of shape (padded_horizon, P * n_temp_nodes).

Source code in src/scale_forecasting/temporal_reconciliation.py
def reconcile_temporal_matrix(
    spec: TemporalHierarchySpec,
    method: str = "wls_struct",
    *,
    residuals_by_k: Mapping[int, np.ndarray] | None = None,
    history_by_k: Mapping[int, np.ndarray] | None = None,
    use_full_horizon: bool = False,
) -> np.ndarray:
    """Compute the temporal reconciliation matrix ``G_temp`` of shape ``(m, n_temp_nodes)``.

    Given stacked single-cycle forecasts ``y_hat_cycle`` ordered from coarsest factor ``k_max`` to
    finest ``k=1``, the reconciled bottom-level trajectory is ``b_tilde = G_temp @ y_hat_cycle``
    and the coherent hierarchy is ``y_tilde = S_temp @ b_tilde``.
    When ``use_full_horizon=True``, returns ``I_P \\otimes G_temp`` of shape
    ``(padded_horizon, P * n_temp_nodes)``.
    """
    norm_method = _normalize_temporal_method(method)
    s_mat = spec.summing_matrix
    n_nodes, m = s_mat.shape
    n_agg = n_nodes - m

    if norm_method == "bottom_up":
        g_single = np.hstack([np.zeros((m, n_agg), dtype=float), np.eye(m, dtype=float)])
    elif norm_method == "top_down":
        g_single = np.zeros((m, n_nodes), dtype=float)
        props = np.full(m, 1.0 / max(1, m), dtype=float)
        if history_by_k and 1 in history_by_k:
            y1 = np.asarray(history_by_k[1], dtype=float).reshape(-1)
            y1 = y1[np.isfinite(y1)]
            n_cyc = len(y1) // m
            if n_cyc >= 1:
                cyc = y1[-n_cyc * m :].reshape(n_cyc, m)
                totals = cyc.sum(axis=1)
                valid = np.abs(totals) > 1e-12
                if np.any(valid):
                    raw_p = np.maximum(0.0, (cyc[valid] / totals[valid, np.newaxis]).mean(axis=0))
                    if raw_p.sum() > 1e-12:
                        props = raw_p / raw_p.sum()
        g_single[:, 0] = props
    elif norm_method == "middle_out":
        coarse_factors = [k for k in spec.aggregations if k > 1]
        if len(coarse_factors) < 2:
            # Fall back to wls_struct when no intermediate temporal level exists.
            w_mat = _temporal_covariance(spec, "wls_struct", residuals_by_k)
            winv_s = np.linalg.solve(w_mat, s_mat)
            g_single = np.linalg.solve(s_mat.T @ winv_s, winv_s.T)
        else:
            mid_k = coarse_factors[len(coarse_factors) // 2]
            sl = spec.level_slices[mid_k]
            n_mid = m // mid_k
            g_single = np.zeros((m, n_nodes), dtype=float)
            for b_idx in range(n_mid):
                col_idx = sl.start + b_idx
                start_step = b_idx * mid_k
                end_step = start_step + mid_k
                g_single[start_step:end_step, col_idx] = 1.0 / float(mid_k)
    else:
        w_mat = _temporal_covariance(spec, norm_method, residuals_by_k)
        reg = 1e-8 * max(float(np.trace(w_mat)) / max(1, n_nodes), 1.0)
        w_reg = w_mat + reg * np.eye(n_nodes, dtype=float)
        winv_s = np.linalg.solve(w_reg, s_mat)
        st_winv_s = s_mat.T @ winv_s
        g_single = np.linalg.solve(
            st_winv_s + 1e-10 * np.eye(m, dtype=float),
            winv_s.T,
        )

    if use_full_horizon and spec.n_macro_periods > 1:
        return np.kron(np.eye(spec.n_macro_periods, dtype=float), g_single)
    return g_single

sample_coherent_paths

sample_coherent_paths(
    predictions_df,
    spec=None,
    *,
    hier_spec=None,
    history_df=None,
    oof_df=None,
    cfg=None,
    n_paths=200,
    seed=42,
    method="mint_shrink",
    nonneg=False,
    temporal_aggregations=None,
    aggregations=None,
    freq=None,
    date_col="forecast_date",
    ts_id_col="ts_id",
    target_col="y",
)

Draw n_paths joint Gaussian/residual sample paths projected onto the coherent subspace.

Returns a tidy DataFrame with columns ["path_id", "ts_id", "forecast_date", "yhat_sample"] (plus "model_type" when present in predictions_df, and "ds" when "ds" was in predictions_df) where every simulated path_id satisfies exact cross-sectional and temporal additive coherence (< 1e-6).

Source code in src/scale_forecasting/temporal_reconciliation.py
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
def sample_coherent_paths(
    predictions_df: pd.DataFrame,
    spec: HierarchySpec | None = None,
    *,
    hier_spec: HierarchySpec | None = None,
    history_df: pd.DataFrame | None = None,
    oof_df: pd.DataFrame | None = None,
    cfg: RunConfig | None = None,
    n_paths: int = 200,
    seed: int = 42,
    method: str = "mint_shrink",
    nonneg: bool = False,
    temporal_aggregations: Sequence[int] | None = None,
    aggregations: Sequence[int] | None = None,
    freq: str | None = None,
    date_col: str = "forecast_date",
    ts_id_col: str = "ts_id",
    target_col: str = "y",
) -> pd.DataFrame:
    """Draw ``n_paths`` joint Gaussian/residual sample paths projected onto the coherent subspace.

    Returns a tidy DataFrame with columns ``["path_id", "ts_id", "forecast_date", "yhat_sample"]``
    (plus ``"model_type"`` when present in ``predictions_df``, and ``"ds"`` when ``"ds"`` was in
    ``predictions_df``) where every simulated ``path_id`` satisfies exact cross-sectional and
    temporal additive coherence (``< 1e-6``).
    """
    if isinstance(n_paths, bool) or not isinstance(n_paths, int) or n_paths < 1:
        raise ConfigError(f"n_paths must be a positive integer >= 1, got {n_paths!r}")

    if predictions_df is None or predictions_df.empty:
        return pd.DataFrame(columns=["path_id", ts_id_col, "forecast_date", "yhat_sample"])

    eff_spec = spec if spec is not None else hier_spec
    if eff_spec is None:
        eff_spec = predictions_df.attrs.get("hierarchy_spec")
    if (
        eff_spec is None
        and cfg is not None
        and cfg.hierarchy.enabled
        and (cfg.hierarchy.levels or getattr(cfg.hierarchy, "similarity_hierarchy", False))
        and history_df is not None
        and not history_df.empty
    ):
        needed_cols = {c for lvl in cfg.hierarchy.levels for c in lvl} - {"similarity_cluster_id"}
        if needed_cols.issubset(history_df.columns):
            _, eff_spec = build_hierarchy(history_df, cfg)

    had_ds = "ds" in predictions_df.columns
    has_model_in_input = "model_type" in predictions_df.columns
    dcol = date_col
    if dcol not in predictions_df.columns:
        for cand in ("forecast_date", "ds", "date"):
            if cand in predictions_df.columns:
                dcol = cand
                break
    if eff_spec is not None:
        predictions_df = ensure_hierarchy_nodes_present(
            predictions_df,
            eff_spec,
            ts_id_col=ts_id_col,
            date_col=dcol,
            target_col="yhat",
        )

    eff_aggs = temporal_aggregations if temporal_aggregations is not None else aggregations
    if eff_aggs is None and cfg is not None:
        eff_aggs = cfg.hierarchy.temporal_aggregations
    eff_freq = freq or (cfg.data.freq if cfg is not None else "D")
    is_nonneg = bool(
        nonneg
        or method == "mint_nonneg"
        or (cfg is not None and cfg.output.floor is not None and cfg.output.floor >= 0.0)
    )

    # Step 1: Reconcile point forecasts so the mean trajectory is coherent.
    use_temporal = bool(
        eff_aggs is not None
        or method == "cross_temporal_mint"
        or (cfg is not None and (cfg.hierarchy.reconcile_temporal or cfg.hierarchy.cross_temporal))
        or eff_spec is None
    )
    if eff_spec is not None and use_temporal:
        base_rec_res = reconcile_cross_temporal(
            predictions_df,
            eff_spec,
            history_df=history_df,
            oof_df=oof_df,
            cfg=cfg,
            freq=eff_freq,
            temporal_aggregations=eff_aggs,
            method=method,
            nonneg=is_nonneg,
            stamp_method=False,
            return_aggregations=False,
            date_col=dcol,
            ts_id_col=ts_id_col,
            target_col=target_col,
        )
        assert isinstance(base_rec_res, pd.DataFrame)
        base_rec_df = base_rec_res
    elif eff_spec is not None:
        cs_m: Any = "mint_shrink" if method == "cross_temporal_mint" else method
        base_rec_df = reconcile_forecasts(
            predictions_df,
            eff_spec,
            cs_m,
            cfg=cfg,
            history_df=history_df,
            oof_df=oof_df,
            date_col=dcol,
            ts_id_col=ts_id_col,
            target_col=target_col,
            stamp_method=False,
        )
    else:
        t_m = "mint_shrink" if method == "cross_temporal_mint" else method
        t_res = reconcile_temporal_forecasts(
            predictions_df,
            history_df=history_df,
            oof_df=oof_df,
            cfg=cfg,
            freq=eff_freq,
            temporal_aggregations=eff_aggs,
            method=t_m,
            nonneg=is_nonneg,
            stamp_method=False,
            return_aggregations=False,
            date_col=dcol,
            ts_id_col=ts_id_col,
            target_col=target_col,
        )
        assert isinstance(t_res, pd.DataFrame)
        base_rec_df = t_res

    models = (
        base_rec_df["model_type"].drop_duplicates().tolist()
        if has_model_in_input and "model_type" in base_rec_df.columns
        else [None]
    )
    rng = np.random.default_rng(seed)

    path_frames: list[pd.DataFrame] = []
    agg_by_path_node_k: dict[tuple[int, str, int], np.ndarray] = _SafeArrayDict()
    agg_by_node_k_p0: dict[tuple[str, int], np.ndarray] = _SafeArrayDict()
    resolved_tspec: TemporalHierarchySpec | None = None

    for m_name in models:
        m_df = (
            base_rec_df[base_rec_df["model_type"] == m_name].copy()
            if m_name is not None
            else base_rec_df.copy()
        )
        dates = sorted(m_df[dcol].unique())
        h = len(dates)
        if h == 0:
            continue

        resolved_tspec = build_temporal_hierarchy(eff_freq, max(1, h), eff_aggs)
        bottom_ids = (
            list(eff_spec.bottom_ids)
            if eff_spec is not None
            else (
                sorted(m_df[ts_id_col].astype(str).unique().tolist())
                if ts_id_col in m_df.columns
                else ["series_0"]
            )
        )
        node_ids = list(eff_spec.node_ids) if eff_spec is not None else bottom_ids
        n_b = len(bottom_ids)

        if ts_id_col not in m_df.columns:
            m_df = m_df.assign(**{ts_id_col: bottom_ids[0]})

        piv_b = (
            m_df.pivot_table(index=ts_id_col, columns=dcol, values="yhat", aggfunc="first")
            .reindex(index=bottom_ids, columns=dates)
            .ffill(axis=1)
            .bfill(axis=1)
            .fillna(0.0)
            .to_numpy(dtype=float)
        )

        # Estimate bottom-series innovation covariance W_b of shape (n_b, n_b).
        w_b: np.ndarray | None = None
        if oof_df is not None and not oof_df.empty and {"y_true", "yhat"}.issubset(oof_df.columns):
            m_oof = (
                oof_df[oof_df["model_type"] == m_name]
                if ("model_type" in oof_df.columns and m_name is not None)
                else oof_df
            )
            oof_dcol = (
                "ds"
                if "ds" in m_oof.columns
                else ("forecast_date" if "forecast_date" in m_oof.columns else dcol)
            )
            if ts_id_col in m_oof.columns and oof_dcol in m_oof.columns:
                tmp_oof = m_oof.copy()
                tmp_oof["_res"] = tmp_oof["y_true"].to_numpy(dtype=float) - tmp_oof[
                    "yhat"
                ].to_numpy(dtype=float)
                res_mat = (
                    tmp_oof.pivot_table(
                        index=oof_dcol, columns=ts_id_col, values="_res", aggfunc="mean"
                    )
                    .reindex(columns=bottom_ids)
                    .dropna(how="all")
                    .fillna(0.0)
                    .to_numpy(dtype=float)
                )
                if res_mat.shape[0] >= 2 and float(np.max(np.abs(res_mat))) > 1e-12:
                    w_b, _ = schafer_strimmer_shrinkage(res_mat)

        if w_b is None:
            if "yhat_lower" in m_df.columns and "yhat_upper" in m_df.columns:
                piv_lo = (
                    m_df.pivot_table(
                        index=ts_id_col, columns=dcol, values="yhat_lower", aggfunc="first"
                    )
                    .reindex(index=bottom_ids, columns=dates)
                    .fillna(0.0)
                    .to_numpy(dtype=float)
                )
                piv_hi = (
                    m_df.pivot_table(
                        index=ts_id_col, columns=dcol, values="yhat_upper", aggfunc="first"
                    )
                    .reindex(index=bottom_ids, columns=dates)
                    .fillna(0.0)
                    .to_numpy(dtype=float)
                )
                sigma_vec = np.maximum(np.mean(0.5 * np.abs(piv_hi - piv_lo) / _Z_80, axis=1), 1e-3)
                w_b = np.diag(sigma_vec**2)
            elif (
                history_df is not None
                and not history_df.empty
                and ts_id_col in history_df.columns
                and target_col in history_df.columns
            ):
                h_dcol = (
                    dcol
                    if dcol in history_df.columns
                    else ("ds" if "ds" in history_df.columns else "forecast_date")
                )
                h_piv = (
                    history_df.pivot_table(
                        index=ts_id_col, columns=h_dcol, values=target_col, aggfunc="mean"
                    )
                    .reindex(index=bottom_ids)
                    .ffill(axis=1)
                    .bfill(axis=1)
                    .fillna(0.0)
                    .to_numpy(dtype=float)
                )
                if h_piv.shape[1] >= 2:
                    sigma_vec = np.maximum(np.std(np.diff(h_piv, axis=1), axis=1), 1e-3)
                else:
                    sigma_vec = np.maximum(0.05 * np.mean(np.abs(piv_b), axis=1), 1e-2)
                w_b = np.diag(sigma_vec**2)
            else:
                sigma_vec = np.maximum(0.05 * np.mean(np.abs(piv_b), axis=1), 1e-2)
                w_b = np.diag(sigma_vec**2)

        # Cholesky factor of W_b for fast vectorized sampling across (n_paths, n_b, h).
        reg = 1e-9 * max(float(np.trace(w_b)) / max(1, n_b), 1.0)
        chol_l = np.linalg.cholesky(w_b + reg * np.eye(n_b, dtype=float))
        z_std = rng.standard_normal(size=(n_paths, n_b, h))
        step_scale = np.sqrt(1.0 + 0.05 * np.arange(h, dtype=float))[None, None, :]
        noise = np.einsum("ij,njt->nit", chol_l, z_std) * step_scale
        b_draws = piv_b[None, :, :] + noise
        if is_nonneg:
            b_draws = np.maximum(0.0, b_draws)

        # Project onto S_cs across all hierarchy nodes.
        if eff_spec is not None:
            y_draws = np.einsum("ij,njt->nit", eff_spec.summing_matrix, b_draws)
        else:
            y_draws = b_draws

        # Compute coherent temporal bucket sums for every path and node.
        base_season = seasonal_period(eff_freq)
        for p_idx in range(n_paths):
            for n_idx, nid in enumerate(node_ids):
                series_h = y_draws[p_idx, n_idx]
                padded_s = _pad_base_forecast(series_h, resolved_tspec.padded_horizon, base_season)
                agg_by_path_node_k[(p_idx, nid, 1)] = series_h.copy()
                if p_idx == 0:
                    agg_by_node_k_p0[(nid, 1)] = series_h.copy()
                for k in resolved_tspec.aggregations:
                    if int(k) == 1:
                        continue
                    k_int = int(k)
                    agg_arr = aggregate_temporal_history(padded_s, k_int)
                    agg_by_path_node_k[(p_idx, nid, k_int)] = agg_arr
                    if p_idx == 0:
                        agg_by_node_k_p0[(nid, k_int)] = agg_arr

        # Vectorized tidy DataFrame construction of length n_paths * n_nodes * h.
        n_nodes = len(node_ids)
        path_col = np.repeat(np.arange(n_paths, dtype=int), n_nodes * h)
        ts_col = np.tile(np.repeat(np.asarray(node_ids, dtype=object), h), n_paths)
        dt_col = np.tile(np.asarray(dates), n_paths * n_nodes)
        val_col = y_draws.reshape(-1)

        data_dict: dict[str, Any] = {
            "path_id": path_col,
            ts_id_col: ts_col,
        }
        if m_name is not None:
            data_dict["model_type"] = m_name
        data_dict["forecast_date"] = dt_col
        if had_ds:
            data_dict["ds"] = dt_col
        data_dict["yhat_sample"] = val_col
        path_frames.append(pd.DataFrame(data_dict))

    out_df = (
        pd.concat(path_frames, ignore_index=True)
        if path_frames
        else pd.DataFrame(columns=["path_id", ts_id_col, "forecast_date", "yhat_sample"])
    )
    if eff_spec is not None:
        out_df.attrs["hierarchy_spec"] = eff_spec
    if resolved_tspec is not None:
        out_df.attrs["temporal_spec"] = resolved_tspec
    out_df.attrs["aggregated_by_node_k"] = agg_by_node_k_p0
    out_df.attrs["aggregated_by_path_node_k"] = agg_by_path_node_k
    return out_df

verify_cross_temporal_coherence

verify_cross_temporal_coherence(
    reconciled_df,
    hier_spec,
    aggregated_by_node_k=None,
    temp_spec=None,
    *,
    cfg=None,
    date_col="ds",
    ts_id_col="ts_id",
    atol=1e-06,
    raise_on_error=True,
)

Verify simultaneous cross-sectional and temporal coherence across nodes and factors k.

Returns max(cross_sectional_discrepancy, temporal_discrepancy) and raises DataError if either axis exceeds atol.

Source code in src/scale_forecasting/temporal_reconciliation.py
def verify_cross_temporal_coherence(
    reconciled_df: pd.DataFrame,
    hier_spec: HierarchySpec,
    aggregated_by_node_k: Mapping[tuple[str, int], np.ndarray]
    | TemporalHierarchySpec
    | None = None,
    temp_spec: TemporalHierarchySpec | None = None,
    *,
    cfg: RunConfig | None = None,
    date_col: str = "ds",
    ts_id_col: str = "ts_id",
    atol: float = 1e-6,
    raise_on_error: bool = True,
) -> float:
    """Verify simultaneous cross-sectional and temporal coherence across nodes and factors ``k``.

    Returns ``max(cross_sectional_discrepancy, temporal_discrepancy)`` and raises `DataError` if
    either axis exceeds ``atol``.
    """
    _ = cfg
    if reconciled_df.empty:
        return 0.0
    dcol = (
        date_col
        if date_col in reconciled_df.columns
        else ("forecast_date" if "forecast_date" in reconciled_df.columns else "ds")
    )
    if raise_on_error:
        cs_err = verify_coherence(
            reconciled_df,
            hier_spec,
            date_col=dcol,
            ts_id_col=ts_id_col,
            atol=atol,
        )
    else:
        try:
            cs_err = verify_coherence(
                reconciled_df,
                hier_spec,
                date_col=dcol,
                ts_id_col=ts_id_col,
                atol=float("inf"),
            )
        except DataError:
            cs_err = float("inf")

    if isinstance(aggregated_by_node_k, TemporalHierarchySpec) and temp_spec is None:
        temp_spec = aggregated_by_node_k
        node_k_map = reconciled_df.attrs.get("aggregated_by_node_k")
    else:
        node_k_map = aggregated_by_node_k or reconciled_df.attrs.get("aggregated_by_node_k")

    temp_err = 0.0
    if node_k_map or reconciled_df.attrs.get("aggregated_by_path_node_k"):
        temp_err = verify_temporal_coherence(
            reconciled_df,
            node_k_map,
            spec=temp_spec,
            atol=atol,
            raise_on_error=raise_on_error,
        )
    return max(cs_err, temp_err)

verify_temporal_coherence

verify_temporal_coherence(
    bottom_yhat,
    aggregated_by_k=None,
    spec=None,
    *,
    cfg=None,
    m=None,
    aggregations=None,
    target_col="yhat",
    atol=1e-06,
    raise_on_error=True,
)

Verify exact additive temporal coherence across all complete coarse buckets.

Supports both: * verify_temporal_coherence(bottom_yhat_1d, aggregated_by_k_dict, spec=...) * verify_temporal_coherence(reconciled_df, ...) (using df.attrs["aggregated_by_k"], df.attrs["aggregated_by_node_k"], or df.attrs["aggregated_by_path_node_k"]).

Returns the maximum absolute discrepancy across all complete buckets and raises DataError if it exceeds atol when raise_on_error=True.

Source code in src/scale_forecasting/temporal_reconciliation.py
def verify_temporal_coherence(
    bottom_yhat: np.ndarray | pd.Series | pd.DataFrame,
    aggregated_by_k: Mapping[Any, np.ndarray] | TemporalHierarchySpec | Sequence[int] | None = None,
    spec: TemporalHierarchySpec | Sequence[int] | None = None,
    *,
    cfg: RunConfig | None = None,
    m: int | None = None,
    aggregations: Sequence[int] | None = None,
    target_col: str = "yhat",
    atol: float = 1e-6,
    raise_on_error: bool = True,
) -> float:
    """Verify exact additive temporal coherence across all complete coarse buckets.

    Supports both:
    * ``verify_temporal_coherence(bottom_yhat_1d, aggregated_by_k_dict, spec=...)``
    * ``verify_temporal_coherence(reconciled_df, ...)`` (using ``df.attrs["aggregated_by_k"]``,
      ``df.attrs["aggregated_by_node_k"]``, or ``df.attrs["aggregated_by_path_node_k"]``).

    Returns the maximum absolute discrepancy across all complete buckets and raises `DataError`
    if it exceeds ``atol`` when ``raise_on_error=True``.
    """
    _ = (cfg, m, aggregations)
    if isinstance(bottom_yhat, pd.DataFrame):
        df = bottom_yhat
        if df.empty:
            return 0.0
        vcol = target_col
        if vcol not in df.columns:
            for cand in ("yhat", "yhat_sample", "y_sample", "y"):
                if cand in df.columns:
                    vcol = cand
                    break
        dcol = None
        for cand in ("forecast_date", "ds", "date"):
            if cand in df.columns:
                dcol = cand
                break

        # Handle multi-path or single-path DataFrame with `aggregated_by_path_node_k`.
        path_agg_map = df.attrs.get("aggregated_by_path_node_k")
        if "path_id" in df.columns and isinstance(path_agg_map, Mapping) and path_agg_map:
            max_p_err = 0.0
            ts_col_p = "ts_id" if "ts_id" in df.columns else None
            indexed_pn: dict[tuple[int, str], dict[int, np.ndarray]] = {}
            for (p_k, n_k, k), arr in path_agg_map.items():
                indexed_pn.setdefault((int(p_k), str(n_k)), {})[int(k)] = arr
            for pid, p_grp in df.groupby("path_id", sort=False):
                pid_int = int(str(pid))
                if ts_col_p is not None:
                    for nid, s_grp in p_grp.groupby(ts_col_p, sort=False):
                        sub_s = s_grp.sort_values(dcol, kind="mergesort") if dcol else s_grp
                        b_v = sub_s[vcol].to_numpy(dtype=float)
                        k_map_p = indexed_pn.get((pid_int, str(nid)))
                        if k_map_p:
                            err = verify_temporal_coherence(
                                b_v,
                                k_map_p,
                                spec=spec,
                                atol=atol,
                                raise_on_error=raise_on_error,
                            )
                            max_p_err = max(max_p_err, err)
            return max_p_err

        if isinstance(aggregated_by_k, Mapping):
            agg_map = aggregated_by_k
        elif df.attrs.get("aggregated_by_node_k"):
            agg_map = df.attrs["aggregated_by_node_k"]
        elif df.attrs.get("aggregated_by_k"):
            agg_map = df.attrs["aggregated_by_k"]
        else:
            # Self-consistency check across aggregated series built from df[vcol].
            factors = (
                list(aggregated_by_k.aggregations)
                if isinstance(aggregated_by_k, TemporalHierarchySpec)
                else (
                    list(aggregated_by_k)
                    if isinstance(aggregated_by_k, Sequence)
                    else (
                        list(spec.aggregations)
                        if isinstance(spec, TemporalHierarchySpec)
                        else (list(spec) if isinstance(spec, Sequence) else [1])
                    )
                )
            )
            grp_cols = [c for c in ("path_id", "model_type", "ts_id") if c in df.columns]
            if grp_cols:
                max_g_err = 0.0
                for _gk, sub_g in df.groupby(grp_cols, sort=False):
                    if dcol is not None:
                        sub_g = sub_g.sort_values(dcol, kind="mergesort")
                    vals = sub_g[vcol].to_numpy(dtype=float)
                    g_map = {int(k): aggregate_temporal_history(vals, int(k)) for k in factors}
                    err = verify_temporal_coherence(
                        vals, g_map, atol=atol, raise_on_error=raise_on_error
                    )
                    max_g_err = max(max_g_err, err)
                return max_g_err
            vals = df[vcol].to_numpy(dtype=float)
            agg_map = {int(k): aggregate_temporal_history(vals, int(k)) for k in factors}
            return verify_temporal_coherence(
                vals, agg_map, atol=atol, raise_on_error=raise_on_error
            )

        sample_key = next(iter(agg_map.keys()), None)
        if isinstance(sample_key, tuple):
            max_err = 0.0
            ts_col = "ts_id" if "ts_id" in df.columns else None
            node_aggs: dict[str, dict[int, np.ndarray]] = {}
            for (nid, k), arr in agg_map.items():
                node_aggs.setdefault(str(nid), {})[int(k)] = np.asarray(arr, dtype=float)
            for nid, k_map in node_aggs.items():
                if ts_col is not None:
                    sub = df[df[ts_col].astype(str) == nid]
                    if "path_id" in sub.columns and sub["path_id"].nunique() > 1:
                        first_pid = sub["path_id"].iloc[0]
                        sub = sub[sub["path_id"] == first_pid]
                    if dcol is not None:
                        sub = sub.sort_values(dcol, kind="mergesort")
                    b_vals = (
                        sub[vcol].to_numpy(dtype=float)
                        if not sub.empty
                        else k_map.get(1, np.empty(0))
                    )
                else:
                    b_vals = df[vcol].to_numpy(dtype=float)
                err = verify_temporal_coherence(
                    b_vals, k_map, spec=spec, atol=atol, raise_on_error=raise_on_error
                )
                max_err = max(max_err, err)
            return max_err

        b_vals = df[vcol].to_numpy(dtype=float)
        return verify_temporal_coherence(
            b_vals, agg_map, spec=spec, atol=atol, raise_on_error=raise_on_error
        )

    b_arr = np.asarray(bottom_yhat, dtype=float).reshape(-1)
    if len(b_arr) == 0 or aggregated_by_k is None or not isinstance(aggregated_by_k, Mapping):
        return 0.0

    max_err = 0.0
    h = len(b_arr)
    for raw_k, coarse_vals in aggregated_by_k.items():
        k = int(raw_k)
        if k <= 1:
            continue
        c_arr = np.asarray(coarse_vals, dtype=float).reshape(-1)
        n_full_buckets = min(h // k, len(c_arr))
        for b_idx in range(n_full_buckets):
            block_sum = float(np.sum(b_arr[b_idx * k : (b_idx + 1) * k]))
            err = abs(block_sum - float(c_arr[b_idx]))
            if raise_on_error and (not np.isfinite(err) or err > atol):
                raise DataError(
                    f"Forecasts are not temporally coherent at factor k={k}, bucket {b_idx} "
                    f"(discrepancy {err:.3e} > {atol:.3e})"
                )
            max_err = max(max_err, err)
    return max_err