cov#
- ivy.cov(x1, x2=None, /, *, rowVar=True, bias=False, ddof=None, fweights=None, aweights=None, dtype=None)[source]#
Compute the covariance of matrix x1, or variables x1 and x2.
- Parameters:
x1 (
Union
[Array
,NativeArray
]) – a 1D or 2D input array, with a numeric data type.x2 (
Optional
[Union
[Array
,NativeArray
]], default:None
) – optional second 1D or 2D input array, with a numeric data type. Must have the same shape asself
.rowVar (
bool
, default:True
) – optional variable where each row of input is interpreted as a variable (default = True). If set to False, each column is instead interpreted as a variable.bias (
bool
, default:False
) – optional variable for normalizing input (default = False) by (N - 1) where N is the number of given observations. If set to True, then normalization is instead by N. Can be overridden by keywordddof
.ddof (
Optional
[int
], default:None
) – optional variable to overridebias
(default = None). ddof=1 will return the unbiased estimate, even with fweights and aweights given. ddof=0 will return the simple average.fweights (
Optional
[Array
], default:None
) – optional 1D array of integer frequency weights; the number of times each observation vector should be repeated.aweights (
Optional
[Array
], default:None
) – optional 1D array of observation vector weights. These relative weights are typically large for observations considered “important” and smaller for observations considered less “important”. If ddof=0 is specified, the array of weights can be used to assign probabilities to observation vectors.dtype (
Optional
[Union
[Dtype
,NativeDtype
]], default:None
) – optional variable to set data-type of the result. By default, data-type will have at leastnumpy.float64
precision.out – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – an array containing the covariance matrix of an input matrix, or the covariance matrix of two variables. The returned array must have a floating-point data type determined by Type Promotion Rules and must be a square matrix of shape (N, N), where N is the number of variables in the input(s).
This function conforms to the `Array API Standard
<https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)
`docstring <https (//data-apis.org/array-api/latest/)
extensions/generated/signatures.linalg.cov.html>`_
in the standard.
Both the description and the type hints above assumes an array input for simplicity,
but this function is nestable, and therefore also accepts
ivy.Container
instances in place of any of the arguments.
Examples
With
ivy.Array
input: >>> x = ivy.array([[1, 2, 3], … [4, 5, 6]]) >>> y = x[0].cov(x[1]) >>> print(y) ivy.array([[1., 1.],[1., 1.]])
With
ivy.Container
inputs: >>> x = ivy.Container(a=ivy.array([1., 2., 3.]), b=ivy.array([1., 2., 3.])) >>> y = ivy.Container(a=ivy.array([3., 2., 1.]), b=ivy.array([3., 2., 1.])) >>> z = ivy.Container.static_cov(x, y) >>> print(z) {- a: ivy.array([[1., -1.],
[-1., 1.]]),
- b: ivy.array([[1., -1.],
[-1., 1.]])
}
With a combination of
ivy.Array
andivy.Container
inputs: >>> x = ivy.array([1., 2., 3.]) >>> y = ivy.Container(a=ivy.array([3. ,2. ,1.]), b=ivy.array([-1., -2., -3.])) >>> z = ivy.cov(x, y) >>> print(z) {- a: ivy.array([[1., -1.],
[-1., 1.]]),
- b: ivy.array([[1., -1.],
[-1., 1.]])
}
With
ivy.Array
input and rowVar flag set to False (True by default): >>> x = ivy.array([[1,2,3], … [4,5,6]]) >>> y = x[0].cov(x[1], rowVar=False) >>> print(y) ivy.array([[1., 1.],[1., 1.]])
With
ivy.Array
input and bias flag set to True (False by default): >>> x = ivy.array([[1,2,3], … [4,5,6]]) >>> y = x[0].cov(x[1], bias=True) >>> print(y) ivy.array([[0.66666667, 0.66666667],[0.66666667, 0.66666667]])
With
ivy.Array
input with both fweights and aweights given: >>> x = ivy.array([[1,2,3], … [4,5,6]]) >>> fw = ivy.array([1,2,3]) >>> aw = ivy.array([ 1.2, 2.3, 3.4 ]) >>> y = x[0].cov(x[1], fweights=fw, aweights=aw) >>> print(y) ivy.array([[0.48447205, 0.48447205],[0.48447205, 0.48447205]])
- Array.cov(self, x2=None, /, *, rowVar=True, bias=False, ddof=None, fweights=None, aweights=None, dtype=None)[source]#
ivy.Array instance method variant of ivy.cov. This method simply wraps the function, and so the docstring for ivy.cov also applies to this method with minimal changes.
- Parameters:
self (
Array
) – a 1D or 2D input array, with a numeric data type.x2 (
Optional
[Union
[Array
,NativeArray
]], default:None
) – optional second 1D or 2D input array, with a numeric data type. Must have the same shape asself
.rowVar (
bool
, default:True
) – optional variable where each row of input is interpreted as a variable (default = True). If set to False, each column is instead interpreted as a variable.bias (
bool
, default:False
) – optional variable for normalizing input (default = False) by (N - 1) where N is the number of given observations. If set to True, then normalization is instead by N. Can be overridden by keywordddof
.ddof (
Optional
[int
], default:None
) – optional variable to overridebias
(default = None). ddof=1 will return the unbiased estimate, even with fweights and aweights given. ddof=0 will return the simple average.fweights (
Optional
[Array
], default:None
) – optional 1D array of integer frequency weights; the number of times each observation vector should be repeated.aweights (
Optional
[Array
], default:None
) – optional 1D array of observation vector weights. These relative weights are typically large for observations considered “important” and smaller for observations considered less “important”. If ddof=0 is specified, the array of weights can be used to assign probabilities to observation vectors.dtype (
Optional
[type
], default:None
) – optional variable to set data-type of the result. By default, data-type will have at leastfloat64
precision.out – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – an array containing the covariance matrix of an input matrix, or the covariance matrix of two variables. The returned array must have a floating-point data type determined by Type Promotion Rules and must be a square matrix of shape (N, N), where N is the number of variables in the input(s).
Examples
>>> x = ivy.array([[1, 2, 3], ... [4, 5, 6]]) >>> y = x[0].cov(x[1]) >>> print(y) ivy.array([[1., 1.], [1., 1.]])
>>> x = ivy.array([1,2,3]) >>> y = ivy.array([4,5,6]) >>> z = x.cov(y) >>> print(z) ivy.array([[1., 1.], [1., 1.]])
- Container.cov(self, x2=None, /, *, rowVar=True, bias=False, ddof=None, fweights=None, aweights=None, dtype=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#
ivy.Container instance method variant of ivy.cov. This method simply wraps the function, and so the docstring for ivy.cov also applies to this method with minimal changes.
- Parameters:
self (
Container
) – a 1D or 2D input container, with a numeric data type.x2 (
Optional
[Container
], default:None
) – optional second 1D or 2D input array, nativearray, or container, with a numeric data type. Must have the same shape asself
.rowVar (
Union
[bool
,Container
], default:True
) – optional variable where each row of input is interpreted as a variable (default = True). If set to False, each column is instead interpreted as a variable.bias (
Union
[bool
,Container
], default:False
) – optional variable for normalizing input (default = False) by (N - 1) where N is the number of given observations. If set to True, then normalization is instead by N. Can be overridden by keywordddof
.ddof (
Optional
[Union
[int
,Container
]], default:None
) – optional variable to overridebias
(default = None). ddof=1 will return the unbiased estimate, even with fweights and aweights given. ddof=0 will return the simple average.fweights (
Optional
[Union
[Array
,Container
]], default:None
) – optional 1D array of integer frequency weights; the number of times each observation vector should be repeated.aweights (
Optional
[Union
[Array
,Container
]], default:None
) – optional 1D array of observation vector weights. These relative weights are typically large for observations considered “important” and smaller for observations considered less “important”. If ddof=0 is specified, the array of weights can be used to assign probabilities to observation vectors.dtype (
Optional
[Union
[Dtype
,NativeDtype
,Container
]], default:None
) – optional variable to set data-type of the result. By default, data-type will have at leastnumpy.float64
precision.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
],Container
]], default:None
) – The key-chains to apply or not apply the method to. Default isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.prune_unapplied (
Union
[bool
,Container
], default:False
) – Whether to prune key_chains for which the function was not applied. Default isFalse
.map_sequences (
Union
[bool
,Container
], default:False
) – Whether to also map method to sequences (lists, tuples). Default isFalse
.out – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container
- Returns:
ret – a container containing the covariance matrix of an input matrix, or the covariance matrix of two variables. The returned container must have a floating-point data type determined by Type Promotion Rules and must be a square matrix of shape (N, N), where N is the number of variables in the input(s).
Examples
>>> x = ivy.Container(a=ivy.array([1., 2., 3.]), b=ivy.array([1., 2., 3.])) >>> y = ivy.Container(a=ivy.array([3., 2., 1.]), b=ivy.array([3., 2., 1.])) >>> z = x.cov(y) >>> print(z)
- {
- a: ivy.array([[1., -1.],
[-1., 1.]]),
- b: ivy.array([[1., -1.],
[-1., 1.]])
}