Skip to content

Commit 80ab905

Browse files
authored
Merge pull request #4 from jturner314/cov-empty
Document and test .cov() for empty arrays
2 parents d75844e + 5a66709 commit 80ab905

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ quickcheck = "0.7"
1414
ndarray-rand = "0.8"
1515

1616
[patch.crates-io]
17+
ndarray = { git = "https://github.com/jturner314/ndarray.git", branch = "master" }
1718
noisy_float = { git = "https://github.com/SergiusIW/noisy_float-rs.git", rev = "c33a94803987475bbd205c9ff5a697af533f9a17" }

src/correlation.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ where
3939
/// ```
4040
/// and similarly for ̅y.
4141
///
42-
/// **Panics** if `ddof` is greater than or equal to the number of
43-
/// observations, if `M` is emtpy or if the type cast of `n_observations`
44-
/// from `usize` to `A` fails.
42+
/// **Panics** if `ddof` is greater than or equal to the number of
43+
/// observations, if the number of observations is zero and division by
44+
/// zero panics for type `A`, or if the type cast of `n_observations` from
45+
/// `usize` to `A` fails.
4546
///
4647
/// # Example
4748
///
@@ -133,11 +134,27 @@ mod tests {
133134
}
134135

135136
#[test]
136-
#[should_panic]
137-
fn test_empty_matrix() {
138-
let a: Array2<f32> = array![[], []];
139-
// Negative ddof (-1 < 0) to avoid invalid-ddof panic
140-
a.cov(-1.);
137+
fn test_covariance_zero_variables() {
138+
let a = Array2::<f32>::zeros((0, 2));
139+
let cov = a.cov(1.);
140+
assert_eq!(cov.shape(), &[0, 0]);
141+
}
142+
143+
#[test]
144+
fn test_covariance_zero_observations() {
145+
let a = Array2::<f32>::zeros((2, 0));
146+
// Negative ddof (-1 < 0) to avoid invalid-ddof panic
147+
let cov = a.cov(-1.);
148+
assert_eq!(cov.shape(), &[2, 2]);
149+
cov.mapv(|x| x.is_nan());
150+
}
151+
152+
#[test]
153+
fn test_covariance_zero_variables_zero_observations() {
154+
let a = Array2::<f32>::zeros((0, 0));
155+
// Negative ddof (-1 < 0) to avoid invalid-ddof panic
156+
let cov = a.cov(-1.);
157+
assert_eq!(cov.shape(), &[0, 0]);
141158
}
142159

143160
#[test]

0 commit comments

Comments
 (0)