{"record":{"id":"b4182d5c0fbb1200","repo":"HKUDS/Vibe-Trading","slug":"autocorrelation-test-needs-lags-1-got-lags","errorCode":null,"errorMessage":"autocorrelation_test needs lags >= 1, got {lags}","messagePattern":"autocorrelation_test needs lags >= 1, got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/timeseries.py","lineNumber":693,"sourceCode":"    Args:\n        residuals: Residual series to test.\n        lags: Highest lag included in the Ljung-Box test.\n        significance: Significance level for the ``has_autocorrelation`` verdict.\n\n    Returns:\n        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\"","sourceCodeStart":675,"sourceCodeEnd":711,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/timeseries.py#L675-L711","documentation":"autocorrelation_test rejects a lags argument below 1 before touching statsmodels. Lags of 0 or negative make no sense for a Ljung-Box/Durbin-Watson style diagnostic, so the function fails fast with a clear ValueError instead of letting statsmodels produce a confusing downstream error.","triggerScenarios":"Calling autocorrelation_test(residuals, lags=0) or with a negative lags value; also computing lags dynamically (e.g. lags = n // 100) and getting 0 for short series.","commonSituations":"Auto-tuning lag counts from sample size, config typos, or defaults that assume long series applied to short ones.","solutions":["Pass a lags >= 1, commonly 10 for daily residuals or min(10, len(residuals)-1).","If lags is derived from data size, clamp it: lags = max(1, min(desired, n - 1))."],"exampleFix":"// before\nautocorrelation_test(residuals, lags=len(residuals) // 100)  # 0 for short series\n// after\nautocorrelation_test(residuals, lags=max(1, min(10, len(residuals) - 1)))","handlingStrategy":"validation","validationCode":"if not isinstance(lags, int) or lags < 1:\n    raise ValueError(f\"lags must be an int >= 1, got {lags!r}\")\nautocorrelation_test(residuals, lags=lags)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp auto-derived lag counts with max(1, ...).","Use min(10, n - 1) as a default lag rule for unknown series lengths."],"tags":["python","statistics","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}