HKUDS/Vibe-Trading · error · ValueError

confidence must be in (0, 1), got {confidence}

Error message

confidence must be in (0, 1), got {confidence}

What it means

_validate_confidence enforces that the confidence level is a strict probability, 0 < confidence < 1. Confidence is interpreted as a quantile level (e.g. 0.95 for 95% VaR), so 0, 1, negative values, or percentages like 95 would produce meaningless quantiles and are rejected.

Source

Thrown at agent/src/quantlib/risk.py:109

        raise ValueError(f"returns must be 1-D, got shape {values.shape}")
    values = values.ravel()
    finite = values[np.isfinite(values)]
    if finite.size == 0:
        raise ValueError("returns contains no finite observation")
    return finite


def _validate_confidence(confidence: float) -> None:
    """Check that a confidence level is a strict probability.

    Args:
        confidence: Confidence level, e.g. 0.95.

    Raises:
        ValueError: If ``confidence`` is not strictly between 0 and 1.
    """
    if not 0.0 < confidence < 1.0:
        raise ValueError(f"confidence must be in (0, 1), got {confidence}")


def _validate_horizon(horizon: int) -> None:
    """Check that a holding period is a positive whole number of periods.

    Args:
        horizon: Holding period in periods (days for a daily return series).

    Raises:
        ValueError: If ``horizon`` is less than 1.
    """
    if horizon < 1:
        raise ValueError(f"horizon must be >= 1, got {horizon}")


def _tail_index(n: int, confidence: float) -> int:
    """Position of the VaR order statistic in an ascending-sorted sample.

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Pass the fraction: use 0.95, not 95
  2. If the value comes from config as a percent, divide by 100 before the call
  3. Add an assertion or unit test on config values in (0,1)

Example fix

// before
var = historical_var(returns, confidence=95)
// after
var = historical_var(returns, confidence=0.95)
Defensive patterns

Strategy: validation

Validate before calling

def ok_confidence(c):
    return isinstance(c, (int, float)) and 0.0 < c < 1.0
assert ok_confidence(confidence)

Type guard

def is_strict_probability(c) -> bool:
    return isinstance(c, (int, float)) and not isinstance(c, bool) and 0.0 < float(c) < 1.0

Try / catch

try:
    var = historical_var(r, confidence)
except ValueError as e:
    if "confidence must be in" in str(e):
        confidence = min(max(confidence / 100 if confidence > 1 else 0.95, 1e-12), 1 - 1e-12)
    else:
        raise

Prevention

When it happens

Trigger: historical_var(r, confidence=95) (passing percent instead of fraction), confidence=0.0, confidence=1.0, or a negative value; also any default misconfigured in a config file as 95 instead of 0.95.

Common situations: Config files or UI dropdowns that express confidence as an integer percentage; copy-pasted code from libraries that accept 95 (e.g. some VaR toolkits) into this one which expects 0.95.

Related errors


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