Mutual Information

The Mutual Information measures how much knowing the return of one asset tells about the return of the other, from histograms of their returns, linear or not; its distance is the variation of information, the information the two do not share (Meilă, 2007; López de Prado, 2020).

The formula for the Mutual Information starts from the entropy H of each asset and the mutual information I of a pair, with \(p_{a , b}\) the share of the periods in bin a of X and bin b of Y of the joint histogram of A by B bins and \(p_{a}\), \(p_{b}\) its margins:

\[H(X) = -\sum_{a=1}^{A} p_{a} \cdot \ln\left(p_{a}\right)\] \[I(X, Y) = \sum_{a=1}^{A} \sum_{b=1}^{B} p_{a,b} \cdot \ln\left(\frac{p_{a,b}}{p_{a} \cdot p_{b}}\right)\]

The codependence normalizes the mutual information by the smaller entropy, and the distance d is the variation of information over the joint entropy H(X, Y):

\[\text{Normalized I}(X, Y) = \frac{I(X, Y)}{\min\left(H(X), H(Y)\right)}\] \[H(X, Y) = H(X) + H(Y) - I(X, Y)\] \[d = \frac{H(X) + H(Y) - 2 \cdot I(X, Y)}{H(X, Y)}\]

The normalized mutual information lies between 0 (independent) and 1, and the distance between 0 (identical) and 1 (independent). It depends on the number of bins, and with few periods per bin it overstates the dependence.

It is the codependence that the hierarchical optimizers (HRP, HERC, NCO) and get_asset_clusters take, or the distance they cluster on. Hierarchical portfolio construction groups the assets by how much they move together and spreads the capital across the groups, so the codependence decides which assets end up together. The Pearson correlation captures only linear co-movement on average; the other codependences capture co-movement in ranks, in large moves only, in any form of dependence or in crashes. Each comes with a distance, zero for assets that move as one, that the clustering works on (Mantegna, 1999).

Also known as: normalized mutual information, variation of information.

In the Finance Toolkit this is get_codependence_matrix with method="mutual_information", the same function as Codependence Matrix.

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

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

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_codependence_matrix(method="mutual_information")

Which returns:

References

  • Paper Meilă, M. (2007). “Comparing Clusterings - An Information Based Distance.” Journal of Multivariate Analysis, 98(5), 873-895.
  • Paper Hacine-Gharbi, A. and Ravier, P. (2018). “A Binning Formula of Bi-histogram for Joint Entropy Estimation Using Mean Square Error Minimization.” Pattern Recognition Letters, 101, 21-28.
  • Paper López de Prado, M. (2020). “Machine Learning for Asset Managers.” Cambridge University Press, chapters 2 and 3.
  • Library Referenced against skfolio’s MutualInformation (Mutual Information)
  • Library Referenced against Riskfolio-Lib’s mutual_info_matrix (Mutual Information, Hacine-Gharbi bins)
  • Library Shares similar logic as skfolio’s MutualInformation (Mutual Information)
  • Library Shares similar logic as Riskfolio-Lib’s var_info_matrix (Mutual Information)
Share