HKUDS/Vibe-Trading · error · ValuationError

{model}: capital-structure weights must be non-negative, got

Error message

{model}: capital-structure weights must be non-negative, got equity_weight={equity_weight!r} debt_weight={debt_weight!r}

What it means

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.

Source

Thrown at agent/src/quantlib/valuation/dcf.py:295

            f"{model}: {name} must be a finite number, got {numeric!r}"
        )
    return numeric


def _validate_weights(equity_weight: float, debt_weight: float, *, model: str) -> None:
    """Check that a pair of capital-structure weights is usable.

    Args:
        equity_weight: Proposed ``E / (D + E)``.
        debt_weight: Proposed ``D / (D + E)``.
        model: Model name for the error message.

    Raises:
        ValuationError: If either weight is negative, or if they do not sum to
            1 within :data:`_RECONCILIATION_TOLERANCE`.
    """
    if equity_weight < 0.0 or debt_weight < 0.0:
        raise ValuationError(
            f"{model}: capital-structure weights must be non-negative, got "
            f"equity_weight={equity_weight!r} debt_weight={debt_weight!r}"
        )
    total = equity_weight + debt_weight
    if abs(total - 1.0) > _RECONCILIATION_TOLERANCE:
        raise ValuationError(
            f"{model}: capital-structure weights must sum to 1 (within "
            f"{_RECONCILIATION_TOLERANCE:g}), got equity_weight={equity_weight!r} "
            f"+ debt_weight={debt_weight!r} = {total!r}"
        )


# ---------------------------------------------------------------------------
# WACC
# ---------------------------------------------------------------------------


@dataclass(frozen=True)

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Correct the negative weight at the source (weights are proportions in [0,1])
  2. Validate weights with 0 <= w before calling wacc or building WACCResult
  3. If weights come from arithmetic, unit-test the derivation against clamping/rounding

Example fix

# before
wacc(..., target_equity_weight=-0.2, target_debt_weight=1.2)

# after
wacc(..., target_equity_weight=0.8, target_debt_weight=0.2)
Defensive patterns

Strategy: validation

Validate before calling

if target_equity_weight < 0 or target_debt_weight < 0:
    raise ValueError('weights must be non-negative')
wacc(..., target_equity_weight=target_equity_weight, target_debt_weight=target_debt_weight)

Type guard

def are_valid_weights(e: float, d: float) -> bool:
    return e >= 0 and d >= 0 and abs(e + d - 1.0) <= 1e-9

Try / catch

try:
    wacc(...)
except ValuationError as e:
    if 'non-negative' in str(e):
        raise DataQualityError(str(e)) from e
    raise

Prevention

When it happens

Trigger: 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.

Common situations: Target capital structure pulled from a spreadsheet with a negative cell; sign errors from netting short positions or liabilities into weights.

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/23f6242eed79e8d6. Report an issue: GitHub.