HKUDS/Vibe-Trading · error · ValueError
returns and var must be the same length, got {ret_values.siz
Error message
returns and var must be the same length, got {ret_values.size} and {var_values.size} What it means
Once both inputs are 1-D (and var was not a broadcast scalar), _align requires equal element counts; a length mismatch raises ValueError showing both sizes. This typically happens when one side lost rows (dropna, rolling-window warmup) or plain arrays without indexes were passed with different lengths.
Source
Thrown at agent/src/quantlib/var_backtest.py:286
"partial join silently compares each day against another day's "
"forecast."
)
ret_values = np.asarray(returns, dtype=float)
if ret_values.ndim > 1:
raise ValueError(f"returns must be 1-D, got shape {ret_values.shape}")
ret_values = ret_values.ravel()
var_values = np.asarray(var, dtype=float)
if var_values.ndim == 0:
var_values = np.full(ret_values.shape, float(var_values))
else:
if var_values.ndim > 1:
raise ValueError(f"var must be 1-D or scalar, got shape {var_values.shape}")
var_values = var_values.ravel()
if ret_values.size != var_values.size:
raise ValueError(
f"returns and var must be the same length, got {ret_values.size} "
f"and {var_values.size}"
)
if ret_values.size == 0:
raise ValueError("returns is empty")
keep = np.isfinite(ret_values) & np.isfinite(var_values)
dropped = int((~keep).sum())
if not keep.any():
raise ValueError("no observation has a finite return and a finite var")
index = ret_index if ret_index is not None else var_index
kept_index = index[keep] if index is not None else None
return ret_values[keep], var_values[keep], kept_index, dropped
def violation_indicator(
returns: pd.Series | np.ndarray | Sequence[float],View on GitHub (pinned to 80ffdda44c)
Solutions
- Trim returns to the var series' valid range or re-attach indexes and use pandas alignment.
- Regenerate var over the full returns sample so lengths match by construction.
- Slice both to the overlapping suffix: rets[-len(var):].
Example fix
# before var_backtest(rets, var) # 500 vs 480 # after var_backtest(rets[-len(var):], var) # aligned tail sample
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np assert np.asarray(returns).size == np.asarray(var).size
Type guard
def same_length(returns, var) -> bool:
import numpy as np
return np.asarray(returns).size == np.asarray(var).size Try / catch
except ValueError as e:
if 'same length' in str(e): trim both to the overlapping tail Prevention
- Trim returns to the VaR warmup window before backtesting
- Use indexed Series so alignment errors surface as label errors early
When it happens
Trigger: Passing a numpy returns array of 500 values and a var array of 480 (e.g. after a 20-day rolling warmup dropped rows), or lists built from different date ranges when indexes are absent so the earlier label check cannot fire.
Common situations: Rolling VaR estimators that return shorter series than the input returns; dropping NaNs from one array only; concatenating train/test segments inconsistently.
Related errors
- returns must be 1-D, got shape {ret_values.shape}
- var must be 1-D or scalar, got shape {var_values.shape}
- label_end_times has {span_ends.size} entries but the sample
- {MODEL_NAME}: every driver sequence must share one length (o
- returns and var must cover exactly the same labels; {len(onl
AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28).
Data as JSON: /api/errors/b7c78502694097ab.
Report an issue: GitHub.