You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH: distance measures for two covariance, correlation or scatter matrices #9163
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).
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
josef-pkt commentedon Feb 23, 2024
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.