View Source Scholar.Covariance (Scholar v0.2.1)
Algorithms to estimate the covariance of features given a set of points.
Summary
Functions
Computes correlation matrix for sample inputs x
.
Computes covariance matrix for sample inputs x
.
Functions
Computes correlation matrix for sample inputs x
.
The value on the position $Corr_{ij}$ in the $Corr$ matrix is calculated using the formula: $$ Corr(X_i, X_j) = \frac{Cov(X_i, X_j)}{\sqrt{Cov(X_i, X_i)Cov(X_j, X_j)}} $$ Where:
$X_i$ is a $i$th row of input
$Cov(X_i, X_j)$ is covariance between features $X_i$ and $X_j$
Options
:center
(boolean/0
) - Iftrue
, data will be centered before computation. Iffalse
, data will not be centered before computation. Useful when working with data whose mean is almost, but not exactly zero. The default value istrue
.:biased
(boolean/0
) - Iftrue
, the matrix will be computed using biased covariation. Iffalse
, algorithm uses unbiased covariation. The default value istrue
.
Example
iex> Scholar.Covariance.correlation_matrix(Nx.tensor([[3, 6, 5], [26, 75, 3], [23, 4, 1]]))
#Nx.Tensor<
f32[3][3]
[
[1.0, 0.580316960811615, -0.7997867465019226],
[0.580316960811615, 1.0, 0.024736011400818825],
[-0.7997867465019226, 0.024736011400818825, 1.0]
]
>
iex> Scholar.Covariance.correlation_matrix(Nx.tensor([[3, 6], [2, 3], [7, 9], [5, 3]]))
#Nx.Tensor<
f32[2][2]
[
[1.0, 0.6673083305358887],
[0.6673083305358887, 1.0]
]
>
iex> Scholar.Covariance.correlation_matrix(Nx.tensor([[3, 6, 5], [26, 75, 3], [23, 4, 1]]),
...> biased: false
...> )
#Nx.Tensor<
f32[3][3]
[
[1.0, 0.5803170204162598, -0.7997867465019226],
[0.5803170204162598, 1.0, 0.024736013263463974],
[-0.7997867465019226, 0.024736013263463974, 1.0]
]
>
Computes covariance matrix for sample inputs x
.
The value on the position $Cov_{ij}$ in the $Cov$ matrix is calculated using the formula:
$$ Cov(X_i, X_j) = \frac{\sum_{k}\left(x_k - \bar{x}\right)\left(y_k - \bar{y}\right)}{N - 1} $$ Where:
$X_i$ is a $i$th row of input
$x_k$ is a $k$th value of $X_i$
$y_k$ is a $k$th value of $X_j$
$\bar{x}$ is the mean of $X_i$
$\bar{y}$ is the mean of $X_j$
$N$ is the number of samples
This is a non-biased version of covariance. The biased version has $N$ in denominator instead of $N - 1$.
Options
:center
(boolean/0
) - Iftrue
, data will be centered before computation. Iffalse
, data will not be centered before computation. Useful when working with data whose mean is almost, but not exactly zero. The default value istrue
.:biased
(boolean/0
) - Iftrue
, the matrix will be computed using biased covariation. Iffalse
, algorithm uses unbiased covariation. The default value istrue
.
Example
iex> Scholar.Covariance.covariance_matrix(Nx.tensor([[3, 6, 5], [26, 75, 3], [23, 4, 1]]))
#Nx.Tensor<
f32[3][3]
[
[104.22222137451172, 195.5555419921875, -13.333333015441895],
[195.5555419921875, 1089.5555419921875, 1.3333333730697632],
[-13.333333015441895, 1.3333333730697632, 2.6666667461395264]
]
>
iex> Scholar.Covariance.covariance_matrix(Nx.tensor([[3, 6], [2, 3], [7, 9], [5, 3]]))
#Nx.Tensor<
f32[2][2]
[
[3.6875, 3.1875],
[3.1875, 6.1875]
]
>
iex> Scholar.Covariance.covariance_matrix(Nx.tensor([[3, 6, 5], [26, 75, 3], [23, 4, 1]]),
...> biased: false
...> )
#Nx.Tensor<
f32[3][3]
[
[156.3333282470703, 293.33331298828125, -20.0],
[293.33331298828125, 1634.333251953125, 2.0],
[-20.0, 2.0, 4.0]
]
>