Asset Clusters

Group the assets into clusters of assets that move together, the clusters that HERC and NCO allocate across.

  • Hierarchical clustering: a hierarchical clustering on the distance of the codependence (see get_codependence_matrix), cut into the given number of clusters or, without a number, at the sharpest bend of the within-cluster dispersion (the two-order difference of the gap statistic, Yue, Wang and Wei, 2008), as HERC and NCO do, or at the highest standardized silhouette (López de Prado, 2020).
  • FTCA: the Fast Threshold Clustering Algorithm of Varadi (2013). Among the assets not yet clustered, take the one with the highest average codependence (H) and the lowest (L). If H and L are codependent above the threshold, they form a cluster with every remaining asset whose average codependence with H and L is above it; otherwise H forms a cluster with the remaining assets above the threshold with H, and then L with those above it with L. This repeats until every asset belongs to a cluster. It needs no number of clusters.
  • ONC: the Optimal Number of Clusters algorithm of López de Prado and Lewis (2019). Every asset is the point given by its row of the distance matrix. For \(k = 2\) to N - 1 clusters, k-means runs ten times (from k-means++ starts drawn with the seed), and the clustering with the highest quality \(E [S] / \sqrt{V} [S]\) of the silhouette coefficients \(S_{i} = (b_{i} - a_{i}) / \max (a_{i} , b_{i})\) is kept. The clusters of below-average quality are then clustered again on their own, and their new clusters kept when they raise the average quality above that of the clusters they replace (López de Prado, 2020, snippets 4.1 and 4.2). It finds the number of clusters itself, needs no tree and suits uneven groups of assets.

Also known as: asset clustering, FTCA, correlation clusters, ONC, optimal number of clusters, k-means clustering.

Example · AAPL, MSFT, JNJ, XOM · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_asset_clusters:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["AAPL", "MSFT", "JNJ", "XOM"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.portfolio.get_asset_clusters(method="ftca")

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

Into which clusters does the fast threshold clustering algorithm group Apple, Microsoft, Johnson & Johnson and ExxonMobil, based on 2021 to 2025?

Which returns:

ItemCluster
AAPL1
MSFT1
JNJ2
XOM3

4 rows

Parameters

get_asset_clusters accepts the following parameters.

ParameterTypeDescriptionExample
methodstrOptions are: “hierarchical” (hierarchical clustering), “ftca” (Fast Threshold Clustering Algorithm), “onc” (Optimal Number of Clusters). Defaults to “hierarchical”."hierarchical"
periodstrThe frequency of the returns. Options are: “daily” (daily returns), “weekly” (weekly returns), “monthly” (monthly returns), “quarterly” (quarterly returns), “yearly” (yearly returns). Defaults to “daily”."weekly"
codependencestrThe codependence the assets are clustered on (see get_codependence_matrix). Defaults to. Options are: “pearson” (the linear correlation of the covariance matrix), “spearman” (the rank correlation of the returns, Spearman, 1904), “kendall” (the rank correlation \(\tau_{b}\) of the returns, Kendall, 1938), “gerber” (co-movements beyond half a standard deviation, Gerber et al., 2022), “detoned” (the correlation without the market component, López de Prado, 2020), “distance” (the distance correlation, Székely et al., 2007, through √(1 - dCor)), “mutual_information” (through the variation of information, Meilă, 2007), “lower_tail” (how often two assets fall into their lowest 5% of returns together, through -ln \(\lambda\)), “absolute_angular” (\(\vert \rho \vert\) through \(\sqrt{1 - \vert \rho \vert}\), López de Prado, 2020), “squared_angular” (\(\rho^{2}\) through \(\sqrt{1 - \rho^{2}}\), López de Prado, 2020), “gnpr” (the rank co-movement and the difference between the return distributions, \(\theta = 0.5\), Marti et al., 2016). Defaults to “pearson”."pearson"
covariancestrThe covariance estimator whose correlation the Pearson codependence is. Options are: “sample” (sample covariance), “ledoit_wolf” (Ledoit-Wolf), “ledoit_wolf_constant_correlation” (Ledoit-Wolf constant correlation), “ledoit_wolf_single_factor” (Ledoit-Wolf single factor), “oas” (Oracle Approximating Shrinkage), “shrunk” (fixed shrinkage), “semicovariance” (semicovariance), “exponential” (exponentially weighted), “denoised” (denoised, constant residual eigenvalue), “denoised_spectral” (denoised, spectral), “denoised_shrink” (denoised, targeted shrinkage), “gerber” (Gerber), “factor” (factor model), “minimum_covariance_determinant” (Minimum Covariance Determinant), “jlogo” (j-LoGo), “geodesic” (geodesic shrinkage), “regime_adjusted_ew” (regime adjusted exponentially weighted), “nercome” (NERCOME), “ccc_garch” (CCC-GARCH), “mm” (MM-estimator), “rocke” (Rocke’s S-estimator). Defaults to “sample”."sample"
linkage_methodstrThe linkage of the hierarchical clustering. Options are: “single” (single linkage), “complete” (complete linkage), “average” (average linkage), “ward” (Ward’s method), “dbht” (the Directed Bubble Hierarchical Tree of Song, Di Matteo and Aste, 2012, which finds the number of clusters itself in the TMFG planar graph of the codependence). Defaults to “ward”."ward"
clustersint | NoneThe number of clusters of the hierarchical clustering, or the most clusters ONC tries. Defaults to None, chosen by the two-order difference of the gap statistic (by the DBHT itself for its linkage; up to N - 1 for ONC).None, chosen by the two-order difference of the gap statistic (by the DBHT itself for its linkage; up to N - 1 for ONC)
cluster_criterionstrHow the hierarchical clustering chooses the number of clusters when it is not given. Options are: “two_difference_gap” (the two-order difference of the gap statistic), “standardized_silhouette” (the highest mean over standard deviation of the silhouette coefficients). Defaults to “two_difference_gap”."two_difference_gap"
thresholdfloatThe codependence above which FTCA puts assets in one cluster. Defaults to 0.5.0.5
seedint | NoneThe seed of the k-means starts of ONC (numpy’s default generator), so the same returns give the same clusters. Defaults to 0.42
include_benchmarkboolWhether to include the benchmark as an asset. Defaults to False.True
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper Varadi, D. (2013). “The Fast Threshold Clustering Algorithm (FTCA).” CSS Analytics.
  • Paper López de Prado, M. and Lewis, M. J. (2019). “Detection of False Investment Strategies Using Unsupervised Learning Methods.” Quantitative Finance, 19(9), 1555-1565.
  • Paper Yue, S., Wang, X. and Wei, M. (2008). “Application of Two-Order Difference to Gap Statistic.” Transactions of Tianjin University, 14(3), 217-221.
  • Paper López de Prado, M. (2020). “Machine Learning for Asset Managers.” Cambridge University Press.
  • Library Referenced against skfolio’s HierarchicalClustering (hierarchical)
  • Library Referenced against ffn’s calc_ftca
  • Library Shares similar logic as RiskLabAI’s cluster_k_means_top (López de Prado, Machine Learning for Asset Managers, snippet 4.2)
Share