HKUDS/Vibe-Trading · error · ValueError

method must be 'spearman' or 'pearson', got {method!r}

Error message

method must be 'spearman' or 'pearson', got {method!r}

What it means

factor_ic_analysis computes per-date cross-sectional correlations and only supports rank (spearman) or linear (pearson) correlation; any other method string is rejected immediately.

Source

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

    Measures the predictive power and persistence of a factor by calculating
    daily/periodic cross-sectional correlations between factor scores and subsequent
    forward returns (Grinold-Kahn fundamental law framework).

    Args:
        factor_panel: DataFrame of factor scores (index = dates, columns = assets).
        forward_returns: DataFrame of forward returns (same shape and alignment; should be pre-shifted by caller).
        method: Correlation method, ``'spearman'`` (Rank IC) or ``'pearson'`` (Linear IC).
        min_cross_section: Minimum number of valid assets on a date to compute IC.

    Returns:
        :class:`FactorICResult` containing mean IC, IC IR, t-statistic, p-value,
        higher moments, and full IC time series.

    Raises:
        ValueError: If inputs are empty, share no common dates or assets, or method is unknown.
    """
    if method not in ("spearman", "pearson"):
        raise ValueError(f"method must be 'spearman' or 'pearson', got {method!r}")

    if factor_panel.empty or forward_returns.empty:
        raise ValueError("factor_panel and forward_returns must be non-empty")

    # Align dates and assets
    common_dates = factor_panel.index.intersection(forward_returns.index)
    common_assets = factor_panel.columns.intersection(forward_returns.columns)

    if common_dates.empty or common_assets.empty:
        raise ValueError("No common dates and assets between factor_panel and forward_returns")

    f_sub = factor_panel.loc[common_dates, common_assets]
    r_sub = forward_returns.loc[common_dates, common_assets]

    ic_records: dict[object, float] = {}

    for date in common_dates:
        f_row = f_sub.loc[date].dropna()

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Use exactly 'spearman' or 'pearson' lowercase
  2. Normalize config values: method=str(method).lower() before the call
  3. If you need kendall, compute it yourself per cross-section

Example fix

# before
ic = factor_ic_analysis(panel, rets, method=config['method'])
# after
ic = factor_ic_analysis(panel, rets, method=config['method'].lower())
Defensive patterns

Strategy: validation

Validate before calling

assert method in ('spearman', 'pearson')

Type guard

def is_valid_method(m: str) -> bool:
    return str(m).lower() in ('spearman', 'pearson')

Prevention

When it happens

Trigger: factor_ic_analysis(panel, rets, method='kendall') or a typo like 'Spearman' (capitalized) or 'spearmaan'.

Common situations: Case-sensitivity bug; method read from a config file with different capitalization; copy-paste from a library that allows 'kendall'.

Related errors


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