{"record":{"id":"46a939a935146cf9","repo":"HKUDS/Vibe-Trading","slug":"survival-prob-must-be-in-0-0-1-0-got-survival","errorCode":null,"errorMessage":"survival_prob must be in (0.0, 1.0], got {survival_prob}","messagePattern":"survival_prob must be in \\(0\\.0, 1\\.0\\], got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/credit.py","lineNumber":741,"sourceCode":"\n\ndef survival_probability_to_hazard_rate(survival_prob: float, tenor_years: float) -> float:\n    \"\"\"Convert a survival probability Q(T) to implied constant hazard rate lambda = -ln(Q(T)) / T.\n\n    Args:\n        survival_prob: Survival probability in (0.0, 1.0].\n        tenor_years: Time horizon in years > 0.\n\n    Returns:\n        Annualised hazard rate lambda.\n\n    Raises:\n        ValueError: If survival_prob is not in (0.0, 1.0] or tenor_years <= 0.\n    \"\"\"\n    survival_prob = _require_finite(survival_prob, \"survival_prob\")\n    tenor_years = _require_finite(tenor_years, \"tenor_years\")\n    if survival_prob <= 0.0 or survival_prob > 1.0:\n        raise ValueError(f\"survival_prob must be in (0.0, 1.0], got {survival_prob}\")\n    if tenor_years <= 0.0:\n        raise ValueError(f\"tenor_years must be strictly positive, got {tenor_years}\")\n    return float(-np.log(survival_prob) / tenor_years)\n\n\ndef cds_price(\n    spread_bps: float,\n    recovery_rate: float = 0.40,\n    tenor_years: float = 5.0,\n    risk_free_rate: float = 0.03,\n    coupon_bps: float = 100.0,\n    notional: float = 10_000_000.0,\n    payment_frequency: int = 4,\n) -> dict:\n    \"\"\"Flat-hazard single-name Credit Default Swap (CDS) valuation engine.\n\n    Computes the implied hazard rate, survival probability curve, Risky Present Value\n    of a Basis Point (RPV01), protection leg PV, premium leg PV, fair par spread,","sourceCodeStart":723,"sourceCodeEnd":759,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/credit.py#L723-L759","documentation":"survival_probability_to_hazard_rate converts a survival probability into a constant hazard rate via -ln(S)/T. The survival probability is a probability and only has meaning in (0.0, 1.0]: 0 implies certain default (infinite hazard), values above 1 are impossible, and negative values are nonsensical. The library validates this before taking the logarithm to avoid NaN/inf results.","triggerScenarios":"Calling survival_probability_to_hazard_rate(survival_prob, tenor_years) with survival_prob <= 0.0 or > 1.0, e.g. 0.0, -0.2, 1.5, or a percentage like 95.0 instead of 0.95.","commonSituations":"Passing percentages (95.0) instead of decimals (0.95); passing a hazard rate where a survival probability was expected; chained computations that underflow to exactly 0.0 for long horizons.","solutions":["Check that survival_prob is a decimal in (0.0, 1.0], not a percentage","If the value comes from exp(-hazard*T), clamp tiny underflow results to a small epsilon like 1e-16","Verify you are not passing a probability of default (PD) where survival probability S = 1 - PD is required"],"exampleFix":"# before\nh = survival_probability_to_hazard_rate(95.0, 5.0)\n\n# after\nh = survival_probability_to_hazard_rate(0.95, 5.0)","handlingStrategy":"validation","validationCode":"if not (0.0 < survival_prob <= 1.0):\n    raise ValueError(f\"invalid survival_prob: {survival_prob}\")\nh = survival_probability_to_hazard_rate(survival_prob, tenor_years)","typeGuard":"def is_valid_survival_prob(x: float) -> bool:\n    return isinstance(x, (int, float)) and 0.0 < float(x) <= 1.0","tryCatchPattern":"try:\n    h = survival_probability_to_hazard_rate(sp, t)\nexcept ValueError as e:\n    logger.warning(\"bad survival probability input: %s\", e)\n    h = float('nan')","preventionTips":["Store probabilities as decimals, never percentages, throughout the codebase","Clamp exp(-lambda*t) outputs to [1e-16, 1.0] to avoid underflow to exactly 0","Add unit tests for boundary values 0.0 and 1.0"],"tags":["credit","survival-probability","hazard-rate","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"}