quantlab.analysis.factor_correlation.FactorCorrelation#

class quantlab.analysis.factor_correlation.FactorCorrelation(mean, std, periods, clusters, threshold)[source]#

Bases: object

Mean cross-sectional rank correlation between factor variables.

Every matrix is indexed by factor name on both axes, in the cluster order, so correlated variables sit next to each other.

Attributes:
meanpd.DataFrame

Mean over timestamps of the per-timestamp Spearman correlation. NaN for a pair that shares no usable timestamp.

stdpd.DataFrame

Standard deviation (ddof=1) of the per-timestamp correlation.

periodspd.DataFrame

Timestamps each pair was computed on.

clusterspd.Series

Cluster number of each variable, numbered 1, 2, ... along the order.

thresholdfloat

The |correlation| the clusters were cut at.

cluster_summary()[source]#

Return one row per cluster of two or more variables, largest first.

Columns are cluster, size, mean_abs_correlation (the mean |mean| over the pairs inside the cluster) and members (the variable names, in matrix order).

Examples

>>> corr.cluster_summary()
   cluster  size  mean_abs_correlation     members
0        2     2                   1.0  [a, a_neg]
cluster_table()[source]#

Return each variable’s cluster, the cluster size and its position.

Columns are factor, cluster, cluster_size and position (0-based, along the matrix order).

Examples

>>> corr.cluster_table()
  factor  cluster  cluster_size  position
0  noise        1             1         0
1      a        2             2         1
2  a_neg        2             2         2
classmethod compute(features, threshold=0.7, block_size=None)[source]#

Correlate every pair of variables of features.

Parameters:
featuresxr.Dataset

A (timestamp, symbol) panel, one variable per factor.

thresholdfloat, default 0.7

|correlation| the clusters are cut at, in (0, 1].

block_sizeint, optional

Timestamps processed at once. By default as many as keep the work arrays near 256 MiB, counting both symbols and factors; the result does not depend on it. The panel itself is never copied whole: each block is stacked on its own.

Returns:
FactorCorrelation

The matrices in cluster order.

Raises:
ValueError

If features has fewer than two variables or threshold is not in (0, 1].

Examples

>>> corr = FactorCorrelation.compute(panel, threshold=0.7)
>>> corr.clusters.to_dict()
{'noise': 1, 'a': 2, 'a_neg': 2}
pairs_table()[source]#

Return one row per pair of variables, strongest |mean| first.

Columns are factor_a, factor_b, mean, std, periods and same_cluster. Pairs with no usable timestamp come last.

Examples

>>> corr.pairs_table()[["factor_a", "factor_b", "mean"]].round(2)
  factor_a factor_b  mean
0        a    a_neg -1.00
1    noise        a -0.01
2    noise    a_neg  0.01
property summary#

Counts that describe the matrix at a glance.

n_factors; n_clusters; n_pairs_above_threshold, the pairs whose |mean| reaches threshold; mean_abs_correlation over all pairs; and threshold.

Examples

>>> corr.summary["n_clusters"], corr.summary["n_pairs_above_threshold"]
(2, 1)