Quant Memo
Coding/●●●●●

Count clusters in a correlation matrix

Asked at Two Sigma

You threshold a correlation matrix so that linked[i][j] = 1 when assets i and j are "co-moving" (correlation above your cutoff) and 0 otherwise. The matrix is symmetric with a 1 on the diagonal. Two assets belong to the same cluster if they are linked directly or through a chain of linked assets.

linked = [[1, 1, 0],
          [1, 1, 0],
          [0, 0, 1]]
-> 2      # {0, 1} and {2}

Return the number of clusters. Aim for O(n2)O(n^2).

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions