HKUDS/Vibe-Trading · error · ValueError

market_caps must be positive and defined for every asset in

Error message

market_caps must be positive and defined for every asset in the regression

What it means

When market_caps is provided for weighted least squares, cross_sectional_factor_returns requires every asset in the regression sample to have a finite, strictly positive cap; NaN or non-positive weights would corrupt or drop observations silently, so it raises.

Source

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

    y = returns.loc[common].to_numpy(dtype=float)
    factor_names = list(exposures.columns)
    design = np.column_stack(
        [np.ones(len(common)), exposures.loc[common].to_numpy(dtype=float)]
    )
    names = [MARKET_FACTOR, *factor_names]

    if design.shape[1] > design.shape[0]:
        raise ValueError(
            f"{design.shape[1]} regressors but only {design.shape[0]} assets; "
            "the fit would be exactly determined and meaningless"
        )

    if market_caps is None:
        weights = np.ones(len(common))
    else:
        caps = pd.Series(market_caps, dtype=float).reindex(common)
        if caps.isna().any() or (caps <= 0).any():
            raise ValueError(
                "market_caps must be positive and defined for every asset in the "
                "regression"
            )
        weights = np.sqrt(caps.to_numpy(dtype=float))

    sqrt_w = np.sqrt(weights)
    design_w = design * sqrt_w[:, None]
    y_w = y * sqrt_w

    rank = np.linalg.matrix_rank(design_w)
    if rank < design_w.shape[1]:
        raise ValueError(
            "the exposure matrix is collinear with the market factor or with "
            "itself, so the coefficients are not identified"
        )

    coefficients, *_ = np.linalg.lstsq(design_w, y_w, rcond=None)
    fitted = design @ coefficients

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Align and clean: caps = pd.Series(market_caps).reindex(common); assert caps.notna().all() and (caps > 0).all().
  2. Replace sentinels/zeros: caps = caps.where(caps > 0).dropna(), and restrict the regression sample to assets with valid caps.

Example fix

# before
fr = cross_sectional_factor_returns(returns, exposures, market_caps=caps)  # has zeros
# after
valid = caps.reindex(returns.index).fillna(0) > 0
fr = cross_sectional_factor_returns(returns[valid], exposures[valid], market_caps=caps)
Defensive patterns

Strategy: validation

Validate before calling

caps = pd.Series(market_caps).reindex(common)
assert caps.notna().all() and (caps > 0).all()

Prevention

When it happens

Trigger: A caps Series indexed differently from the regression sample (reindex produces NaN), caps containing zeros (delisted/bankrupt names), or negative sentinel values like -1 for missing.

Common situations: Caps snapshot with -1 sentinels for missing market cap, delisted tickers carrying 0, or index/ticker mismatches between the caps frame and the returns/exposures universe.

Related errors


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