HKUDS/Vibe-Trading · error · ValueError

spread_bps must be non-negative, got {spread_bps}

Error message

spread_bps must be non-negative, got {spread_bps}

What it means

cds_price prices a credit default swap from its par spread; a negative spread would imply the protection buyer is paid to buy protection, which is economically invalid in this model. The function rejects negative spreads before computing the hazard rate approximation s/LGD.

Source

Thrown at agent/src/quantlib/credit.py:802

            * ``protection_leg_pv`` (float): Present value of default protection per dollar notional.
            * ``premium_leg_pv`` (float): Present value of fixed running premium per dollar notional.
            * ``par_spread_bps`` (float): Model par spread in basis points.
            * ``upfront_pct`` (float): Upfront payment as decimal fraction of notional.
            * ``upfront_amount`` (float): Net upfront cash payment (positive = buyer pays seller).
            * ``buyer_mtm`` (float): Mark-to-market value for the protection buyer.

    Raises:
        ValueError: If spread_bps < 0, recovery_rate not in [0, 1), tenor_years <= 0, or notional <= 0.
    """
    spread_bps = _require_finite(spread_bps, "spread_bps")
    recovery_rate = _require_finite(recovery_rate, "recovery_rate")
    tenor_years = _require_finite(tenor_years, "tenor_years")
    risk_free_rate = _require_finite(risk_free_rate, "risk_free_rate")
    coupon_bps = _require_finite(coupon_bps, "coupon_bps")
    notional = _require_finite(notional, "notional")
    payment_frequency = _require_finite(payment_frequency, "payment_frequency")
    if spread_bps < 0.0:
        raise ValueError(f"spread_bps must be non-negative, got {spread_bps}")
    if not (0.0 <= recovery_rate < 1.0):
        raise ValueError(f"recovery_rate must be in [0.0, 1.0), got {recovery_rate}")
    if tenor_years <= 0.0:
        raise ValueError(f"tenor_years must be strictly positive, got {tenor_years}")
    if notional <= 0.0:
        raise ValueError(f"notional must be strictly positive, got {notional}")
    if payment_frequency <= 0:
        raise ValueError(f"payment_frequency must be positive, got {payment_frequency}")

    s_dec = spread_bps / 10_000.0
    c_dec = coupon_bps / 10_000.0
    lgd = 1.0 - recovery_rate

    # Implied hazard rate lambda ≈ s / LGD
    lambda_hazard = float(s_dec / lgd) if lgd > 0 else 0.0

    n_periods = max(1, int(round(tenor_years * payment_frequency)))
    t_grid = np.linspace(tenor_years / n_periods, tenor_years, n_periods)

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Verify the sign of your spread input; use abs() only if the sign flip is a known data convention
  2. Confirm the unit is basis points (250 = 2.5%), not decimal or percent
  3. Sanitize market data feeds to clamp or flag negative spreads

Example fix

# before
pv = cds_price(spread_bps=-250, tenor_years=5)

# after
pv = cds_price(spread_bps=250, tenor_years=5)
Defensive patterns

Strategy: validation

Validate before calling

if spread_bps < 0.0:
    spread_bps = abs(spread_bps)  # or raise/log
pv = cds_price(spread_bps, tenor_years=5.0)

Type guard

def is_valid_spread_bps(s: float) -> bool:
    return isinstance(s, (int, float)) and math.isfinite(s) and float(s) >= 0.0

Try / catch

try:
    pv = cds_price(spread, 5.0)
except ValueError as e:
    logger.error("cds_price rejected spread %s: %s", spread, e)
    pv = None

Prevention

When it happens

Trigger: Calling cds_price(spread_bps=-100, ...) or with any negative spread value in basis points.

Common situations: Sign errors when computing spreads from bond prices; passing a decimal (e.g. -0.01) or a percentage where bps are expected; data glitches in market feed pipelines producing negative quotes.

Related errors


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