如何在协方差矩阵中找到退化行/列

时间:2021-08-23 19:21:02

I am using numpy.cov to create a covariance matrix from a dataset of over 400 time series. Using linalg.det gives me a value of zero so matrix is singular. I can use linalg.svd to see that the rank is two less than the number of columns so somewhere in the covariance matrix I have some linear combinations to make the matrix degenerate. I have used corrcoef on the underlying timeseries but no correlation > 0.78 so not obvious there. Can someone suggest a method to determine the location of the degenerate columns. Thank you.

我用numpy。从超过400个时间序列的数据集创建协方差矩阵。使用linalg.det给我一个0的值,所以矩阵是单数。我可以使用linalg。svd看到秩比列数小2所以在协方差矩阵中我有一些线性组合使矩阵退化。我在底层的timeseries上使用了corrcoef但没有关联> 0。78所以这里不明显。有人能提出一种方法来确定简并列的位置吗?谢谢你!

1 个解决方案

#1


7  

If you take the QR decomposition of a matrix A, the columns of R with a non-zero value along the diagonal correspond to linearly independent columns of A.

如果你取矩阵a的QR分解,沿着对角线上的非零值的R的列对应于a的线性无关的列。


import numpy as np
linalg = np.linalg

def independent_columns(A, tol = 1e-05):
    """
    Return an array composed of independent columns of A.

    Note the answer may not be unique; this function returns one of many
    possible answers.

    http://*.com/q/13312498/190597 (user1812712)
    http://math.stackexchange.com/a/199132/1140 (Gerry Myerson)
    http://mail.scipy.org/pipermail/numpy-discussion/2008-November/038705.html
        (Anne Archibald)

    >>> A = np.array([(2,4,1,3),(-1,-2,1,0),(0,0,2,2),(3,6,2,5)])
    >>> independent_columns(A)
    np.array([[1, 4],
              [2, 5],
              [3, 6]])
    """
    Q, R = linalg.qr(A)
    independent = np.where(np.abs(R.diagonal()) > tol)[0]
    return A[:, independent]

def matrixrank(A,tol=1e-8):
    """
    http://mail.scipy.org/pipermail/numpy-discussion/2008-February/031218.html
    """
    s = linalg.svd(A,compute_uv=0)
    return sum( np.where( s>tol, 1, 0 ) )


matrices = [
    np.array([(2,4,1,3),(-1,-2,1,0),(0,0,2,2),(3,6,2,5)]),
    np.array([(1,2,3),(2,4,6),(4,5,6)]).T,
    np.array([(1,2,3,1),(2,4,6,2),(4,5,6,3)]).T,
    np.array([(1,2,3,1),(2,4,6,3),(4,5,6,3)]).T,
    np.array([(1,2,3),(2,4,6),(4,5,6),(7,8,9)]).T
    ]

for A in matrices:
    B = independent_columns(A)
    assert matrixrank(A) == matrixrank(B) == B.shape[-1]

assert matrixrank(A) == matrixrank(B) checks that the independent_columns function returns a matrix of the same rank as A.

assert matrixrank(A) == matrixrank(B)检验independence _columns函数返回与A具有相同秩的矩阵。

assert matrixrank(B) == B.shape[-1] checks that the rank of B equals the number of columns of B.

断言matrixrank(B)= = B。形状[-1]检查B的秩等于B的列数。

#1


7  

If you take the QR decomposition of a matrix A, the columns of R with a non-zero value along the diagonal correspond to linearly independent columns of A.

如果你取矩阵a的QR分解,沿着对角线上的非零值的R的列对应于a的线性无关的列。


import numpy as np
linalg = np.linalg

def independent_columns(A, tol = 1e-05):
    """
    Return an array composed of independent columns of A.

    Note the answer may not be unique; this function returns one of many
    possible answers.

    http://*.com/q/13312498/190597 (user1812712)
    http://math.stackexchange.com/a/199132/1140 (Gerry Myerson)
    http://mail.scipy.org/pipermail/numpy-discussion/2008-November/038705.html
        (Anne Archibald)

    >>> A = np.array([(2,4,1,3),(-1,-2,1,0),(0,0,2,2),(3,6,2,5)])
    >>> independent_columns(A)
    np.array([[1, 4],
              [2, 5],
              [3, 6]])
    """
    Q, R = linalg.qr(A)
    independent = np.where(np.abs(R.diagonal()) > tol)[0]
    return A[:, independent]

def matrixrank(A,tol=1e-8):
    """
    http://mail.scipy.org/pipermail/numpy-discussion/2008-February/031218.html
    """
    s = linalg.svd(A,compute_uv=0)
    return sum( np.where( s>tol, 1, 0 ) )


matrices = [
    np.array([(2,4,1,3),(-1,-2,1,0),(0,0,2,2),(3,6,2,5)]),
    np.array([(1,2,3),(2,4,6),(4,5,6)]).T,
    np.array([(1,2,3,1),(2,4,6,2),(4,5,6,3)]).T,
    np.array([(1,2,3,1),(2,4,6,3),(4,5,6,3)]).T,
    np.array([(1,2,3),(2,4,6),(4,5,6),(7,8,9)]).T
    ]

for A in matrices:
    B = independent_columns(A)
    assert matrixrank(A) == matrixrank(B) == B.shape[-1]

assert matrixrank(A) == matrixrank(B) checks that the independent_columns function returns a matrix of the same rank as A.

assert matrixrank(A) == matrixrank(B)检验independence _columns函数返回与A具有相同秩的矩阵。

assert matrixrank(B) == B.shape[-1] checks that the rank of B equals the number of columns of B.

断言matrixrank(B)= = B。形状[-1]检查B的秩等于B的列数。