quantlab.analysis.factor_correlation#

How much the factor variables of one analysis say the same thing.

The correlation of two factor variables is the Spearman rank correlation of their values across the symbols of one timestamp, averaged over timestamps, the same per-period statistic the IC uses between a factor and a forward return. Each variable is ranked once among its own finite symbols of the timestamp, and a pair’s correlation of those ranks uses the symbols finite in both; a timestamp with fewer than three such symbols is left out of that pair. When both variables cover the same symbols this is exactly Spearman’s correlation. When their coverage differs it is a close approximation, since the ranks are not recomputed on the shared symbols; that keeps hundreds of variables to a few matrix products per timestamp instead of re-ranking every pair.

The variables are clustered by the distance 1 - |mean correlation| (average linkage), so a factor and its negation fall in one cluster, and a cluster cut at threshold groups variables whose correlation is mostly at least that strong. The clustering orders the matrix, which is what makes a heatmap of hundreds of variables readable: redundant factors form blocks on the diagonal.

FactorCorrelation computes and holds the result and FactorCorrelationFigure draws it. Factor.analyze computes it whenever two or more factor variables are analyzed.

Examples#

>>> rng = np.random.default_rng(0)
>>> coords = {"timestamp": pd.date_range("2024-01-01", periods=60),
...           "symbol": [f"S{i}" for i in range(30)]}
>>> a = rng.normal(size=(60, 30))
>>> panel = xr.Dataset({"a": (("timestamp", "symbol"), a),
...                     "a_neg": (("timestamp", "symbol"), -a),
...                     "noise": (("timestamp", "symbol"), rng.normal(size=(60, 30)))},
...                    coords=coords)
>>> corr = FactorCorrelation.compute(panel)
>>> corr.mean.round(2)
       noise     a  a_neg
noise   1.00 -0.01   0.01
a      -0.01  1.00  -1.00
a_neg   0.01 -1.00   1.00

Classes

FactorCorrelation

Mean cross-sectional rank correlation between factor variables.

FactorCorrelationFigure

Draw a FactorCorrelation so that hundreds of variables stay readable.