View source on GitHub |
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.
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.
|