numpy.cov(m,y=None,rowvar=True,bias=False,ddof=None,fweights=None,aweights=None)[source]¶Estimate a covariance matrix, given data and weights.
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 varianceof
.
See the notes for an outline of the algorithm.
| Parameters: | m : array_like
y : array_like, optional
rowvar : bool, optional
bias : bool, optional
ddof : int, optional
fweights : array_like, int, optional
aweights : array_like, optional
|
|---|---|
| Returns: | out : ndarray
|
See also
corrcoefNotes
Assume that the observations are in the columns of the observationarraym and letf=fweights anda=aweights for brevity. Thesteps to compute the weighted covariance are as follows:
>>>w=f*a>>>v1=np.sum(w)>>>v2=np.sum(w*a)>>>m-=np.sum(m*w,axis=1,keepdims=True)/v1>>>cov=np.dot(m*w,m.T)*v1/(v1**2-ddof*v2)
Note that whena==1, the normalization factorv1/(v1**2-ddof*v2) goes over to1/(np.sum(f)-ddof)as it should.
Examples
Consider two variables,
and
, whichcorrelate perfectly, but in opposite directions:
>>>x=np.array([[0,2],[1,1],[2,0]]).T>>>xarray([[0, 1, 2], [2, 1, 0]])
Note how
increases while
decreases. The covariancematrix shows this clearly:
>>>np.cov(x)array([[ 1., -1.], [-1., 1.]])
Note that element
, which shows the correlation between
and
, is negative.
Further, note howx andy are combined:
>>>x=[-2.1,-1,4.3]>>>y=[3,1.1,0.12]>>>X=np.vstack((x,y))>>>print(np.cov(X))[[ 11.71 -4.286 ] [ -4.286 2.14413333]]>>>print(np.cov(x,y))[[ 11.71 -4.286 ] [ -4.286 2.14413333]]>>>print(np.cov(x))11.71