协方差#

概述#

对于数据载体, \({\mathbf x} = \{x_0, x_1, ...\}\) ,任何两个元素之间的协方差 \(x_i\)\(x_j\) 定义的元素 covariance matrix

\[\Sigma_{ij} = \rho_{ij} \sigma_i \sigma_j,\]

哪里 \(\rho_{ij}\)correlation matrix\(V_i \equiv \sigma^2_i\) 是方差, \(x_i\) . 根据定义,协方差矩阵是对称的和半正定的(所有特征值都是非负的)。

Covariance 对象是用于构建、可视化和存储二维协方差矩阵的通用工具。 为了最小化其内存占用,此类使用稀疏矩阵(即,该模块要求 scipy.sparse )并且仅存储协方差矩阵的上三角形。

班级提供两个便利 static 在完整协方差矩阵之间交换的方法 (revert_correlation )以及方差矩阵和相关矩阵的组合 (to_correlation ).

介绍性例子#

As a general introduction to covariance matrices, let \({\mathbf x}\) contain 10 measurements. Let the correlation coefficient between adjacent measurements be 0.5 (\(\rho_{ij} = 0.5\ {\rm for}\ |j-i| = 1\)), 0.2 for next but one measurements (\(\rho_{ij} = 0.2\ {\rm for}\ |j-i| = 2\)), and 0 otherwise. If we adopt unity variance for all elements of \({\mathbf x}\), we can directly construct the (banded) covariance matrix in python as follows:

>>> import numpy as np
>>>
>>> # Create the covariance matrix as a dense array
>>> npts = 10
>>> c = (np.diag(np.full(npts-2, 0.2, dtype=float), k=-2)
...         + np.diag(np.full(npts-1, 0.5, dtype=float), k=-1)
...         + np.diag(np.full(npts, 1.0, dtype=float), k=0)
...         + np.diag(np.full(npts-1, 0.5, dtype=float), k=1)
...         + np.diag(np.full(npts-2, 0.2, dtype=float), k=2))
>>> c
array([[1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. ],
       [0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. ],
       [0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. ],
       [0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. ],
       [0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. ],
       [0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2],
       [0. , 0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5],
       [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. ]])

在这种情况下,相关矩阵和协方差矩阵是 identical 因为方差载体的元素都是单位。

通过相关矩阵,我们可以构建具有任意方差矩阵的协方差矩阵。 继续上面的例子,下面创建了一个新的协方差矩阵,其方差矩阵的所有元素都等于4:

>>> new_var = np.full(npts, 4., dtype=float)
>>> new_c = c * np.sqrt(new_var[:,None] * new_var[None,:])
>>> new_c
array([[4. , 2. , 0.8, 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
       [2. , 4. , 2. , 0.8, 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.8, 2. , 4. , 2. , 0.8, 0. , 0. , 0. , 0. , 0. ],
       [0. , 0.8, 2. , 4. , 2. , 0.8, 0. , 0. , 0. , 0. ],
       [0. , 0. , 0.8, 2. , 4. , 2. , 0.8, 0. , 0. , 0. ],
       [0. , 0. , 0. , 0.8, 2. , 4. , 2. , 0.8, 0. , 0. ],
       [0. , 0. , 0. , 0. , 0.8, 2. , 4. , 2. , 0.8, 0. ],
       [0. , 0. , 0. , 0. , 0. , 0.8, 2. , 4. , 2. , 0.8],
       [0. , 0. , 0. , 0. , 0. , 0. , 0.8, 2. , 4. , 2. ],
       [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.8, 2. , 4. ]])

对于异方差数据也是如此:

>>> new_var = (1. + np.absolute(np.arange(npts) - npts//2).astype(float))**2
>>> new_var
array([36., 25., 16.,  9.,  4.,  1.,  4.,  9., 16., 25.])
>>> new_c = c * np.sqrt(new_var[:,None] * new_var[None,:])
>>> new_c
array([[36. , 15. ,  4.8,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [15. , 25. , 10. ,  3. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 4.8, 10. , 16. ,  6. ,  1.6,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  3. ,  6. ,  9. ,  3. ,  0.6,  0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  1.6,  3. ,  4. ,  1. ,  0.8,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  0. ,  0.6,  1. ,  1. ,  1. ,  0.6,  0. ,  0. ],
       [ 0. ,  0. ,  0. ,  0. ,  0.8,  1. ,  4. ,  3. ,  1.6,  0. ],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0.6,  3. ,  9. ,  6. ,  3. ],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  1.6,  6. , 16. , 10. ],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  3. , 10. , 25. ]])

备注

Covariance 类提供了创建新的便利功能 Covariance 具有相同相关矩阵但具有新方差向量的实例;请参见 here .

重新排序和子集#

当重新排序或向下选择的元素的子集时 \(\mathbf{x}\) ,这些变化必须传播到相关的协方差矩阵,就像不相关数据集的误差载体所需的那样。

下面的示例首先生成具有洗牌索引集的载体。 经重排序 \(\mathbf{x}\) 将通过设置来构建 reordered_x = x[i] 协方差矩阵将使用 numpy.ix_ ,如下:

>>> rng = np.random.default_rng(99)
>>> i = np.arange(npts)
>>> rng.shuffle(i)
>>> i
array([4, 6, 0, 3, 8, 1, 2, 5, 7, 9])
>>> reordered_c = c[np.ix_(i,i)]
>>> reordered_c
array([[1. , 0.2, 0. , 0.5, 0. , 0. , 0.2, 0.5, 0. , 0. ],
       [0.2, 1. , 0. , 0. , 0.2, 0. , 0. , 0.5, 0.5, 0. ],
       [0. , 0. , 1. , 0. , 0. , 0.5, 0.2, 0. , 0. , 0. ],
       [0.5, 0. , 0. , 1. , 0. , 0.2, 0.5, 0.2, 0. , 0. ],
       [0. , 0.2, 0. , 0. , 1. , 0. , 0. , 0. , 0.5, 0.5],
       [0. , 0. , 0.5, 0.2, 0. , 1. , 0.5, 0. , 0. , 0. ],
       [0.2, 0. , 0.2, 0.5, 0. , 0.5, 1. , 0. , 0. , 0. ],
       [0.5, 0.5, 0. , 0.2, 0. , 0. , 0. , 1. , 0.2, 0. ],
       [0. , 0.5, 0. , 0. , 0.5, 0. , 0. , 0.2, 1. , 0.2],
       [0. , 0. , 0. , 0. , 0.5, 0. , 0. , 0. , 0.2, 1. ]])

请注意,的对角线 reordered_c 仍然是团结(所有要素 \(\mathbf{x}\) 与其自身完全相关),但非对角线项已被重新排列以维持先前存在的相关性。

为数据子集创建协方差矩阵是一个非常相似的操作。如果我们想要数据载体前3个元素的协方差矩阵,我们可以执行以下操作:

>>> i = np.arange(3)
>>> sub_c = c[np.ix_(i,i)]
>>> sub_c
array([[1. , 0.5, 0.2],
       [0.5, 1. , 0.5],
       [0.2, 0.5, 1. ]])

备注

Covariance 类提供了一个方便的函数,用于将协方差数据与其父数据数组的切片进行匹配;请参阅 here .

在N维中#

通过拉平数据数组,可以为更高维度的数组构建协方差矩阵。 对于行为主的阵列拉平顺序,可以采用这样的惯例: \(\Sigma_{ij}\) 对于大小的图像 \((N_x,N_y)\) 是图像像素之间的协方差 \(I_{x_i,y_i}\)\(I_{x_j,y_j}\) ,在哪里 \(i = x_i + N_x\ y_i\)\(j = x_j + N_x\ y_j\) .

例如,让协方差矩阵 c ,在本节中使用,是a的协方差矩阵 \(5 \times 2\) 数组,而不是10个元素的载体。 复杂的是确定从数据数组到相关协方差元素的映射;我们可以使用 numpy 功能如下。确定元素之间的协方差 data[1,0]data[2,0] ,我们将索引从 data 要找到0.2的协方差:

>>> data_array_shape = (5,2)
>>> i_data = (np.array([1]), np.array([0]))
>>> j_data = (np.array([2]), np.array([0]))
>>> i_cov = np.ravel_multi_index(i_data, data_array_shape)
>>> j_cov = np.ravel_multi_index(j_data, data_array_shape)
>>> i_cov, j_cov
(array([2]), array([4]))
>>> c[i_cov, j_cov]
array([0.2])

逆操作(在给定协方差矩阵中的索引的情况下确定数据数组的索引)使用 unravel_index (参见。 i_data ):

>>> np.unravel_index(i_cov, data_array_shape)
(array([1]), array([0]))

备注

Covariance 类提供了在处理更高维度数据数组时在数据数组和协方差矩阵索引之间切换的便利函数;请参阅 here .

建设#

提供了许多方法来构建 Covariance object. 在 all 在以下示例中,对象 c 是在开始时创建的带状协方差数组 介绍性例子 科.

从预先存在的阵列实例化#

最简单的实例化方法基于使用已经可用的数据。

创建一个 Covariance 来自方差载体的对象:

>>> from astropy.nddata.covariance import Covariance
>>> # Create from a variance vector
>>> var = np.ones(3, dtype=float)
>>> # Create from the Covariance object
>>> covar = Covariance.from_variance(var)
>>> # Test its contents
>>> print(np.array_equal(covar.to_dense(), np.identity(3)))
True

在这种情况下,数据数组的所有元素的方差都是单位,使得协方差矩阵是对角的并且与单位矩阵相同。

创建一个 Covariance 来自“密集”的对象(即,完全填充)协方差矩阵:

>>> # Instantiate from a covariance array
>>> covar = Covariance(array=c)
>>> print(np.array_equal(covar.to_dense(), c))
True
>>> covar.to_dense()
array([[1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. ],
       [0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. ],
       [0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. ],
       [0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. ],
       [0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. ],
       [0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2],
       [0. , 0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5],
       [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. ]])

重要

最后一句话使用 to_dense 访问阵列;请参阅 访问数据 .

上面,使用了基本实例化方法;但是, from_array 还提供了方法。 主要区别是后者允许对相关性或协方差值(的绝对值)施加限制。

最后,请注意,默认情况下, Covariance 对象检查输入矩阵是否对称。 如果不是,则会发出警告。 要跳过检查和警告,请设置 assume_symmetric=True . 无论是否执行检查,对象 only stores the upper triangle of the input matrix 实际上意味着当摄入时,矩阵中的任何不对称都会消失。

从随机样本实例化#

您可以根据分布中的样本构建协方差矩阵 from_samples :

>>> # Set the mean to 0 for all elements
>>> m = np.zeros(npts, dtype=float)
>>>
>>> # Sample the multivariate normal distribution with the provided
>>> # mean and covariance.
>>> s = rng.multivariate_normal(m, c, size=100000)
>>>
>>> # Construct the covariance matrix from the random samples
>>> covar = Covariance.from_samples(s.T, cov_tol=0.1)
>>>
>>> # Test that the known input covariance matrix is close to the
>>> # measured covariance from the random samples
>>> print(np.all(np.absolute(c - covar.to_dense()) < 0.02))
True

在这里,我们从平均值为零的已知多元正态分布中提取了样本 (m )和已知的协方差矩阵 (c ),定义为10 (npts )数据集中的元素(例如,光谱中的10个像素)。 该代码检查已知协方差矩阵的重建与从这些随机样本构建的结果。

从矩阵相乘实例化#

对数据集的线性操作(例如,合并或平滑)可以写成形式的矩阵相乘

\[{\mathbf y} = {\mathbf T}\ {\mathbf x},\]

where \({\mathbf T}\) is a transfer matrix of size \(N_y\times N_x\), \({\mathbf x}\) is a vector of size \(N_x\), and \({\mathbf y}\) is a vector of length \({N_y}\) that results from the multiplication. If \({\mathbf \Sigma}_x\) is the covariance matrix for \({\mathbf x}\), then the covariance matrix for \({\mathbf y}\) is

\[{\mathbf \Sigma}_y = {\mathbf T}\ {\mathbf \Sigma}_x\ {\mathbf T}^\top.\]

下面的示例展示了如何使用 from_matrix_multiplication :

>>> # Construct a dataset
>>> x = np.arange(npts, dtype=float)
>>>
>>> # Construct a transfer matrix that simply selects the elements at
>>> # indices 0, 2, and 4
>>> t = np.zeros((3,npts), dtype=float)
>>> t[0,0] = 1.0
>>> t[1,2] = 1.0
>>> t[2,4] = 1.0
>>>
>>> # Get y
>>> y = np.dot(t, x)
>>> y
array([0., 2., 4.])
>>>
>>> # Construct the covariance matrix
>>> covar = Covariance.from_matrix_multiplication(t, c)
>>>
>>> # Test the result
>>> _c = (np.diag(np.full(3-1, 0.2, dtype=float), k=-1)
...         + np.diag(np.full(3, 1.0, dtype=float), k=0)
...         + np.diag(np.full(3-1, 0.2, dtype=float), k=1))
>>> _c
array([[1. , 0.2, 0. ],
       [0.2, 1. , 0.2],
       [0. , 0.2, 1. ]])
>>> print(np.array_equal(covar.to_dense(), _c))
True

在N维中#

上面的所有实例化方法都允许您定义关联协方差矩阵的数据数组的“数据形状”。 遵循之前的N维示例,让 c 是a的协方差矩阵 \(5 \times 2\) 数组,而不是10个元素的载体。

>>> data_array_shape
(5, 2)
>>> covar = Covariance(array=c, data_shape=data_array_shape)
>>> covar.to_dense()
array([[1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. ],
       [0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. ],
       [0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. ],
       [0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. ],
       [0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. ],
       [0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2],
       [0. , 0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5],
       [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. ]])

协方差矩阵看起来相同,但更高的维度会影响其 坐标数据 .

访问数据#

Covariance 对象主要是一个存储实用程序。在内部,对象仅存储协方差矩阵的上三角形。 This means that you should not directly access a covariance value within the object itself ;您必须使用下面描述的功能。

协方差矩阵#

有两种方法可以访问完整的协方差矩阵:

  • 使用 to_sparse 生成稀疏矩阵,或者

  • 使用 to_dense 对于密集的矩阵。

这两种方法的输出可以像使用任何 scipy.sparse.csr_matrixnumpy.ndarray 分别是对象。

方差矩阵和相关矩阵#

方差载体存储为可访问属性 (variance ),但请注意,该属性是不可变的。

使用 to_sparse 生成稀疏矩阵,或者 to_dense 通过设置关键字参数来获得密集矩阵 correlation = True .

坐标数据#

尽管协方差数据对于存储准备更有用,但也可以以坐标格式访问:

>>> covar = Covariance(array=c)
>>> i, j, cij = covar.coordinate_data()
>>> print(np.array_equal(covar.to_dense()[i,j], cij))
True

返回的数组 coordinate_data 提供矩阵坐标 (ij )对于非零协方差值 (cij ).

文件IO#

写/读的主要方式 Covariance 对象是首先将数据解析成 Table 使用 to_table 方法:

>>> covar = Covariance(array=c)
>>> tbl = covar.to_table()
>>> tbl.meta
{'COVSHAPE': '(10, 10)'}
>>> tbl[:3]
<Table length=3>
INDXI INDXJ COVARIJ
int64 int64 float64
----- ----- -------
    0     0     1.0
    0     1     0.5
    0     2     0.2

上面的输出仅显示了表的前3行,以证明协方差矩阵的非零元素以“坐标格式”存储。“具体来说,数据分为三列:

  • 'INDXI' :协方差矩阵中的行索引 (\(i\) ).

  • 'INDXJ' :协方差矩阵中的列索引 (\(j\) ).

  • 'COVARIJ' :协方差值 (\(\Sigma_{ij}\) ).

该表还包含以下元数据:

  • 'COVSHAPE' :协方差矩阵的形状。

  • 'BUNIT' :(如果已定义)协方差单位的字符串表示。

  • 'COVDSHP' :(如果维度大于1)关联数据数组的形状。

对于更高维度的数组,坐标数据会自动重塑,以便索引与数据数组相对应。 例如,

>>> data_array_shape
(5, 2)
>>> covar = Covariance(array=c, data_shape=data_array_shape)
>>> tbl = covar.to_table()
>>> tbl.meta
{'COVSHAPE': '(10, 10)', 'COVDSHP': '(5, 2)'}
>>> tbl[:3]
<Table length=3>
 INDXI    INDXJ   COVARIJ
int64[2] int64[2] float64
-------- -------- -------
  0 .. 0   0 .. 0     1.0
  0 .. 0   0 .. 1     0.5
  0 .. 0   1 .. 0     0.2
>>> tbl['INDXI'][0]
array([0, 0])

警告

回想一下,高维数据的协方差矩阵的存储总是假设行优先的存储顺序。

还提供逆操作来实例化 Covariance 表格中的对象。 继续上面的N维示例:

>>> _covar = Covariance.from_table(tbl)
>>> _covar.data_shape
(5, 2)
>>> _covar.to_dense()
array([[1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. ],
       [0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. ],
       [0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. ],
       [0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. ],
       [0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. ],
       [0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2],
       [0. , 0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5],
       [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. ]])

使用 to_tablefrom_table 方法可与Astropy的统一文件I/O系统一起使用来读写协方差矩阵。

例如,将协方差矩阵写入表并重新加载:

>>> ofile = 'test_covar_io.fits'
>>> covar = Covariance(array=c)
>>> tbl = covar.to_table()
>>> tbl.write(ofile, format='fits')
>>> from astropy.io import fits
>>> with fits.open(ofile) as hdu:
...     hdu.info()
...
Filename: test_covar_io.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU       4   ()
  1                1 BinTableHDU     15   27R x 3C   [K, K, D]
>>> from astropy.table import Table
>>> _tbl = Table.read(ofile, format='fits')
>>> _covar = Covariance.from_table(_tbl)
>>> print(np.array_equal(covar.to_dense(), _covar.to_dense()))
True

效用函数#

重新规范方差#

要创建一个新的协方差矩阵,该矩阵保持与现有矩阵相同的相关性,但方差不同,您可以应用新的方差规格化(遵循 introductory section ). 的 Covariance 对象为此提供了便利功能。

>>> covar_var1 = Covariance(array=c)
>>> covar_var1.to_dense()
array([[1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. , 0. ],
       [0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. , 0. ],
       [0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. , 0. ],
       [0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. , 0. ],
       [0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2, 0. ],
       [0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5, 0.2],
       [0. , 0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. , 0.5],
       [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.2, 0.5, 1. ]])
>>> var4 = np.full(c.shape[0], 4.0, dtype=float)
>>> covar_var4 = covar_var1.apply_new_variance(var4)
>>> covar_var4.to_dense()
array([[4. , 2. , 0.8, 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
       [2. , 4. , 2. , 0.8, 0. , 0. , 0. , 0. , 0. , 0. ],
       [0.8, 2. , 4. , 2. , 0.8, 0. , 0. , 0. , 0. , 0. ],
       [0. , 0.8, 2. , 4. , 2. , 0.8, 0. , 0. , 0. , 0. ],
       [0. , 0. , 0.8, 2. , 4. , 2. , 0.8, 0. , 0. , 0. ],
       [0. , 0. , 0. , 0.8, 2. , 4. , 2. , 0.8, 0. , 0. ],
       [0. , 0. , 0. , 0. , 0.8, 2. , 4. , 2. , 0.8, 0. ],
       [0. , 0. , 0. , 0. , 0. , 0.8, 2. , 4. , 2. , 0.8],
       [0. , 0. , 0. , 0. , 0. , 0. , 0.8, 2. , 4. , 2. ],
       [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.8, 2. , 4. ]])

将协方差数据与其父数据数组的切片匹配#

以调整 Covariance 对象,以便它适合其父数据数组的切片,请使用 match_to_data_slice . 例如,要创建包含所有其他条目的矩阵:

>>> covar = Covariance(array=c)
>>> sub_covar = covar.match_to_data_slice(np.s_[::2])
>>> sub_covar
<Covariance; shape = (5, 5)>
>>> sub_covar.to_dense()
array([[1. , 0.2, 0. , 0. , 0. ],
       [0.2, 1. , 0.2, 0. , 0. ],
       [0. , 0.2, 1. , 0.2, 0. ],
       [0. , 0. , 0.2, 1. , 0.2],
       [0. , 0. , 0. , 0.2, 1. ]])

或者调整父数据阵列的重新排序:

>>> covar = Covariance(array=c)
>>> rng = np.random.default_rng(99)
>>> reorder = np.arange(covar.shape[0])
>>> rng.shuffle(reorder)
>>> reorder
array([4, 6, 0, 3, 8, 1, 2, 5, 7, 9])
>>> reorder_covar = covar.match_to_data_slice(reorder)
>>> reorder_covar.to_dense()
array([[1. , 0.2, 0. , 0.5, 0. , 0. , 0.2, 0.5, 0. , 0. ],
       [0.2, 1. , 0. , 0. , 0.2, 0. , 0. , 0.5, 0.5, 0. ],
       [0. , 0. , 1. , 0. , 0. , 0.5, 0.2, 0. , 0. , 0. ],
       [0.5, 0. , 0. , 1. , 0. , 0.2, 0.5, 0.2, 0. , 0. ],
       [0. , 0.2, 0. , 0. , 1. , 0. , 0. , 0. , 0.5, 0.5],
       [0. , 0. , 0.5, 0.2, 0. , 1. , 0.5, 0. , 0. , 0. ],
       [0.2, 0. , 0.2, 0.5, 0. , 0.5, 1. , 0. , 0. , 0. ],
       [0.5, 0.5, 0. , 0.2, 0. , 0. , 0. , 1. , 0.2, 0. ],
       [0. , 0.5, 0. , 0. , 0.5, 0. , 0. , 0.2, 1. , 0.2],
       [0. , 0. , 0. , 0. , 0.5, 0. , 0. , 0. , 0.2, 1. ]])

数据到协方差索引转换#

对于更高维度的数组,提供了两种方法来简化数据数组和协方差矩阵索引之间的转换。 下面的例子,将协方差矩阵中的十个元素定义为来自 \(5 \times 2\) 数组,然后在数据数组中查找索引处的协方差值的索引矩阵位置处的协方差值 (0,3) , (1,4) ,而且 (2,3) :

>>> covar = Covariance(array=c, data_shape=data_array_shape)
>>> i_data, j_data = covar.covariance_to_data_indices([0,1,2], [3,4,3])
>>> i_data
(array([0, 0, 1]), array([0, 1, 0]))
>>> j_data
(array([1, 2, 1]), array([1, 0, 1]))

这表明协方差元素提供了 data[0,0]data[1,1] 、元素 data[0,1]data[2,0] ,和元素 data[1,0]data[1,1] .

逆操作给出指定数据数组索引集的协方差索引。 保留我们上面定义的指数:

>>> i_cov, j_cov = covar.data_to_covariance_indices(i_data, j_data)
>>> i_cov, j_cov
(array([0, 1, 2]), array([3, 4, 3]))