Skip to content

API Reference parrot_tools.quant.correlation

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.quant.correlation

View source

Correlation Engine for QuantToolkit.

Provides correlation analysis for portfolio risk monitoring:

  • Correlation matrix computation (Pearson, Spearman, Kendall)
  • Correlation regime detection (short vs long-term shifts)
  • Cross-asset correlation with calendar alignment

CRITICAL: Always correlate on returns, NOT prices. Correlating prices gives spurious correlations due to random walk behavior.

Alias CorrelationInput

Re-exported from parrot_tools.quant.models.CorrelationInput.

Function compute_correlation_from_input

Compute correlation matrix from CorrelationInput model.

compute_correlation_from_input(inp: CorrelationInput) -> dict

Parameter Type Default Description
inp CorrelationInput CorrelationInput with price_data, method, and returns_based flag.

Returns: dict — Dictionary with correlation matrix and metadata.

Function compute_correlation_matrix

Compute correlation matrix for multiple assets.

IMPORTANT: Always correlate returns, not prices. Correlating prices gives spurious correlations due to random walk behavior.

compute_correlation_matrix(price_data: dict[str, list[float]], method: Literal['pearson', 'spearman', 'kendall'] = 'pearson', returns_based: bool = True) -> dict

Parameter Type Default Description
price_data dict[str, list[float]] Dictionary of {symbol: [prices]}.
method Literal['pearson', 'spearman', 'kendall'] 'pearson' Correlation method ('pearson', 'spearman', 'kendall').
returns_based bool True If True, convert prices to returns first (recommended).

Returns: dict — Dictionary with: - matrix: Nested dict {symbol: {symbol: correlation}} - method: The method used - returns_based: Whether returns were used

Function compute_cross_asset_correlation

Compute correlation between equities (252 trading days) and crypto (365 days).

Aligns on common dates before computing correlation.

compute_cross_asset_correlation(equity_prices: dict[str, list[float]], crypto_prices: dict[str, list[float]], timestamps_equity: list[str], timestamps_crypto: list[str], alignment: str = 'daily_close') -> dict

Parameter Type Default Description
equity_prices dict[str, list[float]] Dictionary of {symbol: [prices]} for equities.
crypto_prices dict[str, list[float]] Dictionary of {symbol: [prices]} for crypto.
timestamps_equity list[str] List of date strings for equity prices.
timestamps_crypto list[str] List of date strings for crypto prices.
alignment str 'daily_close' Alignment method (currently only 'daily_close' supported).

Returns: dict — Dictionary with: - cross_asset_correlations: {pair: correlation} - full_matrix: Complete correlation matrix - common_dates_count: Number of overlapping dates - alignment: Alignment method used

Raises:

  • ValueError — If insufficient overlapping dates.

Function compute_pairwise_correlation

Compute correlation between two return series.

compute_pairwise_correlation(returns_a: list[float], returns_b: list[float], method: Literal['pearson', 'spearman', 'kendall'] = 'pearson') -> float

Parameter Type Default Description
returns_a list[float] First return series.
returns_b list[float] Second return series.
method Literal['pearson', 'spearman', 'kendall'] 'pearson' Correlation method.

Returns: float — Correlation coefficient.

Raises:

  • ValueError — If series have different lengths.

Function compute_rolling_correlation

Compute rolling correlation between two return series.

compute_rolling_correlation(returns_a: list[float], returns_b: list[float], window: int = 20) -> np.ndarray

Parameter Type Default Description
returns_a list[float] First return series.
returns_b list[float] Second return series.
window int 20 Rolling window size.

Returns: np.ndarray — Array of rolling correlations.

Raises:

  • ValueError — If series have different lengths.

Function detect_correlation_regimes

Compare short-term vs long-term correlations to detect regime changes.

This directly serves the risk crew instruction: "Flag when correlations deviate >2 std from historical norm"

detect_correlation_regimes(price_data: dict[str, list[float]], short_window: int = 20, long_window: int = 120, z_threshold: float = 2.0) -> dict

Parameter Type Default Description
price_data dict[str, list[float]] Dictionary of {symbol: [prices]}.
short_window int 20 Recent window for short-term correlation.
long_window int 120 Historical window for long-term baseline.
z_threshold float 2.0 Standard deviation threshold for alerts.

Returns: dict — Dictionary with: - regime_alerts: List of {pair, short_corr, long_corr, z_score, alert} - correlation_matrix_short: Short-term correlation matrix - correlation_matrix_long: Long-term correlation matrix

Raises:

  • ValueError — If insufficient data points.

Function get_correlation_heatmap_data

Get correlation data formatted for heatmap visualization.

get_correlation_heatmap_data(price_data: dict[str, list[float]], method: Literal['pearson', 'spearman', 'kendall'] = 'pearson') -> dict

Parameter Type Default Description
price_data dict[str, list[float]] Dictionary of {symbol: [prices]}.
method Literal['pearson', 'spearman', 'kendall'] 'pearson' Correlation method.

Returns: dict — Dictionary with: - symbols: List of symbols - correlations: 2D list for heatmap - method: Method used

Function prices_to_returns

Convert price series to returns.

prices_to_returns(prices: np.ndarray) -> np.ndarray

Parameter Type Default Description
prices np.ndarray Array of closing prices.

Returns: np.ndarray — Array of daily returns (pct_change).

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally