{"record":{"id":"570b97f9a60e0447","repo":"HKUDS/Vibe-Trading","slug":"lags-must-be-the-number-of-observations-got-lag","errorCode":null,"errorMessage":"lags must be < the number of observations; got lags={lags} for {clean.size} observations","messagePattern":"lags must be < the number of observations; got lags=(.+?) for (.+?) observations","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/timeseries.py","lineNumber":699,"sourceCode":"        Dict with keys ``durbin_watson`` (float), ``dw_interpretation`` (str,\n        one of ``'positive autocorrelation'`` / ``'no autocorrelation'`` /\n        ``'negative autocorrelation'``), ``ljung_box_p`` (numpy array of\n        p-values, one per lag), ``has_autocorrelation`` (bool) and ``fix`` (str).\n\n    Raises:\n        ImportError: If ``statsmodels`` is not installed.\n        ValueError: If ``lags`` is below 1.\n    \"\"\"\n    diagnostic = _require(\"statsmodels.stats.diagnostic\", \"statsmodels\", \"autocorrelation_test\")\n    stattools = _require(\"statsmodels.stats.stattools\", \"statsmodels\", \"autocorrelation_test\")\n    if lags < 1:\n        raise ValueError(f\"autocorrelation_test needs lags >= 1, got {lags}\")\n\n    clean = pd.Series(residuals, dtype=float).dropna()\n    if lags >= clean.size:\n        # acorr_ljungbox silently caps lags and then dies on a shape mismatch\n        # with a numpy message that names neither argument.\n        raise ValueError(\n            f\"lags must be < the number of observations; got lags={lags} \"\n            f\"for {clean.size} observations\"\n        )\n    dw = float(stattools.durbin_watson(clean))\n    lb_p = np.asarray(diagnostic.acorr_ljungbox(clean, lags=lags)[\"lb_pvalue\"].values, dtype=float)\n\n    if dw < _DW_POSITIVE_BELOW:\n        interpretation = \"positive autocorrelation\"\n    elif dw < _DW_NEGATIVE_ABOVE:\n        interpretation = \"no autocorrelation\"\n    else:\n        interpretation = \"negative autocorrelation\"\n\n    return {\n        \"durbin_watson\": dw,\n        \"dw_interpretation\": interpretation,\n        \"ljung_box_p\": lb_p,\n        \"has_autocorrelation\": bool(np.any(lb_p < significance)),","sourceCodeStart":681,"sourceCodeEnd":717,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/timeseries.py#L681-L717","documentation":"After dropping NaNs, autocorrelation_test requires lags to be strictly less than the number of remaining observations. statsmodels' acorr_ljungbox silently caps lags and then dies on a numpy shape mismatch that names neither argument, so this guard gives an actionable message up front.","triggerScenarios":"autocorrelation_test(residuals, lags=50) on a 40-row series, or a series whose NaN-dropped size (clean.size) falls to <= lags; passing lags == n is also rejected.","commonSituations":"Running diagnostics on short backtest windows, small samples after dropna, or reusing a lag count tuned on a longer dataset.","solutions":["Reduce lags below the number of non-NaN observations, e.g. lags < clean.size.","Derive lags from the actual cleaned length: lags = min(desired_lags, clean_size - 1).","Check residuals length and NaN count before calling."],"exampleFix":"// before\nautocorrelation_test(residuals, lags=40)  # residuals has 40 obs -> error\n// after\nclean_n = pd.Series(residuals).dropna().size\nautocorrelation_test(residuals, lags=min(40, clean_n - 1))","handlingStrategy":"validation","validationCode":"clean_n = pd.Series(residuals).dropna().size\nlags = max(1, min(desired_lags, clean_n - 1))\nautocorrelation_test(residuals, lags=lags)","typeGuard":null,"tryCatchPattern":"try:\n    result = autocorrelation_test(residuals, lags=lags)\nexcept ValueError as e:\n    if \"observations\" in str(e):\n        lags = max(1, clean_n - 1)\n        result = autocorrelation_test(residuals, lags=lags)\n    else:\n        raise","preventionTips":["Always size lags relative to the non-NaN length of the series.","Log clean.size alongside lags when running diagnostics over many windows."],"tags":["python","statistics","argument-validation"],"backgroundTag":"lags-exceed-sample-size","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}