{"record":{"id":"6ff01858e31618df","repo":"HKUDS/Vibe-Trading","slug":"every-p-value-must-lie-in-0-1","errorCode":null,"errorMessage":"every p-value must lie in [0, 1]","messagePattern":"every p-value must lie in \\[0, 1\\]","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/multipletesting.py","lineNumber":407,"sourceCode":"\n    Returns:\n        An :class:`FDRResult` whose ``rejected`` and ``adjusted_p_values`` are in\n        the caller's original order.\n\n    Raises:\n        ValueError: If ``p_values`` is empty, holds a value outside ``[0, 1]``\n            or a non-finite value, or if ``fdr`` is not in ``(0, 1)``.\n    \"\"\"\n    if not 0.0 < fdr < 1.0:\n        raise ValueError(f\"fdr must be in (0, 1), got {fdr}\")\n\n    values = np.asarray(p_values, dtype=float).ravel()\n    if values.size == 0:\n        raise ValueError(\"p_values is empty\")\n    if not np.isfinite(values).all():\n        raise ValueError(\"p_values holds a non-finite value\")\n    if ((values < 0.0) | (values > 1.0)).any():\n        raise ValueError(\"every p-value must lie in [0, 1]\")\n\n    n = values.size\n    order = np.argsort(values, kind=\"stable\")\n    sorted_p = values[order]\n    ranks = np.arange(1, n + 1)\n\n    # Step-up: the largest rank whose p-value clears its own threshold, and\n    # everything below it, is rejected.\n    below = sorted_p <= (ranks / n) * fdr\n    if below.any():\n        cutoff_rank = int(ranks[below].max())\n        threshold = float(sorted_p[cutoff_rank - 1])\n    else:\n        cutoff_rank = 0\n        threshold = 0.0\n\n    rejected_sorted = ranks <= cutoff_rank\n","sourceCodeStart":389,"sourceCodeEnd":425,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/multipletesting.py#L389-L425","documentation":"benjamini_hochberg validates that every supplied p-value lies in the closed interval [0, 1]. P-values are probabilities, so any value outside that range indicates an upstream statistical bug (e.g. a malformed test statistic or a negative likelihood ratio), and the BH procedure would produce meaningless adjusted p-values. The library refuses the whole input rather than silently clipping or dropping offenders.","triggerScenarios":"Calling benjamini_hochberg(p_values=[0.01, 1.5]) or with any negative value; also p-values computed with a sign error or from an approximate formula that can exceed 1 (e.g. some tail approximations), or data-entry mistakes like passing returns or z-scores instead of p-values.","commonSituations":"Passing raw test statistics, correlation coefficients, or 1-p values by mistake; numerical p-value approximations (chi-square/Lilliefors style) that overshoot 1.0 due to floating point; feeding in percentages (0-100) instead of fractions.","solutions":["Check the upstream test that produced the p-values and fix the sign/formula error.","Clip only genuine floating-point overshoot: np.clip(p, 0.0, 1.0), but only after confirming values like 1.0000000001.","Verify you are actually passing p-values, not z-scores, ratios, or percentages.","Add a unit test asserting 0 <= p <= 1 on generated p-values before batch jobs run."],"exampleFix":"# before\nadjusted = benjamini_hochberg([0.02, -0.01, 0.5])  # raises\n\n# after\np = np.asarray(raw_p, dtype=float)\nassert np.isfinite(p).all()\np = np.clip(p, 0.0, 1.0)  # only for float overshoot\nadjusted = benjamini_hochberg(p)","handlingStrategy":"validation","validationCode":"p = np.asarray(p_values, dtype=float)\nassert p.size and np.isfinite(p).all() and ((p >= 0) & (p <= 1)).all(), 'invalid p-values'","typeGuard":"def are_valid_p_values(p) -> bool:\n    a = np.asarray(p, dtype=float)\n    return a.size > 0 and np.isfinite(a).all() and ((a >= 0.0) & (a <= 1.0)).all()","tryCatchPattern":"try:\n    res = benjamini_hochberg(p)\nexcept ValueError as e:\n    if 'p-value must lie' in str(e):\n        p = np.clip(p, 0.0, 1.0)\n        res = benjamini_hochberg(p)\n    else:\n        raise","preventionTips":["Unit-test p-value generators for the [0,1] invariant.","Never feed test statistics where p-values are expected; label columns explicitly.","Clip only documented floating-point overshoot, never negative values."],"tags":["statistics","multiple-testing","input-validation","p-value"],"backgroundTag":"argument-out-of-range","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}