{"record":{"id":"95b403cbbbc01690","repo":"HKUDS/Vibe-Trading","slug":"market-caps-must-be-positive-and-defined-for-every","errorCode":null,"errorMessage":"market_caps must be positive and defined for every asset in the regression","messagePattern":"market_caps must be positive and defined for every asset in the regression","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/factormodel.py","lineNumber":431,"sourceCode":"    y = returns.loc[common].to_numpy(dtype=float)\n    factor_names = list(exposures.columns)\n    design = np.column_stack(\n        [np.ones(len(common)), exposures.loc[common].to_numpy(dtype=float)]\n    )\n    names = [MARKET_FACTOR, *factor_names]\n\n    if design.shape[1] > design.shape[0]:\n        raise ValueError(\n            f\"{design.shape[1]} regressors but only {design.shape[0]} assets; \"\n            \"the fit would be exactly determined and meaningless\"\n        )\n\n    if market_caps is None:\n        weights = np.ones(len(common))\n    else:\n        caps = pd.Series(market_caps, dtype=float).reindex(common)\n        if caps.isna().any() or (caps <= 0).any():\n            raise ValueError(\n                \"market_caps must be positive and defined for every asset in the \"\n                \"regression\"\n            )\n        weights = np.sqrt(caps.to_numpy(dtype=float))\n\n    sqrt_w = np.sqrt(weights)\n    design_w = design * sqrt_w[:, None]\n    y_w = y * sqrt_w\n\n    rank = np.linalg.matrix_rank(design_w)\n    if rank < design_w.shape[1]:\n        raise ValueError(\n            \"the exposure matrix is collinear with the market factor or with \"\n            \"itself, so the coefficients are not identified\"\n        )\n\n    coefficients, *_ = np.linalg.lstsq(design_w, y_w, rcond=None)\n    fitted = design @ coefficients","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/factormodel.py#L413-L449","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Align and clean: caps = pd.Series(market_caps).reindex(common); assert caps.notna().all() and (caps > 0).all().","Replace sentinels/zeros: caps = caps.where(caps > 0).dropna(), and restrict the regression sample to assets with valid caps."],"exampleFix":"# before\nfr = cross_sectional_factor_returns(returns, exposures, market_caps=caps)  # has zeros\n# after\nvalid = caps.reindex(returns.index).fillna(0) > 0\nfr = cross_sectional_factor_returns(returns[valid], exposures[valid], market_caps=caps)","handlingStrategy":"validation","validationCode":"caps = pd.Series(market_caps).reindex(common)\nassert caps.notna().all() and (caps > 0).all()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat -1/0 cap sentinels as missing and clean at ingestion.","Restrict the sample to assets with valid caps before regressing."],"tags":["factormodel","market-caps","wls"],"backgroundTag":"invalid-weight-values","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}