{"record":{"id":"02ff1052f8fe27b0","repo":"HKUDS/Vibe-Trading","slug":"no-observation-has-a-finite-return-and-a-finite-va","errorCode":null,"errorMessage":"no observation has a finite return and a finite var","messagePattern":"no observation has a finite return and a finite var","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/var_backtest.py","lineNumber":296,"sourceCode":"    if var_values.ndim == 0:\n        var_values = np.full(ret_values.shape, float(var_values))\n    else:\n        if var_values.ndim > 1:\n            raise ValueError(f\"var must be 1-D or scalar, got shape {var_values.shape}\")\n        var_values = var_values.ravel()\n\n    if ret_values.size != var_values.size:\n        raise ValueError(\n            f\"returns and var must be the same length, got {ret_values.size} \"\n            f\"and {var_values.size}\"\n        )\n    if ret_values.size == 0:\n        raise ValueError(\"returns is empty\")\n\n    keep = np.isfinite(ret_values) & np.isfinite(var_values)\n    dropped = int((~keep).sum())\n    if not keep.any():\n        raise ValueError(\"no observation has a finite return and a finite var\")\n\n    index = ret_index if ret_index is not None else var_index\n    kept_index = index[keep] if index is not None else None\n    return ret_values[keep], var_values[keep], kept_index, dropped\n\n\ndef violation_indicator(\n    returns: pd.Series | np.ndarray | Sequence[float],\n    var: pd.Series | np.ndarray | Sequence[float] | float,\n) -> np.ndarray:\n    \"\"\"Flag the days on which the realised loss exceeded the VaR forecast.\n\n    Args:\n        returns: Realised returns, signed. A 3% loss is ``-0.03``.\n        var: VaR forecasts as positive loss magnitudes, one per return or a\n            single scalar. A negative entry is not flipped: it is taken at face\n            value, meaning \"the model expects a gain even in the tail\", which is\n            almost always a caller-side sign error and shows up here as an","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/var_backtest.py#L278-L314","documentation":"After dropping non-finite pairs, _align requires at least one observation where both the return and the var are finite; otherwise it raises ValueError. If all pairs contain NaN/inf on either side, no backtest statistic can be computed.","triggerScenarios":"Passing all-NaN returns (e.g. log returns of a constant/zero price series), a var series that is entirely NaN during its estimation warmup, or inf values from zero-division in either input.","commonSituations":"Rolling VaR with a warmup longer than the sample; percentage-change on unadjusted prices containing zeros; both series offset so every pair has one NaN.","solutions":["Check the dropped count in the returned metadata to see how many pairs were discarded and align the valid windows.","Shorten the VaR warmup or drop the warmup rows from both series before calling.","Fix zero-division/NaN-producing steps upstream (use pct_change with fill_method=None and then dropna both)."],"exampleFix":"# before\nrets, var = rets, rolling_var  # rolling_var starts with 20 NaNs, sample is 15 rows\n# after\nvalid = rets.dropna().index.intersection(rolling_var.dropna().index)\nvar_backtest(rets.loc[valid], rolling_var.loc[valid])","handlingStrategy":"validation","validationCode":"import numpy as np\nkeep = np.isfinite(np.asarray(returns, float)) & np.isfinite(np.asarray(var, float))\nassert keep.any(), 'no finite (return, var) pairs'","typeGuard":"def has_finite_pairs(returns, var) -> bool:\n    import numpy as np\n    return (np.isfinite(np.asarray(returns, float)) & np.isfinite(np.asarray(var, float))).any()","tryCatchPattern":"except ValueError as e:\n    if 'finite return and a finite var' in str(e): realign_and_dropna()","preventionTips":["dropna() both series and intersect indexes before backtesting","Avoid warmups longer than the available sample"],"tags":["var-backtest","nan","data-quality"],"backgroundTag":"all-nan-input","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}