polars_online.corr.block_means

polars_online.corr.block_means(r: Any, labels: Sequence[Any]) → tuple[Any, Any][source]

Compute the mean correlation within and between labeled blocks of columns.

Use it to summarize a correlation matrix by block, such as by sector, and to test how far a block-equicorrelation matrix (from_blocks()) describes it. For one common correlation over every column, use equicorr().

Parameters

r

A k x k correlation matrix, or a Gram or a closed group’s row, read as polars_online.gram.correlation() reads it, without the intercept (the module’s docstring says why).

labels

One block label per column, of any type that compares equal: for a Gram, per column other than the intercept.

Returns

(B, counts), two arrays with one row and one column per block, in the order the labels first name the blocks. B[a, b] is the mean of r[i, j] over i in block a and j in block b, leaving out the diagonal: a column’s correlation with itself is 1 and says nothing about the block. counts[a, b] is the number of entries behind that mean: the off-diagonal entries of r in the two blocks that are finite. nan entries of r are left out of both. B[a, b] is nan where the count is 0, as within a block of one column.

Raises

ValueError

labels without one label per column, r an array that is not square, or a Gram column other than the intercept with no variance.

Examples

r = np.array([[1.0, 0.6, 0.1, 0.0],
              [0.6, 1.0, 0.2, 0.1],
              [0.1, 0.2, 1.0, 0.5],
              [0.0, 0.1, 0.5, 1.0]])
means, counts = po.corr.block_means(r, ["a", "a", "b", "b"])
print(means, counts.tolist())
[[0.6 0.1]
 [0.1 0.5]] [[2, 4], [4, 2]]

See also