skfolio.utils.stats.cs_pearson_correlation#

skfolio.utils.stats.cs_pearson_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, optional

Non-negative weights, broadcastable to a along axis. None uses equal weights. Non-finite and zero weights are excluded from weighted correlations.

axisint, default=0

The cross-sectional axis along which correlation is computed.

min_countint, default=3

Minimum number of effective observations along axis. Without weights, this is the number of jointly finite observations. With weights, this is the number of jointly finite observations with finite strictly positive weight.

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.