Skip to content

ENH: distance measures for two covariance, correlation or scatter matrices #9163

Open

Description

@josef-pkt

I'm running into various measure for the closeness of two covariance or scatter matrices.

We should get a function with a collection of those.

current usecase: checking how well a robust cov or scatter estimate estimates the true/dgp covariance or how it compares to standard pearson covariance in the clean gaussian case.

The one I usually use is mse (Frobenious norm of difference cov1 - cov2 divided by nobs).
Others are Kullback-Leibler distance, or some on range of eigenvalues or trace of cov1^{-1} @ cov2.
Note KL and some of the others are not "norms", e.g. symmetry may not hold.

Other options should be to compare shapes only, i.e. scatter matrices with arbitrary scale (e.g. standardize scaling to satisfy trace or determinant constraint).

https://stats.stackexchange.com/questions/14673/measures-of-similarity-or-distance-between-two-covariance-matrices

e.g. based on second answer on stats.stackexchange
from https://www.researchgate.net/publication/4194743_Correlation_Matrix_Distance_a_Meaningful_Measure_for_Evaluation_of_Non-Stationary_MIMO_Channels equation (4)

not subtracting means shows smaller distance than using np.corrcoeff
applied to x1, x2 being raveled covariance estimates.

Note: corrdist is zero if two cov are the same except for a scale factor, i.e. should be fine for scatter and shape matrices.

def corrdist(x1, x2):
    """correlation distance between two vectors

    This correspond to one minus the correlation coefficient under the assumption that means are zero.
    """
    if x1.ndim !=1 or x2.ndim !=1:
        raise ValueError("data should be 1-dimensional")
    cross = x1.T @ x2 
    s1 = (x1**2).sum(0)
    s2 = (x1**2).sum(0)
    cmd = 1 - cross / np.sqrt(s1 * s2)
    return cmd

Activity

  1. josef-pkt commented on Feb 23, 2024

    @josef-pkt
    MemberAuthor

    Another reference in the stackexchange answers might also be interesting for looking at where the differences are
    https://bmcecolevol.biomedcentral.com/articles/10.1186/1471-2148-12-222#CR21_2258

    compares principal components based on own and foreign covariance eigenvalue decomposition, AFAICS based on brief look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions