{"record":{"id":"23f6242eed79e8d6","repo":"HKUDS/Vibe-Trading","slug":"model-capital-structure-weights-must-be-non-neg","errorCode":null,"errorMessage":"{model}: capital-structure weights must be non-negative, got equity_weight={equity_weight!r} debt_weight={debt_weight!r}","messagePattern":"(.+?): capital-structure weights must be non-negative, got equity_weight=(.+?) debt_weight=(.+?)","errorType":"validation","errorClass":"ValuationError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/valuation/dcf.py","lineNumber":295,"sourceCode":"            f\"{model}: {name} must be a finite number, got {numeric!r}\"\n        )\n    return numeric\n\n\ndef _validate_weights(equity_weight: float, debt_weight: float, *, model: str) -> None:\n    \"\"\"Check that a pair of capital-structure weights is usable.\n\n    Args:\n        equity_weight: Proposed ``E / (D + E)``.\n        debt_weight: Proposed ``D / (D + E)``.\n        model: Model name for the error message.\n\n    Raises:\n        ValuationError: If either weight is negative, or if they do not sum to\n            1 within :data:`_RECONCILIATION_TOLERANCE`.\n    \"\"\"\n    if equity_weight < 0.0 or debt_weight < 0.0:\n        raise ValuationError(\n            f\"{model}: capital-structure weights must be non-negative, got \"\n            f\"equity_weight={equity_weight!r} debt_weight={debt_weight!r}\"\n        )\n    total = equity_weight + debt_weight\n    if abs(total - 1.0) > _RECONCILIATION_TOLERANCE:\n        raise ValuationError(\n            f\"{model}: capital-structure weights must sum to 1 (within \"\n            f\"{_RECONCILIATION_TOLERANCE:g}), got equity_weight={equity_weight!r} \"\n            f\"+ debt_weight={debt_weight!r} = {total!r}\"\n        )\n\n\n# ---------------------------------------------------------------------------\n# WACC\n# ---------------------------------------------------------------------------\n\n\n@dataclass(frozen=True)","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/valuation/dcf.py#L277-L313","documentation":"Raised by _validate_weights (used by WACCResult.__post_init__ and wacc) when equity_weight or debt_weight is negative. Capital-structure weights are proportions and cannot be negative.","triggerScenarios":"wacc(capital_structure_basis='target', target_equity_weight=-0.3, ...) or constructing WACCResult with a negative weight; also reachable when market values are individually valid but the derived weight computation yields a negative due to bad data.","commonSituations":"Target capital structure pulled from a spreadsheet with a negative cell; sign errors from netting short positions or liabilities into weights.","solutions":["Correct the negative weight at the source (weights are proportions in [0,1])","Validate weights with 0 <= w before calling wacc or building WACCResult","If weights come from arithmetic, unit-test the derivation against clamping/rounding"],"exampleFix":"# before\nwacc(..., target_equity_weight=-0.2, target_debt_weight=1.2)\n\n# after\nwacc(..., target_equity_weight=0.8, target_debt_weight=0.2)","handlingStrategy":"validation","validationCode":"if target_equity_weight < 0 or target_debt_weight < 0:\n    raise ValueError('weights must be non-negative')\nwacc(..., target_equity_weight=target_equity_weight, target_debt_weight=target_debt_weight)","typeGuard":"def are_valid_weights(e: float, d: float) -> bool:\n    return e >= 0 and d >= 0 and abs(e + d - 1.0) <= 1e-9","tryCatchPattern":"try:\n    wacc(...)\nexcept ValuationError as e:\n    if 'non-negative' in str(e):\n        raise DataQualityError(str(e)) from e\n    raise","preventionTips":["Validate weight signs at the spreadsheet-import boundary","Unit-test weight derivation code paths","Never allow netted/negative capital-structure values to reach weights"],"tags":["valuation","wacc","weights","input-validation"],"backgroundTag":"invalid-weight-validation","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}