mindspore.ops.cov
- mindspore.ops.cov(input, *, correction=1, fweights=None, aweights=None)[source]
Given the input and weights, returns the covariance matrix (the square matrix of the covariance of each pair of variables) of input, where the input row is the variable and the column is the observation value.
The diagonal contains each variable and its own covariance. If input is a scalar or 1D vector of a single variable, its variance will be returned.
The unbiased sample covariance of the variables \(a\) and \(b\) is given by the following formula:
\[\text{cov}_w(a,b) = \frac{\sum^{N}_{i = 1}(a_{i} - \bar{a})(b_{i} - \bar{b})}{N~-~1}\]where \(\bar{a}\) and \(\bar{b}\) are the simple means of the \(a\) and \(b\) respectively.
If fweights and/or aweights are provided, the unbiased weighted covariance is calculated, which is given by:
\[\text{cov}_w(a,b) = \frac{\sum^{N}_{i = 1}w_i(a_{i} - \mu_a^*)(b_{i} - \mu_b^*)}{\sum^{N}_{i = 1}w_i~-~1}\]where \(w\) denotes fweights or aweights based on whichever is provided, or \(w = fweights \times aweights\) if both are provided, and \(\mu_x^* = \frac{\sum^{N}_{i = 1}w_ix_{i} }{\sum^{N}_{i = 1}w_i}\) is the weighted mean of the variable.
Warning
The values of fweights and aweights cannot be negative, and the negative weight scene result is undefined.
Note
Currently, complex number is not supported.
- Parameters
input (Tensor) – A 2D matrix, or a scalar or 1D vector of a single variable
- Keyword Arguments
correction (int, optional) – The difference between sample size and sample degrees of freedom. Defaults to Bessel's correction, correction = 1 which returns the unbiased estimate, even if both fweights and aweights are specified. correction = 0 will return the simple average. Default:
1
.fweights (Tensor, optional) – Scalar or one-dimensional Tensor containing integer frequency weight, indicating the number of repetition of each observation vector. Its numel must equal the number of columns of input. Ignored if None. Default:
None
.aweights (Tensor, optional) – A scalar or 1D Tensor containing float observation weights represents the importance of each observation vector. The higher the importance, the greater the corresponding value. Its numel must equal the number of columns of input. Must have floating point dtype. Ignored if None. Default:
None
.
- Returns
Tensor, the covariance matrix Tensor of input.
- Raises
ValueError – If the dimensions of input is greater than 2.
ValueError – If the dimensions of fweights is greater than 1.
ValueError – If the numel of fweights not equal the number of columns of input.
ValueError – If the numel of aweights not equal the number of columns of input.
ValueError – If the dimensions of aweights is greater than 1.
TypeError – If the dtype of input is bool.
TypeError – If the dtype of fweights is not an integer type.
TypeError – If the dtype of aweights is not a floating point type.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore import ops >>> x = ms.Tensor([[0., 3.], [5., 5.], [7., 0.]]).T >>> print(x) [[0. 5. 7.] [3. 5. 0.]] >>> print(ops.cov(x)) [[13. -3.5 ] [-3.5 6.3333335]] >>> print(ops.cov(x, correction=0)) [[ 8.666667 -2.3333333] [-2.3333333 4.2222223]] >>> fw = ms.Tensor([5, 2, 4], dtype=ms.int64) >>> aw = ms.Tensor([0.4588, 0.9083, 0.7616], ms.float32) >>> print(ops.cov(x, fweights=fw, aweights=aw)) [[10.146146 -3.47241 ] [-3.47241 4.716825]]