HKUDS/Vibe-Trading · error · ValueError

factor_cov must be a non-empty DataFrame

Error message

factor_cov must be a non-empty DataFrame

What it means

factor_cov must be a non-empty square pandas DataFrame of factor covariances. A Series, ndarray, dict, or empty frame fails this isinstance/empty check.

Source

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

        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)})"
        )

    unmatched_weight = float(w_series.drop(index=assets, errors="ignore").abs().sum())
    w = w_series.loc[assets]
    X = exposures.loc[assets]

    # Align factors
    factors = X.columns.intersection(factor_cov.index).intersection(factor_cov.columns)
    if factors.empty:
        raise ValueError(

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Wrap ndarray output: pd.DataFrame(cov, index=factors, columns=factors)
  2. Ensure the factor list used for the index matches exposures.columns
  3. Fix the loader that returned an empty frame

Example fix

# before
risk = factor_risk_decomposition(w, X, np.cov(R))
# after
F = pd.DataFrame(np.cov(R), index=factors, columns=factors)
risk = factor_risk_decomposition(w, X, F)
Defensive patterns

Strategy: type-guard

Validate before calling

assert isinstance(factor_cov, pd.DataFrame) and not factor_cov.empty

Type guard

def is_cov_frame(x) -> bool:
    return (isinstance(x, pd.DataFrame) and not x.empty
            and x.shape[0] == x.shape[1])

Prevention

When it happens

Trigger: factor_cov=np.cov(returns) (ndarray), a dict of variances, or pd.DataFrame() passed as third argument.

Common situations: Covariance estimated with numpy directly instead of pandas; a loader returned {} on failure; refactor changed the return type.

Understand the failure class

Background: "Wrong argument type", "must be a string", "expected Array or Prism::Scope": TypeError and ArgumentError when a library receives a value of the wrong type — this error's family across 28 libraries.

Related errors


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