Trait ndarray_stats::CorrelationExt
source · pub trait CorrelationExt<A, S>where
S: Data<Elem = A>,{
fn cov(&self, ddof: A) -> Array2<A>
where
A: Float + FromPrimitive;
fn pearson_correlation(&self) -> Array2<A>
where
A: Float + FromPrimitive;
}
Expand description
Extension trait for ArrayBase
providing functions
to compute different correlation measures.
Required Methods
sourcefn cov(&self, ddof: A) -> Array2<A>where
A: Float + FromPrimitive,
fn cov(&self, ddof: A) -> Array2<A>where
A: Float + FromPrimitive,
Return the covariance matrix C
for a 2-dimensional
array of observations M
.
Let (r, o)
be the shape of M
:
r
is the number of random variables;o
is the number of observations we have collected for each random variable.
Every column in M
is an experiment: a single observation for each
random variable.
Each row in M
contains all the observations for a certain random variable.
The parameter ddof
specifies the “delta degrees of freedom”. For
example, to calculate the population covariance, use ddof = 0
, or to
calculate the sample covariance (unbiased estimate), use ddof = 1
.
The covariance of two random variables is defined as:
1 n
cov(X, Y) = ―――――――― ∑ (xᵢ - x̅)(yᵢ - y̅)
n - ddof i=1
where
1 n
x̅ = ― ∑ xᵢ
n i=1
and similarly for ̅y.
Panics if ddof
is greater than or equal to the number of
observations, if the number of observations is zero and division by
zero panics for type A
, or if the type cast of n_observations
from
usize
to A
fails.
Example
extern crate ndarray;
extern crate ndarray_stats;
use ndarray::{aview2, arr2};
use ndarray_stats::CorrelationExt;
let a = arr2(&[[1., 3., 5.],
[2., 4., 6.]]);
let covariance = a.cov(1.);
assert_eq!(
covariance,
aview2(&[[4., 4.], [4., 4.]])
);
sourcefn pearson_correlation(&self) -> Array2<A>where
A: Float + FromPrimitive,
fn pearson_correlation(&self) -> Array2<A>where
A: Float + FromPrimitive,
Return the Pearson correlation coefficients
for a 2-dimensional array of observations M
.
Let (r, o)
be the shape of M
:
r
is the number of random variables;o
is the number of observations we have collected for each random variable.
Every column in M
is an experiment: a single observation for each
random variable.
Each row in M
contains all the observations for a certain random variable.
The Pearson correlation coefficient of two random variables is defined as:
cov(X, Y)
rho(X, Y) = ――――――――――――
std(X)std(Y)
Let R
be the matrix returned by this function. Then
R_ij = rho(X_i, X_j)
Panics if M
is empty, if the type cast of n_observations
from usize
to A
fails or if the standard deviation of one of the random
Example
variables is zero and division by zero panics for type A.
extern crate ndarray;
extern crate ndarray_stats;
use ndarray::arr2;
use ndarray_stats::CorrelationExt;
let a = arr2(&[[1., 3., 5.],
[2., 4., 6.]]);
let corr = a.pearson_correlation();
assert!(
corr.all_close(
&arr2(&[
[1., 1.],
[1., 1.],
]),
1e-7
)
);