bundles / numpy latest / numpy / cov
_ArrayFunctionDispatcher
numpy:cov
source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/lib/_function_base_impl.py :2674
Signature
def cov ( m , y = None , rowvar = True , bias = False , ddof = None , fweights = None , aweights = None , * , dtype = None ) Summary
Estimate a covariance matrix, given data and weights.
Extended Summary
Covariance indicates the level to which two variables vary together. If we examine N-dimensional samples, , then the covariance matrix element is the covariance of and . The element is the variance of .
See the notes for an outline of the algorithm.
Parameters
m: array_likeA 1-D or 2-D array containing multiple variables and observations. Each row of
mrepresents a variable, and each column a single observation of all those variables. Also seerowvarbelow.y: array_like, optionalAn additional set of variables and observations.
yhas the same form as that ofm.rowvar: bool, optionalIf
rowvaris True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.bias: bool, optionalDefault normalization (False) is by
(N - 1), whereNis the number of observations given (unbiased estimate). Ifbiasis True, then normalization is byN. These values can be overridden by using the keywordddofin numpy versions >= 1.5.ddof: int, optionalIf not
Nonethe default value implied bybiasis overridden. Note thatddof=1will return the unbiased estimate, even if bothfweightsandaweightsare specified, andddof=0will return the simple average. See the notes for the details. The default value isNone.fweights: array_like, int, optional1-D array of integer frequency weights; the number of times each observation vector should be repeated.
aweights: array_like, optional1-D array of observation vector weights. These relative weights are typically large for observations considered "important" and smaller for observations considered less "important". If
ddof=0the array of weights can be used to assign probabilities to observation vectors.dtype: data-type, optionalData-type of the result. By default, the return data-type will have at least numpy.float64 precision.
Returns
out: ndarrayThe covariance matrix of the variables.
Notes
Assume that the observations are in the columns of the observation array m and let f = fweights and a = aweights for brevity. The steps to compute the weighted covariance are as follows
>>> m = np.arange(10, dtype=np.float64) >>> f = np.arange(10) * 2 >>> a = np.arange(10) ** 2. >>> ddof = 1 >>> w = f * a >>> v1 = np.sum(w) >>> v2 = np.sum(w * a) >>> m -= np.sum(m * w, axis=None, keepdims=True) / v1 >>> cov = np.dot(m * w, m.T) * v1 / (v1**2 - ddof * v2)
Note that when a == 1, the normalization factor v1 / (v1**2 - ddof * v2) goes over to 1 / (np.sum(f) - ddof) as it should.
Examples
import numpy as np
✓x = np.array([[0, 2], [1, 1], [2, 0]]).T x✓
np.cov(x)
✓x = [-2.1, -1, 4.3] y = [3, 1.1, 0.12] X = np.stack((x, y), axis=0)✓
np.cov(X) np.cov(x, y)✗
np.cov(x)
✓See also
- corrcoef
Normalized covariance matrix
Aliases
-
numpy.cov