tfp.stats.covariance
Stay organized with collections
Save and categorize content based on your preferences.
Sample covariance between observations indexed by event_axis
.
tfp.stats.covariance(
x, y=None, sample_axis=0, event_axis=-1, keepdims=False, name=None
)
Given N
samples of scalar random variables X
and Y
, covariance may be
estimated as
Cov[X, Y] := N^{-1} sum_{n=1}^N (X_n - Xbar) Conj{(Y_n - Ybar)}
Xbar := N^{-1} sum_{n=1}^N X_n
Ybar := N^{-1} sum_{n=1}^N Y_n
For vector-variate random variables X = (X1, ..., Xd)
, Y = (Y1, ..., Yd)
,
one is often interested in the covariance matrix, C_{ij} := Cov[Xi, Yj]
.
x = tf.random.normal(shape=(100, 2, 3))
y = tf.random.normal(shape=(100, 2, 3))
# cov[i, j] is the sample covariance between x[:, i, j] and y[:, i, j].
cov = tfp.stats.covariance(x, y, sample_axis=0, event_axis=None)
# cov_matrix[i, m, n] is the sample covariance of x[:, i, m] and y[:, i, n]
cov_matrix = tfp.stats.covariance(x, y, sample_axis=0, event_axis=-1)
Notice we divide by N
, which does not create NaN
when N = 1
, but is
slightly biased.
Args |
x
|
A numeric Tensor holding samples.
|
y
|
Optional Tensor with same dtype and shape as x .
Default value: None (y is effectively set to x ).
|
sample_axis
|
Scalar or vector Tensor designating axis holding samples, or
None (meaning all axis hold samples).
Default value: 0 (leftmost dimension).
|
event_axis
|
Scalar or vector Tensor , or None (scalar events).
Axis indexing random events, whose covariance we are interested in.
If a vector, entries must form a contiguous block of dims. sample_axis
and event_axis should not intersect.
Default value: -1 (rightmost axis holds events).
|
keepdims
|
Boolean. Whether to keep the sample axis as singletons.
|
name
|
Python str name prefixed to Ops created by this function.
Default value: None (i.e., 'covariance' ).
|
Returns |
cov
|
A Tensor of same dtype as the x , and rank equal to
rank(x) - len(sample_axis) + 2 * len(event_axis) .
|
Raises |
AssertionError
|
If x and y are found to have different shape.
|
ValueError
|
If sample_axis and event_axis are found to overlap.
|
ValueError
|
If event_axis is found to not be contiguous.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-11-21 UTC.
[null,null,["Last updated 2023-11-21 UTC."],[],[],null,["# tfp.stats.covariance\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/python/stats/sample_stats.py#L290-L466) |\n\nSample covariance between observations indexed by `event_axis`. \n\n tfp.stats.covariance(\n x, y=None, sample_axis=0, event_axis=-1, keepdims=False, name=None\n )\n\nGiven `N` samples of scalar random variables `X` and `Y`, covariance may be\nestimated as \n\n Cov[X, Y] := N^{-1} sum_{n=1}^N (X_n - Xbar) Conj{(Y_n - Ybar)}\n Xbar := N^{-1} sum_{n=1}^N X_n\n Ybar := N^{-1} sum_{n=1}^N Y_n\n\nFor vector-variate random variables `X = (X1, ..., Xd)`, `Y = (Y1, ..., Yd)`,\none is often interested in the covariance matrix, `C_{ij} := Cov[Xi, Yj]`. \n\n x = tf.random.normal(shape=(100, 2, 3))\n y = tf.random.normal(shape=(100, 2, 3))\n\n # cov[i, j] is the sample covariance between x[:, i, j] and y[:, i, j].\n cov = tfp.stats.covariance(x, y, sample_axis=0, event_axis=None)\n\n # cov_matrix[i, m, n] is the sample covariance of x[:, i, m] and y[:, i, n]\n cov_matrix = tfp.stats.covariance(x, y, sample_axis=0, event_axis=-1)\n\nNotice we divide by `N`, which does not create `NaN` when `N = 1`, but is\nslightly biased.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `x` | A numeric `Tensor` holding samples. |\n| `y` | Optional `Tensor` with same `dtype` and `shape` as `x`. Default value: `None` (`y` is effectively set to `x`). |\n| `sample_axis` | Scalar or vector `Tensor` designating axis holding samples, or `None` (meaning all axis hold samples). Default value: `0` (leftmost dimension). |\n| `event_axis` | Scalar or vector `Tensor`, or `None` (scalar events). Axis indexing random events, whose covariance we are interested in. If a vector, entries must form a contiguous block of dims. `sample_axis` and `event_axis` should not intersect. Default value: `-1` (rightmost axis holds events). |\n| `keepdims` | Boolean. Whether to keep the sample axis as singletons. |\n| `name` | Python `str` name prefixed to Ops created by this function. Default value: `None` (i.e., `'covariance'`). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|-------|--------------------------------------------------------------------------------------------------------------|\n| `cov` | A `Tensor` of same `dtype` as the `x`, and rank equal to `rank(x) - len(sample_axis) + 2 * len(event_axis)`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|------------------|---------------------------------------------------------|\n| `AssertionError` | If `x` and `y` are found to have different shape. |\n| `ValueError` | If `sample_axis` and `event_axis` are found to overlap. |\n| `ValueError` | If `event_axis` is found to not be contiguous. |\n\n\u003cbr /\u003e"]]