rheon.corr.block_means¶
- rheon.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, useequicorr().Parameters
rA
k x kcorrelation matrix, or a Gram or a closed group’s row, read asrheon.gram.correlation()reads it, without the intercept (the module’s docstring says why).labelsOne 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 ofr[i, j]overiin blockaandjin blockb, 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 ofrin the two blocks that are finite.nanentries ofrare left out of both.B[a, b]isnanwhere the count is 0, as within a block of one column.Raises
ValueErrorlabelswithout one label per column,ran 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 = rp.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
from_blocks(), the matrix the block means describe.equicorr(), one mean over every column.