{"record":{"id":"098574d031d99336","repo":"HKUDS/Vibe-Trading","slug":"pd-must-be-in-0-0-1-0-got-pd","errorCode":null,"errorMessage":"pd must be in [0.0, 1.0], got {pd}","messagePattern":"pd must be in \\[0\\.0, 1\\.0\\], got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/credit.py","lineNumber":887,"sourceCode":"\n    Args:\n        ead: Exposure at Default in currency units >= 0.\n        pd: Probability of Default in [0.0, 1.0].\n        lgd: Loss Given Default in [0.0, 1.0].\n\n    Returns:\n        Expected loss amount in currency units.\n\n    Raises:\n        ValueError: If ead < 0, pd not in [0, 1], or lgd not in [0, 1].\n    \"\"\"\n    ead = _require_finite(ead, \"ead\")\n    pd = _require_finite(pd, \"pd\")\n    lgd = _require_finite(lgd, \"lgd\")\n    if ead < 0.0:\n        raise ValueError(f\"ead must be non-negative, got {ead}\")\n    if not (0.0 <= pd <= 1.0):\n        raise ValueError(f\"pd must be in [0.0, 1.0], got {pd}\")\n    if not (0.0 <= lgd <= 1.0):\n        raise ValueError(f\"lgd must be in [0.0, 1.0], got {lgd}\")\n    return float(ead * pd * lgd)\n\n\ndef vasicek_credit_var(\n    ead: float,\n    pd: float,\n    lgd: float,\n    asset_correlation: float,\n    confidence: float = 0.999,\n) -> dict:\n    \"\"\"Vasicek single-factor asymptotic credit risk portfolio model (Basel II/III capital framework).\n\n    Under the Asymptotic Single Risk Factor (ASRF) model, conditional default\n    probability at confidence level alpha is:\n        WCDR(alpha) = Phi( (Phi^{-1}(PD) + sqrt(rho) * Phi^{-1}(alpha)) / sqrt(1 - rho) )\n","sourceCodeStart":869,"sourceCodeEnd":905,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/credit.py#L869-L905","documentation":"expected_loss requires pd (probability of default) to be a proper probability in [0.0, 1.0]. Values outside this range are meaningless as probabilities and usually indicate a unit or model bug (e.g. basis points or percentages passed as decimals).","triggerScenarios":"Calling expected_loss with pd = 2.0, pd = -0.1, pd = 200 (basis points), or pd = 5 (percent).","commonSituations":"Passing 2 for 2% instead of 0.02; model outputs that escaped [0,1] due to numerical issues; mixing rating-scale numbers with probabilities.","solutions":["Convert units: percent/100 or bps/10000 before calling","Clip model outputs to [0,1] with np.clip(pd, 0.0, 1.0) if tiny excursions are expected","Verify rating-to-PD mapping tables produce decimals"],"exampleFix":"# before\nel = expected_loss(1_000_000, pd=200, lgd=0.6)\n\n# after\nel = expected_loss(1_000_000, pd=0.02, lgd=0.6)","handlingStrategy":"validation","validationCode":"pd = min(max(pd, 0.0), 1.0)  # clip only tiny numerical excursions\nif not 0.0 <= pd <= 1.0:\n    raise ValueError(f\"pd out of range: {pd}\")\nel = expected_loss(ead, pd, lgd)","typeGuard":"def is_valid_probability(p: float) -> bool:\n    return isinstance(p, (int, float)) and 0.0 <= float(p) <= 1.0","tryCatchPattern":"try:\n    el = expected_loss(ead, pd, lgd)\nexcept ValueError as e:\n    if 'pd' in str(e):\n        raise DataQualityError(f\"invalid PD {pd!r} — check units\") from e\n    raise","preventionTips":["Convert percent/bps to decimals at the ingestion boundary","Clip model outputs with np.clip(p, 0.0, 1.0)","Unit-test PD conversions against known rating tables"],"tags":["credit","probability-of-default","expected-loss","input-validation","python"],"backgroundTag":"argument-out-of-range","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}