HKUDS/Vibe-Trading · error · ValueError

portfolio_weights contains non-finite values

Error message

portfolio_weights contains non-finite values

What it means

factor_risk_decomposition validates that all weights are finite; NaN or ±inf weights make variance wᵀXF Xᵀw undefined, so np.isfinite fails and the error is raised.

Source

Thrown at agent/src/quantlib/factormodel.py:636

        exposures: Asset factor exposures (rows = assets, columns = factors).
        factor_cov: Covariance matrix of factor returns (K x K).
        specific_variances: Asset-specific (idiosyncratic) return variances.
            Defaults to zero if omitted.

    Returns:
        :class:`FactorRiskDecomposition` containing total/factor/specific
        variances, volatilities, marginal contributions to risk (MCR), and
        percentage contributions to risk (PCR) per factor and per asset.

    Raises:
        ValueError: If weights or matrices are empty, contain non-finite values,
            or share no common assets or factors.
    """
    w_series = pd.Series(portfolio_weights, dtype=float)
    if w_series.empty:
        raise ValueError("portfolio_weights cannot be empty")
    if not np.isfinite(w_series.values).all():
        raise ValueError("portfolio_weights contains non-finite values")

    if not isinstance(exposures, pd.DataFrame) or exposures.empty:
        raise ValueError("exposures must be a non-empty DataFrame")
    if not np.isfinite(exposures.values).all():
        raise ValueError("exposures contains non-finite values")

    if not isinstance(factor_cov, pd.DataFrame) or factor_cov.empty:
        raise ValueError("factor_cov must be a non-empty DataFrame")
    if not np.isfinite(factor_cov.values).all():
        raise ValueError("factor_cov contains non-finite values")

    # Align assets
    assets = w_series.index.intersection(exposures.index)
    if assets.empty:
        raise ValueError(
            f"No matching assets between weights ({sorted(w_series.index)}) and exposures ({sorted(exposures.index)})"
        )

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Print w_series[~np.isfinite(w_series)] to find the offending asset
  2. Fill or drop bad weights: .fillna(0) or .replace([np.inf,-np.inf],0).dropna()
  3. Fix the NAV normalization that produced inf

Example fix

# before
risk = factor_risk_decomposition(w, X, F)
# after
w = w.replace([np.inf, -np.inf], np.nan).dropna()
risk = factor_risk_decomposition(w, X, F)
Defensive patterns

Strategy: validation

Validate before calling

assert np.isfinite(pd.Series(portfolio_weights, dtype=float).values).all()

Prevention

When it happens

Trigger: A weight value is NaN (missing join) or inf (division by a near-zero NAV when normalizing).

Common situations: Weights computed as positions/NAV where NAV was 0; NaN introduced by a reindex/merge on tickers; dirty CSV holdings.

Related errors


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