skfolio.utils.stats.cs_weighted_correlation#

skfolio.utils.stats.cs_weighted_correlation(a, b, weights=None, axis=0, min_count=3, eps=1e-12)[source]#

Weighted cross-sectional Pearson correlation.

Computes the weighted Pearson correlation between a and b along axis. All other dimensions are treated as independent batch dimensions over which the computation is vectorized.

For vectors \(a\) and \(b\) with weights \(w\):

\[\rho = \frac{ \sum_n w_n \,(a_n - \bar a)\,(b_n - \bar b) }{ \sqrt{\sum_n w_n \,(a_n - \bar a)^2}\; \sqrt{\sum_n w_n \,(b_n - \bar b)^2} }\]

where \(\bar a = \sum_n w_n a_n / \sum_n w_n\) (and likewise for \(\bar b\)).

Parameters:
andarray

First array.

bndarray

Second array, broadcastable to the same shape as a.

weightsndarray or None, default=None

Non-negative weights, broadcastable to a along axis. None uses equal weights.

axisint, default=0

The cross-sectional axis along which correlation is computed.

min_countint, default=3

Minimum number of jointly finite observations along axis. Slices with fewer valid pairs return NaN.

epsfloat, default=1e-12

Denominator threshold below which NaN is returned to guard against near-constant vectors.

Returns:
corrfloat or ndarray

Scalar when inputs are 1D, otherwise an array with axis removed.