HKUDS/Vibe-Trading · error · ValueError

var_backtest needs at least 2 aligned observations, got {ret

Error message

var_backtest needs at least 2 aligned observations, got {ret_values.size}

What it means

var_backtest aligns returns and VaR forecasts, drops non-finite pairs, and requires at least two surviving observations because the Christoffersen independence component needs a minimum of one lag-1 transition. Fewer than two aligned points cannot support any backtest statistic.

Source

Thrown at agent/src/quantlib/var_backtest.py:629

    Args:
        returns: Realised returns, signed, in chronological order.
        var: VaR forecasts as positive loss magnitudes -- one per return, or a
            scalar for a constant-VaR model. When both sides are indexed Series
            the labels must match exactly.
        confidence: VaR confidence level the model claims, e.g. 0.99.
        significance: Level at which each ``rejected`` flag is decided.

    Returns:
        A :class:`VarBacktestReport` carrying the Kupiec, independence, joint
        and Basel results plus the breach dates when an index was supplied.

    Raises:
        ValueError: If the inputs cannot be aligned, if fewer than two finite
            pairs survive, or if either probability is out of range.
    """
    ret_values, var_values, index, dropped = _align(returns, var)
    if ret_values.size < 2:
        raise ValueError(
            f"var_backtest needs at least 2 aligned observations, got {ret_values.size}"
        )

    breaches = ret_values < -var_values
    conditional = christoffersen_conditional_coverage(
        breaches, confidence=confidence, significance=significance
    )
    traffic = basel_traffic_light(
        violations=int(breaches.sum()),
        observations=int(breaches.size),
        confidence=confidence,
    )
    breach_dates = tuple(index[breaches]) if index is not None else None

    return VarBacktestReport(
        confidence=confidence,
        observations=int(breaches.size),
        violations=int(breaches.sum()),

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Check the overlap of the returns and VaR indices before calling; reindex/join explicitly on shared dates.
  2. Drop NaN pairs yourself and assert len >= 2 (the function also returns a dropped count — inspect it).
  3. Ensure the VaR series covers the same date range as returns.

Example fix

# before
var_backtest(returns, var)  # non-overlapping date indices -> all NaN
# after
common = returns.index.intersection(var.index)
var_backtest(returns.loc[common], var.loc[common])
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np, pandas as pd
r = pd.Series(returns).dropna()
v = pd.Series(var).dropna()
common = r.index.intersection(v.index)
if len(common) < 2:
    raise ValueError(f'insufficient overlap: {len(common)} shared points')
result = var_backtest(r.loc[common], v.loc[common])

Try / catch

try:
    result = var_backtest(returns, var)
except ValueError as e:
    if 'aligned observations' in str(e):
        logger.warning('skipping backtest: %s', e)
    else:
        raise

Prevention

When it happens

Trigger: Calling var_backtest with empty series, one-row series, or series where all but one pair contain NaN/Inf (mismatched dates with NaN fills are the classic cause). Also when returns and var have no overlapping index.

Common situations: Merging returns and VaR on dates with no overlap (all-NaN alignment), loading a VaR file that is stale relative to the returns series, or backtesting during a model warm-up period where the VaR forecast is NaN.

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/0bbc3d70876721c0. Report an issue: GitHub.